@fabrika/db 0.9.0-dev.12 → 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.
@@ -1,5 +1,6 @@
1
1
  import type { Insertable, Selectable, SelectType, Updateable } from 'kysely';
2
2
  import { type NameSet } from './builtins';
3
+ import { type ConditionModel, type ConditionPolicy } from './conditions';
3
4
  import type { Unavailable } from './orm-types';
4
5
  export type { RowOf } from './orm-types';
5
6
  import type { ColumnTypes, ColumnViews, DefineVocabularyPolicies, TableVocabulary } from './vocabulary-tables';
@@ -65,6 +66,82 @@ interface WritablePolicy<Col extends string, A extends string, V extends Col, Ac
65
66
  * file reference. The table's actions are the file's authority.
66
67
  */
67
68
  readonly files?: FilePolicies<Col>;
69
+ /** The relations this table's rows confer (`relations` of the vocabulary): each row holds one principal at one node. */
70
+ readonly confers?: {
71
+ readonly [relation: string]: ConfersPolicy<Col>;
72
+ };
73
+ /** Conditions that narrow an action of this table: a row the condition does not admit is out of that action's reach. */
74
+ readonly when?: {
75
+ readonly [action in A]?: ConditionPolicy<Col>;
76
+ };
77
+ /** Relation of this table's own dimension, or of one it reads through → the ancestor node a row the relation reaches reveals. */
78
+ readonly reveals?: {
79
+ readonly [relation: string]: RevealsPolicy<Col, A>;
80
+ };
81
+ /**
82
+ * Dimension outside the chain → the foreign key column a read reaches a row through: a `Within` of the dimension, or a
83
+ * relation at it, reads the row, and every write follows containment only. No update writes the column. Or
84
+ * `<nearest container column>.<its column>`: the row is read through its nearest container's own read path.
85
+ */
86
+ readonly readThrough?: {
87
+ readonly [dimension: string]: Col | `${Col}.${string}`;
88
+ };
89
+ /**
90
+ * This table's own dimension → the ancestor node a row reveals to its members: a grant of one of `actions` at the
91
+ * row's own node holds that action at its node in `to` too, for a `Within`'s reads, while `when` admits the row.
92
+ */
93
+ readonly memberReveals?: {
94
+ readonly [dimension: string]: MemberRevealsPolicy<Col, A>;
95
+ };
96
+ }
97
+ /** What a row reveals to a caller with a grant at its node: its node in `to`, for `actions`, while `when` admits the row. */
98
+ export interface MemberRevealsPolicy<Col extends string, A extends string> {
99
+ readonly to: string;
100
+ readonly actions: readonly A[];
101
+ /** A condition on the revealing row, under the same path rule as an action's `when`. */
102
+ readonly when?: ConditionPolicy<Col>;
103
+ }
104
+ /**
105
+ * A row of the table that a relation reaches reveals its node in `to`, an ancestor dimension of its chain: the relation
106
+ * then holds `actions` (those of them its roles list in `via`) there, while `when` admits the row.
107
+ */
108
+ export interface RevealsPolicy<Col extends string, A extends string> {
109
+ readonly to: string;
110
+ readonly actions: readonly A[];
111
+ /** A condition on the revealing row, under the same path rule as an action's `when`. */
112
+ readonly when?: ConditionPolicy<Col>;
113
+ /**
114
+ * Required on a reveal matched through `readThrough` of a table with an insert action: the row's writer picks the node,
115
+ * so an insert delegates. Without `self`, an insert that names a node where its writer holds the relation is refused.
116
+ */
117
+ readonly assign?: {
118
+ readonly self?: true;
119
+ };
120
+ }
121
+ /**
122
+ * A relation source: each row says that the principal in `principal` stands in the relation to the node `node` holds,
123
+ * until `expires` (none: no deadline) and while `when` admits the row. Writing a row is delegation.
124
+ */
125
+ export interface ConfersPolicy<Col extends string> {
126
+ /**
127
+ * The column that holds the principal, or `<column>.<principal column>`: one foreign key of the table into the key of
128
+ * the table whose column holds it. The table's `createdBy` names the row's writer (it needs `assign.self`).
129
+ */
130
+ readonly principal: Col | `${Col}.${string}`;
131
+ /** The column that names the node; by default the table's column for the relation's dimension (its key or its chain). */
132
+ readonly node?: Col;
133
+ /** A timestamp column: the row confers nothing once it passed. */
134
+ readonly expires?: Col;
135
+ /** A condition on the row's own columns (no foreign-key path): `{ revoked_at: { isNull: true } }`. */
136
+ readonly when?: ConditionPolicy<Col>;
137
+ /**
138
+ * Inserting a row delegates the relation's actions to its principal, bounded by the inserting Ref's node: at most
139
+ * `maxDays` ahead (which needs `expires`), and to the inserting principal itself only with `self`.
140
+ */
141
+ readonly assign?: {
142
+ readonly maxDays?: number;
143
+ readonly self?: true;
144
+ };
68
145
  }
69
146
  /** A table's file columns: column → the name of a file kind of the contract (`c.files`). */
70
147
  export type FilePolicies<Col extends string> = {
@@ -136,6 +213,11 @@ export interface TableColumns<Col extends string = string, Table extends string
136
213
  readonly booleans?: readonly Col[];
137
214
  /** The JSON columns (`jsonb`, or declared `JSONB` on SQLite), written as a JSON value and never compared; absent when none. */
138
215
  readonly json?: readonly Col[];
216
+ /**
217
+ * The columns declared NOT NULL; absent when none. A foreign key that holds a row's tenant needs its ancestor columns
218
+ * among them: under MATCH SIMPLE a null in one skips the whole key.
219
+ */
220
+ readonly notNull?: readonly Col[];
139
221
  /** A view: read-only, with no foreign key; absent for a table. */
140
222
  readonly view?: true;
141
223
  }
@@ -212,6 +294,8 @@ export interface TableModel {
212
294
  readonly booleans: NameSet;
213
295
  /** The JSON columns: a write binds a JSON value's text, and no condition, order or aggregate names one. */
214
296
  readonly json: NameSet;
297
+ /** The columns the column list declares NOT NULL. */
298
+ readonly notNull: NameSet;
215
299
  /** The table's foreign keys, from the column list; a containment column must be one to its container's key. */
216
300
  readonly foreignKeys: readonly ForeignKey[];
217
301
  /** A view: a child or reference table no operation but a read reaches, whose containment only its SQL states. */
@@ -221,6 +305,99 @@ export interface TableModel {
221
305
  * the container's own chain; a child's parent and the parent's chain. Every predicate of a Ref covers all.
222
306
  */
223
307
  readonly chain: readonly Container[];
308
+ /** The relations this table's rows confer, each with its columns, its condition and its delegation bound. */
309
+ readonly confers: readonly RelationSource[];
310
+ /** The condition each action of the table holds a row under; an action without one is not narrowed. */
311
+ readonly when: Readonly<Record<string, ConditionModel>>;
312
+ /** The ancestor nodes this table's rows reveal to a relation of its own dimension or of one it reads through. */
313
+ readonly reveals: readonly RevealModel[];
314
+ /** The secondary read paths: a dimension outside the chain, by the column that holds the row's node in it. */
315
+ readonly readThrough: readonly SecondaryPath[];
316
+ /** The ancestor nodes this table's rows reveal to a grant at their own node. */
317
+ readonly memberReveals: readonly MemberRevealModel[];
318
+ }
319
+ /**
320
+ * A read path to a dimension outside the chain: `column` holds a key of `table`, which owns `dimension` and lies in
321
+ * `chain`. The column's foreign key covers every ancestor the two chains share, so the node lies in the row's own tenant.
322
+ */
323
+ export interface SecondaryPath {
324
+ readonly dimension: string;
325
+ readonly column: string;
326
+ readonly table: string;
327
+ readonly key: string;
328
+ readonly chain: readonly Container[];
329
+ /**
330
+ * The nearest container whose own path this is: the row's `column` of it, its `table` and `key`. `column` above is
331
+ * then the container's column, and the path is read as `T.<via column> in (select C.<key> from C where …)`.
332
+ */
333
+ readonly via: ContainerHop | undefined;
334
+ }
335
+ /** The row's nearest containment column, and the table and key it names. */
336
+ export interface ContainerHop {
337
+ readonly column: string;
338
+ readonly table: string;
339
+ readonly key: string;
340
+ }
341
+ /**
342
+ * A row of `table` that `relation` reaches reveals its node in `dimension`, held in `column`: the relation holds
343
+ * `actions` at that node while `when` admits the row.
344
+ */
345
+ export interface RevealModel {
346
+ readonly relation: string;
347
+ readonly table: string;
348
+ /** The table's key, which the relation's nodes match unless the reveal is matched through `path`. */
349
+ readonly key: string;
350
+ /**
351
+ * The secondary path whose node, or an ancestor of it, the relation's nodes match. A reveal matched through one holds
352
+ * its actions for a `Within`'s reads only, as the path itself does.
353
+ */
354
+ readonly path: SecondaryPath | undefined;
355
+ /** The bound on an insert's delegation through `path`: `undefined` when no handler inserts a row of the table. */
356
+ readonly assign: {
357
+ readonly self: boolean;
358
+ } | undefined;
359
+ readonly dimension: string;
360
+ readonly column: string;
361
+ readonly actions: readonly string[];
362
+ readonly when: ConditionModel | undefined;
363
+ }
364
+ /**
365
+ * A row of `table`, a node of `source` (its own dimension, keyed by `key`), reveals its node in `dimension`, held in
366
+ * `column`: a grant of one of `actions` at the row's node holds the action there too, for a `Within`'s reads, while
367
+ * `when` admits the row. Only a grant at the node itself counts, never one at a container of it: an insert would
368
+ * otherwise reveal the ancestor to the container's members at once.
369
+ */
370
+ export interface MemberRevealModel {
371
+ readonly source: string;
372
+ readonly table: string;
373
+ readonly key: string;
374
+ readonly dimension: string;
375
+ readonly column: string;
376
+ readonly actions: readonly string[];
377
+ readonly when: ConditionModel | undefined;
378
+ }
379
+ /** One relation's source table: what a relation term reads, and what a write of its rows is bounded by. */
380
+ export interface RelationSource {
381
+ readonly relation: string;
382
+ readonly dimension: string;
383
+ readonly table: string;
384
+ /** The source's column that decides the principal: it holds it, or it references the row of `principalPath` that does. */
385
+ readonly principal: string;
386
+ readonly principalPath: PrincipalPath | undefined;
387
+ readonly node: string;
388
+ readonly expires: string | undefined;
389
+ readonly when: ConditionModel | undefined;
390
+ /** `undefined` when the table has no insert action, so no handler writes a row of it. */
391
+ readonly assign: {
392
+ readonly maxDays: number | undefined;
393
+ readonly self: boolean;
394
+ } | undefined;
395
+ }
396
+ /** `S.<principal>` references `key` of `table`, whose `column` holds the principal the source row names. */
397
+ export interface PrincipalPath {
398
+ readonly table: string;
399
+ readonly key: string;
400
+ readonly column: string;
224
401
  }
225
402
  /** Every table's model by name; read it through `modelOf`, never by a plain property access. */
226
403
  export type TableModels = Readonly<Record<string, TableModel>>;
@@ -264,6 +441,20 @@ export declare function visibleFor(model: TableModel, action: string): readonly
264
441
  export declare function updateActions(model: TableModel): readonly string[];
265
442
  /** Whether `names` holds `name`: an operation's actions, the one way a policy check reads a list. */
266
443
  export declare function includes(names: readonly string[], name: unknown): boolean;
444
+ /**
445
+ * A row of the table hands out access that its writer places: it confers a relation, reveals through a path, or reveals
446
+ * its ancestor to the grants at its node. Such a row is written only through the handles, and never moves.
447
+ */
448
+ export declare function delegates(model: TableModel): boolean;
449
+ /**
450
+ * The tables whose rows hand out access, which no raw statement writes, lowercased as the tag compares names: a relation
451
+ * source or a path reveal, whose writer the layer bounds; a member-revealing table, whose key the layer generates so no
452
+ * write revives a node a stale grant still names; and the tables a principal path or a relation's condition reads, whose
453
+ * columns decide whom a source row names and what it confers.
454
+ */
455
+ export declare function accessTablesOf(models: TableModels): readonly string[];
456
+ /** The column of `table` that places a row at a node of `dimension`: its key for its own dimension, else a chain link. */
457
+ export declare function nodeColumnOf(table: Pick<TableModel, 'scope' | 'chain'>, dimension: string): string | undefined;
267
458
  type PolicyOf<P, T> = T extends keyof P ? P[T] : never;
268
459
  type ScopeOf<P, T> = PolicyOf<P, T> extends {
269
460
  readonly scope: infer S;
@@ -11,6 +11,13 @@ import type { StoredFiles } from './stored-files';
11
11
  * the session to read in (a job's transaction, which sees the object it just wrote); a statement of its own otherwise.
12
12
  */
13
13
  export type Locator = (dimension: string, id: string, using?: Session) => Promise<ReadonlyMap<string, string> | null>;
14
+ /** Whether an active row of `relation`'s source names `principal` at `node`: what a Ref held through it is minted on. */
15
+ export type RelationCheck = (relation: string, node: string, principal: string) => Promise<boolean>;
16
+ /**
17
+ * Whether a row `relation` reaches for `principal` reveals `node` of `dimension` now: what a Ref held through a reveal
18
+ * is minted on.
19
+ */
20
+ export type RevealCheck = (relation: string, dimension: string, node: string, principal: string) => Promise<boolean>;
14
21
  /** What the framework writes itself: its own `fabrika_*` tables. Usable once bound. */
15
22
  export interface FrameworkTables {
16
23
  readonly webhooks: WebhookTables;
@@ -25,6 +32,8 @@ export interface FrameworkTables {
25
32
  /** A connection of the layer and its locator. Only its holder can `release` it, which lets the layer be bound again. */
26
33
  export interface ProvisionalBinding {
27
34
  readonly locate: Locator;
35
+ readonly relates: RelationCheck;
36
+ readonly reveals: RevealCheck;
28
37
  release(): void;
29
38
  }
30
39
  export interface FrameworkSide {
@@ -59,9 +59,9 @@ export interface SqlSite {
59
59
  * name outside `schema` and a function outside the reviewed built-ins; on SQLite also a `sqlite_*` name, and a
60
60
  * parameter the text writes itself. workerd's authorizer bounds SQLite's functions, PRAGMAs and DDL.
61
61
  */
62
- export declare function classifySql(parts: readonly string[], mode: SqlMode, database: SqlDatabase): string;
62
+ export declare function classifySql(parts: readonly string[], mode: SqlMode, database: SqlDatabase, sources?: readonly string[]): string;
63
63
  /**
64
64
  * `ctx.sql` of one site. Every call first checks that the work running now is the site's invocation, then classifies
65
65
  * the text, then binds the values; only then does it reach the session, so a refused statement sends nothing.
66
66
  */
67
- export declare function sqlTag(site: SqlSite, database: SqlDatabase, termsOf: ScopeOf): SqlTag;
67
+ export declare function sqlTag(site: SqlSite, database: SqlDatabase, termsOf: ScopeOf, sources: readonly string[]): SqlTag;
@@ -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
- /** Rows of a child table that belong to the parent object the Ref names, over the whole chain. */
71
- export declare function parentPredicate(context: TableContext, grant: RefGrant): OperationNode;
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
- export declare function insertRow<O>(context: TableContext, run: Runner, given: ReadonlyMap<string, Primitive | null>, destination: RefGrant | undefined): Promise<O>;
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
@@ -15,6 +15,12 @@ export type { ColumnTypes, ColumnViews, DefineVocabularyPolicies, NormalizedPoli
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,4 +1,5 @@
1
- import type { ColumnList, ColumnPolicies, FilePolicies, RecordActions, ReferencePolicy, TableAction, TableActions, Tables, VersionColumn } from './policy';
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;
3
4
  declare const viewNames: unique symbol;
4
5
  /**
@@ -35,6 +36,11 @@ export interface TableVocabulary {
35
36
  readonly in?: string;
36
37
  };
37
38
  };
39
+ readonly relations?: {
40
+ readonly [relation: string]: {
41
+ readonly dimension: string;
42
+ };
43
+ };
38
44
  }
39
45
  export type VocabularyAction<V> = V extends {
40
46
  readonly actions: infer A;
@@ -92,6 +98,29 @@ interface WritableFields<Col extends string, A extends string, Ver extends Col,
92
98
  readonly columns?: ColumnPolicies<Col, A>;
93
99
  /** The file columns, each with the file kind it holds (`c.files`): a `uuid` foreign key to `fabrika_files`. */
94
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
+ };
95
124
  }
96
125
  /**
97
126
  * The scope of the dimension the policy states (`S`, its scope as written); for any other name, the dimension alone,
@@ -172,9 +201,15 @@ interface ReadOnlyFields<Col extends string, A extends string> {
172
201
  };
173
202
  };
174
203
  }
175
- /** A view read as a child table: its parent's `Ref` and `Within` read it; nothing writes it (ADR-0072). */
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
+ */
176
208
  type AuthoredChildViewPolicy<Table extends string, Col extends string, A extends string, S, P> = ReadOnlyFields<Col, A> & {
177
209
  readonly scope: ChildScopes<Table, Col, S, P>;
210
+ readonly when?: {
211
+ readonly [action in A]?: ConditionPolicy<Col>;
212
+ };
178
213
  };
179
214
  /** A view's policy as written: a read-only child, or a reference table every site reads. */
180
215
  export type VocabularyViewPolicy<Table extends string, Col extends string, A extends string, S = unknown, P = unknown> = S extends {