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

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