@fabrika/db 0.9.0-dev.6 → 0.9.0-dev.61

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 +59 -11
  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 +55 -9
  10. package/src/errors.ts +8 -4
  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 +124 -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 +208 -74
  24. package/src/orm.ts +417 -158
  25. package/src/policy.ts +1973 -141
  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 +1933 -168
  33. package/src/tables.ts +156 -6
  34. package/src/temporal.ts +8 -1
  35. package/src/testing-shared.ts +758 -140
  36. package/src/testing-sqlite.ts +212 -40
  37. package/src/testing.ts +151 -22
  38. package/src/vocabulary-tables.ts +159 -27
  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 +31 -9
  47. package/types/src/errors.d.ts +7 -4
  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 +18 -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 +165 -73
  60. package/types/src/orm.d.ts +21 -0
  61. package/types/src/policy.d.ts +347 -23
  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 +4 -1
  70. package/types/src/testing-shared.d.ts +112 -23
  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 +169 -22
@@ -1,8 +1,10 @@
1
+ import { READ_ANY } from '@fabrika/auth-core';
1
2
  import type { Insertable, Selectable, SelectType, Updateable } from 'kysely';
2
3
  import { type NameSet } from './builtins';
4
+ import { type ConditionModel, type ConditionPolicy } from './conditions';
3
5
  import type { Unavailable } from './orm-types';
4
6
  export type { RowOf } from './orm-types';
5
- import type { ColumnTypes, DefineVocabularyPolicies, TableVocabulary } from './vocabulary-tables';
7
+ import type { ColumnTypes, ColumnViews, DefineVocabularyPolicies, TableVocabulary } from './vocabulary-tables';
6
8
  export interface DimensionScope<Col extends string> {
7
9
  readonly dimension: string;
8
10
  readonly key: Col;
@@ -27,12 +29,17 @@ export interface ApplicationScope<Col extends string> {
27
29
  }
28
30
  /** One action, or a list of them: a table that staff and a public procedure both read names both actions. */
29
31
  export type TableAction<A extends string> = A | readonly A[];
32
+ export { READ_ANY };
30
33
  export interface TableActions<A extends string> {
31
34
  readonly read: TableAction<A>;
32
35
  readonly insert?: TableAction<A>;
33
36
  readonly update?: TableAction<A>;
34
37
  readonly delete?: TableAction<A>;
35
38
  }
39
+ /** An application table's actions: its read may be `'any'` instead of its actions (`READ_ANY`). */
40
+ export interface ApplicationActions<A extends string> extends Omit<TableActions<A>, 'read'> {
41
+ readonly read: TableAction<A> | typeof READ_ANY;
42
+ }
36
43
  /**
37
44
  * A queue table's columns (ADR-0060's drain): `due` is when a row is next claimed, `null` once it is done; `attempts`
38
45
  * counts its claims, and a row with `maxAttempts` (default 10) of them is no longer claimed.
@@ -65,28 +72,141 @@ interface WritablePolicy<Col extends string, A extends string, V extends Col, Ac
65
72
  * file reference. The table's actions are the file's authority.
66
73
  */
67
74
  readonly files?: FilePolicies<Col>;
75
+ /** The relations this table's rows confer (`relations` of the vocabulary): each row holds one principal at one node. */
76
+ readonly confers?: {
77
+ readonly [relation: string]: ConfersPolicy<Col>;
78
+ };
79
+ /** Conditions that narrow an action of this table: a row the condition does not admit is out of that action's reach. */
80
+ readonly when?: {
81
+ readonly [action in A]?: ConditionPolicy<Col>;
82
+ };
83
+ /**
84
+ * Relation → action → a condition on the rows that relation reaches for that action: it narrows the relation's terms
85
+ * only, never an IAM grant, a member reveal or a path grant of the action.
86
+ */
87
+ readonly relationWhen?: {
88
+ readonly [relation: string]: {
89
+ readonly [action in A]?: ConditionPolicy<Col>;
90
+ };
91
+ };
92
+ /** Relation of this table's own dimension, or of one it reads through → the ancestor node a row the relation reaches reveals. */
93
+ readonly reveals?: {
94
+ readonly [relation: string]: RevealsPolicy<Col, A>;
95
+ };
96
+ /**
97
+ * Dimension outside the chain → the foreign key column a read reaches a row through: a `Within` of the dimension, or a
98
+ * relation at it, reads the row, and every write follows containment only. No update writes the column. Or
99
+ * `<nearest container column>.<its column>`: the row is read through its nearest container's own read path.
100
+ */
101
+ readonly readThrough?: {
102
+ readonly [dimension: string]: Col | `${Col}.${string}`;
103
+ };
104
+ /**
105
+ * Dimension of `readThrough` → the actions a `Ref` held through that path holds on the table's rows, writes included:
106
+ * a grant of one of them at the node the path names, or above it, reaches the row for that action. Whoever inserts
107
+ * the path's column must hold every listed action at the node it names.
108
+ */
109
+ readonly writeThrough?: {
110
+ readonly [dimension: string]: readonly A[];
111
+ };
112
+ /**
113
+ * This table's own dimension, or one it reads through (`readThrough`) → the ancestor node a row reveals to its
114
+ * 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
115
+ * node in `to` too, while `when` admits the row.
116
+ */
117
+ readonly memberReveals?: {
118
+ readonly [dimension: string]: MemberRevealsPolicy<Col, A>;
119
+ };
120
+ /**
121
+ * Relation → an access view that lists the rows the relation reaches: the escape path for a rule no structured
122
+ * declaration expresses. The view's SQL decides; the layer binds only the caller's principal and the nodes it holds
123
+ * the relation inside, never a handler value.
124
+ */
125
+ readonly access?: {
126
+ readonly [relation: string]: AccessPolicy;
127
+ };
128
+ }
129
+ /** An access view: `row` names this table's key; `principal` is compared to the caller, `member` to the nodes it holds the relation inside. */
130
+ export interface AccessPolicy {
131
+ readonly view: string;
132
+ readonly row: string;
133
+ readonly principal?: string;
134
+ readonly member?: string;
135
+ }
136
+ /** What a row reveals to a caller with a grant at its node: its node in `to`, for `actions`, while `when` admits the row. */
137
+ export interface MemberRevealsPolicy<Col extends string, A extends string> {
138
+ readonly to: string;
139
+ readonly actions: readonly A[];
140
+ /** A condition on the revealing row, under the same path rule as an action's `when`. */
141
+ readonly when?: ConditionPolicy<Col>;
142
+ /** Through a path, on a table with an insert action: the writer picks the node, and `self` lets it pick its own. */
143
+ readonly assign?: {
144
+ readonly self?: true;
145
+ };
146
+ }
147
+ /**
148
+ * A row of the table that a relation reaches reveals its node in `to`, an ancestor dimension of its chain: the relation
149
+ * then holds `actions` (those of them its roles list in `via`) there, while `when` admits the row.
150
+ */
151
+ export interface RevealsPolicy<Col extends string, A extends string> {
152
+ readonly to: string;
153
+ readonly actions: readonly A[];
154
+ /** A condition on the revealing row, under the same path rule as an action's `when`. */
155
+ readonly when?: ConditionPolicy<Col>;
156
+ /**
157
+ * Required on a reveal matched through `readThrough` of a table with an insert action: the row's writer picks the node,
158
+ * so an insert delegates. Without `self`, an insert that names a node where its writer holds the relation is refused.
159
+ */
160
+ readonly assign?: {
161
+ readonly self?: true;
162
+ };
163
+ }
164
+ /**
165
+ * A relation source: each row says that the principal in `principal` stands in the relation to the node `node` holds,
166
+ * until `expires` (none: no deadline) and while `when` admits the row. Writing a row is delegation.
167
+ */
168
+ export interface ConfersPolicy<Col extends string> {
169
+ /**
170
+ * The column that holds the principal, or `<column>.<principal column>`: one foreign key of the table into the key of
171
+ * the table whose column holds it. The table's `createdBy` names the row's writer (it needs `assign.self`).
172
+ */
173
+ readonly principal: Col | `${Col}.${string}`;
174
+ /** The column that names the node; by default the table's column for the relation's dimension (its key or its chain). */
175
+ readonly node?: Col;
176
+ /** A timestamp column: the row confers nothing once it passed. */
177
+ readonly expires?: Col;
178
+ /** A condition on the row's own columns (no foreign-key path): `{ revoked_at: { isNull: true } }`. */
179
+ readonly when?: ConditionPolicy<Col>;
180
+ /**
181
+ * Inserting a row delegates the relation's actions to its principal, bounded by the inserting Ref's node: at most
182
+ * `maxDays` ahead (which needs `expires`), and to the inserting principal itself only with `self`.
183
+ */
184
+ readonly assign?: {
185
+ readonly maxDays?: number;
186
+ readonly self?: true;
187
+ };
68
188
  }
69
189
  /** A table's file columns: column → the name of a file kind of the contract (`c.files`). */
70
190
  export type FilePolicies<Col extends string> = {
71
191
  readonly [C in Col]?: string;
72
192
  };
73
193
  /**
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`.
194
+ * A column's own actions. `read`: only a capability of one of those actions reads, filters or sorts by the column;
195
+ * without one it falls back to the table's read action. `write`: only a capability of one of those actions updates the
196
+ * column, so the column is not in `update`; a listed table update action writes it beside its update list.
77
197
  */
78
198
  export type ColumnPolicies<Col extends string, A extends string> = {
79
199
  readonly [C in Col]?: ColumnPolicy<A>;
80
200
  };
81
- /** A column's own read action, its own write action, or both. */
201
+ /** A column's own read actions and its own write actions (each one, or a list as a table operation takes), or both. */
82
202
  export interface ColumnPolicy<A extends string> {
83
- readonly read?: A;
84
- readonly write?: A;
203
+ readonly read?: TableAction<A>;
204
+ readonly write?: TableAction<A>;
85
205
  }
86
206
  /** A table no handler writes (a reference table) declares column read actions only. */
87
207
  export type ColumnReadPolicies<Col extends string, A extends string> = {
88
208
  readonly [C in Col]?: {
89
- readonly read: A;
209
+ readonly read: TableAction<A>;
90
210
  };
91
211
  };
92
212
  export interface DimensionPolicy<Col extends string, A extends string, V extends Col = Col> extends WritablePolicy<Col, A, V, RecordActions<A>> {
@@ -96,7 +216,7 @@ export interface ChildPolicy<Table extends string, Col extends string, A extends
96
216
  readonly scope: ChildScope<Table, Col>;
97
217
  }
98
218
  /** 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> {
219
+ export interface ApplicationPolicy<Col extends string, A extends string, V extends Col = Col> extends WritablePolicy<Col, A, V, ApplicationActions<A>> {
100
220
  readonly scope: ApplicationScope<Col>;
101
221
  }
102
222
  interface ReadOnlyPolicy<Col extends string, A extends string> {
@@ -134,6 +254,15 @@ export interface TableColumns<Col extends string = string, Table extends string
134
254
  readonly timestamps?: readonly Col[];
135
255
  /** On SQLite, the columns declared `BOOLEAN`, stored as 0 or 1 and read as a boolean; absent when none. */
136
256
  readonly booleans?: readonly Col[];
257
+ /** The JSON columns (`jsonb`, or declared `JSONB` on SQLite), written as a JSON value and never compared; absent when none. */
258
+ readonly json?: readonly Col[];
259
+ /**
260
+ * The columns declared NOT NULL; absent when none. A foreign key that holds a row's tenant needs its ancestor columns
261
+ * among them: under MATCH SIMPLE a null in one skips the whole key.
262
+ */
263
+ readonly notNull?: readonly Col[];
264
+ /** A view: read-only, with no foreign key; absent for a table. */
265
+ readonly view?: true;
137
266
  }
138
267
  /** The columns and foreign keys of every table, generated next to the schema types. */
139
268
  export type ColumnList<DB> = {
@@ -162,9 +291,17 @@ export type ScopeModel = {
162
291
  readonly reason: string;
163
292
  readonly key: string;
164
293
  };
165
- /** Each operation's actions: any one of them allows it. `read` names at least one; an empty list allows no operation. */
294
+ /**
295
+ * Each operation's actions: any one of them allows it. `read` names at least one, unless `readAny`; an empty list
296
+ * allows no operation.
297
+ */
166
298
  export interface ActionModel {
167
299
  readonly read: readonly string[];
300
+ /**
301
+ * An application table read with `'any'`: every `app` endpoint, job and webhook reads it, whatever its action; never a
302
+ * public procedure or a scoped site. `read` is empty then, and a `when` narrows only the writes of its action.
303
+ */
304
+ readonly readAny: boolean;
168
305
  readonly insert: readonly string[];
169
306
  readonly update: readonly string[];
170
307
  readonly delete: readonly string[];
@@ -196,23 +333,145 @@ export interface TableModel {
196
333
  readonly queue: QueueModel | undefined;
197
334
  /** The file columns, each with its file kind: a reference to `fabrika_files`, written only with a file reference. */
198
335
  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>>;
336
+ /** The columns that declare their own read actions, as declared (also those equal to the table's). */
337
+ readonly columnReads: Readonly<Record<string, readonly string[]>>;
338
+ /** The columns that declare their own write actions, as declared: only a capability of one of them updates them. */
339
+ readonly columnWrites: Readonly<Record<string, readonly string[]>>;
203
340
  /** The columns of Postgres type `date`, read as `YYYY-MM-DD` text. */
204
341
  readonly dates: NameSet;
205
342
  /** The `timestamp` and `timestamptz` columns: a write's text must be strict ISO 8601 with an offset. */
206
343
  readonly timestamps: NameSet;
207
344
  /** On SQLite, the `BOOLEAN` columns, stored as 0 or 1. */
208
345
  readonly booleans: NameSet;
346
+ /** The JSON columns: a write binds a JSON value's text, and no condition, order or aggregate names one. */
347
+ readonly json: NameSet;
348
+ /** The columns the column list declares NOT NULL. */
349
+ readonly notNull: NameSet;
209
350
  /** The table's foreign keys, from the column list; a containment column must be one to its container's key. */
210
351
  readonly foreignKeys: readonly ForeignKey[];
352
+ /** A view: a child or reference table no operation but a read reaches, whose containment only its SQL states. */
353
+ readonly view: boolean;
211
354
  /**
212
355
  * The containment links this table carries a column for, nearest first: a dimension table's container and
213
356
  * the container's own chain; a child's parent and the parent's chain. Every predicate of a Ref covers all.
214
357
  */
215
358
  readonly chain: readonly Container[];
359
+ /** The relations this table's rows confer, each with its columns, its condition and its delegation bound. */
360
+ readonly confers: readonly RelationSource[];
361
+ /** The condition each action of the table holds a row under; an action without one is not narrowed. */
362
+ readonly when: Readonly<Record<string, ConditionModel>>;
363
+ /** Relation → action → the condition the rows that relation reaches hold for that action under; ANDed onto its terms only. */
364
+ readonly relationWhen: Readonly<Record<string, Readonly<Record<string, ConditionModel>>>>;
365
+ /** The ancestor nodes this table's rows reveal to a relation of its own dimension or of one it reads through. */
366
+ readonly reveals: readonly RevealModel[];
367
+ /** The secondary read paths: a dimension outside the chain, by the column that holds the row's node in it. */
368
+ readonly readThrough: readonly SecondaryPath[];
369
+ /** The ancestor nodes this table's rows reveal to a grant at their own node. */
370
+ readonly memberReveals: readonly MemberRevealModel[];
371
+ /** The access views of the relations whose reach a view's SQL decides. */
372
+ readonly access: readonly AccessModel[];
373
+ }
374
+ /** A relation's access view over this table: `T.<key> in (select V.<row> from V where <bindings>)`. */
375
+ export interface AccessModel {
376
+ readonly relation: string;
377
+ /** The relation's dimension: a hold inside a node of it alone binds `member`. */
378
+ readonly dimension: string;
379
+ readonly view: string;
380
+ readonly row: string;
381
+ readonly principal: string | undefined;
382
+ readonly member: string | undefined;
383
+ }
384
+ /**
385
+ * A read path to a dimension outside the chain: `column` holds a key of `table`, which owns `dimension` and lies in
386
+ * `chain`. The column's foreign key covers every ancestor the two chains share, so the node lies in the row's own tenant.
387
+ */
388
+ export interface SecondaryPath {
389
+ readonly dimension: string;
390
+ readonly column: string;
391
+ readonly table: string;
392
+ readonly key: string;
393
+ readonly chain: readonly Container[];
394
+ /**
395
+ * The nearest container whose own path this is: the row's `column` of it, its `table` and `key`. `column` above is
396
+ * then the container's column, and the path is read as `T.<via column> in (select C.<key> from C where …)`.
397
+ */
398
+ readonly via: ContainerHop | undefined;
399
+ /** The actions a `Ref` held through the path holds on the table's rows (`writeThrough`); a `Within` reads through every path. */
400
+ readonly writes: readonly string[];
401
+ }
402
+ /** The row's nearest containment column, and the table and key it names. */
403
+ export interface ContainerHop {
404
+ readonly column: string;
405
+ readonly table: string;
406
+ readonly key: string;
407
+ }
408
+ /**
409
+ * A row of `table` that `relation` reaches reveals its node in `dimension`, held in `column`: the relation holds
410
+ * `actions` at that node while `when` admits the row.
411
+ */
412
+ export interface RevealModel {
413
+ readonly relation: string;
414
+ readonly table: string;
415
+ /** The table's key, which the relation's nodes match unless the reveal is matched through `path`. */
416
+ readonly key: string;
417
+ /**
418
+ * The secondary path whose node, or an ancestor of it, the relation's nodes match. A reveal matched through one holds
419
+ * a `Ref` of an action only where the path does (`writeThrough`).
420
+ */
421
+ readonly path: SecondaryPath | undefined;
422
+ /** The bound on an insert's delegation through `path`: `undefined` when no handler inserts a row of the table. */
423
+ readonly assign: {
424
+ readonly self: boolean;
425
+ } | undefined;
426
+ readonly dimension: string;
427
+ readonly column: string;
428
+ readonly actions: readonly string[];
429
+ readonly when: ConditionModel | undefined;
430
+ }
431
+ /**
432
+ * A row of `table`, a node of `source` (its own dimension, keyed by `key`), reveals its node in `dimension`, held in
433
+ * `column`: a grant of one of `actions` at the row's node holds the action there too, for a `Within`'s reads, while
434
+ * `when` admits the row. Only a grant at the node itself counts, never one at a container of it: an insert would
435
+ * otherwise reveal the ancestor to the container's members at once.
436
+ */
437
+ export interface MemberRevealModel {
438
+ readonly source: string;
439
+ readonly table: string;
440
+ /** The column a grant's nodes match: the table's key, or `path`'s column. */
441
+ readonly key: string;
442
+ /** The secondary path whose node a grant names; a `Ref` holds an action through it only where the path does. */
443
+ readonly path: SecondaryPath | undefined;
444
+ /** The bound on an insert's pick of the node through `path`: `undefined` when no handler inserts a row of the table. */
445
+ readonly assign: {
446
+ readonly self: boolean;
447
+ } | undefined;
448
+ readonly dimension: string;
449
+ readonly column: string;
450
+ readonly actions: readonly string[];
451
+ readonly when: ConditionModel | undefined;
452
+ }
453
+ /** One relation's source table: what a relation term reads, and what a write of its rows is bounded by. */
454
+ export interface RelationSource {
455
+ readonly relation: string;
456
+ readonly dimension: string;
457
+ readonly table: string;
458
+ /** The source's column that decides the principal: it holds it, or it references the row of `principalPath` that does. */
459
+ readonly principal: string;
460
+ readonly principalPath: PrincipalPath | undefined;
461
+ readonly node: string;
462
+ readonly expires: string | undefined;
463
+ readonly when: ConditionModel | undefined;
464
+ /** `undefined` when the table has no insert action, so no handler writes a row of it. */
465
+ readonly assign: {
466
+ readonly maxDays: number | undefined;
467
+ readonly self: boolean;
468
+ } | undefined;
469
+ }
470
+ /** `S.<principal>` references `key` of `table`, whose `column` holds the principal the source row names. */
471
+ export interface PrincipalPath {
472
+ readonly table: string;
473
+ readonly key: string;
474
+ readonly column: string;
216
475
  }
217
476
  /** Every table's model by name; read it through `modelOf`, never by a plain property access. */
218
477
  export type TableModels = Readonly<Record<string, TableModel>>;
@@ -234,39 +493,104 @@ export declare function modelOf(models: TableModels, table: string): TableModel
234
493
  */
235
494
  export declare function defineTables<const V extends TableVocabulary, C extends object>(vocabulary: V, columns: C): VocabularyTables<V, C>;
236
495
  /** `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>;
496
+ 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
497
  /**
239
498
  * The same policies read from values nobody type-checked: `src/db/tables.ts` and its vocabulary as the static reader
240
499
  * has them from a commit. Every shape `defineTables` refuses at runtime is refused here, through the same parse.
241
500
  */
242
501
  export declare function readTables(columns: unknown, policies: unknown, vocabulary: unknown): Tables<unknown, unknown>;
243
502
  /**
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.
503
+ * The columns a capability of `action` updates in `model`: the update list for a table update action, and every
504
+ * column that lists it among its own write actions; none for an action that writes nothing here.
246
505
  */
247
506
  export declare function updatableBy(model: TableModel, action: string): NameSet;
507
+ /**
508
+ * The columns any of `actions` updates in `model`, as one role holding them: the union of `updatableBy` over them.
509
+ * One action's set is `updatableBy`'s own.
510
+ */
511
+ export declare function updatableByAny(model: TableModel, actions: readonly string[]): NameSet;
248
512
  /** Whether the table names `action`: one of its table actions, or a column's own write action. */
249
513
  export declare function namesAction(model: TableModel, action: string): boolean;
250
514
  /**
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.
515
+ * The columns a capability of `action` reads: every one that declares no read action of its own, or one whose own
516
+ * 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
517
  */
254
518
  export declare function visibleFor(model: TableModel, action: string): readonly string[];
519
+ /**
520
+ * The actions whose reach one read answers: an `app` capability's held actions that admit the operation, each row
521
+ * passing where one of them admits it (`conditionedAny`), and whether their `when` narrows the rows (`narrowed`), which
522
+ * it does not in a table read with `'any'`.
523
+ */
524
+ export interface ReadReach {
525
+ readonly actions: readonly string[];
526
+ readonly narrowed: boolean;
527
+ }
528
+ /** The reach of a capability of one action, whose own condition narrows its rows. */
529
+ export declare function soleReach(action: string): ReadReach;
530
+ /**
531
+ * Whether every row a read of `reach` answers is a row `action` reads alone: it is one of the reaching actions, and either
532
+ * 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
533
+ * actions never put one action's column on another action's rows.
534
+ */
535
+ export declare function readsAsAction(model: TableModel, action: string, reach: ReadReach): boolean;
536
+ /**
537
+ * The columns a read of `reach` answers, the union of what each reaching action reads alone: every one that declares no
538
+ * read action of its own, and one that does where that action reads every answered row (`readsAsAction`).
539
+ */
540
+ export declare function visibleThrough(model: TableModel, reach: ReadReach): readonly string[];
541
+ /** Whether one of a column's own read actions reads every row a read of `reach` answers (`readsAsAction`). */
542
+ export declare function readsAsOneOf(model: TableModel, actions: readonly string[], reach: ReadReach): boolean;
255
543
  /** Every action that updates a column of `model`: its update action and each column's own write action. */
256
544
  export declare function updateActions(model: TableModel): readonly string[];
257
545
  /** Whether `names` holds `name`: an operation's actions, the one way a policy check reads a list. */
258
546
  export declare function includes(names: readonly string[], name: unknown): boolean;
547
+ /**
548
+ * A row of the table hands out access that its writer places: it confers a relation, reveals through a path, or reveals
549
+ * its ancestor to the grants at its node. Such a row is written only through the handles, and never moves.
550
+ */
551
+ export declare function delegates(model: TableModel): boolean;
552
+ /**
553
+ * The tables whose rows hand out access, which no raw statement writes, lowercased as the tag compares names: a relation
554
+ * source, a path reveal or a path column that holds actions (`writeThrough`), whose writer the layer bounds; a
555
+ * member-revealing table, whose key the layer generates so no write revives a node a stale grant still names; and the
556
+ * tables a principal path or a relation's condition reads, whose columns decide whom a source row names and what it
557
+ * confers.
558
+ */
559
+ export declare function accessTablesOf(models: TableModels): readonly string[];
560
+ /** A path column whose insert hands out the listed actions at the node it names: the bound its writer is held to. */
561
+ export interface PathWriter {
562
+ readonly path: SecondaryPath;
563
+ /** The path's own `writes` and those of every table that reads the same dimension through this table's column. */
564
+ readonly actions: readonly string[];
565
+ }
566
+ /**
567
+ * The columns of `model` whose insert hands out actions through a secondary path: its own paths that hold actions, and
568
+ * its paths that a table below follows through it (`'<link>.<column>'`) with actions of its own. Such a table is written
569
+ * only through the handles, so no write sets the column past the bound.
570
+ */
571
+ export declare function pathWriters(models: Readonly<Record<string, TableModel>>, model: TableModel): readonly PathWriter[];
572
+ /** The column of `table` that places a row at a node of `dimension`: its key for its own dimension, else a chain link. */
573
+ export declare function nodeColumnOf(table: Pick<TableModel, 'scope' | 'chain'>, dimension: string): string | undefined;
259
574
  type PolicyOf<P, T> = T extends keyof P ? P[T] : never;
260
575
  type ScopeOf<P, T> = PolicyOf<P, T> extends {
261
576
  readonly scope: infer S;
262
577
  } ? S : never;
263
578
  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. */
579
+ /**
580
+ * The action, or the union of the actions, of operation `Op` on table `T`; `never` when the policy names none, and for
581
+ * the read of a table read with `'any'` (`ReadsAny`).
582
+ */
265
583
  export type ActionOf<P, T, Op extends Operation> = PolicyOf<P, T> extends {
266
584
  readonly actions: infer Acts;
267
585
  } ? Acts extends {
268
586
  readonly [K in Op]: infer A;
269
- } ? A extends string ? A : A extends readonly (infer L extends string)[] ? L : never : never : never;
587
+ } ? A extends typeof READ_ANY ? never : A extends string ? A : A extends readonly (infer L extends string)[] ? L : never : never : never;
588
+ /** Whether table `T` is read with `'any'`: by every `app` endpoint, job and webhook, whatever its action. */
589
+ export type ReadsAny<P, T> = PolicyOf<P, T> extends {
590
+ readonly actions: {
591
+ readonly read: typeof READ_ANY;
592
+ };
593
+ } ? true : false;
270
594
  export type DimensionOf<P, T> = ScopeOf<P, T> extends {
271
595
  readonly dimension: infer D extends string;
272
596
  } ? D : never;
@@ -315,11 +639,11 @@ type TableReadAction<P, T> = PolicyOf<P, T> extends {
315
639
  readonly read: infer A;
316
640
  };
317
641
  } ? 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. */
642
+ /** Columns whose own read actions leave out one of the table's: no read by the table's read action returns them. */
319
643
  type RestrictedColumns<P, T> = [ColumnPoliciesOf<P, T>] extends [never] ? never : {
320
644
  [K in keyof ColumnPoliciesOf<P, T>]: ColumnPoliciesOf<P, T>[K] extends {
321
645
  readonly read: infer R;
322
- } ? ([TableReadAction<P, T>] extends [R] ? never : K) : never;
646
+ } ? ([TableReadAction<P, T>] extends [R extends readonly (infer L extends string)[] ? L : R] ? never : K) : never;
323
647
  }[keyof ColumnPoliciesOf<P, T>];
324
648
  /** A row of table `T` as a store reads it, by the table's read action. */
325
649
  export type PolicyRow<DB, P, T> = T extends keyof DB ? Flat<Omit<Selectable<DB[T]>, RestrictedColumns<P, T>>> : never;
@@ -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;
@@ -1,9 +1,13 @@
1
1
  import type { Admissions } from './admissions';
2
+ import type { ContainerGrant, RelationHold } from './capability';
2
3
  import type { Session } from './database';
3
4
  import type { WebhookTables } from './framework-tables';
4
5
  import type { JobLeases } from './job-leases';
5
6
  import type { JobWakes } from './job-wakes';
7
+ import type { Notifications } from './notifications';
6
8
  import type { DataAccess } from './orm';
9
+ import type { PushSubscriptions } from './push-subscriptions';
10
+ import type { RequestReceipts } from './request-receipts';
7
11
  import type { DataLayerRuntime, TableContext, TableProbe } from './store';
8
12
  import type { StoredFiles } from './stored-files';
9
13
  /**
@@ -11,6 +15,27 @@ import type { StoredFiles } from './stored-files';
11
15
  * the session to read in (a job's transaction, which sees the object it just wrote); a statement of its own otherwise.
12
16
  */
13
17
  export type Locator = (dimension: string, id: string, using?: Session) => Promise<ReadonlyMap<string, string> | null>;
18
+ /** Whether an active row of `relation`'s source names `principal` at `node`: what a Ref held through it is minted on. */
19
+ export type RelationCheck = (relation: string, node: string, principal: string) => Promise<boolean>;
20
+ /**
21
+ * Whether a row `relation` reaches for `principal` reveals `node` of `dimension` now: what a Ref held through a reveal
22
+ * is minted on.
23
+ */
24
+ export type RevealCheck = (relation: string, dimension: string, node: string, principal: string, action: string) => Promise<boolean>;
25
+ /**
26
+ * Of `relations` and `paths`, the caller's relations and grants of `action` that reach the object `id` of `dimension`
27
+ * through a secondary path that holds the action for a Ref (`writeThrough`), or through a relation row or reveal: what a
28
+ * Ref held through a path is minted on, and carries.
29
+ */
30
+ export type PathCheck = (dimension: string, id: string, action: string, principal: string, relations: readonly RelationHold[], paths: readonly ContainerGrant[]) => Promise<{
31
+ readonly relations: readonly RelationHold[];
32
+ readonly paths: readonly ContainerGrant[];
33
+ }>;
34
+ /**
35
+ * Of `values`, the caller's grants of `action` at nodes of `revealing`, those whose row reveals `node` of `dimension` to
36
+ * its members now: what a Ref held through member reveals is minted on, and carries.
37
+ */
38
+ export type MemberRevealCheck = (revealing: string, values: readonly string[], dimension: string, node: string, action: string) => Promise<readonly string[]>;
14
39
  /** What the framework writes itself: its own `fabrika_*` tables. Usable once bound. */
15
40
  export interface FrameworkTables {
16
41
  readonly webhooks: WebhookTables;
@@ -21,17 +46,27 @@ export interface FrameworkTables {
21
46
  readonly admissions: Admissions;
22
47
  /** Every stored file (`fabrika_files`): staged before its object, claimed at its first reference, swept unreferenced. */
23
48
  readonly files: StoredFiles;
49
+ /** The answers of keyed mutations, read and written in the invocation's transaction (ADR-0078). */
50
+ readonly receipts: RequestReceipts;
51
+ /** The browsers' Web Push subscriptions (ADR-0088), read and written in the invocation's transaction. */
52
+ readonly push: PushSubscriptions;
53
+ /** The notifications written for people (ADR-0088 §6), which the inbox answers. */
54
+ readonly notifications: Notifications;
24
55
  }
25
56
  /** A connection of the layer and its locator. Only its holder can `release` it, which lets the layer be bound again. */
26
57
  export interface ProvisionalBinding {
27
58
  readonly locate: Locator;
59
+ readonly relates: RelationCheck;
60
+ readonly reveals: RevealCheck;
61
+ readonly holdsThrough: PathCheck;
62
+ readonly revealsToMembers: MemberRevealCheck;
28
63
  release(): void;
29
64
  }
30
65
  export interface FrameworkSide {
31
66
  /** Connects the layer once and answers its binding; a call while one is connected throws. */
32
67
  bind(runtime: DataLayerRuntime): ProvisionalBinding;
33
68
  readonly framework: FrameworkTables;
34
- /** The untyped handle over one table that `@fabrika/db/testing` hands the isolation tests. */
69
+ /** The untyped handle over one table that `@fabrika/db/testing` hands the policy isolation kit. */
35
70
  probe(table: string): TableProbe;
36
71
  /** One table's context, for the framework's own store builders (`defineStore`, `@fabrika/db/testing`). */
37
72
  tableContext(table: string, mode: 'dimension' | 'child'): TableContext;
@@ -0,0 +1,22 @@
1
+ import { type Session } from './database';
2
+ /** What a receipt records of the request it answers: its operation, the digest of its input, and the output's JSON. */
3
+ export interface RequestReceipt {
4
+ readonly operation: string;
5
+ readonly identity: string;
6
+ readonly output: string;
7
+ }
8
+ export interface RequestReceipts {
9
+ /**
10
+ * The receipt of `principal`'s `key`, read in `session`; `null` when there is none. On Postgres it first takes the
11
+ * key's transaction lock, so a concurrent copy of the request waits for this transaction and then reads its receipt
12
+ * instead of running too; SQLite's one writer already serializes them.
13
+ */
14
+ find(session: Session, principal: string, key: string): Promise<RequestReceipt | null>;
15
+ /**
16
+ * Records `receipt` under `principal`'s `key` in `session`, created at `now` (epoch ms), after pruning a batch of
17
+ * receipts created before `retainedSince`. `false` when the key already has a receipt: another copy of the request
18
+ * recorded it meanwhile, and this one's transaction must not commit.
19
+ */
20
+ record(session: Session, principal: string, key: string, receipt: RequestReceipt, now: number, retainedSince: number): Promise<boolean>;
21
+ }
22
+ export declare function requestReceipts(): RequestReceipts;
@@ -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;