@fabrika/db 0.9.0-dev.7 → 0.9.0-dev.71

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.
Files changed (73) hide show
  1. package/package.json +9 -5
  2. package/src/authority.ts +560 -0
  3. package/src/builtins.ts +21 -0
  4. package/src/capability.ts +51 -2
  5. package/src/codegen-sqlite.ts +69 -13
  6. package/src/codegen.ts +181 -42
  7. package/src/condition-nodes.ts +119 -0
  8. package/src/conditions.ts +683 -0
  9. package/src/description.ts +67 -9
  10. package/src/errors.ts +9 -5
  11. package/src/file-columns.ts +52 -10
  12. package/src/framework.ts +5 -2
  13. package/src/guard.ts +365 -15
  14. package/src/index.ts +8 -0
  15. package/src/job-leases.ts +19 -2
  16. package/src/job-wakes.ts +183 -33
  17. package/src/json.ts +187 -0
  18. package/src/list-cursor.ts +82 -0
  19. package/src/migrations-sqlite.ts +93 -0
  20. package/src/migrations.ts +99 -0
  21. package/src/nodes.ts +33 -14
  22. package/src/notifications.ts +152 -0
  23. package/src/orm-types.ts +216 -77
  24. package/src/orm.ts +417 -158
  25. package/src/policy.ts +2043 -131
  26. package/src/push-subscriptions.ts +287 -0
  27. package/src/registry.ts +50 -1
  28. package/src/request-receipts.ts +107 -0
  29. package/src/sql.ts +39 -11
  30. package/src/sqlite-migrate.ts +102 -1
  31. package/src/sqlite.ts +46 -1
  32. package/src/store.ts +1958 -167
  33. package/src/tables.ts +156 -6
  34. package/src/temporal.ts +39 -4
  35. package/src/testing-shared.ts +872 -148
  36. package/src/testing-sqlite.ts +215 -40
  37. package/src/testing.ts +151 -22
  38. package/src/vocabulary-tables.ts +172 -26
  39. package/types/src/authority.d.ts +147 -0
  40. package/types/src/builtins.d.ts +2 -0
  41. package/types/src/capability.d.ts +50 -2
  42. package/types/src/codegen-sqlite.d.ts +8 -1
  43. package/types/src/codegen.d.ts +54 -10
  44. package/types/src/condition-nodes.d.ts +9 -0
  45. package/types/src/conditions.d.ts +151 -0
  46. package/types/src/description.d.ts +40 -9
  47. package/types/src/errors.d.ts +8 -5
  48. package/types/src/file-columns.d.ts +6 -4
  49. package/types/src/framework.d.ts +5 -2
  50. package/types/src/guard.d.ts +24 -0
  51. package/types/src/index.d.ts +4 -3
  52. package/types/src/job-leases.d.ts +8 -1
  53. package/types/src/job-wakes.d.ts +28 -5
  54. package/types/src/json.d.ts +35 -0
  55. package/types/src/list-cursor.d.ts +13 -0
  56. package/types/src/migrations.d.ts +5 -0
  57. package/types/src/nodes.d.ts +5 -1
  58. package/types/src/notifications.d.ts +37 -0
  59. package/types/src/orm-types.d.ts +167 -75
  60. package/types/src/orm.d.ts +21 -0
  61. package/types/src/policy.d.ts +378 -27
  62. package/types/src/push-subscriptions.d.ts +49 -0
  63. package/types/src/registry.d.ts +36 -1
  64. package/types/src/request-receipts.d.ts +22 -0
  65. package/types/src/sql.d.ts +2 -2
  66. package/types/src/sqlite-migrate.d.ts +32 -0
  67. package/types/src/store.d.ts +118 -31
  68. package/types/src/tables.d.ts +8 -2
  69. package/types/src/temporal.d.ts +7 -1
  70. package/types/src/testing-shared.d.ts +125 -26
  71. package/types/src/testing-sqlite.d.ts +17 -2
  72. package/types/src/testing.d.ts +1 -1
  73. package/types/src/vocabulary-tables.d.ts +176 -22
@@ -1,8 +1,13 @@
1
- import type { Insertable, Selectable, SelectType, Updateable } from 'kysely';
1
+ import { READ_ANY } from '@fabrika/auth-core';
2
+ import type { Insertable, InsertType, Selectable, SelectType, Updateable } from 'kysely';
3
+ import { z } from 'zod/v4';
2
4
  import { type NameSet } from './builtins';
5
+ import { type ConditionModel, type ConditionPolicy } from './conditions';
6
+ import type { DeclaredJson } from './description';
7
+ import type { JsonColumnValue, JsonSchema } from './json';
3
8
  import type { Unavailable } from './orm-types';
4
9
  export type { RowOf } from './orm-types';
5
- import type { ColumnTypes, DefineVocabularyPolicies, TableVocabulary } from './vocabulary-tables';
10
+ import type { ColumnTypes, ColumnViews, DefineVocabularyPolicies, TableVocabulary } from './vocabulary-tables';
6
11
  export interface DimensionScope<Col extends string> {
7
12
  readonly dimension: string;
8
13
  readonly key: Col;
@@ -27,12 +32,17 @@ export interface ApplicationScope<Col extends string> {
27
32
  }
28
33
  /** One action, or a list of them: a table that staff and a public procedure both read names both actions. */
29
34
  export type TableAction<A extends string> = A | readonly A[];
35
+ export { READ_ANY };
30
36
  export interface TableActions<A extends string> {
31
37
  readonly read: TableAction<A>;
32
38
  readonly insert?: TableAction<A>;
33
39
  readonly update?: TableAction<A>;
34
40
  readonly delete?: TableAction<A>;
35
41
  }
42
+ /** An application table's actions: its read may be `'any'` instead of its actions (`READ_ANY`). */
43
+ export interface ApplicationActions<A extends string> extends Omit<TableActions<A>, 'read'> {
44
+ readonly read: TableAction<A> | typeof READ_ANY;
45
+ }
36
46
  /**
37
47
  * A queue table's columns (ADR-0060's drain): `due` is when a row is next claimed, `null` once it is done; `attempts`
38
48
  * counts its claims, and a row with `maxAttempts` (default 10) of them is no longer claimed.
@@ -65,28 +75,163 @@ interface WritablePolicy<Col extends string, A extends string, V extends Col, Ac
65
75
  * file reference. The table's actions are the file's authority.
66
76
  */
67
77
  readonly files?: FilePolicies<Col>;
78
+ /** The relations this table's rows confer (`relations` of the vocabulary): each row holds one principal at one node. */
79
+ readonly confers?: {
80
+ readonly [relation: string]: ConfersPolicy<Col>;
81
+ };
82
+ /** Conditions that narrow an action of this table: a row the condition does not admit is out of that action's reach. */
83
+ readonly when?: {
84
+ readonly [action in A]?: ConditionPolicy<Col>;
85
+ };
86
+ /**
87
+ * Relation → action → a condition on the rows that relation reaches for that action: it narrows the relation's terms
88
+ * only, never an IAM grant, a member reveal or a path grant of the action.
89
+ */
90
+ readonly relationWhen?: {
91
+ readonly [relation: string]: {
92
+ readonly [action in A]?: ConditionPolicy<Col>;
93
+ };
94
+ };
95
+ /** Relation of this table's own dimension, or of one it reads through → the ancestor node a row the relation reaches reveals. */
96
+ readonly reveals?: {
97
+ readonly [relation: string]: RevealsPolicy<Col, A>;
98
+ };
99
+ /**
100
+ * Dimension outside the chain → the foreign key column a read reaches a row through: a `Within` of the dimension, or a
101
+ * relation at it, reads the row, and every write follows containment only. No update writes the column. Or
102
+ * `<nearest container column>.<its column>`: the row is read through its nearest container's own read path.
103
+ */
104
+ readonly readThrough?: {
105
+ readonly [dimension: string]: Col | `${Col}.${string}`;
106
+ };
107
+ /**
108
+ * Dimension of `readThrough` → the actions a `Ref` held through that path holds on the table's rows, writes included:
109
+ * a grant of one of them at the node the path names, or above it, reaches the row for that action. Whoever inserts
110
+ * the path's column must hold every listed action at the node it names.
111
+ */
112
+ readonly writeThrough?: {
113
+ readonly [dimension: string]: readonly A[];
114
+ };
115
+ /**
116
+ * This table's own dimension, or one it reads through (`readThrough`) → the ancestor node a row reveals to its
117
+ * members: a grant of one of `actions` at the row's own node, or at the node its path names, holds that action at its
118
+ * node in `to` too, while `when` admits the row.
119
+ */
120
+ readonly memberReveals?: {
121
+ readonly [dimension: string]: MemberRevealsPolicy<Col, A>;
122
+ };
123
+ /**
124
+ * Relation → an access view that lists the rows the relation reaches: the escape path for a rule no structured
125
+ * declaration expresses. The view's SQL decides; the layer binds only the caller's principal and the nodes it holds
126
+ * the relation inside, never a handler value.
127
+ */
128
+ readonly access?: {
129
+ readonly [relation: string]: AccessPolicy;
130
+ };
131
+ }
132
+ /** An access view: `row` names this table's key; `principal` is compared to the caller, `member` to the nodes it holds the relation inside. */
133
+ export interface AccessPolicy {
134
+ readonly view: string;
135
+ readonly row: string;
136
+ readonly principal?: string;
137
+ readonly member?: string;
138
+ }
139
+ /** What a row reveals to a caller with a grant at its node: its node in `to`, for `actions`, while `when` admits the row. */
140
+ export interface MemberRevealsPolicy<Col extends string, A extends string> {
141
+ readonly to: string;
142
+ readonly actions: readonly A[];
143
+ /** A condition on the revealing row, under the same path rule as an action's `when`. */
144
+ readonly when?: ConditionPolicy<Col>;
145
+ /** Through a path, on a table with an insert action: the writer picks the node, and `self` lets it pick its own. */
146
+ readonly assign?: {
147
+ readonly self?: true;
148
+ };
149
+ }
150
+ /**
151
+ * A row of the table that a relation reaches reveals its node in `to`, an ancestor dimension of its chain: the relation
152
+ * then holds `actions` (those of them its roles list in `via`) there, while `when` admits the row.
153
+ */
154
+ export interface RevealsPolicy<Col extends string, A extends string> {
155
+ readonly to: string;
156
+ readonly actions: readonly A[];
157
+ /** A condition on the revealing row, under the same path rule as an action's `when`. */
158
+ readonly when?: ConditionPolicy<Col>;
159
+ /**
160
+ * Required on a reveal matched through `readThrough` of a table with an insert action: the row's writer picks the node,
161
+ * so an insert delegates. Without `self`, an insert that names a node where its writer holds the relation is refused.
162
+ */
163
+ readonly assign?: {
164
+ readonly self?: true;
165
+ };
166
+ }
167
+ /**
168
+ * A relation source: each row says that the principal in `principal` stands in the relation to the node `node` holds,
169
+ * until `expires` (none: no deadline) and while `when` admits the row. Writing a row is delegation.
170
+ */
171
+ export interface ConfersPolicy<Col extends string> {
172
+ /**
173
+ * The column that holds the principal, or `<column>.<principal column>`: one foreign key of the table into the key of
174
+ * the table whose column holds it. The table's `createdBy` names the row's writer (it needs `assign.self`).
175
+ */
176
+ readonly principal: Col | `${Col}.${string}`;
177
+ /** The column that names the node; by default the table's column for the relation's dimension (its key or its chain). */
178
+ readonly node?: Col;
179
+ /** A timestamp column: the row confers nothing once it passed. */
180
+ readonly expires?: Col;
181
+ /** A condition on the row's own columns (no foreign-key path): `{ revoked_at: { isNull: true } }`. */
182
+ readonly when?: ConditionPolicy<Col>;
183
+ /**
184
+ * Inserting a row delegates the relation's actions to its principal, bounded by the inserting Ref's node: at most
185
+ * `maxDays` ahead (which needs `expires`), and to the inserting principal itself only with `self`.
186
+ */
187
+ readonly assign?: {
188
+ readonly maxDays?: number;
189
+ readonly self?: true;
190
+ };
68
191
  }
69
192
  /** A table's file columns: column → the name of a file kind of the contract (`c.files`). */
70
193
  export type FilePolicies<Col extends string> = {
71
194
  readonly [C in Col]?: string;
72
195
  };
196
+ /** A table's JSON columns with a declared shape: column → a zod schema whose output is JSON. */
197
+ export type JsonPolicies<Col extends string> = {
198
+ readonly [C in Col]?: JsonSchema;
199
+ };
200
+ /** The JSON columns of a generated row type: those whose write type is a JSON value (`JsonColumnValue`). */
201
+ export type JsonColumn<Row> = {
202
+ [K in keyof Row & string]: [Exclude<InsertType<Row[K]>, null | undefined>] extends [JsonColumnValue] ? [JsonColumnValue] extends [Exclude<InsertType<Row[K]>, null | undefined>] ? K : never : never;
203
+ }[keyof Row & string];
204
+ /** The schemas table `T`'s policy declares for its JSON columns (`json`); none when it declares none. */
205
+ export type JsonSchemasOf<P, T> = PolicyOf<P, T> extends {
206
+ readonly json: infer J;
207
+ } ? J : Record<never, never>;
208
+ /** `null` where column `K` of the generated row is nullable. */
209
+ type NullOf<Row, K> = K extends keyof Row ? (null extends InsertType<Row[K]> ? null : never) : never;
73
210
  /**
74
- * A column's own actions. `read`: only a capability of exactly that action reads, filters or sorts by the column;
75
- * without one it falls back to the table's read action. `write`: only a capability of that action updates the column,
76
- * and the table's update action does not, so the column is not in `update`.
211
+ * A row (`'output'`) or a write (`'input'`) of a table whose policy declares JSON schemas `J`: each declared column is
212
+ * the schema's output or input, and `null` where the column is nullable. A row also carries the declared columns in
213
+ * `DeclaredJson`, which keeps them out of a condition and an order (`ComparableColumn`).
214
+ */
215
+ export type WithJson<R, J, Row, Io extends 'input' | 'output'> = [keyof J] extends [never] ? R : {
216
+ [K in keyof R]: K extends keyof J ? J[K] extends JsonSchema ? (Io extends 'output' ? z.output<J[K]> : z.input<J[K]>) | NullOf<Row, K> : R[K] : R[K];
217
+ } & (Io extends 'output' ? DeclaredJson<keyof J & string> : unknown);
218
+ /**
219
+ * A column's own actions. `read`: only a capability of one of those actions reads, filters or sorts by the column;
220
+ * without one it falls back to the table's read action. `write`: only a capability of one of those actions updates the
221
+ * column, so the column is not in `update`; a listed table update action writes it beside its update list.
77
222
  */
78
223
  export type ColumnPolicies<Col extends string, A extends string> = {
79
224
  readonly [C in Col]?: ColumnPolicy<A>;
80
225
  };
81
- /** A column's own read action, its own write action, or both. */
226
+ /** A column's own read actions and its own write actions (each one, or a list as a table operation takes), or both. */
82
227
  export interface ColumnPolicy<A extends string> {
83
- readonly read?: A;
84
- readonly write?: A;
228
+ readonly read?: TableAction<A>;
229
+ readonly write?: TableAction<A>;
85
230
  }
86
231
  /** A table no handler writes (a reference table) declares column read actions only. */
87
232
  export type ColumnReadPolicies<Col extends string, A extends string> = {
88
233
  readonly [C in Col]?: {
89
- readonly read: A;
234
+ readonly read: TableAction<A>;
90
235
  };
91
236
  };
92
237
  export interface DimensionPolicy<Col extends string, A extends string, V extends Col = Col> extends WritablePolicy<Col, A, V, RecordActions<A>> {
@@ -96,7 +241,7 @@ export interface ChildPolicy<Table extends string, Col extends string, A extends
96
241
  readonly scope: ChildScope<Table, Col>;
97
242
  }
98
243
  /** An application table: written like a dimension table, by an `app` endpoint whose action is the operation's. */
99
- export interface ApplicationPolicy<Col extends string, A extends string, V extends Col = Col> extends WritablePolicy<Col, A, V> {
244
+ export interface ApplicationPolicy<Col extends string, A extends string, V extends Col = Col> extends WritablePolicy<Col, A, V, ApplicationActions<A>> {
100
245
  readonly scope: ApplicationScope<Col>;
101
246
  }
102
247
  interface ReadOnlyPolicy<Col extends string, A extends string> {
@@ -134,6 +279,15 @@ export interface TableColumns<Col extends string = string, Table extends string
134
279
  readonly timestamps?: readonly Col[];
135
280
  /** On SQLite, the columns declared `BOOLEAN`, stored as 0 or 1 and read as a boolean; absent when none. */
136
281
  readonly booleans?: readonly Col[];
282
+ /** The JSON columns (`jsonb`, or declared `JSONB` on SQLite), written as a JSON value and never compared; absent when none. */
283
+ readonly json?: readonly Col[];
284
+ /**
285
+ * The columns declared NOT NULL; absent when none. A foreign key that holds a row's tenant needs its ancestor columns
286
+ * among them: under MATCH SIMPLE a null in one skips the whole key.
287
+ */
288
+ readonly notNull?: readonly Col[];
289
+ /** A view: read-only, with no foreign key; absent for a table. */
290
+ readonly view?: true;
137
291
  }
138
292
  /** The columns and foreign keys of every table, generated next to the schema types. */
139
293
  export type ColumnList<DB> = {
@@ -162,9 +316,17 @@ export type ScopeModel = {
162
316
  readonly reason: string;
163
317
  readonly key: string;
164
318
  };
165
- /** Each operation's actions: any one of them allows it. `read` names at least one; an empty list allows no operation. */
319
+ /**
320
+ * Each operation's actions: any one of them allows it. `read` names at least one, unless `readAny`; an empty list
321
+ * allows no operation.
322
+ */
166
323
  export interface ActionModel {
167
324
  readonly read: readonly string[];
325
+ /**
326
+ * An application table read with `'any'`: every `app` endpoint, job and webhook reads it, whatever its action; never a
327
+ * public procedure or a scoped site. `read` is empty then, and a `when` narrows only the writes of its action.
328
+ */
329
+ readonly readAny: boolean;
168
330
  readonly insert: readonly string[];
169
331
  readonly update: readonly string[];
170
332
  readonly delete: readonly string[];
@@ -196,23 +358,147 @@ export interface TableModel {
196
358
  readonly queue: QueueModel | undefined;
197
359
  /** The file columns, each with its file kind: a reference to `fabrika_files`, written only with a file reference. */
198
360
  readonly files: Readonly<Record<string, string>>;
199
- /** The columns that declare their own read action, as declared (also those equal to the table's). */
200
- readonly columnReads: Readonly<Record<string, string>>;
201
- /** The columns that declare their own write action: only a capability of it updates them. */
202
- readonly columnWrites: Readonly<Record<string, string>>;
361
+ /** The columns that declare their own read actions, as declared (also those equal to the table's). */
362
+ readonly columnReads: Readonly<Record<string, readonly string[]>>;
363
+ /** The columns that declare their own write actions, as declared: only a capability of one of them updates them. */
364
+ readonly columnWrites: Readonly<Record<string, readonly string[]>>;
203
365
  /** The columns of Postgres type `date`, read as `YYYY-MM-DD` text. */
204
366
  readonly dates: NameSet;
205
367
  /** The `timestamp` and `timestamptz` columns: a write's text must be strict ISO 8601 with an offset. */
206
368
  readonly timestamps: NameSet;
207
369
  /** On SQLite, the `BOOLEAN` columns, stored as 0 or 1. */
208
370
  readonly booleans: NameSet;
371
+ /** The JSON columns: a write binds a JSON value's text, and no condition, order or aggregate names one. */
372
+ readonly json: NameSet;
373
+ /** The JSON columns the policy declares a schema for: each write is parsed by it, and each read value too. */
374
+ readonly jsonSchemas: Readonly<Record<string, JsonSchema>>;
375
+ /** The columns the column list declares NOT NULL. */
376
+ readonly notNull: NameSet;
209
377
  /** The table's foreign keys, from the column list; a containment column must be one to its container's key. */
210
378
  readonly foreignKeys: readonly ForeignKey[];
379
+ /** A view: a child or reference table no operation but a read reaches, whose containment only its SQL states. */
380
+ readonly view: boolean;
211
381
  /**
212
382
  * The containment links this table carries a column for, nearest first: a dimension table's container and
213
383
  * the container's own chain; a child's parent and the parent's chain. Every predicate of a Ref covers all.
214
384
  */
215
385
  readonly chain: readonly Container[];
386
+ /** The relations this table's rows confer, each with its columns, its condition and its delegation bound. */
387
+ readonly confers: readonly RelationSource[];
388
+ /** The condition each action of the table holds a row under; an action without one is not narrowed. */
389
+ readonly when: Readonly<Record<string, ConditionModel>>;
390
+ /** Relation → action → the condition the rows that relation reaches hold for that action under; ANDed onto its terms only. */
391
+ readonly relationWhen: Readonly<Record<string, Readonly<Record<string, ConditionModel>>>>;
392
+ /** The ancestor nodes this table's rows reveal to a relation of its own dimension or of one it reads through. */
393
+ readonly reveals: readonly RevealModel[];
394
+ /** The secondary read paths: a dimension outside the chain, by the column that holds the row's node in it. */
395
+ readonly readThrough: readonly SecondaryPath[];
396
+ /** The ancestor nodes this table's rows reveal to a grant at their own node. */
397
+ readonly memberReveals: readonly MemberRevealModel[];
398
+ /** The access views of the relations whose reach a view's SQL decides. */
399
+ readonly access: readonly AccessModel[];
400
+ }
401
+ /** A relation's access view over this table: `T.<key> in (select V.<row> from V where <bindings>)`. */
402
+ export interface AccessModel {
403
+ readonly relation: string;
404
+ /** The relation's dimension: a hold inside a node of it alone binds `member`. */
405
+ readonly dimension: string;
406
+ readonly view: string;
407
+ readonly row: string;
408
+ readonly principal: string | undefined;
409
+ readonly member: string | undefined;
410
+ }
411
+ /**
412
+ * A read path to a dimension outside the chain: `column` holds a key of `table`, which owns `dimension` and lies in
413
+ * `chain`. The column's foreign key covers every ancestor the two chains share, so the node lies in the row's own tenant.
414
+ */
415
+ export interface SecondaryPath {
416
+ readonly dimension: string;
417
+ readonly column: string;
418
+ readonly table: string;
419
+ readonly key: string;
420
+ readonly chain: readonly Container[];
421
+ /**
422
+ * The nearest container whose own path this is: the row's `column` of it, its `table` and `key`. `column` above is
423
+ * then the container's column, and the path is read as `T.<via column> in (select C.<key> from C where …)`.
424
+ */
425
+ readonly via: ContainerHop | undefined;
426
+ /** The actions a `Ref` held through the path holds on the table's rows (`writeThrough`); a `Within` reads through every path. */
427
+ readonly writes: readonly string[];
428
+ }
429
+ /** The row's nearest containment column, and the table and key it names. */
430
+ export interface ContainerHop {
431
+ readonly column: string;
432
+ readonly table: string;
433
+ readonly key: string;
434
+ }
435
+ /**
436
+ * A row of `table` that `relation` reaches reveals its node in `dimension`, held in `column`: the relation holds
437
+ * `actions` at that node while `when` admits the row.
438
+ */
439
+ export interface RevealModel {
440
+ readonly relation: string;
441
+ readonly table: string;
442
+ /** The table's key, which the relation's nodes match unless the reveal is matched through `path`. */
443
+ readonly key: string;
444
+ /**
445
+ * The secondary path whose node, or an ancestor of it, the relation's nodes match. A reveal matched through one holds
446
+ * a `Ref` of an action only where the path does (`writeThrough`).
447
+ */
448
+ readonly path: SecondaryPath | undefined;
449
+ /** The bound on an insert's delegation through `path`: `undefined` when no handler inserts a row of the table. */
450
+ readonly assign: {
451
+ readonly self: boolean;
452
+ } | undefined;
453
+ readonly dimension: string;
454
+ readonly column: string;
455
+ readonly actions: readonly string[];
456
+ readonly when: ConditionModel | undefined;
457
+ }
458
+ /**
459
+ * A row of `table`, a node of `source` (its own dimension, keyed by `key`), reveals its node in `dimension`, held in
460
+ * `column`: a grant of one of `actions` at the row's node holds the action there too, for a `Within`'s reads, while
461
+ * `when` admits the row. Only a grant at the node itself counts, never one at a container of it: an insert would
462
+ * otherwise reveal the ancestor to the container's members at once.
463
+ */
464
+ export interface MemberRevealModel {
465
+ readonly source: string;
466
+ readonly table: string;
467
+ /** The column a grant's nodes match: the table's key, or `path`'s column. */
468
+ readonly key: string;
469
+ /** The secondary path whose node a grant names; a `Ref` holds an action through it only where the path does. */
470
+ readonly path: SecondaryPath | undefined;
471
+ /** The bound on an insert's pick of the node through `path`: `undefined` when no handler inserts a row of the table. */
472
+ readonly assign: {
473
+ readonly self: boolean;
474
+ } | undefined;
475
+ readonly dimension: string;
476
+ readonly column: string;
477
+ readonly actions: readonly string[];
478
+ readonly when: ConditionModel | undefined;
479
+ }
480
+ /** One relation's source table: what a relation term reads, and what a write of its rows is bounded by. */
481
+ export interface RelationSource {
482
+ readonly relation: string;
483
+ readonly dimension: string;
484
+ readonly table: string;
485
+ /** The source's column that decides the principal: it holds it, or it references the row of `principalPath` that does. */
486
+ readonly principal: string;
487
+ readonly principalPath: PrincipalPath | undefined;
488
+ readonly node: string;
489
+ readonly expires: string | undefined;
490
+ readonly when: ConditionModel | undefined;
491
+ /** `undefined` when the table has no insert action, so no handler writes a row of it. */
492
+ readonly assign: {
493
+ readonly maxDays: number | undefined;
494
+ readonly self: boolean;
495
+ } | undefined;
496
+ }
497
+ /** `S.<principal>` references `key` of `table`, whose `column` holds the principal the source row names. */
498
+ export interface PrincipalPath {
499
+ readonly table: string;
500
+ readonly key: string;
501
+ readonly column: string;
216
502
  }
217
503
  /** Every table's model by name; read it through `modelOf`, never by a plain property access. */
218
504
  export type TableModels = Readonly<Record<string, TableModel>>;
@@ -234,39 +520,104 @@ export declare function modelOf(models: TableModels, table: string): TableModel
234
520
  */
235
521
  export declare function defineTables<const V extends TableVocabulary, C extends object>(vocabulary: V, columns: C): VocabularyTables<V, C>;
236
522
  /** `defineTables(vocabulary, columns)` answers the policies' builder, or names the fix for an untyped column list. */
237
- export type VocabularyTables<V, C> = [ColumnTypes<C>] extends [never] ? Unavailable<'the column list carries no row types: import columns from the generated src/db/schema.generated.ts (fabrika db codegen)'> : DefineVocabularyPolicies<ColumnTypes<C>, V>;
523
+ export type VocabularyTables<V, C> = [ColumnTypes<C>] extends [never] ? Unavailable<'the column list carries no row types: import columns from the generated src/db/schema.generated.ts (fabrika db codegen)'> : DefineVocabularyPolicies<ColumnTypes<C>, V, ColumnViews<C>>;
238
524
  /**
239
525
  * The same policies read from values nobody type-checked: `src/db/tables.ts` and its vocabulary as the static reader
240
526
  * has them from a commit. Every shape `defineTables` refuses at runtime is refused here, through the same parse.
241
527
  */
242
528
  export declare function readTables(columns: unknown, policies: unknown, vocabulary: unknown): Tables<unknown, unknown>;
243
529
  /**
244
- * The columns a capability of `action` updates in `model`: the update list for the table's update action, the
245
- * columns that declare it as their own write action for any other; none for an action that writes nothing here.
530
+ * The columns a capability of `action` updates in `model`: the update list for a table update action, and every
531
+ * column that lists it among its own write actions; none for an action that writes nothing here.
246
532
  */
247
533
  export declare function updatableBy(model: TableModel, action: string): NameSet;
534
+ /**
535
+ * The columns any of `actions` updates in `model`, as one role holding them: the union of `updatableBy` over them.
536
+ * One action's set is `updatableBy`'s own.
537
+ */
538
+ export declare function updatableByAny(model: TableModel, actions: readonly string[]): NameSet;
248
539
  /** Whether the table names `action`: one of its table actions, or a column's own write action. */
249
540
  export declare function namesAction(model: TableModel, action: string): boolean;
250
541
  /**
251
- * The columns a capability of `action` reads: every one that declares no read action of its own, or exactly this
252
- * one. A capability reads its own row, so a write `Ref` sees the columns of its action and none of another's.
542
+ * The columns a capability of `action` reads: every one that declares no read action of its own, or one whose own
543
+ * read actions include this one. A capability reads its own row, so a write `Ref` sees the columns of its action and none of another's.
253
544
  */
254
545
  export declare function visibleFor(model: TableModel, action: string): readonly string[];
546
+ /**
547
+ * The actions whose reach one read answers: an `app` capability's held actions that admit the operation, each row
548
+ * passing where one of them admits it (`conditionedAny`), and whether their `when` narrows the rows (`narrowed`), which
549
+ * it does not in a table read with `'any'`.
550
+ */
551
+ export interface ReadReach {
552
+ readonly actions: readonly string[];
553
+ readonly narrowed: boolean;
554
+ }
555
+ /** The reach of a capability of one action, whose own condition narrows its rows. */
556
+ export declare function soleReach(action: string): ReadReach;
557
+ /**
558
+ * Whether every row a read of `reach` answers is a row `action` reads alone: it is one of the reaching actions, and either
559
+ * no `when` of its own narrows it or it alone reaches the rows. A column of its own is shown only then, so several held
560
+ * actions never put one action's column on another action's rows.
561
+ */
562
+ export declare function readsAsAction(model: TableModel, action: string, reach: ReadReach): boolean;
563
+ /**
564
+ * The columns a read of `reach` answers, the union of what each reaching action reads alone: every one that declares no
565
+ * read action of its own, and one that does where that action reads every answered row (`readsAsAction`).
566
+ */
567
+ export declare function visibleThrough(model: TableModel, reach: ReadReach): readonly string[];
568
+ /** Whether one of a column's own read actions reads every row a read of `reach` answers (`readsAsAction`). */
569
+ export declare function readsAsOneOf(model: TableModel, actions: readonly string[], reach: ReadReach): boolean;
255
570
  /** Every action that updates a column of `model`: its update action and each column's own write action. */
256
571
  export declare function updateActions(model: TableModel): readonly string[];
257
572
  /** Whether `names` holds `name`: an operation's actions, the one way a policy check reads a list. */
258
573
  export declare function includes(names: readonly string[], name: unknown): boolean;
574
+ /**
575
+ * A row of the table hands out access that its writer places: it confers a relation, reveals through a path, or reveals
576
+ * its ancestor to the grants at its node. Such a row is written only through the handles, and never moves.
577
+ */
578
+ export declare function delegates(model: TableModel): boolean;
579
+ /**
580
+ * The tables whose rows hand out access, which no raw statement writes, lowercased as the tag compares names: a relation
581
+ * source, a path reveal or a path column that holds actions (`writeThrough`), whose writer the layer bounds; a
582
+ * member-revealing table, whose key the layer generates so no write revives a node a stale grant still names; and the
583
+ * tables a principal path or a relation's condition reads, whose columns decide whom a source row names and what it
584
+ * confers.
585
+ */
586
+ export declare function accessTablesOf(models: TableModels): readonly string[];
587
+ /** A path column whose insert hands out the listed actions at the node it names: the bound its writer is held to. */
588
+ export interface PathWriter {
589
+ readonly path: SecondaryPath;
590
+ /** The path's own `writes` and those of every table that reads the same dimension through this table's column. */
591
+ readonly actions: readonly string[];
592
+ }
593
+ /**
594
+ * The columns of `model` whose insert hands out actions through a secondary path: its own paths that hold actions, and
595
+ * its paths that a table below follows through it (`'<link>.<column>'`) with actions of its own. Such a table is written
596
+ * only through the handles, so no write sets the column past the bound.
597
+ */
598
+ export declare function pathWriters(models: Readonly<Record<string, TableModel>>, model: TableModel): readonly PathWriter[];
599
+ /** The column of `table` that places a row at a node of `dimension`: its key for its own dimension, else a chain link. */
600
+ export declare function nodeColumnOf(table: Pick<TableModel, 'scope' | 'chain'>, dimension: string): string | undefined;
259
601
  type PolicyOf<P, T> = T extends keyof P ? P[T] : never;
260
602
  type ScopeOf<P, T> = PolicyOf<P, T> extends {
261
603
  readonly scope: infer S;
262
604
  } ? S : never;
263
605
  export type Operation = 'read' | 'insert' | 'update' | 'delete';
264
- /** The action, or the union of the actions, of operation `Op` on table `T`; `never` when the policy names none. */
606
+ /**
607
+ * The action, or the union of the actions, of operation `Op` on table `T`; `never` when the policy names none, and for
608
+ * the read of a table read with `'any'` (`ReadsAny`).
609
+ */
265
610
  export type ActionOf<P, T, Op extends Operation> = PolicyOf<P, T> extends {
266
611
  readonly actions: infer Acts;
267
612
  } ? Acts extends {
268
613
  readonly [K in Op]: infer A;
269
- } ? A extends string ? A : A extends readonly (infer L extends string)[] ? L : never : never : never;
614
+ } ? A extends typeof READ_ANY ? never : A extends string ? A : A extends readonly (infer L extends string)[] ? L : never : never : never;
615
+ /** Whether table `T` is read with `'any'`: by every `app` endpoint, job and webhook, whatever its action. */
616
+ export type ReadsAny<P, T> = PolicyOf<P, T> extends {
617
+ readonly actions: {
618
+ readonly read: typeof READ_ANY;
619
+ };
620
+ } ? true : false;
270
621
  export type DimensionOf<P, T> = ScopeOf<P, T> extends {
271
622
  readonly dimension: infer D extends string;
272
623
  } ? D : never;
@@ -315,16 +666,16 @@ type TableReadAction<P, T> = PolicyOf<P, T> extends {
315
666
  readonly read: infer A;
316
667
  };
317
668
  } ? A : ActionOf<P, T, 'read'>;
318
- /** Columns whose own read action is not the table's: no read by the table's read action returns them. */
669
+ /** Columns whose own read actions leave out one of the table's: no read by the table's read action returns them. */
319
670
  type RestrictedColumns<P, T> = [ColumnPoliciesOf<P, T>] extends [never] ? never : {
320
671
  [K in keyof ColumnPoliciesOf<P, T>]: ColumnPoliciesOf<P, T>[K] extends {
321
672
  readonly read: infer R;
322
- } ? ([TableReadAction<P, T>] extends [R] ? never : K) : never;
673
+ } ? ([TableReadAction<P, T>] extends [R extends readonly (infer L extends string)[] ? L : R] ? never : K) : never;
323
674
  }[keyof ColumnPoliciesOf<P, T>];
324
675
  /** A row of table `T` as a store reads it, by the table's read action. */
325
- export type PolicyRow<DB, P, T> = T extends keyof DB ? Flat<Omit<Selectable<DB[T]>, RestrictedColumns<P, T>>> : never;
326
- export type InsertValues<DB, P, T> = T extends keyof DB ? Flat<Pick<Insertable<DB[T]>, ListedColumns<P, T, 'insert'> & keyof Insertable<DB[T]>>> : never;
327
- export type UpdateValues<DB, P, T> = T extends keyof DB ? Flat<Pick<Updateable<DB[T]>, ListedColumns<P, T, 'update'> & keyof Updateable<DB[T]>>> : never;
676
+ export type PolicyRow<DB, P, T> = T extends keyof DB ? WithJson<Flat<Omit<Selectable<DB[T]>, RestrictedColumns<P, T>>>, JsonSchemasOf<P, T>, DB[T], 'output'> : never;
677
+ export type InsertValues<DB, P, T> = T extends keyof DB ? WithJson<Flat<Pick<Insertable<DB[T]>, ListedColumns<P, T, 'insert'> & keyof Insertable<DB[T]>>>, JsonSchemasOf<P, T>, DB[T], 'input'> : never;
678
+ export type UpdateValues<DB, P, T> = T extends keyof DB ? WithJson<Flat<Pick<Updateable<DB[T]>, ListedColumns<P, T, 'update'> & keyof Updateable<DB[T]>>>, JsonSchemasOf<P, T>, DB[T], 'input'> : never;
328
679
  /** The values a store inserts into table `T` of a `defineTables` result: `TableInsert<typeof tables, 'deals'>`. */
329
680
  export type TableInsert<Tb, T> = Tb extends Tables<infer DB, infer P> ? InsertValues<DB, P, T> : never;
330
681
  /** The values a store updates in table `T` of a `defineTables` result: `TableUpdate<typeof tables, 'deals'>`. */
@@ -0,0 +1,49 @@
1
+ import { type Session } from './database';
2
+ export declare const PUSH_SUBSCRIPTION_LIMITS: Readonly<{
3
+ /** Live subscriptions one principal holds; the next record drops the oldest. */
4
+ perPrincipal: 10;
5
+ /** How long a tombstone answers a replacement: 30 days. */
6
+ tombstoneRetentionMs: number;
7
+ }>;
8
+ /** A subscription as the page hands it over, with the VAPID public key it was made with. */
9
+ export interface PushSubscriptionKeys {
10
+ readonly endpoint: string;
11
+ /** The browser's P-256 key, base64url. */
12
+ readonly p256dh: string;
13
+ /** The browser's authentication secret, base64url. */
14
+ readonly auth: string;
15
+ /** The VAPID public key the browser subscribed with, base64url. */
16
+ readonly vapidKey: string;
17
+ }
18
+ /** Whose a browser's endpoint is, as the caller may learn it: its own live row, another principal's, or none (a tombstone included). */
19
+ export type PushBinding = 'mine' | 'other' | 'unknown';
20
+ /** The caller's live subscription and the newest notification the inbox answered for it, or `null` when it is not theirs. */
21
+ export type PushInboxSubscription = {
22
+ readonly cursor: string | null;
23
+ } | null;
24
+ /** `recorded`, or why not: the endpoint is bound to another principal, or the replaced endpoint is not the caller's. */
25
+ export type PushRecordOutcome = 'recorded' | 'taken' | 'not_replaceable';
26
+ export interface PushSubscriptions {
27
+ /** Whose `endpoint` is, for `principal`. */
28
+ binding(session: Session, principal: string, endpoint: string): Promise<PushBinding>;
29
+ /**
30
+ * Records `subscription` for `principal` under `id` (a new row's key, a UUIDv7 made now, which is also a new row's
31
+ * cursor, so it reads only notifications written after it; an existing row of the caller keeps its key and cursor),
32
+ * replacing the endpoint `replace` when it names one, at `now`. Prunes a batch of expired tombstones first, and
33
+ * drops the principal's oldest live rows past the bound after. An outcome but `recorded` changes no subscription: the
34
+ * old row is removed only after the new one was written.
35
+ */
36
+ record(session: Session, principal: string, id: string, subscription: PushSubscriptionKeys, replace: string | null, now: number): Promise<PushRecordOutcome>;
37
+ /**
38
+ * What the inbox reads `endpoint` as for `principal`: the caller's live subscription and its cursor, or none. A live row
39
+ * of another principal is deleted here (§7): the browser it wakes is someone else's now, and stops waking for them.
40
+ */
41
+ inbox(session: Session, principal: string, endpoint: string): Promise<PushInboxSubscription>;
42
+ /** Moves the cursor of `principal`'s live `endpoint` forward to `cursor`, never back. */
43
+ advance(session: Session, principal: string, endpoint: string, cursor: string): Promise<void>;
44
+ /** Removes `principal`'s own row (or tombstone) of `endpoint`; `false` when it has none. */
45
+ remove(session: Session, principal: string, endpoint: string): Promise<boolean>;
46
+ /** The push service called `endpoint` gone: its live row becomes a tombstone at `now`. `false` when it had none. */
47
+ retire(session: Session, endpoint: string, now: number): Promise<boolean>;
48
+ }
49
+ export declare function pushSubscriptions(): PushSubscriptions;