@fabrika/db 0.9.0-dev.3 → 0.9.0-dev.30

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