@fabrika/db 0.9.0-dev.11 → 0.9.0-dev.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +5 -5
- package/src/authority.ts +254 -0
- package/src/builtins.ts +21 -0
- package/src/capability.ts +15 -0
- package/src/codegen-sqlite.ts +8 -5
- package/src/codegen.ts +75 -28
- package/src/condition-nodes.ts +119 -0
- package/src/conditions.ts +683 -0
- package/src/description.ts +3 -0
- package/src/framework.ts +1 -1
- package/src/guard.ts +302 -15
- package/src/index.ts +2 -0
- package/src/nodes.ts +9 -6
- package/src/orm-types.ts +31 -12
- package/src/orm.ts +85 -33
- package/src/policy.ts +1247 -40
- package/src/registry.ts +11 -0
- package/src/sql.ts +22 -3
- package/src/store.ts +744 -55
- package/src/tables.ts +104 -2
- package/src/testing-shared.ts +3 -0
- package/src/vocabulary-tables.ts +110 -17
- package/types/src/authority.d.ts +66 -0
- package/types/src/builtins.d.ts +2 -0
- package/types/src/capability.d.ts +15 -0
- package/types/src/codegen.d.ts +24 -11
- package/types/src/condition-nodes.d.ts +9 -0
- package/types/src/conditions.d.ts +151 -0
- package/types/src/description.d.ts +3 -0
- package/types/src/framework.d.ts +1 -1
- package/types/src/guard.d.ts +16 -0
- package/types/src/index.d.ts +1 -1
- package/types/src/nodes.d.ts +2 -0
- package/types/src/orm-types.d.ts +31 -11
- package/types/src/orm.d.ts +5 -0
- package/types/src/policy.d.ts +197 -2
- package/types/src/registry.d.ts +9 -0
- package/types/src/sql.d.ts +2 -2
- package/types/src/store.d.ts +45 -7
- package/types/src/tables.d.ts +7 -1
- package/types/src/vocabulary-tables.d.ts +117 -16
package/types/src/store.d.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { type OperationNode, type ReturningNode } from 'kysely';
|
|
2
|
+
import { type MemberReveals, type RelationSources } from './authority';
|
|
2
3
|
import { type NameSet } from './builtins';
|
|
3
4
|
import { type AppGrant, type CapabilityAuthority, type Ref, type RefGrant, type Within, type WithinGrant } from './capability';
|
|
5
|
+
import { type ConditionModel } from './conditions';
|
|
4
6
|
import { type Database, type Session, type SqlDialect } from './database';
|
|
5
7
|
import { type ComparableColumn, type Compare, type Contains, type NoColumns, type Primitive, type ScopedSelect, type TableShape } from './description';
|
|
6
8
|
import { type FileBinding } from './file-columns';
|
|
7
|
-
import type { ForeignKeyPair, GuardContext, RootKind } from './guard';
|
|
8
|
-
import { type Acknowledgement, type ActionOf, type ChildTableName, type Container, type ContainerOf, type DimensionOf, type DimensionTableName, type InsertValues, type JoinableChildren, type ListDimensionOf, type MoveAcknowledgement, type Operation, type ParentOf, type PolicyRow, type TableModel, type TableModels, type Tables, type UpdateValues, type VersionArguments } from './policy';
|
|
9
|
+
import type { ForeignKeyPair, GuardContext, RootKind, ViewParentLink } from './guard';
|
|
10
|
+
import { type Acknowledgement, type ActionOf, type ChildTableName, type Container, type ContainerOf, type DimensionOf, type DimensionTableName, type InsertValues, type JoinableChildren, type ListDimensionOf, type MoveAcknowledgement, type Operation, type ParentOf, type PolicyRow, type SecondaryPath, type TableModel, type TableModels, type Tables, type UpdateValues, type VersionArguments } from './policy';
|
|
9
11
|
import type { ScopeTerms, ScopeUse } from './sql';
|
|
10
12
|
import { type TablePolicies } from './tables';
|
|
11
13
|
import { type RowColumns } from './temporal';
|
|
@@ -34,6 +36,18 @@ export interface TableContext {
|
|
|
34
36
|
/** The model of each child a select may join, by its table name. */
|
|
35
37
|
readonly children: Readonly<Record<string, TableModel>>;
|
|
36
38
|
readonly connection: Connection;
|
|
39
|
+
/** Every relation of the layer with its source table: what a relation term of any table reads. */
|
|
40
|
+
readonly relations: RelationSources;
|
|
41
|
+
/** Every member reveal of the layer, by the dimension whose grants it reveals to: what a `Within`'s read adds. */
|
|
42
|
+
readonly members: MemberReveals;
|
|
43
|
+
/** The tables a scope predicate's subqueries read, each with the column they select: what the guard admits. */
|
|
44
|
+
readonly subqueries: Readonly<Record<string, readonly string[]>>;
|
|
45
|
+
/** A child view's parent, which every capability reads the view's rows through; `undefined` for any other table. */
|
|
46
|
+
readonly through: ViewParent | undefined;
|
|
47
|
+
}
|
|
48
|
+
/** A child view's parent link, with the parent's context: its model without conditions, as only the view's own narrow a read. */
|
|
49
|
+
export interface ViewParent extends ViewParentLink {
|
|
50
|
+
readonly context: TableContext;
|
|
37
51
|
}
|
|
38
52
|
/** The dialect of the database the layer is bound to: what a read answers and a write binds depends on it. */
|
|
39
53
|
export declare function dialectOf(context: TableContext): SqlDialect;
|
|
@@ -67,17 +81,28 @@ export declare function selfLinks(model: TableModel): readonly Container[];
|
|
|
67
81
|
export declare function objectPredicate(context: TableContext, capability: unknown, operation: Operation): OperationNode;
|
|
68
82
|
/** Rows of a dimension table within the scope values of its list dimension. */
|
|
69
83
|
export declare function dimensionListPredicate(context: TableContext, capability: unknown, operation: Operation): OperationNode | undefined;
|
|
70
|
-
/**
|
|
71
|
-
|
|
84
|
+
/**
|
|
85
|
+
* Rows of a child table that belong to the parent object the Ref names, over the whole chain; with `key`, the one row
|
|
86
|
+
* of them it keys. The key's equality joins the chain's, so the action's condition narrows both.
|
|
87
|
+
*/
|
|
88
|
+
export declare function parentPredicate(context: TableContext, grant: RefGrant, key?: string): OperationNode;
|
|
72
89
|
export declare function childListPredicate(context: TableContext, capability: unknown, operation: Operation): OperationNode | undefined;
|
|
73
90
|
/**
|
|
74
91
|
* The column that places a row of `model` in `dimension`: the column of that link of its containment chain, or a
|
|
75
92
|
* top-level table's own key; `undefined` when no `Within` of the dimension reaches the table.
|
|
76
93
|
*/
|
|
77
94
|
export declare function reachColumn(model: TableModel, dimension: string): string | undefined;
|
|
95
|
+
/**
|
|
96
|
+
* Whether a `Within` of `dimension` lists the table: through its containment chain or a secondary path, and a child
|
|
97
|
+
* view through its parent's, its parent's own dimension included.
|
|
98
|
+
*/
|
|
99
|
+
export declare function reachedBy(context: TableContext, dimension: string): boolean;
|
|
100
|
+
/** The secondary read paths of `model` that reach `dimension`, a dimension outside its chain: at their node or above it. */
|
|
101
|
+
export declare function secondaryPathsOf(model: TableModel, dimension: string): readonly SecondaryPath[];
|
|
78
102
|
/**
|
|
79
103
|
* Rows of a table a `Within` of any dimension of its chain lists, by the table's read action: tasks under a `Within` of
|
|
80
|
-
* `org` through their stamped `org_id`, as under one of `project` through `project_id
|
|
104
|
+
* `org` through their stamped `org_id`, as under one of `project` through `project_id`; a `Within` of a dimension the
|
|
105
|
+
* table reaches through a secondary path, through that path's column.
|
|
81
106
|
*/
|
|
82
107
|
export declare function reachPredicate(context: TableContext, capability: unknown): OperationNode | undefined;
|
|
83
108
|
/**
|
|
@@ -112,7 +137,7 @@ export declare function appListPredicate(context: TableContext, capability: unkn
|
|
|
112
137
|
* Every row of an application table, for a `Ref` or a `Within` of its read action: what `ctx.db.app` of an `on`, `in`
|
|
113
138
|
* or `over` endpoint reads, read-only, as a reference table is read. The table's read action decides who reads it.
|
|
114
139
|
*/
|
|
115
|
-
export declare function scopedApplicationRead(context: TableContext, capability: unknown): OperationNode;
|
|
140
|
+
export declare function scopedApplicationRead(context: TableContext, capability: unknown, branch?: OperationNode | 'all'): OperationNode;
|
|
116
141
|
/** One row of an application table by its key, for an `app` capability of `operation`'s action. */
|
|
117
142
|
export declare function appRowPredicate(context: TableContext, capability: unknown, key: unknown, operation: Operation): OperationNode;
|
|
118
143
|
/**
|
|
@@ -128,6 +153,8 @@ export declare function claimScope(context: TableContext, capability: unknown):
|
|
|
128
153
|
* it was claimed, so a row moved since is not reached.
|
|
129
154
|
*/
|
|
130
155
|
export declare function claimedRowPredicate(context: TableContext, capability: unknown, operation: 'update' | 'delete'): OperationNode;
|
|
156
|
+
/** The row of an application table `key` names, as `action` reaches it: within the action's condition. */
|
|
157
|
+
export declare function appKeyScope(model: TableModel, action: string, key: unknown): OperationNode;
|
|
131
158
|
/** The row of an application table `key` names. */
|
|
132
159
|
export declare function appKeyPredicate(model: TableModel, key: unknown): OperationNode;
|
|
133
160
|
/**
|
|
@@ -170,7 +197,8 @@ export declare function versionArgument(context: TableContext, options: readonly
|
|
|
170
197
|
export declare function selectOne<O>(context: TableContext, run: Runner, scope: OperationNode, visible?: readonly string[], action?: string): Promise<O>;
|
|
171
198
|
export declare function answer(context: TableContext): ReturningNode;
|
|
172
199
|
export declare function keyOf(model: TableModel): string;
|
|
173
|
-
|
|
200
|
+
/** An insert, in a transaction that first locks its destination and judges its writer's bounds and its condition. */
|
|
201
|
+
export declare function insertRow<O>(context: TableContext, run: Runner, given: ReadonlyMap<string, Primitive | null>, prepared: Pick<PreparedInsert, 'destination' | 'writer' | 'judged'> | undefined): Promise<O>;
|
|
174
202
|
/**
|
|
175
203
|
* `insert(values, { onConflict: { columns, do } })`: on a conflict of `columns` (a unique index of the table), `nothing`
|
|
176
204
|
* writes nothing and `update` sets the columns the table's update action writes, from `updates`.
|
|
@@ -180,6 +208,8 @@ export interface OnConflict {
|
|
|
180
208
|
readonly mode: 'nothing' | 'update';
|
|
181
209
|
/** The values given for the table's update columns: what `update` sets on the conflicting row. */
|
|
182
210
|
readonly updates: ReadonlyMap<string, Primitive | null>;
|
|
211
|
+
/** The capability's action, whose condition the conflicting row must satisfy to be updated. */
|
|
212
|
+
readonly action: string;
|
|
183
213
|
}
|
|
184
214
|
/**
|
|
185
215
|
* The `onConflict` of an insert's options, read through own data properties; `undefined` without options. `'update'`
|
|
@@ -238,8 +268,16 @@ export declare function aggregateRows<Row, O>(context: TableContext, run: Runner
|
|
|
238
268
|
/** An insert's row, stamped from its capability, the destination it locks, and what it does on a conflict. */
|
|
239
269
|
export interface PreparedInsert {
|
|
240
270
|
readonly row: Map<string, Primitive | null>;
|
|
271
|
+
/** The principal the insert acts for: the bound of a reveal through a secondary path is judged against it. */
|
|
272
|
+
readonly writer: string;
|
|
241
273
|
readonly destination: RefGrant | undefined;
|
|
242
274
|
readonly conflict: OnConflict | undefined;
|
|
275
|
+
/** The insert action's condition when it follows a path: judged in the insert's transaction, on the rows it names. */
|
|
276
|
+
readonly judged: JudgedInsert | undefined;
|
|
277
|
+
}
|
|
278
|
+
export interface JudgedInsert {
|
|
279
|
+
readonly action: string;
|
|
280
|
+
readonly condition: ConditionModel;
|
|
243
281
|
}
|
|
244
282
|
/**
|
|
245
283
|
* A dimension table's insert over a capability nobody type-checked: into a container (a new UUIDv7 key, the chain from
|
package/types/src/tables.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ export { PolicyError } from './errors';
|
|
|
7
7
|
export type { FileReference } from './file-references';
|
|
8
8
|
export type { AnyDbIndex, ApplicationRows, ApplicationTables, AppRecordRows, ChildRows, ClaimedRow, ClaimedRowHandle, ClaimOptions, ContainedRows, CountOptions, DataKind, DbIndex, FragmentTable, IndexedTable, InsertConflict, InsertMethod, ListOptions, ListPage, NoUnscoped, OrderTerm, OwnerRow, PublicApplicationRows, PublicApplicationTables, PublicInsertMethod, ReadOnlyUnscoped, ReferenceRows, ReferenceTables, RowFor, RowOf, Saved, ScopedApplicationRows, ScopedApplicationTables, ScopeTarget, TableNameOf, TargetForm, TargetRow, Unavailable, UnownedTarget, UnscopedDeclaration, UnscopedInsert, UnscopedMode, UnscopedRows, UnscopedTables, VersionArguments, WithinRows, WithinTables, } from './orm-types';
|
|
9
9
|
export { type ApplicationPolicy, type ApplicationScope, type ChildPolicy, type ChildScope, type ColumnList, defineTables, type DimensionPolicy, type DimensionScope, type FilePolicies, isTables, type QueuePolicy, readTables, type RecordActions, type ReferencePolicy, type ReferenceScope, type TableAction, type TableActions, type TableInsert, Tables, type TableUpdate, type VersionColumn, type VocabularyTables, } from './policy';
|
|
10
|
-
export type { ColumnTypes, DefineVocabularyPolicies, NormalizedPolicies, TableVocabulary, TypedColumnList, VocabularyAction, VocabularyChildScope, VocabularyContainer, VocabularyContainerOf, VocabularyDimension, VocabularyDimensionScope, VocabularyPolicyMap, VocabularyTablePolicy, } from './vocabulary-tables';
|
|
10
|
+
export type { ColumnTypes, ColumnViews, DefineVocabularyPolicies, NormalizedPolicies, TableVocabulary, TypedColumnList, VocabularyAction, VocabularyChildScope, VocabularyContainer, VocabularyContainerOf, VocabularyDimension, VocabularyDimensionScope, VocabularyPolicyMap, VocabularyTablePolicy, VocabularyViewPolicy, } from './vocabulary-tables';
|
|
11
11
|
/**
|
|
12
12
|
* The plan of seeding one row of each application table `rows` names (table → column → value), as an endpoint access
|
|
13
13
|
* case seeds its `appRows`: the order, the foreign keys that link one given row to another, and every refusal.
|
|
@@ -15,6 +15,12 @@ export type { ColumnTypes, DefineVocabularyPolicies, NormalizedPolicies, TableVo
|
|
|
15
15
|
* `applicationRowRefusalText`.
|
|
16
16
|
*/
|
|
17
17
|
export declare function applicationRowsPlan<DB, P>(tables: Tables<DB, P>, rows: Readonly<Record<string, Readonly<Record<string, unknown>>>>): ApplicationRowsPlan;
|
|
18
|
+
/**
|
|
19
|
+
* The tables whose rows hand out access, lowercased: a relation source or a path reveal, a member-revealing table, and
|
|
20
|
+
* the tables a principal path or a relation's condition reads. Only the data layer writes them; the migration classifier
|
|
21
|
+
* refuses database code that does.
|
|
22
|
+
*/
|
|
23
|
+
export declare function accessTables<DB, P>(tables: Tables<DB, P>): readonly string[];
|
|
18
24
|
/** Every table's policy in the form the contract artifact carries, by table name. */
|
|
19
25
|
export type TablePolicies = Readonly<Record<string, TablePolicyArtifact>>;
|
|
20
26
|
/** The artifact form of every policy `defineTables` (or `readTables`) checked. */
|
|
@@ -1,16 +1,28 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ConditionPolicy } from './conditions';
|
|
2
|
+
import type { ColumnList, ColumnPolicies, ConfersPolicy, FilePolicies, MemberRevealsPolicy, RecordActions, ReferencePolicy, RevealsPolicy, TableAction, TableActions, Tables, VersionColumn } from './policy';
|
|
2
3
|
declare const databaseType: unique symbol;
|
|
4
|
+
declare const viewNames: unique symbol;
|
|
3
5
|
/**
|
|
4
|
-
* The generated column list (`src/db/schema.generated.ts`), which also carries the schema's row types
|
|
5
|
-
* `defineTables(vocabulary, columns)` takes no type argument. The
|
|
6
|
+
* The generated column list (`src/db/schema.generated.ts`), which also carries the schema's row types and the names of
|
|
7
|
+
* its views, so that `defineTables(vocabulary, columns)` takes no type argument. The brands are type-only: no value
|
|
8
|
+
* has either property.
|
|
6
9
|
*/
|
|
7
|
-
export type TypedColumnList<DB> = ColumnList<DB> & {
|
|
10
|
+
export type TypedColumnList<DB, Views extends keyof DB & string = never> = ColumnList<DB> & {
|
|
8
11
|
readonly [databaseType]?: DB;
|
|
12
|
+
readonly [viewNames]?: {
|
|
13
|
+
readonly views: Views;
|
|
14
|
+
};
|
|
9
15
|
};
|
|
10
16
|
/** The row types a typed column list carries; `never` for a list without them. */
|
|
11
17
|
export type ColumnTypes<C> = typeof databaseType extends keyof C ? C extends {
|
|
12
18
|
readonly [databaseType]?: infer DB;
|
|
13
19
|
} ? Exclude<DB, undefined> : never : never;
|
|
20
|
+
/** The views a typed column list names; `never` for a list without views. */
|
|
21
|
+
export type ColumnViews<C> = typeof viewNames extends keyof C ? C extends {
|
|
22
|
+
readonly [viewNames]?: {
|
|
23
|
+
readonly views: infer Views extends string;
|
|
24
|
+
};
|
|
25
|
+
} ? Views : never : never;
|
|
14
26
|
/**
|
|
15
27
|
* What `defineTables(vocabulary, columns)` reads of `@fabrika/app/contract`'s `defineVocabulary` result: the action
|
|
16
28
|
* and dimension names, and each dimension's container. `@fabrika/db` does not import `@fabrika/app`.
|
|
@@ -24,6 +36,11 @@ export interface TableVocabulary {
|
|
|
24
36
|
readonly in?: string;
|
|
25
37
|
};
|
|
26
38
|
};
|
|
39
|
+
readonly relations?: {
|
|
40
|
+
readonly [relation: string]: {
|
|
41
|
+
readonly dimension: string;
|
|
42
|
+
};
|
|
43
|
+
};
|
|
27
44
|
}
|
|
28
45
|
export type VocabularyAction<V> = V extends {
|
|
29
46
|
readonly actions: infer A;
|
|
@@ -81,6 +98,29 @@ interface WritableFields<Col extends string, A extends string, Ver extends Col,
|
|
|
81
98
|
readonly columns?: ColumnPolicies<Col, A>;
|
|
82
99
|
/** The file columns, each with the file kind it holds (`c.files`): a `uuid` foreign key to `fabrika_files`. */
|
|
83
100
|
readonly files?: FilePolicies<Col>;
|
|
101
|
+
/** Relation → how a row of this table confers it: whom it names, at which node, until when. */
|
|
102
|
+
readonly confers?: {
|
|
103
|
+
readonly [relation: string]: ConfersPolicy<Col>;
|
|
104
|
+
};
|
|
105
|
+
/** Action → the condition a row must meet for it, whoever holds it. */
|
|
106
|
+
readonly when?: {
|
|
107
|
+
readonly [action in A]?: ConditionPolicy<Col>;
|
|
108
|
+
};
|
|
109
|
+
/** Relation of the table's own dimension → the ancestor node a row it reaches reveals, and for which actions. */
|
|
110
|
+
readonly reveals?: {
|
|
111
|
+
readonly [relation: string]: RevealsPolicy<Col, A>;
|
|
112
|
+
};
|
|
113
|
+
/**
|
|
114
|
+
* Dimension outside the chain → the foreign key column a `Within`'s read reaches a row through, no update writes it; or
|
|
115
|
+
* `<nearest container column>.<its column>`, the nearest container's own path.
|
|
116
|
+
*/
|
|
117
|
+
readonly readThrough?: {
|
|
118
|
+
readonly [dimension: string]: Col | `${Col}.${string}`;
|
|
119
|
+
};
|
|
120
|
+
/** The table's own dimension → the ancestor node a row reveals to a grant at its node, for a `Within`'s reads. */
|
|
121
|
+
readonly memberReveals?: {
|
|
122
|
+
readonly [dimension: string]: MemberRevealsPolicy<Col, A>;
|
|
123
|
+
};
|
|
84
124
|
}
|
|
85
125
|
/**
|
|
86
126
|
* The scope of the dimension the policy states (`S`, its scope as written); for any other name, the dimension alone,
|
|
@@ -134,6 +174,58 @@ type AuthoredDimensionPolicy<Col extends string, A extends string, V, Ver extend
|
|
|
134
174
|
type AuthoredChildPolicy<Table extends string, Col extends string, A extends string, Ver extends Col, S, P> = WritableFields<Col, A, Ver, RecordActions<A>> & {
|
|
135
175
|
readonly scope: ChildScopes<Table, Col, S, P>;
|
|
136
176
|
};
|
|
177
|
+
/** What a view's policy holds besides its scope: read actions only, no write list and no stamped column. */
|
|
178
|
+
type ViewIsReadOnly = 'a view is never written: its actions are read, appRead and appCount';
|
|
179
|
+
/** The write actions a view's policy may not name, each typed as the message: a literal is not checked for excess keys. */
|
|
180
|
+
interface NoWriteActions {
|
|
181
|
+
readonly insert?: ViewIsReadOnly;
|
|
182
|
+
readonly update?: ViewIsReadOnly;
|
|
183
|
+
readonly delete?: ViewIsReadOnly;
|
|
184
|
+
}
|
|
185
|
+
interface ReadOnlyFields<Col extends string, A extends string> {
|
|
186
|
+
readonly actions: {
|
|
187
|
+
readonly read: TableAction<A>;
|
|
188
|
+
readonly appRead?: readonly A[];
|
|
189
|
+
readonly appCount?: readonly A[];
|
|
190
|
+
} & NoWriteActions;
|
|
191
|
+
readonly insert?: never;
|
|
192
|
+
readonly update?: never;
|
|
193
|
+
readonly version?: never;
|
|
194
|
+
readonly createdBy?: never;
|
|
195
|
+
readonly queue?: never;
|
|
196
|
+
readonly files?: never;
|
|
197
|
+
readonly columns?: {
|
|
198
|
+
readonly [C in Col]?: {
|
|
199
|
+
readonly read: A;
|
|
200
|
+
readonly write?: never;
|
|
201
|
+
};
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* A view read as a child table: its parent's `Ref` and `Within` read it; nothing writes it (ADR-0072). Its `when` narrows
|
|
206
|
+
* its read actions; a path may start at its parent column, to the parent row.
|
|
207
|
+
*/
|
|
208
|
+
type AuthoredChildViewPolicy<Table extends string, Col extends string, A extends string, S, P> = ReadOnlyFields<Col, A> & {
|
|
209
|
+
readonly scope: ChildScopes<Table, Col, S, P>;
|
|
210
|
+
readonly when?: {
|
|
211
|
+
readonly [action in A]?: ConditionPolicy<Col>;
|
|
212
|
+
};
|
|
213
|
+
};
|
|
214
|
+
/** A view's policy as written: a read-only child, or a reference table every site reads. */
|
|
215
|
+
export type VocabularyViewPolicy<Table extends string, Col extends string, A extends string, S = unknown, P = unknown> = S extends {
|
|
216
|
+
readonly child: unknown;
|
|
217
|
+
} ? AuthoredChildViewPolicy<Table, Col, A, S, P> : S extends {
|
|
218
|
+
readonly reference: unknown;
|
|
219
|
+
} ? ReferenceViewPolicy<Col, A> : AuthoredChildViewPolicy<Table, Col, A, S, P> | ReferenceViewPolicy<Col, A>;
|
|
220
|
+
type ReferenceViewPolicy<Col extends string, A extends string> = ReferencePolicy<Col, A> & {
|
|
221
|
+
readonly actions: NoWriteActions;
|
|
222
|
+
readonly columns?: {
|
|
223
|
+
readonly [C in Col]?: {
|
|
224
|
+
readonly read: A;
|
|
225
|
+
readonly write?: never;
|
|
226
|
+
};
|
|
227
|
+
};
|
|
228
|
+
};
|
|
137
229
|
/** An application table's scope: why its rows belong to no dimension; `key` defaults to `id`. */
|
|
138
230
|
type AuthoredApplicationPolicy<Col extends string, A extends string, Ver extends Col> = WritableFields<Col, A, Ver> & {
|
|
139
231
|
readonly scope: {
|
|
@@ -158,13 +250,18 @@ type StatedScope<P, T> = T extends keyof P ? P[T] extends {
|
|
|
158
250
|
} ? S : unknown : unknown;
|
|
159
251
|
/** The policy of table `T` with nothing stated: every form, every dimension's scope, any parent. */
|
|
160
252
|
type AnyTablePolicy<DB, V, T extends keyof DB & string> = VocabularyTablePolicy<keyof DB & string, keyof DB[T] & string, VocabularyAction<V>, V, VersionColumn<DB[T]>>;
|
|
253
|
+
/** The policy of view `T` with nothing stated: both forms, any parent. */
|
|
254
|
+
type AnyViewPolicy<DB, V, T extends keyof DB & string> = VocabularyViewPolicy<keyof DB & string, keyof DB[T] & string, VocabularyAction<V>>;
|
|
161
255
|
/**
|
|
162
|
-
* Every table of the schema, and no other, maps to a policy; a
|
|
163
|
-
* `P` is the policies as written, which picks each table's form by the scope it
|
|
164
|
-
* map's own annotation states nothing narrower, so it is checked against every
|
|
256
|
+
* Every table of the schema, and no other, maps to a policy; a view `Views` names may (ADR-0072). A misspelt action or
|
|
257
|
+
* dimension fails at its own line. `P` is the policies as written, which picks each table's form by the scope it
|
|
258
|
+
* states; a policy written under this map's own annotation states nothing narrower, so it is checked against every
|
|
259
|
+
* form.
|
|
165
260
|
*/
|
|
166
|
-
export type VocabularyPolicyMap<DB, V, P = unknown> = {
|
|
167
|
-
readonly [T in keyof DB & string]: AnyTablePolicy<DB, V, T> extends (T extends keyof P ? P[T] : unknown) ? AnyTablePolicy<DB, V, T> : VocabularyTablePolicy<keyof DB & string, keyof DB[T] & string, VocabularyAction<V>, V, VersionColumn<DB[T]>, StatedScope<P, T>, P>;
|
|
261
|
+
export type VocabularyPolicyMap<DB, V, P = unknown, Views extends string = never> = {
|
|
262
|
+
readonly [T in Exclude<keyof DB & string, Views>]: AnyTablePolicy<DB, V, T> extends (T extends keyof P ? P[T] : unknown) ? AnyTablePolicy<DB, V, T> : VocabularyTablePolicy<keyof DB & string, keyof DB[T] & string, VocabularyAction<V>, V, VersionColumn<DB[T]>, StatedScope<P, T>, P>;
|
|
263
|
+
} & {
|
|
264
|
+
readonly [T in Views & keyof DB & string]?: AnyViewPolicy<DB, V, T> extends (T extends keyof P ? P[T] : unknown) ? AnyViewPolicy<DB, V, T> : VocabularyViewPolicy<keyof DB & string, keyof DB[T] & string, VocabularyAction<V>, StatedScope<P, T>, P>;
|
|
168
265
|
};
|
|
169
266
|
type Flat<T> = {
|
|
170
267
|
[K in keyof T]: T[K];
|
|
@@ -207,19 +304,23 @@ type NormalScope<S, V, P> = S extends {
|
|
|
207
304
|
readonly app: R;
|
|
208
305
|
readonly key: KeyOf<S>;
|
|
209
306
|
} : S;
|
|
307
|
+
/** A view's normalized policy says so, which the handle types read: no site writes it. */
|
|
308
|
+
type ViewMark<T, Views> = T extends Views ? {
|
|
309
|
+
readonly view: true;
|
|
310
|
+
} : unknown;
|
|
210
311
|
/**
|
|
211
312
|
* The policies as the layer models them: every dimension table's key and container, and every child's key and
|
|
212
|
-
* column, stated as the vocabulary and the conventions derive them.
|
|
313
|
+
* column, stated as the vocabulary and the conventions derive them; each view marked.
|
|
213
314
|
*/
|
|
214
|
-
export type NormalizedPolicies<P, V> = {
|
|
315
|
+
export type NormalizedPolicies<P, V, Views = never> = {
|
|
215
316
|
readonly [T in keyof P]: P[T] extends {
|
|
216
317
|
readonly scope: infer S;
|
|
217
|
-
} ? Flat<Omit<P[T], 'scope'> & {
|
|
318
|
+
} ? Flat<Omit<P[T], 'scope' | 'view'> & {
|
|
218
319
|
readonly scope: NormalScope<S, V, P>;
|
|
219
|
-
}
|
|
320
|
+
} & ViewMark<T, Views>> : P[T];
|
|
220
321
|
};
|
|
221
|
-
/** Takes the policies of exactly the tables of the schema, written in the vocabulary form. */
|
|
222
|
-
export type DefineVocabularyPolicies<DB, V> = <const P extends VocabularyPolicyMap<DB, V, P>>(policies: P & {
|
|
322
|
+
/** Takes the policies of exactly the tables of the schema, and of any of its views, written in the vocabulary form. */
|
|
323
|
+
export type DefineVocabularyPolicies<DB, V, Views extends string = never> = <const P extends VocabularyPolicyMap<DB, V, P, Views>>(policies: P & {
|
|
223
324
|
readonly [K in Exclude<keyof P, keyof DB>]: never;
|
|
224
|
-
}) => Tables<DB, NormalizedPolicies<P, V>>;
|
|
325
|
+
}) => Tables<DB, NormalizedPolicies<P, V, Views>>;
|
|
225
326
|
export {};
|