@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.
package/src/tables.ts CHANGED
@@ -1,11 +1,31 @@
1
1
  // @fabrika/db/tables — the table policies of `src/db/tables.ts` as data. Browser- and Worker-safe: no Bun API and no
2
2
  // driver, so `@fabrika/app/contract` and the static contract reader in `@fabrika/check` import it.
3
3
 
4
- import type { ColumnPolicyArtifact, TableActionArtifact, TablePolicyArtifact, TableScope } from '@fabrika/auth-core'
4
+ import type {
5
+ ColumnPolicyArtifact,
6
+ ConditionArtifact,
7
+ ConfersArtifact,
8
+ MemberRevealsArtifact,
9
+ RevealsArtifact,
10
+ TableActionArtifact,
11
+ TablePolicyArtifact,
12
+ TableScope,
13
+ } from '@fabrika/auth-core'
5
14
  import { type ApplicationRowsPlan, planApplicationRows } from './application-rows'
6
15
  import { freeze, namesOf, ownEntry, ownKeys, setOwn } from './builtins'
16
+ import { conditionArtifact, type ConditionModel } from './conditions'
7
17
  import { PolicyError } from './errors'
8
- import { modelOf, type ScopeModel, type TableModel, tableModels, type Tables } from './policy'
18
+ import {
19
+ accessTablesOf,
20
+ type MemberRevealModel,
21
+ modelOf,
22
+ type RelationSource,
23
+ type RevealModel,
24
+ type ScopeModel,
25
+ type TableModel,
26
+ tableModels,
27
+ type Tables,
28
+ } from './policy'
9
29
 
10
30
  export { applicationRowRefusalText } from './application-rows'
11
31
  export type { ApplicationRowRefusal, ApplicationRowsPlan } from './application-rows'
@@ -114,6 +134,15 @@ export function applicationRowsPlan<DB, P>(
114
134
  return planApplicationRows(tableModels(tables), rows)
115
135
  }
116
136
 
137
+ /**
138
+ * The tables whose rows hand out access, lowercased: a relation source or a path reveal, a member-revealing table, and
139
+ * the tables a principal path or a relation's condition reads. Only the data layer writes them; the migration classifier
140
+ * refuses database code that does.
141
+ */
142
+ export function accessTables<DB, P>(tables: Tables<DB, P>): readonly string[] {
143
+ return accessTablesOf(tableModels(tables))
144
+ }
145
+
117
146
  /** Every table's policy in the form the contract artifact carries, by table name. */
118
147
  export type TablePolicies = Readonly<Record<string, TablePolicyArtifact>>
119
148
 
@@ -157,7 +186,77 @@ function artifactOf(model: TableModel): TablePolicyArtifact {
157
186
  columns,
158
187
  ...(ownKeys(model.files).length === 0 ? {} : { files: { ...model.files } }),
159
188
  ...(model.view ? { view: true } : {}),
189
+ ...(model.confers.length === 0 ? {} : { confers: confersArtifact(model.confers) }),
190
+ ...(ownKeys(model.when).length === 0 ? {} : { when: whenArtifact(model.when) }),
191
+ ...(model.reveals.length === 0 ? {} : { reveals: revealsArtifact(model.reveals) }),
192
+ ...(model.readThrough.length === 0 ? {} : { readThrough: readThroughArtifact(model) }),
193
+ ...(model.memberReveals.length === 0 ? {} : { memberReveals: memberRevealsArtifact(model.memberReveals) }),
194
+ }
195
+ }
196
+
197
+ function memberRevealsArtifact(reveals: readonly MemberRevealModel[]): Record<string, MemberRevealsArtifact> {
198
+ const artifact: Record<string, MemberRevealsArtifact> = {}
199
+ for (let index = 0; index < reveals.length; index++) {
200
+ const reveal = reveals[index]
201
+ if (reveal === undefined) continue
202
+ setOwn(artifact, reveal.source, {
203
+ to: reveal.dimension,
204
+ actions: [...reveal.actions],
205
+ ...(reveal.when === undefined ? {} : { when: conditionArtifact(reveal.when) }),
206
+ })
207
+ }
208
+ return artifact
209
+ }
210
+
211
+ function readThroughArtifact(model: TableModel): Record<string, string> {
212
+ const artifact: Record<string, string> = {}
213
+ for (let index = 0; index < model.readThrough.length; index++) {
214
+ const path = model.readThrough[index]
215
+ if (path !== undefined) setOwn(artifact, path.dimension, path.via === undefined ? path.column : `${path.via.column}.${path.column}`)
216
+ }
217
+ return artifact
218
+ }
219
+
220
+ function revealsArtifact(reveals: readonly RevealModel[]): Record<string, RevealsArtifact> {
221
+ const artifact: Record<string, RevealsArtifact> = {}
222
+ for (let index = 0; index < reveals.length; index++) {
223
+ const reveal = reveals[index]
224
+ if (reveal === undefined) continue
225
+ setOwn(artifact, reveal.relation, {
226
+ to: reveal.dimension,
227
+ actions: [...reveal.actions],
228
+ ...(reveal.when === undefined ? {} : { when: conditionArtifact(reveal.when) }),
229
+ ...(reveal.assign === undefined ? {} : { assign: { self: reveal.assign.self } }),
230
+ })
231
+ }
232
+ return artifact
233
+ }
234
+
235
+ function confersArtifact(sources: readonly RelationSource[]): Record<string, ConfersArtifact> {
236
+ const confers: Record<string, ConfersArtifact> = {}
237
+ for (let index = 0; index < sources.length; index++) {
238
+ const source = sources[index]
239
+ if (source === undefined) continue
240
+ setOwn(confers, source.relation, {
241
+ principal: source.principalPath === undefined ? source.principal : `${source.principal}.${source.principalPath.column}`,
242
+ node: source.node,
243
+ expires: source.expires ?? null,
244
+ ...(source.when === undefined ? {} : { when: conditionArtifact(source.when) }),
245
+ assign: source.assign === undefined ? null : { maxDays: source.assign.maxDays ?? null, self: source.assign.self },
246
+ })
247
+ }
248
+ return confers
249
+ }
250
+
251
+ function whenArtifact(when: Readonly<Record<string, ConditionModel>>): Record<string, ConditionArtifact> {
252
+ const artifact: Record<string, ConditionArtifact> = {}
253
+ const actions = ownKeys(when)
254
+ for (let index = 0; index < actions.length; index++) {
255
+ const action = actions[index] ?? ''
256
+ const condition = ownEntry(when, action)
257
+ if (condition !== undefined) setOwn(artifact, action, conditionArtifact(condition))
160
258
  }
259
+ return artifact
161
260
  }
162
261
 
163
262
  /** One action as its text, so a single-action policy keeps the artifact and digest it had; several as a list; none as `null`. */
@@ -1,12 +1,16 @@
1
1
  // The table policies of the vocabulary form, `defineTables(vocabulary, columns)`: how an application writes them, with
2
2
  // containment taken from the vocabulary, and the normalized policies the kit and the handles read. Types only.
3
3
 
4
+ import type { ConditionPolicy } from './conditions'
4
5
  import type {
5
6
  ColumnList,
6
7
  ColumnPolicies,
8
+ ConfersPolicy,
7
9
  FilePolicies,
10
+ MemberRevealsPolicy,
8
11
  RecordActions,
9
12
  ReferencePolicy,
13
+ RevealsPolicy,
10
14
  TableAction,
11
15
  TableActions,
12
16
  Tables,
@@ -42,6 +46,7 @@ export type ColumnViews<C> = typeof viewNames extends keyof C
42
46
  export interface TableVocabulary {
43
47
  readonly actions: { readonly [action: string]: string }
44
48
  readonly dimensions: { readonly [dimension: string]: { readonly in?: string } }
49
+ readonly relations?: { readonly [relation: string]: { readonly dimension: string } }
45
50
  }
46
51
 
47
52
  export type VocabularyAction<V> = V extends { readonly actions: infer A } ? keyof A & string : never
@@ -96,6 +101,19 @@ interface WritableFields<Col extends string, A extends string, Ver extends Col,
96
101
  readonly columns?: ColumnPolicies<Col, A>
97
102
  /** The file columns, each with the file kind it holds (`c.files`): a `uuid` foreign key to `fabrika_files`. */
98
103
  readonly files?: FilePolicies<Col>
104
+ /** Relation → how a row of this table confers it: whom it names, at which node, until when. */
105
+ readonly confers?: { readonly [relation: string]: ConfersPolicy<Col> }
106
+ /** Action → the condition a row must meet for it, whoever holds it. */
107
+ readonly when?: { readonly [action in A]?: ConditionPolicy<Col> }
108
+ /** Relation of the table's own dimension → the ancestor node a row it reaches reveals, and for which actions. */
109
+ readonly reveals?: { readonly [relation: string]: RevealsPolicy<Col, A> }
110
+ /**
111
+ * Dimension outside the chain → the foreign key column a `Within`'s read reaches a row through, no update writes it; or
112
+ * `<nearest container column>.<its column>`, the nearest container's own path.
113
+ */
114
+ readonly readThrough?: { readonly [dimension: string]: Col | `${Col}.${string}` }
115
+ /** The table's own dimension → the ancestor node a row reveals to a grant at its node, for a `Within`'s reads. */
116
+ readonly memberReveals?: { readonly [dimension: string]: MemberRevealsPolicy<Col, A> }
99
117
  }
100
118
 
101
119
  /**
@@ -167,9 +185,13 @@ interface ReadOnlyFields<Col extends string, A extends string> {
167
185
  readonly columns?: { readonly [C in Col]?: { readonly read: A; readonly write?: never } }
168
186
  }
169
187
 
170
- /** A view read as a child table: its parent's `Ref` and `Within` read it; nothing writes it (ADR-0072). */
188
+ /**
189
+ * A view read as a child table: its parent's `Ref` and `Within` read it; nothing writes it (ADR-0072). Its `when` narrows
190
+ * its read actions; a path may start at its parent column, to the parent row.
191
+ */
171
192
  type AuthoredChildViewPolicy<Table extends string, Col extends string, A extends string, S, P> = ReadOnlyFields<Col, A> & {
172
193
  readonly scope: ChildScopes<Table, Col, S, P>
194
+ readonly when?: { readonly [action in A]?: ConditionPolicy<Col> }
173
195
  }
174
196
 
175
197
  /** A view's policy as written: a read-only child, or a reference table every site reads. */
@@ -0,0 +1,66 @@
1
+ import { type OperationNode } from 'kysely';
2
+ import type { RefGrant, WithinGrant } from './capability';
3
+ import type { ViewParentLink } from './guard';
4
+ import { type Container, type MemberRevealModel, type RelationSource, type RevealModel, type SecondaryPath, type TableModel } from './policy';
5
+ /** A relation of the layer: its source table, and the tables whose rows it reaches reveal an ancestor node. */
6
+ export interface LayerRelation extends RelationSource {
7
+ readonly reveals: readonly RevealModel[];
8
+ }
9
+ /** Every relation of the layer, by name, with its source table. */
10
+ export type RelationSources = Readonly<Record<string, LayerRelation>>;
11
+ /** Every member reveal of the layer, by the dimension whose grants it reveals to. */
12
+ export type MemberReveals = Readonly<Record<string, readonly MemberRevealModel[]>>;
13
+ export declare function relationSource(relations: RelationSources, relation: string): LayerRelation;
14
+ /** A source row that confers its relation now: unexpired, and admitted by the relation's own condition. */
15
+ export declare function activeRow(source: RelationSource, now: Date): readonly OperationNode[];
16
+ /**
17
+ * A source row naming `principal`: `S.<principal> = $me`, or through its path `S.<column> in (select P.<key> from P where
18
+ * P.<column> = $me)`. Every check of a source row's principal is built here.
19
+ */
20
+ export declare function namesPrincipal(source: RelationSource, principal: string): OperationNode;
21
+ /**
22
+ * The rows of `table` whose `path` column names one of `nodes` of `dimension`: `T.<column> in (nodes)` at the path's own
23
+ * dimension, `T.<column> in (select D.<key> from D where D.<link> in (nodes))` at one above it; `undefined` elsewhere. A
24
+ * path through the nearest container reads the container's: `T.<link> in (select C.<key> from C where <C's term>)`.
25
+ */
26
+ export declare function throughPath(table: string, path: SecondaryPath, dimension: string, nodes: OperationNode): OperationNode | undefined;
27
+ /** `select R.<ancestor> from R where R.<key> in (<the relation's nodes>) and <when>`: the nodes a reveal holds `principal` at. */
28
+ export declare function revealedNodes(source: RelationSource, reveal: RevealModel, principal: string, now: Date): OperationNode;
29
+ /**
30
+ * The rows of `model` a relation reaches for `principal` under `action`: `T.<node> in (select S.<node> from S where
31
+ * S.<principal> = $me and <active>)` where `model` lies at a node of the relation's dimension, and one
32
+ * `T.<ancestor> in (<revealed nodes>)` per reveal of `action` to an ancestor `model` lies at. Empty where neither holds.
33
+ * A `Within`'s read (`reads`) also follows the secondary paths: the column `model` reaches the relation's dimension
34
+ * through, and the reveals matched through one. Every other statement follows containment only.
35
+ */
36
+ export declare function relationTerms(relations: RelationSources, model: TableModel, relation: string, principal: string, action: string, now: Date, reads: boolean): readonly OperationNode[];
37
+ /**
38
+ * The rows of `model` a `Within`'s read reaches through member reveals: per grant it carries at a revealing dimension,
39
+ * `T.<ancestor> in (select R.<ancestor> from R where R.<key> in (values) and <when>)` for each reveal of `action` to an
40
+ * ancestor `model` lies at. A grant at a dimension that reveals no node for `action` is refused: the framework passes
41
+ * none.
42
+ */
43
+ export declare function memberTerms(members: MemberReveals, model: TableModel, grant: WithinGrant, now: Date): readonly OperationNode[];
44
+ /** The condition `action` holds a row of `model` under, or `undefined` when the action is not narrowed there. */
45
+ export declare function actionCondition(model: TableModel, action: string, now: Date): OperationNode | undefined;
46
+ /**
47
+ * `branch`, narrowed by the condition of `action`: `<reach> and (<when>)`, for a global grant `true and (<when>)`, the
48
+ * one shape the guard admits a condition in, so a condition never stands in for the reach.
49
+ */
50
+ export declare function conditioned(model: TableModel, action: string, branch: OperationNode | 'all', now: Date): OperationNode;
51
+ /**
52
+ * A Ref's predicate over `links` of `model`: one equality per link of its chain (after `row`, the equality that
53
+ * addresses one row by its key), then, when the Ref is held through relations, a row of one of them at the node, and
54
+ * its action's condition. A relation that does not reach `model` leaves the Ref unenforceable there, never
55
+ * unconditioned.
56
+ */
57
+ export declare function refScope(relations: RelationSources, model: TableModel, links: readonly Container[], grant: RefGrant, now: Date, row?: OperationNode): OperationNode;
58
+ /**
59
+ * The rows of a child view whose parent row `reached` admits: `V.<column> in (select P.<key> from P where <reached>)`,
60
+ * after `row` when it addresses one row, then the view's own condition of `action`. For `all`, every parent row, so a
61
+ * row whose parent column names none is still read by no capability. No column of the view places a row: its
62
+ * containment rests on the parent's foreign keys (ADR-0072).
63
+ */
64
+ export declare function viewScope(model: TableModel, parent: ViewParentLink, action: string, reached: OperationNode | 'all', now: Date, row?: OperationNode): OperationNode;
65
+ /** The relation rows a Ref held through relations needs at `model`, or `undefined` for a Ref an IAM grant holds. */
66
+ export declare function relationGate(relations: RelationSources, model: TableModel, grant: RefGrant, now: Date): OperationNode | undefined;
@@ -40,3 +40,5 @@ export declare function setOwn<V>(record: Record<string, V>, key: string, value:
40
40
  export declare function defineValue(target: object, key: string, value: unknown): void;
41
41
  /** An own property of a record, or undefined: an inherited one is never read. */
42
42
  export declare function ownEntry<V>(record: Readonly<Record<string, V>>, key: string): V | undefined;
43
+ /** A copy of a plain object's own data properties; anything else (an instance, a symbol key, an accessor) is refused. */
44
+ export declare function plainRecord(value: unknown, what: string): Readonly<Record<string, unknown>>;
@@ -47,6 +47,11 @@ export interface RefGrant {
47
47
  readonly id: string;
48
48
  /** The values of the containers this object lies in, by dimension (`workspace → w1` for a note). */
49
49
  readonly chain: ReadonlyMap<string, string>;
50
+ /**
51
+ * The relations the caller holds the action through on this object or a container, when no grant holds it: every
52
+ * statement of the Ref then also requires a row of one of them. Empty for a Ref an IAM grant holds.
53
+ */
54
+ readonly relations: readonly string[];
50
55
  }
51
56
  /** The values of one container dimension in which the caller holds the action: `organization → [o1]`. */
52
57
  export interface ContainerGrant {
@@ -75,6 +80,16 @@ export interface WithinGrant {
75
80
  * the action: a row that lies in one of their values is listed, as a single-object read of it is allowed.
76
81
  */
77
82
  readonly contained: readonly ContainerGrant[];
83
+ /**
84
+ * The relations through which the caller also holds the action (`~rel` entries of its token): a row lies in the
85
+ * `Within` too where a row of one of them names the caller at the row's node, read by the statement itself.
86
+ */
87
+ readonly relations: readonly string[];
88
+ /**
89
+ * The caller's grants of the action at nodes whose rows reveal the `Within`'s dimension or a container of it to their
90
+ * members (`memberReveals`): a row under a node such a row reveals is read too, the statement judging the revealing row.
91
+ */
92
+ readonly revealing: readonly ContainerGrant[];
78
93
  }
79
94
  /** An `app` endpoint's grant: the action everywhere in the application, which the proxy checked was held globally. */
80
95
  export interface AppGrant {
@@ -0,0 +1,9 @@
1
+ import { type OperationNode } from 'kysely';
2
+ import { type ConditionModel } from './conditions';
3
+ /** The predicate of `condition` over `table`'s rows; `now` is the statement's clock, bound as a value. */
4
+ export declare function conditionNode(table: string, condition: ConditionModel, now: Date): OperationNode;
5
+ /**
6
+ * `select table.key from table where <where>`: the one subquery shape the guard admits in a scope predicate. Without
7
+ * `where`, every row's key: only a child view's parent subquery takes that form.
8
+ */
9
+ export declare function subquery(table: string, key: string, where?: OperationNode): OperationNode;
@@ -0,0 +1,151 @@
1
+ import type { ConditionArtifact } from '@fabrika/auth-core';
2
+ import { type NameSet } from './builtins';
3
+ import type { Primitive } from './description';
4
+ /** A literal a condition compares with. `{ now: true }` is the statement's clock, never the text `now`. */
5
+ export type ConditionLiteral = string | number | boolean;
6
+ export interface NowOperand {
7
+ readonly now: true;
8
+ }
9
+ /** The parsed clock operand, compared by identity: no literal, the text `now` included, is ever it. */
10
+ export declare const NOW: NowOperand;
11
+ /** One test of a column: exactly one operator. */
12
+ export interface ColumnTest {
13
+ readonly eq?: ConditionLiteral;
14
+ readonly notEq?: ConditionLiteral;
15
+ readonly in?: readonly ConditionLiteral[];
16
+ readonly notIn?: readonly ConditionLiteral[];
17
+ readonly isNull?: boolean;
18
+ readonly lt?: ConditionLiteral | NowOperand;
19
+ readonly lte?: ConditionLiteral | NowOperand;
20
+ readonly gt?: ConditionLiteral | NowOperand;
21
+ readonly gte?: ConditionLiteral | NowOperand;
22
+ }
23
+ /** `{ some: { table: … } }` or `{ none: { table: … } }` at the end of a path: over the rows naming the row it reaches. */
24
+ export type RowsTest = {
25
+ readonly some: {
26
+ readonly [table: string]: ConditionPolicy;
27
+ };
28
+ } | {
29
+ readonly none: {
30
+ readonly [table: string]: ConditionPolicy;
31
+ };
32
+ };
33
+ /**
34
+ * A condition as a policy writes it: `{ column: test, … }` (all of them), `{ 'fk_column.column': test }` along a
35
+ * foreign key, `{ anyOf: […] }`, `{ allOf: […] }`, `{ not: … }`, or `{ some: { table: … } }` and `{ none: { table: … } }`
36
+ * over the rows of `table` whose foreign key names the row; `{ 'fk_column': { some: { table: … } } }` over the rows
37
+ * naming the row a path reaches.
38
+ */
39
+ export type ConditionPolicy<Col extends string = string> = {
40
+ readonly anyOf: readonly ConditionPolicy<Col>[];
41
+ } | {
42
+ readonly allOf: readonly ConditionPolicy<Col>[];
43
+ } | {
44
+ readonly not: ConditionPolicy<Col>;
45
+ } | {
46
+ readonly some: {
47
+ readonly [table: string]: ConditionPolicy;
48
+ };
49
+ } | {
50
+ readonly none: {
51
+ readonly [table: string]: ConditionPolicy;
52
+ };
53
+ } | {
54
+ readonly [C in Col | `${Col}.${string}`]?: ColumnTest | RowsTest;
55
+ };
56
+ export type ConditionOperator = 'eq' | 'notEq' | 'in' | 'notIn' | 'isNull' | 'lt' | 'lte' | 'gt' | 'gte';
57
+ /** One foreign-key hop of a path: `column` of the table it leaves references `key` of `table`. */
58
+ export interface ConditionHop {
59
+ readonly column: string;
60
+ readonly table: string;
61
+ readonly key: string;
62
+ }
63
+ export type ConditionModel = {
64
+ readonly kind: 'all';
65
+ readonly terms: readonly ConditionModel[];
66
+ } | {
67
+ readonly kind: 'any';
68
+ readonly terms: readonly ConditionModel[];
69
+ } | {
70
+ readonly kind: 'not';
71
+ readonly term: ConditionModel;
72
+ } | {
73
+ /**
74
+ * `key in (select table.column from table where <term>)`, or its negation for `none`, over the row `hops` reach
75
+ * from the condition's own row (none: the row itself).
76
+ */
77
+ readonly kind: 'some';
78
+ readonly hops: readonly ConditionHop[];
79
+ readonly negated: boolean;
80
+ readonly key: string;
81
+ readonly table: string;
82
+ readonly column: string;
83
+ readonly term: ConditionModel;
84
+ } | {
85
+ readonly kind: 'test';
86
+ readonly hops: readonly ConditionHop[];
87
+ readonly column: string;
88
+ readonly operator: ConditionOperator;
89
+ readonly value: ConditionLiteral | readonly ConditionLiteral[] | boolean | NowOperand;
90
+ };
91
+ /** What the parse reads of each table: its columns, its foreign keys and its typed columns. */
92
+ export interface ConditionTable {
93
+ readonly name: string;
94
+ readonly columns: NameSet;
95
+ readonly foreignKeys: readonly {
96
+ readonly columns: readonly string[];
97
+ readonly table: string;
98
+ readonly references: readonly string[];
99
+ }[];
100
+ readonly timestamps: NameSet;
101
+ readonly json: NameSet;
102
+ /** The columns the column list declares NOT NULL: a path under `not` follows only these. */
103
+ readonly notNull: NameSet;
104
+ }
105
+ /** A path follows at most this many foreign keys: each hop is one more subquery in every statement the condition scopes. */
106
+ export declare const MAX_CONDITION_HOPS = 3;
107
+ /**
108
+ * Whether a path may follow `from.column` to the row of `to` whose `key` it holds. A hop reads its target unscoped, so
109
+ * the rule admits only one that stays where the row is (its own container or parent), stays in its tenant (a foreign
110
+ * key that also holds every ancestor the two tables share), or reaches what every site reads (a reference table).
111
+ * `backward` is a `some` reading the rows of `from` that name a row of `to`: those must stay in that row's tenant, so
112
+ * a reference row, which every tenant's rows name, is no shortcut there.
113
+ */
114
+ export type HopRule = (from: string, column: string, to: string, key: string, backward: boolean) => boolean;
115
+ /** Parses `input` as a condition over `table`. `hops` is the rule a path's foreign keys are held to. */
116
+ export declare function parseCondition(input: unknown, table: ConditionTable, tables: (name: string) => ConditionTable | undefined, what: string, hops: HopRule): ConditionModel;
117
+ export declare function splitPath(path: string): readonly string[];
118
+ export declare function singleForeignKey(table: ConditionTable, name: string, what: string): ConditionHop;
119
+ /** The tables a condition's subqueries read, each with the column it selects: what the guard admits under it. */
120
+ export declare function conditionReach(condition: ConditionModel, select: (table: string, column: string) => void): void;
121
+ /** A test's single literal; a list is compared only with in or notIn. */
122
+ export declare function scalar(value: ConditionLiteral | readonly ConditionLiteral[] | boolean | NowOperand): ConditionLiteral;
123
+ /**
124
+ * One step of the rows an insert's condition reads: from the rows reached so far, the rows of `table` whose `to` column
125
+ * holds their `from` column. A path's hop goes to the row a foreign key names (`from` the key column, `to` the target's
126
+ * key); a `some` goes back to the rows naming it (`from` the referenced key, `to` the naming column).
127
+ */
128
+ export interface ReadStep {
129
+ readonly from: string;
130
+ readonly table: string;
131
+ readonly to: string;
132
+ }
133
+ /**
134
+ * Every sequence of steps from an insert's own row to the rows its condition reads, each one no other extends: the rows
135
+ * the insert locks, step by step, before it judges them. Empty when the condition reads the row alone.
136
+ */
137
+ export declare function readPaths(condition: ConditionModel): readonly (readonly ReadStep[])[];
138
+ /**
139
+ * Why `condition` cannot be judged on an inserted row, or `undefined` when it can. The insert locks every row its
140
+ * condition reads, which holds a column test and a row a `some` found until the commit, but not a row inserted meanwhile:
141
+ * that turns `none`, and a `some` under `not`, false. And no row names the inserted row before it exists.
142
+ */
143
+ export declare function insertProblem(condition: ConditionModel, table: string): string | undefined;
144
+ /**
145
+ * The value of `condition` on an insert's own row, under SQL's three-valued logic: a NULL compared is unknown, `not`
146
+ * of unknown stays unknown, and only `true` passes. A condition with a path is judged in SQL on the written row instead.
147
+ */
148
+ export declare function rowSatisfies(condition: ConditionModel, row: ReadonlyMap<string, Primitive | null>, now: Date): boolean;
149
+ export declare function hasOwnCondition(record: Readonly<Record<string, ConditionModel>>, action: string): boolean;
150
+ /** A condition as the artifact records it: the policy's own form, a path written back as `fk_column.column`. */
151
+ export declare function conditionArtifact(condition: ConditionModel): ConditionArtifact;
@@ -1,3 +1,4 @@
1
+ import type { OperationNode } from 'kysely';
1
2
  import { type NameSet } from './builtins';
2
3
  export type Primitive = string | number | bigint | boolean | Date;
3
4
  export type ComparisonOperator = '=' | '<>' | '<' | '<=' | '>' | '>=';
@@ -45,6 +46,8 @@ export interface JoinShape {
45
46
  export interface JoinState {
46
47
  readonly join: JoinShape;
47
48
  readonly select: SelectState;
49
+ /** The child's condition for the action the select holds, which the layer adds to the ON; never the builder's. */
50
+ readonly condition?: OperationNode;
48
51
  }
49
52
  export interface SelectState {
50
53
  /** Identifies the description a callback received, so it cannot return one from elsewhere. */
@@ -11,7 +11,7 @@ export type { DeliveryClaim, NewWebhookBinding, WebhookBinding, WebhookTables }
11
11
  export type { JobLeases, JobLeaseState, JobOutcome, LeaseChange, LeaseProjection, OverdueClaim } from './job-leases';
12
12
  export type { FinishWakes, JobWakes, NewWake, Wake, WakeReason } from './job-wakes';
13
13
  export type { DataAccess, DataHandles, DataSite } from './orm';
14
- export type { FrameworkTables, Locator, ProvisionalBinding } from './registry';
14
+ export type { FrameworkTables, Locator, ProvisionalBinding, RelationCheck, RevealCheck } from './registry';
15
15
  export type { SqlMode, SqlSite, SqlTag, SqlValue } from './sql';
16
16
  export type { FileColumnRef, QuarantinedStoredFile, ServedFile, StagedFile, StoredFileMeta, StoredFiles, StoredFileStatus } from './stored-files';
17
17
  /** Whether `value` is an object `scoped(tables)` created, rather than a look-alike with the same fields. */
@@ -18,6 +18,22 @@ export interface GuardContext {
18
18
  readonly lock?: 'share' | 'update' | 'claim';
19
19
  /** An insert that does nothing on a conflict of its listed columns: `on conflict (columns) do nothing`, and no other. */
20
20
  readonly onConflict?: 'nothing';
21
+ /**
22
+ * The tables a scope predicate's subqueries may read, each with the columns one may select: a relation's source by
23
+ * its node column, a condition path's target by its key. Without it, a scope holds no subquery.
24
+ */
25
+ readonly subqueries?: Readonly<Record<string, readonly string[]>>;
26
+ /**
27
+ * A child view's parent: its scope may read the view's rows through `<column> in (select P.<key> from P where <P's
28
+ * reach>)`. Without it, a scope holds no such subquery.
29
+ */
30
+ readonly through?: ViewParentLink | undefined;
31
+ }
32
+ /** The parent a child view's scope reads its rows through: the view's parent column, the parent table and its key. */
33
+ export interface ViewParentLink {
34
+ readonly column: string;
35
+ readonly table: string;
36
+ readonly key: string;
21
37
  }
22
38
  /** Refuses any operation tree outside the allowlist. Runs on the final tree, immediately before it is compiled. */
23
39
  export declare function assertAllowedTree(root: OperationNode, context: GuardContext): void;
@@ -8,6 +8,8 @@ export declare function allOf(nodes: readonly OperationNode[]): OperationNode;
8
8
  /** One node, or the OR of several in a parenthesis: inside an AND, `a and b or c` would read as `(a and b) or c`. */
9
9
  export declare function anyOf(nodes: readonly OperationNode[]): OperationNode;
10
10
  export declare function inList(table: string, name: string, values: readonly string[]): OperationNode;
11
+ /** `($1, $2, …)`: bound values, as the right operand of an `in`. */
12
+ export declare function valueList(values: readonly string[]): OperationNode;
11
13
  /** The scope of a `Within` that is not limited to scope values. */
12
14
  export declare function everything(): OperationNode;
13
15
  /** LIKE treats `\` as its escape character by default, so the text matches literally. */
@@ -173,6 +173,18 @@ type ChainDimensions<P, T, V> = PolicyOf<P, T> extends {
173
173
  } ? DimensionOf<P, Parent> | ChainDimensions<P, Parent, V> : never : never;
174
174
  /** The dimensions whose `Within` lists table `T`: every dimension of its chain, and a top-level table's own. */
175
175
  type ReachOf<P, T, V> = [ContainerOf<P, T>] extends [never] ? DimensionOf<P, T> | ChainDimensions<P, T, V> : ChainDimensions<P, T, V>;
176
+ /**
177
+ * The dimensions outside its chain whose `Within` reads table `T` through a foreign key (`readThrough`); a child view's
178
+ * are its parent's, as it is read through its parent row.
179
+ */
180
+ type ReadThroughOf<P, T> = PolicyOf<P, T> extends {
181
+ readonly readThrough: infer R;
182
+ } ? keyof R & string : PolicyOf<P, T> extends {
183
+ readonly view: true;
184
+ readonly scope: {
185
+ readonly child: infer Parent extends string;
186
+ };
187
+ } ? ReadThroughOf<P, Parent> : never;
176
188
  /**
177
189
  * A row as reads return it: the generated interface itself where no column has a separate write type, so a
178
190
  * diagnostic names it (`DealsTable`), and Kysely's `Selectable` of it otherwise.
@@ -214,9 +226,12 @@ export type DbIndex<DB, P, V = unknown> = {
214
226
  readonly listed: {
215
227
  readonly [T in DimensionTableName<P> as ListDimensionOf<P, T>]: T;
216
228
  };
217
- /** Dimension → the tables an `over` of it lists: each whose containment chain holds it, and a top-level table its own. */
229
+ /**
230
+ * Dimension → the tables an `over` of it lists: each whose containment chain holds it, a top-level table its own, and
231
+ * each that reads through it (`readThrough`).
232
+ */
218
233
  readonly within: {
219
- readonly [T in DimensionTableName<P> | ChildTableName<P> as ReachOf<P, T, V>]: T;
234
+ readonly [T in DimensionTableName<P> | ChildTableName<P> as ReachOf<P, T, V> | ReadThroughOf<P, T>]: T;
220
235
  };
221
236
  readonly references: ReferenceTableName<P>;
222
237
  /** The application tables: an `app` endpoint reads and writes them. */