@fabrika/db 0.9.0-dev.4 → 0.9.0-dev.40

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 (84) hide show
  1. package/package.json +39 -4
  2. package/src/admissions.ts +78 -12
  3. package/src/application-rows.ts +108 -0
  4. package/src/authority.ts +560 -0
  5. package/src/builtins.ts +21 -0
  6. package/src/capability.ts +56 -2
  7. package/src/codegen-sqlite.ts +194 -0
  8. package/src/codegen.ts +230 -41
  9. package/src/condition-nodes.ts +119 -0
  10. package/src/conditions.ts +683 -0
  11. package/src/database.ts +227 -61
  12. package/src/description.ts +37 -8
  13. package/src/errors.ts +10 -6
  14. package/src/file-columns.ts +56 -11
  15. package/src/framework-tables.ts +28 -8
  16. package/src/framework.ts +15 -5
  17. package/src/guard.ts +373 -15
  18. package/src/index.ts +17 -1
  19. package/src/job-leases.ts +228 -55
  20. package/src/job-wakes.ts +600 -0
  21. package/src/json.ts +124 -0
  22. package/src/links.ts +8 -1
  23. package/src/list-cursor.ts +82 -0
  24. package/src/migrations-sqlite.ts +210 -0
  25. package/src/migrations.ts +73 -0
  26. package/src/nodes.ts +84 -14
  27. package/src/orm-types.ts +206 -71
  28. package/src/orm.ts +425 -170
  29. package/src/policy.ts +1979 -141
  30. package/src/postgres.ts +26 -0
  31. package/src/registry.ts +55 -3
  32. package/src/request-receipts.ts +107 -0
  33. package/src/sql.ts +247 -476
  34. package/src/sqlite-bun.ts +149 -0
  35. package/src/sqlite-migrate.ts +242 -0
  36. package/src/sqlite.ts +269 -0
  37. package/src/store.ts +2292 -239
  38. package/src/stored-files.ts +188 -52
  39. package/src/tables.ts +172 -6
  40. package/src/temporal.ts +122 -9
  41. package/src/testing-shared.ts +1351 -0
  42. package/src/testing-sqlite.ts +685 -0
  43. package/src/testing.ts +310 -494
  44. package/src/vocabulary-tables.ts +159 -27
  45. package/types/src/application-rows.d.ts +58 -0
  46. package/types/src/authority.d.ts +147 -0
  47. package/types/src/builtins.d.ts +2 -0
  48. package/types/src/capability.d.ts +55 -2
  49. package/types/src/codegen-sqlite.d.ts +13 -0
  50. package/types/src/codegen.d.ts +79 -5
  51. package/types/src/condition-nodes.d.ts +9 -0
  52. package/types/src/conditions.d.ts +151 -0
  53. package/types/src/database.d.ts +81 -20
  54. package/types/src/description.d.ts +26 -8
  55. package/types/src/errors.d.ts +9 -6
  56. package/types/src/file-columns.d.ts +6 -4
  57. package/types/src/framework.d.ts +12 -5
  58. package/types/src/guard.d.ts +24 -0
  59. package/types/src/index.d.ts +5 -4
  60. package/types/src/job-leases.d.ts +40 -5
  61. package/types/src/job-wakes.d.ts +124 -0
  62. package/types/src/json.d.ts +18 -0
  63. package/types/src/list-cursor.d.ts +13 -0
  64. package/types/src/migrations-sqlite.d.ts +4 -0
  65. package/types/src/migrations.d.ts +4 -0
  66. package/types/src/nodes.d.ts +17 -2
  67. package/types/src/orm-types.d.ts +163 -71
  68. package/types/src/orm.d.ts +21 -0
  69. package/types/src/policy.d.ts +351 -23
  70. package/types/src/postgres.d.ts +12 -0
  71. package/types/src/registry.d.ts +40 -3
  72. package/types/src/request-receipts.d.ts +22 -0
  73. package/types/src/sql.d.ts +29 -21
  74. package/types/src/sqlite-bun.d.ts +21 -0
  75. package/types/src/sqlite-migrate.d.ts +28 -0
  76. package/types/src/sqlite.d.ts +24 -0
  77. package/types/src/store.d.ts +143 -34
  78. package/types/src/stored-files.d.ts +6 -2
  79. package/types/src/tables.d.ts +18 -2
  80. package/types/src/temporal.d.ts +24 -0
  81. package/types/src/testing-shared.d.ts +290 -0
  82. package/types/src/testing-sqlite.d.ts +37 -0
  83. package/types/src/testing.d.ts +2 -93
  84. package/types/src/vocabulary-tables.d.ts +169 -22
package/src/orm-types.ts CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  import type { Selectable, Updateable } from 'kysely'
6
6
  import type { RowRef } from './capability'
7
- import type { ConditionTuple } from './description'
7
+ import type { ComparableColumn, ConditionTuple } from './description'
8
8
  import type { FileReference } from './file-references'
9
9
  import type {
10
10
  ActionOf,
@@ -16,6 +16,7 @@ import type {
16
16
  InsertValues,
17
17
  ListDimensionOf,
18
18
  ParentOf,
19
+ ReadsAny,
19
20
  Tables,
20
21
  UpdateValues,
21
22
  } from './policy'
@@ -47,20 +48,29 @@ type AppActionsOf<P, T, F extends 'appRead' | 'appCount'> = PolicyOf<P, T> exten
47
48
  { readonly actions: { readonly [K in F]: readonly (infer A extends string)[] } } ? A
48
49
  : never
49
50
 
50
- /** Each column that declares its own read action, by column. */
51
+ /** Each column that declares its own read actions, by column: the union of the actions it lists. */
51
52
  type ColumnReadsOf<P, T> = PolicyOf<P, T> extends { readonly columns: infer C } ? {
52
- readonly [K in keyof C as C[K] extends { readonly read: string } ? K : never]: C[K] extends { readonly read: infer A extends string } ? A : never
53
+ readonly [K in keyof C as C[K] extends { readonly read: string | readonly string[] } ? K : never]: C[K] extends
54
+ { readonly read: infer A extends string | readonly string[] } ? A extends readonly (infer L extends string)[] ? L : A
55
+ : never
53
56
  }
54
57
  : Record<never, never>
55
58
 
59
+ /** The actions the table's `when` narrows; every action of a policy typed wider than its literal, which may hold one. */
60
+ type ConditionedOf<P, T> = PolicyOf<P, T> extends { readonly when?: infer W } ? keyof NonNullable<W> & string : never
61
+
56
62
  type ColumnPoliciesOf<P, T> = PolicyOf<P, T> extends { readonly columns: infer C } ? C : never
57
63
 
58
- /** The columns that declare `A` as their own write action. */
59
- type ColumnsWrittenBy<C, A> = { [K in keyof C]: C[K] extends { readonly write: A } ? K : never }[keyof C]
64
+ /** The actions a column's own `write` names: one action, or each of a list. */
65
+ type WriteActionsOf<P> = P extends { readonly write: infer W } ? W extends readonly (infer L extends string)[] ? L : W extends string ? W : never
66
+ : never
67
+
68
+ /** The columns that list `A` among their own write actions. */
69
+ type ColumnsWrittenBy<C, A> = { [K in keyof C]: [A] extends [WriteActionsOf<C[K]>] ? K : never }[keyof C]
60
70
 
61
71
  /**
62
72
  * Each action that updates the table, with the values a capability of it updates: the update action its update list,
63
- * a column's own write action the columns that declare it, and no other.
73
+ * a column's own write action the columns that list it (an update action both), and no other.
64
74
  */
65
75
  type UpdatesOf<DB, P, T extends keyof DB> =
66
76
  & { readonly [A in ActionOf<P, T, 'update'>]: UpdateValues<DB, P, T> }
@@ -70,7 +80,7 @@ type UpdatesOf<DB, P, T extends keyof DB> =
70
80
  >
71
81
  }
72
82
 
73
- type ColumnWriteAction<C> = { [K in keyof C]: C[K] extends { readonly write: infer A extends string } ? A : never }[keyof C]
83
+ type ColumnWriteAction<C> = { [K in keyof C]: WriteActionsOf<C[K]> }[keyof C]
74
84
 
75
85
  /** One table, as every handle reads it. */
76
86
  export interface IndexedTable {
@@ -81,7 +91,10 @@ export interface IndexedTable {
81
91
  readonly list: string
82
92
  /** A child table's parent table. */
83
93
  readonly parent: string
94
+ /** `read` is `never` for a table read with `'any'` (`readsAny`). */
84
95
  readonly actions: { readonly read: string; readonly insert: string; readonly update: string; readonly delete: string }
96
+ /** An application table read with `'any'`: every `app` endpoint, job and webhook reads it, no public procedure or scoped site. */
97
+ readonly readsAny: boolean
85
98
  /** The actions besides `read` with which an `app` endpoint lists every row (`actions.appRead`). */
86
99
  readonly appRead: string
87
100
  /** The actions with which an `app` endpoint only counts every row (`actions.appCount`). */
@@ -90,6 +103,8 @@ export interface IndexedTable {
90
103
  readonly row: object
91
104
  /** The columns that declare their own read action. */
92
105
  readonly reads: object
106
+ /** The actions whose `when` narrows the rows they reach. */
107
+ readonly conditioned: string
93
108
  readonly insert: object
94
109
  readonly update: object
95
110
  /** Each action that updates the table, with the values it updates (`columns: { assignee: { write: 'tasks.assign' } }`). */
@@ -103,6 +118,8 @@ export interface IndexedTable {
103
118
  readonly versioned: boolean
104
119
  /** A queue table: its rows are claimed and finished through `row.ref`. */
105
120
  readonly queued: boolean
121
+ /** A view: read like its scope's tables, written by no site (ADR-0072). */
122
+ readonly view: boolean
106
123
  }
107
124
 
108
125
  type IndexedTableOf<DB, P, T extends keyof P & keyof DB & string, V = unknown> = {
@@ -116,10 +133,12 @@ type IndexedTableOf<DB, P, T extends keyof P & keyof DB & string, V = unknown> =
116
133
  readonly update: ActionOf<P, T, 'update'>
117
134
  readonly delete: ActionOf<P, T, 'delete'>
118
135
  }
136
+ readonly readsAny: ReadsAny<P, T>
119
137
  readonly appRead: AppActionsOf<P, T, 'appRead'>
120
138
  readonly appCount: AppActionsOf<P, T, 'appCount'>
121
139
  readonly row: WithFiles<RowType<DB[T]>, FileColumnsOf<P, T>>
122
140
  readonly reads: ColumnReadsOf<P, T>
141
+ readonly conditioned: ConditionedOf<P, T>
123
142
  readonly insert: WithFiles<InsertValues<DB, P, T>, FileColumnsOf<P, T>>
124
143
  readonly update: WithFiles<UpdateValues<DB, P, T>, FileColumnsOf<P, T>>
125
144
  readonly writes: { readonly [A in keyof UpdatesOf<DB, P, T>]: WithFiles<UpdatesOf<DB, P, T>[A], FileColumnsOf<P, T>> }
@@ -128,6 +147,7 @@ type IndexedTableOf<DB, P, T extends keyof P & keyof DB & string, V = unknown> =
128
147
  readonly reach: ReachOf<P, T, V>
129
148
  readonly versioned: PolicyOf<P, T> extends { readonly version: string } ? true : false
130
149
  readonly queued: PolicyOf<P, T> extends { readonly queue: object } ? true : false
150
+ readonly view: PolicyOf<P, T> extends { readonly view: true } ? true : false
131
151
  }
132
152
 
133
153
  type OwnerTable<P, D> = { [K in keyof P]: P[K] extends { readonly scope: { readonly dimension: D } } ? K : never }[keyof P]
@@ -159,6 +179,14 @@ type ChainDimensions<P, T, V> = PolicyOf<P, T> extends { readonly scope: infer S
159
179
  /** The dimensions whose `Within` lists table `T`: every dimension of its chain, and a top-level table's own. */
160
180
  type ReachOf<P, T, V> = [ContainerOf<P, T>] extends [never] ? DimensionOf<P, T> | ChainDimensions<P, T, V> : ChainDimensions<P, T, V>
161
181
 
182
+ /**
183
+ * The dimensions outside its chain whose `Within` reads table `T` through a foreign key (`readThrough`); a child view's
184
+ * are its parent's, as it is read through its parent row.
185
+ */
186
+ type ReadThroughOf<P, T> = PolicyOf<P, T> extends { readonly readThrough: infer R } ? keyof R & string
187
+ : PolicyOf<P, T> extends { readonly view: true; readonly scope: { readonly child: infer Parent extends string } } ? ReadThroughOf<P, Parent>
188
+ : never
189
+
162
190
  /**
163
191
  * A row as reads return it: the generated interface itself where no column has a separate write type, so a
164
192
  * diagnostic names it (`DealsTable`), and Kysely's `Selectable` of it otherwise.
@@ -187,8 +215,11 @@ export type DbIndex<DB, P, V = unknown> = {
187
215
  readonly children: { readonly [T in ChildTableName<P> as ParentOf<P, T>]: T }
188
216
  /** Dimension → the dimension tables listed in it: those it contains, and a top-level table its own. */
189
217
  readonly listed: { readonly [T in DimensionTableName<P> as ListDimensionOf<P, T>]: T }
190
- /** Dimension → the tables an `over` of it lists: each whose containment chain holds it, and a top-level table its own. */
191
- readonly within: { readonly [T in DimensionTableName<P> | ChildTableName<P> as ReachOf<P, T, V>]: T }
218
+ /**
219
+ * Dimension → the tables an `over` of it lists: each whose containment chain holds it, a top-level table its own, and
220
+ * each that reads through it (`readThrough`).
221
+ */
222
+ readonly within: { readonly [T in DimensionTableName<P> | ChildTableName<P> as ReachOf<P, T, V> | ReadThroughOf<P, T>]: T }
192
223
  readonly references: ReferenceTableName<P>
193
224
  /** The application tables: an `app` endpoint reads and writes them. */
194
225
  readonly applications: ApplicationTableName<P>
@@ -216,18 +247,27 @@ type ListedIn<Ix extends AnyDbIndex, D> = Lookup<Ix['listed'], D>
216
247
  type WithinIn<Ix extends AnyDbIndex, D> = Lookup<Ix['within'], D>
217
248
 
218
249
  /**
219
- * The columns a capability of action `A` cannot read: each that declares a read action other than exactly `A`. A
220
- * union `A` (a widened capability type) holds no single action provably, so it reads none of them.
250
+ * The columns a capability of `A` cannot read: each whose own read actions do not include every action of `A`. A union
251
+ * `A` (an `anyOf` endpoint's, or a widened capability type) reads one only where no action of it is `Conditioned`: the
252
+ * runtime answers it where every reaching action reads every row alone (`readsAsAction`), and the held ones are known
253
+ * only per request.
221
254
  */
222
- type HiddenFor<Reads, A> = { [C in keyof Reads]: [A] extends [Reads[C]] ? ([Reads[C]] extends [A] ? never : C) : C }[keyof Reads]
255
+ type HiddenFor<Reads, A, Conditioned> = {
256
+ [C in keyof Reads]: [A] extends [never] ? C
257
+ : [A] extends [Reads[C]] ? (IsSingle<A> extends true ? never : [Extract<A, Conditioned>] extends [never] ? never : C)
258
+ : C
259
+ }[keyof Reads]
260
+
261
+ /** Whether `A` is one literal, not a union of several. */
262
+ type IsSingle<A, All = A> = [A] extends [never] ? false : A extends unknown ? ([All] extends [A] ? true : false) : never
223
263
 
224
264
  /**
225
- * A row as a capability of action `A` reads, filters and sorts it: a column that declares another read action is
226
- * absent. The type keeps the generated row's name, so a read of a hidden column reports
227
- * `Property 'margin' does not exist on type 'Omit<Selectable<DealsTable>, "margin">'`.
265
+ * A row as a capability of `A` reads, filters and sorts it: a column whose own read actions leave out an action of `A`
266
+ * is absent, and so is one a union `A` with a `when` may not read (`HiddenFor`). The type keeps the generated row's
267
+ * name, so a read of a hidden column reports `Property 'margin' does not exist on type 'Omit<Selectable<DealsTable>, "margin">'`.
228
268
  */
229
- export type RowFor<E extends IndexedTable, A extends string> = [HiddenFor<E['reads'], A>] extends [never] ? E['row']
230
- : Omit<E['row'], HiddenFor<E['reads'], A>>
269
+ export type RowFor<E extends IndexedTable, A extends string> = [HiddenFor<E['reads'], A, E['conditioned']>] extends [never] ? E['row']
270
+ : Omit<E['row'], HiddenFor<E['reads'], A, E['conditioned']>>
231
271
 
232
272
  /** What a paved-road write answers: the key, and the new version of a versioned table. */
233
273
  export type Saved<E extends IndexedTable> = E['versioned'] extends true ? { readonly id: string; readonly version: number } : { readonly id: string }
@@ -240,7 +280,7 @@ type InsertValuesOf<E extends IndexedTable> = { [C in keyof E['insert']]: E['ins
240
280
 
241
281
  /** `onConflict` of an insert: the columns of a unique index the migrations create, and what a conflict does. */
242
282
  export interface InsertConflict<E extends IndexedTable, M extends 'nothing' | 'update'> {
243
- readonly onConflict: { readonly columns: readonly (keyof E['row'] & string)[]; readonly do: M }
283
+ readonly onConflict: { readonly columns: readonly ComparableColumn<E['row']>[]; readonly do: M }
244
284
  }
245
285
 
246
286
  /**
@@ -300,11 +340,30 @@ type NamedActions<E extends IndexedTable> = (E['actions'][keyof E['actions']] |
300
340
 
301
341
  type Names<E extends IndexedTable, A extends string> = [A] extends [NamedActions<E>] ? true : false
302
342
 
343
+ /**
344
+ * Whether an action of `A` is one of `Allowed`. An `app` capability of several actions, an `anyOf` endpoint's, allows
345
+ * what any of them allows, as one role holding them; the runtime refuses an operation none of the held ones allows.
346
+ */
347
+ type AnyAllowed<A extends string, Allowed> = [Extract<A, Allowed>] extends [never] ? false : true
348
+
349
+ /** `U`'s members as one object: the columns any action of an `app` capability updates, with their values. */
350
+ type Merged<U> = (U extends unknown ? (value: U) => void : never) extends (value: infer I) => void ? I : never
351
+
303
352
  type Operation = 'insert' | 'update' | 'delete'
304
353
 
305
- /** `Member` in a mutation whose action is the table's `Op` action; otherwise the message that says which fix applies. */
306
- type Write<T extends string, E extends IndexedTable, Op extends Operation, A extends string, K extends DataKind, Member> = K extends 'mutation'
307
- ? [A] extends [E['actions'][Op]] ? Member
354
+ /**
355
+ * `Member` in a mutation whose action is the table's `Op` action (`Allowed`, any action of an `app` capability's);
356
+ * otherwise the message that says which fix applies.
357
+ */
358
+ type Write<
359
+ T extends string,
360
+ E extends IndexedTable,
361
+ Op extends Operation,
362
+ A extends string,
363
+ K extends DataKind,
364
+ Member,
365
+ Allowed extends boolean = [A] extends [E['actions'][Op]] ? true : false,
366
+ > = K extends 'mutation' ? Allowed extends true ? Member
308
367
  : Unavailable<`${Op} on ${T} needs the action '${E['actions'][Op] & string}', and this endpoint holds '${A}'`>
309
368
  : K extends 'job' ? Unavailable<'a job writes inside ctx.transaction((tx) => …): use tx.db there'>
310
369
  : Unavailable<`${Op extends 'delete' ? 'remove' : Op} writes, so it runs only in a mutation: declare the endpoint with r.mutation`>
@@ -317,17 +376,26 @@ type UpdateRefusal<T extends string, E extends IndexedTable, A extends string> =
317
376
 
318
377
  /**
319
378
  * `Member` in a mutation whose action updates columns of the table (the update action, or a column's own write
320
- * action); otherwise the message that says which fix applies.
379
+ * action; `Allowed`, any action of an `app` capability's); otherwise the message that says which fix applies.
321
380
  */
322
- type Update<T extends string, E extends IndexedTable, A extends string, K extends DataKind, Member> = K extends 'mutation'
323
- ? [A] extends [keyof E['writes']] ? Member : Unavailable<UpdateRefusal<T, E, A>>
381
+ type Update<
382
+ T extends string,
383
+ E extends IndexedTable,
384
+ A extends string,
385
+ K extends DataKind,
386
+ Member,
387
+ Allowed extends boolean = [A] extends [keyof E['writes']] ? true : false,
388
+ > = K extends 'mutation' ? Allowed extends true ? Member : Unavailable<UpdateRefusal<T, E, A>>
324
389
  : K extends 'job' ? Unavailable<'a job writes inside ctx.transaction((tx) => …): use tx.db there'>
325
390
  : Unavailable<'update writes, so it runs only in a mutation: declare the endpoint with r.mutation'>
326
391
 
327
392
  /** An update's values: any non-empty subset of the columns a capability of `A` updates; an empty one is refused. */
328
393
  type UpdateValuesOf<E extends IndexedTable, A extends string> = E['writes'][A & keyof E['writes']]
329
394
 
330
- type Read<T extends string, E extends IndexedTable, A extends string, Member> = Names<E, A> extends true ? Member
395
+ /** An `app` capability's update values: the columns any of its actions updates, one action's as `UpdateValuesOf`. */
396
+ type AppUpdateValuesOf<E extends IndexedTable, A extends string> = Merged<UpdateValuesOf<E, A>>
397
+
398
+ type Read<T extends string, E extends IndexedTable, A extends string, Member, Allowed extends boolean = Names<E, A>> = Allowed extends true ? Member
331
399
  : Unavailable<`${T} does not name the action '${A}': read it from an endpoint whose action ${T} names, or declare unscoped`>
332
400
 
333
401
  /** One page of a keyset list; `next` is the cursor `after` takes, `null` on the last page. */
@@ -336,18 +404,35 @@ export interface ListPage<Row> {
336
404
  readonly next: string | null
337
405
  }
338
406
 
339
- export type OrderTerm<Row> = { [C in keyof Row & string]: readonly [C, 'asc' | 'desc'] }[keyof Row & string]
407
+ export type OrderTerm<Row> = { [C in ComparableColumn<Row>]: readonly [C, 'asc' | 'desc'] }[ComparableColumn<Row>]
340
408
 
341
- export interface ListOptions<Row> {
342
- readonly where?: readonly ConditionTuple<Row>[]
343
- readonly orderBy?: readonly OrderTerm<Row>[]
409
+ /**
410
+ * What `list()` takes, by the conditions and order terms of its row. It is generic in those unions, not the row, so a
411
+ * handle that reads more columns, whose options take more, passes where one that reads fewer is expected.
412
+ */
413
+ export interface ListQuery<Condition, Order> {
414
+ readonly where?: readonly Condition[]
415
+ readonly orderBy?: readonly Order[]
344
416
  readonly limit?: number
345
417
  readonly after?: string
346
418
  }
419
+ /** `ListQuery` over the columns of `Row`. */
420
+ export type ListOptions<Row> = ListQuery<ConditionTuple<Row>, OrderTerm<Row>>
347
421
 
348
- export interface CountOptions<Row> {
349
- readonly where?: readonly ConditionTuple<Row>[]
422
+ /** What `all()` takes: a list's filter and order, and no page; it answers every row or refuses past `MAX_ALL_ROWS`. */
423
+ export interface AllQuery<Condition, Order> {
424
+ readonly where?: readonly Condition[]
425
+ readonly orderBy?: readonly Order[]
350
426
  }
427
+ /** `AllQuery` over the columns of `Row`. */
428
+ export type AllOptions<Row> = AllQuery<ConditionTuple<Row>, OrderTerm<Row>>
429
+
430
+ /** What `count()` takes: a list's filter. */
431
+ export interface CountQuery<Condition> {
432
+ readonly where?: readonly Condition[]
433
+ }
434
+ /** `CountQuery` over the columns of `Row`. */
435
+ export type CountOptions<Row> = CountQuery<ConditionTuple<Row>>
351
436
 
352
437
  /** The target's own row: `ctx.db.target` (or `ctx.db.created`) of an `on`, `in` or `creates` endpoint. */
353
438
  export interface TargetRow<T extends string, E extends IndexedTable, A extends string, K extends DataKind, F extends TargetForm> {
@@ -386,24 +471,28 @@ export interface UnownedTarget<D extends string> {
386
471
  readonly remove: Unavailable<`no table owns the dimension ${D}, so ctx.db.target has no row of its own`>
387
472
  }
388
473
 
389
- /** A child table of the target: its rows are addressed by their own key under the target. */
474
+ /** A child table of the target: its rows are addressed by their own key under the target; a view's are only read. */
390
475
  export interface ChildRows<T extends string, E extends IndexedTable, A extends string, K extends DataKind> {
391
476
  readonly list: Read<T, E, A, (options?: ListOptions<RowFor<E, A>>) => Promise<ListPage<RowFor<E, A>>>>
477
+ readonly all: Read<T, E, A, (options?: AllOptions<RowFor<E, A>>) => Promise<RowFor<E, A>[]>>
392
478
  readonly get: Read<T, E, A, (key: string) => Promise<RowFor<E, A>>>
393
- readonly insert: Write<T, E, 'insert', A, K, InsertMethod<E>>
394
- readonly update: Update<
479
+ readonly insert: E['view'] extends true ? Unavailable<`${T} is a view, which no site writes: write the tables it reads`>
480
+ : Write<T, E, 'insert', A, K, InsertMethod<E>>
481
+ readonly update: E['view'] extends true ? Unavailable<`${T} is a view, which no site writes: write the tables it reads`> : Update<
395
482
  T,
396
483
  E,
397
484
  A,
398
485
  K,
399
486
  (key: string, values: { [C in keyof UpdateValuesOf<E, A>]: UpdateValuesOf<E, A>[C] }, ...version: VersionArguments<E>) => Promise<Saved<E>>
400
487
  >
401
- readonly remove: Write<T, E, 'delete', A, K, (key: string, ...version: VersionArguments<E>) => Promise<void>>
488
+ readonly remove: E['view'] extends true ? Unavailable<`${T} is a view, which no site writes: write the tables it reads`>
489
+ : Write<T, E, 'delete', A, K, (key: string, ...version: VersionArguments<E>) => Promise<void>>
402
490
  }
403
491
 
404
492
  /** A dimension table the target contains: listed in it, and inserted into it by a mutation of its insert action. */
405
493
  export interface ContainedRows<T extends string, E extends IndexedTable, A extends string, K extends DataKind> {
406
494
  readonly list: Read<T, E, A, (options?: ListOptions<RowFor<E, A>>) => Promise<ListPage<RowFor<E, A>>>>
495
+ readonly all: Read<T, E, A, (options?: AllOptions<RowFor<E, A>>) => Promise<RowFor<E, A>[]>>
407
496
  readonly insert: Write<T, E, 'insert', A, K, InsertMethod<E>>
408
497
  }
409
498
 
@@ -423,14 +512,20 @@ export type ScopeTarget<Ix extends AnyDbIndex, D extends string, A extends strin
423
512
  /** A table listed in an `over` endpoint's dimension, read through its `Within`; a job claims a queue table's rows through it. */
424
513
  export interface WithinRows<T extends string, E extends IndexedTable, A extends string, K extends DataKind> {
425
514
  readonly claim: Claim<T, E, A, K>
426
- readonly list: A extends E['actions']['read'] ? (options?: ListOptions<RowFor<E, A>>) => Promise<ListPage<RowFor<E, A>>>
427
- : Unavailable<ListRefusal<T, E, A>>
428
- readonly count: A extends E['actions']['read'] ? (options?: CountOptions<RowFor<E, A>>) => Promise<number> : Unavailable<ListRefusal<T, E, A>>
429
- readonly aggregate: A extends E['actions']['read']
430
- ? <const S extends AggregateSpec<RowFor<E, A>>>(spec: S) => Promise<AggregateRow<RowFor<E, A>, S>[]>
431
- : Unavailable<ListRefusal<T, E, A>>
515
+ readonly list: WithinRead<T, E, A, (options?: ListOptions<RowFor<E, A>>) => Promise<ListPage<RowFor<E, A>>>>
516
+ readonly all: WithinRead<T, E, A, (options?: AllOptions<RowFor<E, A>>) => Promise<RowFor<E, A>[]>>
517
+ readonly count: WithinRead<T, E, A, (options?: CountOptions<RowFor<E, A>>) => Promise<number>>
518
+ readonly aggregate: WithinRead<T, E, A, <const S extends AggregateSpec<RowFor<E, A>>>(spec: S) => Promise<AggregateRow<RowFor<E, A>, S>[]>>
432
519
  }
433
520
 
521
+ /**
522
+ * `Member` where a `Within` of `A` lists the table by its read action. An alias, not inline in `WithinRows`: inline, a
523
+ * `Pick` of a handle that reads more columns is refused where one of fewer is expected. A `Within` typed for a union of
524
+ * actions reads its row by the union rule (`HiddenFor`), stricter than the one action it holds at run time.
525
+ */
526
+ type WithinRead<T extends string, E extends IndexedTable, A extends string, Member> = A extends E['actions']['read'] ? Member
527
+ : Unavailable<ListRefusal<T, E, A>>
528
+
434
529
  /** Inlined where used: an alias of its own would be printed by name, with the table's index entry spelled out. */
435
530
  type ListRefusal<T extends string, E extends IndexedTable, A extends string> = `a list of ${T} needs its read action '${
436
531
  & E['actions']['read']
@@ -444,31 +539,54 @@ export type WithinTables<Ix extends AnyDbIndex, D extends string, A extends stri
444
539
  readonly [T in WithinIn<Ix, D>]: WithinRows<T, Ix['tables'][T], A, K>
445
540
  }
446
541
 
447
- /** An application table under the capability of an `app` endpoint of action `A`, in a site of kind `K`. */
542
+ /**
543
+ * `Member` where `ctx.db.app` of action `A` lists the application table: by its read action, or any action for `'any'`;
544
+ * an action of `A`'s does for an `anyOf` endpoint.
545
+ */
546
+ type AppList<T extends string, E extends IndexedTable, A extends string, Member> = [E['readsAny']] extends [true] ? Member
547
+ : AnyAllowed<A, E['actions']['read']> extends true ? Member
548
+ : Unavailable<ListRefusal<T, E, A>>
549
+
550
+ /** `Member` where `ctx.db.app` of action `A` (any of them) reads a row of the application table by its key. */
551
+ type AppGet<T extends string, E extends IndexedTable, A extends string, Member> = [E['readsAny']] extends [true] ? Member
552
+ : Read<T, E, A, Member, AnyAllowed<A, NamedActions<E>>>
553
+
554
+ /** `Member` where a public procedure of action `A` reads the application table: under its read action, never `'any'`. */
555
+ type PublicRead<T extends string, E extends IndexedTable, A extends string, Member> = [E['readsAny']] extends [true] ? Unavailable<
556
+ `a public procedure does not read ${T}: it is read with 'any', which an app endpoint, a job and a webhook hold`
557
+ >
558
+ : A extends E['actions']['read'] ? Member
559
+ : Unavailable<ListRefusal<T, E, A>>
560
+
561
+ /**
562
+ * An application table under the capability of an `app` endpoint of action `A`, in a site of kind `K`; an `anyOf`
563
+ * endpoint's `A` is its listed actions, and an operation any of them allows is typed. A column with its own read actions
564
+ * shows only where every listed action is one of them and none has a `when` (`RowFor`): which listed actions the
565
+ * caller holds is known only at run time.
566
+ */
448
567
  export interface ApplicationRows<T extends string, E extends IndexedTable, A extends string, K extends DataKind> {
449
- readonly list: A extends E['actions']['read'] ? (options?: ListOptions<RowFor<E, A>>) => Promise<ListPage<RowFor<E, A>>>
450
- : Unavailable<ListRefusal<T, E, A>>
451
- readonly count: A extends E['actions']['read'] ? (options?: CountOptions<RowFor<E, A>>) => Promise<number> : Unavailable<ListRefusal<T, E, A>>
452
- readonly aggregate: A extends E['actions']['read']
453
- ? <const S extends AggregateSpec<RowFor<E, A>>>(spec: S) => Promise<AggregateRow<RowFor<E, A>, S>[]>
454
- : Unavailable<ListRefusal<T, E, A>>
568
+ readonly list: AppList<T, E, A, (options?: ListOptions<RowFor<E, A>>) => Promise<ListPage<RowFor<E, A>>>>
569
+ readonly all: AppList<T, E, A, (options?: AllOptions<RowFor<E, A>>) => Promise<RowFor<E, A>[]>>
570
+ readonly count: AppList<T, E, A, (options?: CountOptions<RowFor<E, A>>) => Promise<number>>
571
+ readonly aggregate: AppList<T, E, A, <const S extends AggregateSpec<RowFor<E, A>>>(spec: S) => Promise<AggregateRow<RowFor<E, A>, S>[]>>
455
572
  /** One row by its key; a column that declares another read action than `A` is hidden. */
456
- readonly get: Read<T, E, A, (key: string) => Promise<RowFor<E, A>>>
573
+ readonly get: AppGet<T, E, A, (key: string) => Promise<RowFor<E, A>>>
457
574
  /** `SELECT … FOR UPDATE` of one row by its key: a precondition read that no concurrent write overtakes. */
458
- readonly getForUpdate: K extends 'mutation' ? Read<T, E, A, (key: string) => Promise<RowFor<E, A>>>
575
+ readonly getForUpdate: K extends 'mutation' ? AppGet<T, E, A, (key: string) => Promise<RowFor<E, A>>>
459
576
  : K extends 'job' ? Unavailable<'a job writes inside ctx.transaction((tx) => …): use tx.db there'>
460
577
  : Unavailable<'getForUpdate locks the row, so it runs only in a mutation: declare the endpoint with r.mutation'>
461
578
  /** Leases due rows of a queue table: a job's drain, before its transactions. */
462
579
  readonly claim: Claim<T, E, A, K>
463
- readonly insert: Write<T, E, 'insert', A, K, InsertMethod<E>>
580
+ readonly insert: Write<T, E, 'insert', A, K, InsertMethod<E>, AnyAllowed<A, E['actions']['insert']>>
464
581
  readonly update: Update<
465
582
  T,
466
583
  E,
467
584
  A,
468
585
  K,
469
- (key: string, values: { [C in keyof UpdateValuesOf<E, A>]: UpdateValuesOf<E, A>[C] }, ...version: VersionArguments<E>) => Promise<Saved<E>>
586
+ (key: string, values: { [C in keyof AppUpdateValuesOf<E, A>]: AppUpdateValuesOf<E, A>[C] }, ...version: VersionArguments<E>) => Promise<Saved<E>>,
587
+ AnyAllowed<A, keyof E['writes']>
470
588
  >
471
- readonly remove: Write<T, E, 'delete', A, K, (key: string, ...version: VersionArguments<E>) => Promise<void>>
589
+ readonly remove: Write<T, E, 'delete', A, K, (key: string, ...version: VersionArguments<E>) => Promise<void>, AnyAllowed<A, E['actions']['delete']>>
472
590
  }
473
591
 
474
592
  /**
@@ -484,7 +602,7 @@ type PublicRefusal<Op extends string> = Unavailable<`a public procedure does not
484
602
 
485
603
  /** A public procedure updates and removes only a versioned row, by an id a verified signed token of its request carries. */
486
604
  type PublicVersioned<T extends string, E extends IndexedTable, Member> = E['versioned'] extends true ? Member
487
- : Unavailable<`a public procedure updates and removes only a versioned row: declare version in the policy of ${T}`>
605
+ : Unavailable<`a public procedure updates and removes only a versioned row: give ${T} a version column`>
488
606
 
489
607
  /**
490
608
  * An application table of a public procedure of action `A`, in a site of kind `K`: read under its read action only,
@@ -492,10 +610,11 @@ type PublicVersioned<T extends string, E extends IndexedTable, Member> = E['vers
492
610
  * under `expectVersion`; never locked or claimed.
493
611
  */
494
612
  export interface PublicApplicationRows<T extends string, E extends IndexedTable, A extends string, K extends DataKind> {
495
- readonly list: ApplicationRows<T, E, A, K>['list']
496
- readonly count: ApplicationRows<T, E, A, K>['count']
497
- readonly aggregate: ApplicationRows<T, E, A, K>['aggregate']
498
- readonly get: A extends E['actions']['read'] ? (key: string) => Promise<RowFor<E, A>> : Unavailable<ListRefusal<T, E, A>>
613
+ readonly list: PublicRead<T, E, A, (options?: ListOptions<RowFor<E, A>>) => Promise<ListPage<RowFor<E, A>>>>
614
+ readonly all: Unavailable<'a public procedure pages its reads: read the table with list'>
615
+ readonly count: PublicRead<T, E, A, (options?: CountOptions<RowFor<E, A>>) => Promise<number>>
616
+ readonly aggregate: PublicRead<T, E, A, <const S extends AggregateSpec<RowFor<E, A>>>(spec: S) => Promise<AggregateRow<RowFor<E, A>, S>[]>>
617
+ readonly get: PublicRead<T, E, A, (key: string) => Promise<RowFor<E, A>>>
499
618
  readonly getForUpdate: PublicRefusal<'lock'>
500
619
  readonly claim: PublicRefusal<'claim'>
501
620
  /** By the id a verified signed token of the request carries; answers only the new version. */
@@ -529,15 +648,21 @@ export type PublicApplicationTables<Ix extends AnyDbIndex, A extends string, K e
529
648
  * (`RowFor`), or only how many rows match. A count-only action has no `aggregate`: a group or a minimum is a value.
530
649
  */
531
650
  export interface AppRecordRows<T extends string, E extends IndexedTable, A extends string> {
532
- readonly list: [A] extends [E['actions']['read'] | E['appRead']] ? (options?: ListOptions<RowFor<E, A>>) => Promise<ListPage<RowFor<E, A>>>
651
+ readonly list: AnyAllowed<A, E['actions']['read'] | E['appRead']> extends true
652
+ ? (options?: ListOptions<RowFor<E, A>>) => Promise<ListPage<RowFor<E, A>>>
533
653
  : Unavailable<
534
654
  `a list of ${T} needs its read action '${E['actions']['read'] & string}' or an action of its appRead, and this endpoint holds '${A}'`
535
655
  >
536
- readonly count: [A] extends [E['actions']['read'] | E['appRead'] | E['appCount']] ? (options?: CountOptions<RowFor<E, A>>) => Promise<number>
656
+ readonly all: AnyAllowed<A, E['actions']['read'] | E['appRead']> extends true ? (options?: AllOptions<RowFor<E, A>>) => Promise<RowFor<E, A>[]>
657
+ : Unavailable<
658
+ `a list of ${T} needs its read action '${E['actions']['read'] & string}' or an action of its appRead, and this endpoint holds '${A}'`
659
+ >
660
+ readonly count: AnyAllowed<A, E['actions']['read'] | E['appRead'] | E['appCount']> extends true
661
+ ? (options?: CountOptions<RowFor<E, A>>) => Promise<number>
537
662
  : Unavailable<
538
663
  `a count of ${T} needs its read action '${E['actions']['read'] & string}' or an action of its appRead or appCount, and this endpoint holds '${A}'`
539
664
  >
540
- readonly aggregate: [A] extends [E['actions']['read'] | E['appRead']]
665
+ readonly aggregate: AnyAllowed<A, E['actions']['read'] | E['appRead']> extends true
541
666
  ? <const S extends AggregateSpec<RowFor<E, A>>>(spec: S) => Promise<AggregateRow<RowFor<E, A>, S>[]>
542
667
  : Unavailable<
543
668
  `an aggregate of ${T} needs its read action '${
@@ -564,6 +689,7 @@ type TableRow<E extends IndexedTable> = RowFor<E, E['actions']['read']>
564
689
  */
565
690
  export interface ScopedApplicationRows<E extends IndexedTable> {
566
691
  list(options?: ListOptions<TableRow<E>>): Promise<ListPage<TableRow<E>>>
692
+ all(options?: AllOptions<TableRow<E>>): Promise<TableRow<E>[]>
567
693
  count(options?: CountOptions<TableRow<E>>): Promise<number>
568
694
  aggregate<const S extends AggregateSpec<TableRow<E>>>(spec: S): Promise<AggregateRow<TableRow<E>, S>[]>
569
695
  /** One row by its key. */
@@ -581,7 +707,10 @@ export interface ScopedApplicationRows<E extends IndexedTable> {
581
707
  * read-only; any other is the message that names its read action.
582
708
  */
583
709
  export type ScopedApplicationTables<Ix extends AnyDbIndex, A extends string> = {
584
- readonly [T in Ix['applications']]: [A] extends [Ix['tables'][T]['actions']['read']] ? ScopedApplicationRows<Ix['tables'][T]>
710
+ readonly [T in Ix['applications']]: [Ix['tables'][T]['readsAny']] extends [true] ? Unavailable<
711
+ `a scoped endpoint does not read ${T}: it is read with 'any', which an app endpoint, a job and a webhook hold`
712
+ >
713
+ : [A] extends [Ix['tables'][T]['actions']['read']] ? ScopedApplicationRows<Ix['tables'][T]>
585
714
  : Unavailable<
586
715
  `a scoped endpoint reads ${T} with its read action '${Ix['tables'][T]['actions']['read'] & string}', and this endpoint holds '${A}'`
587
716
  >
@@ -590,6 +719,7 @@ export type ScopedApplicationTables<Ix extends AnyDbIndex, A extends string> = {
590
719
  /** A reference table: every endpoint reads it, through its read action, and none writes it. */
591
720
  export interface ReferenceRows<E extends IndexedTable> {
592
721
  list(options?: ListOptions<TableRow<E>>): Promise<ListPage<TableRow<E>>>
722
+ all(options?: AllOptions<TableRow<E>>): Promise<TableRow<E>[]>
593
723
  count(options?: CountOptions<TableRow<E>>): Promise<number>
594
724
  }
595
725
 
@@ -603,25 +733,29 @@ export type UnscopedMode = 'read' | 'write'
603
733
  export type UnscopedDeclaration<Table extends string, Mode extends UnscopedMode> = { readonly [T in Table]?: Mode }
604
734
 
605
735
  /**
606
- * The tables of `U` whose mode may be `'write'` that no site writes: a reference table. A union or optional mode that
607
- * admits `'write'` counts, because the declaration it types may hold it.
736
+ * The tables of `U` whose mode may be `'write'` that no site writes: a reference table or a view. A union or optional
737
+ * mode that admits `'write'` counts, because the declaration it types may hold it.
608
738
  */
609
739
  type ReadOnlyWritten<Ix extends AnyDbIndex, U> = {
610
- [T in keyof U & TableName<Ix>]: 'write' extends U[T] ? Ix['tables'][T]['mode'] extends 'reference' ? T : never : never
740
+ [T in keyof U & TableName<Ix>]: 'write' extends U[T]
741
+ ? Ix['tables'][T]['mode'] extends 'reference' ? T : Ix['tables'][T]['view'] extends true ? T : never
742
+ : never
611
743
  }[keyof U & TableName<Ix>]
612
744
 
613
- type ReadOnlyMessage<T extends string> =
614
- `${T} is a reference table, which no site writes: its rows come from a migration, so declare unscoped: { ${T}: 'read' }`
745
+ type ReadOnlyMessage<T extends string, E extends IndexedTable> = E['view'] extends true
746
+ ? `${T} is a view, which no site writes: its rows come from the tables it reads, so declare unscoped: { ${T}: 'read' }`
747
+ : `${T} is a reference table, which no site writes: its rows come from a migration, so declare unscoped: { ${T}: 'read' }`
615
748
 
616
749
  /** An `unscoped` declaration's `'write'` of a reference table, refused at its own line. */
617
750
  export type ReadOnlyUnscoped<Ix extends AnyDbIndex, U> = [ReadOnlyWritten<Ix, U>] extends [never] ? unknown
618
- : { readonly [T in ReadOnlyWritten<Ix, U>]: Unavailable<ReadOnlyMessage<T>> }
751
+ : { readonly [T in ReadOnlyWritten<Ix, U>]: Unavailable<ReadOnlyMessage<T, Ix['tables'][T]>> }
619
752
 
620
753
  type KeylessMessage<T extends string> = `${T} is a reference table, which has no key: read it with list`
621
754
 
622
755
  /** A table a site declared `unscoped`: its scope predicate is `true`, and `'read'` writes nothing. */
623
756
  export interface UnscopedRows<T extends string, E extends IndexedTable, M extends UnscopedMode, K extends DataKind> {
624
757
  list(options?: ListOptions<TableRow<E>>): Promise<ListPage<TableRow<E>>>
758
+ all(options?: AllOptions<TableRow<E>>): Promise<TableRow<E>[]>
625
759
  count(options?: CountOptions<TableRow<E>>): Promise<number>
626
760
  aggregate<const S extends AggregateSpec<TableRow<E>>>(spec: S): Promise<AggregateRow<TableRow<E>, S>[]>
627
761
  readonly get: E['mode'] extends 'reference' ? Unavailable<KeylessMessage<T>> : (key: string) => Promise<TableRow<E>>
@@ -686,8 +820,8 @@ type RefFragment<Ix extends AnyDbIndex, D extends string, A extends string> = {
686
820
  }[TableName<Ix>]
687
821
 
688
822
  /**
689
- * What a `Within` writes on a `'write'` site: the target of an UPDATE or DELETE whose action it holds, a column's own
690
- * write action included (the statement's SET is checked against its columns at run time).
823
+ * What a `Within` writes on a `'write'` site: the table of a fragment of `use: 'update'` or `'delete'` whose action it
824
+ * holds, a column's own write action included (the statement's columns are the approver's to review, not checked).
691
825
  */
692
826
  type WithinWrites<E extends IndexedTable, M extends 'read' | 'write'> = M extends 'write'
693
827
  ? E['actions']['update'] | E['actions']['delete'] | (keyof E['writes'] & string)
@@ -702,7 +836,8 @@ type WithinFragment<Ix extends AnyDbIndex, D extends string, A extends string, M
702
836
 
703
837
  /** The tables a fragment of an `app` capability of `A` reaches: as `ctx.db.app` lists them for that action. */
704
838
  type AppFragment<Ix extends AnyDbIndex, A extends string> = {
705
- [T in TableName<Ix>]: Ix['tables'][T]['mode'] extends 'app' ? (A extends NamedActions<Ix['tables'][T]> ? T : never)
839
+ [T in TableName<Ix>]: Ix['tables'][T]['mode'] extends 'app'
840
+ ? ([Ix['tables'][T]['readsAny']] extends [true] ? T : A extends NamedActions<Ix['tables'][T]> ? T : never)
706
841
  : Ix['tables'][T]['mode'] extends 'dimension' | 'child' ? (A extends Ix['tables'][T]['actions']['read'] | Ix['tables'][T]['appRead'] ? T : never)
707
842
  : never
708
843
  }[TableName<Ix>]