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