@fabrika/db 0.9.0-dev.2 → 0.9.0-dev.21
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.
- package/package.json +35 -4
- package/src/admissions.ts +78 -12
- package/src/application-rows.ts +108 -0
- package/src/authority.ts +560 -0
- package/src/builtins.ts +21 -0
- package/src/capability.ts +56 -2
- package/src/codegen-sqlite.ts +194 -0
- package/src/codegen.ts +229 -40
- package/src/condition-nodes.ts +119 -0
- package/src/conditions.ts +683 -0
- package/src/database.ts +227 -61
- package/src/description.ts +37 -8
- package/src/errors.ts +6 -4
- package/src/file-columns.ts +56 -11
- package/src/framework-tables.ts +28 -8
- package/src/framework.ts +14 -5
- package/src/guard.ts +373 -15
- package/src/index.ts +13 -1
- package/src/job-leases.ts +212 -55
- package/src/job-wakes.ts +450 -0
- package/src/json.ts +124 -0
- package/src/links.ts +8 -1
- package/src/migrations-sqlite.ts +163 -0
- package/src/migrations.ts +25 -0
- package/src/nodes.ts +84 -14
- package/src/orm-types.ts +124 -45
- package/src/orm.ts +283 -133
- package/src/policy.ts +1851 -119
- package/src/postgres.ts +26 -0
- package/src/registry.ts +52 -3
- package/src/sql.ts +247 -476
- package/src/sqlite-bun.ts +149 -0
- package/src/sqlite-migrate.ts +242 -0
- package/src/sqlite.ts +269 -0
- package/src/store.ts +2290 -239
- package/src/stored-files.ts +188 -52
- package/src/tables.ts +164 -3
- package/src/temporal.ts +122 -9
- package/src/testing-shared.ts +1348 -0
- package/src/testing-sqlite.ts +685 -0
- package/src/testing.ts +310 -494
- package/src/vocabulary-tables.ts +124 -18
- package/types/src/application-rows.d.ts +58 -0
- package/types/src/authority.d.ts +147 -0
- package/types/src/builtins.d.ts +2 -0
- package/types/src/capability.d.ts +55 -2
- package/types/src/codegen-sqlite.d.ts +13 -0
- package/types/src/codegen.d.ts +79 -5
- package/types/src/condition-nodes.d.ts +9 -0
- package/types/src/conditions.d.ts +151 -0
- package/types/src/database.d.ts +81 -20
- package/types/src/description.d.ts +26 -8
- package/types/src/errors.d.ts +5 -4
- package/types/src/file-columns.d.ts +6 -4
- package/types/src/framework.d.ts +11 -5
- package/types/src/guard.d.ts +24 -0
- package/types/src/index.d.ts +4 -3
- package/types/src/job-leases.d.ts +34 -5
- package/types/src/job-wakes.d.ts +101 -0
- package/types/src/json.d.ts +18 -0
- package/types/src/migrations-sqlite.d.ts +4 -0
- package/types/src/migrations.d.ts +1 -0
- package/types/src/nodes.d.ts +17 -2
- package/types/src/orm-types.d.ts +91 -42
- package/types/src/orm.d.ts +10 -0
- package/types/src/policy.d.ts +332 -6
- package/types/src/postgres.d.ts +12 -0
- package/types/src/registry.d.ts +37 -3
- package/types/src/sql.d.ts +29 -21
- package/types/src/sqlite-bun.d.ts +21 -0
- package/types/src/sqlite-migrate.d.ts +28 -0
- package/types/src/sqlite.d.ts +24 -0
- package/types/src/store.d.ts +143 -34
- package/types/src/stored-files.d.ts +6 -2
- package/types/src/tables.d.ts +17 -1
- package/types/src/temporal.d.ts +24 -0
- package/types/src/testing-shared.d.ts +290 -0
- package/types/src/testing-sqlite.d.ts +37 -0
- package/types/src/testing.d.ts +2 -93
- package/types/src/vocabulary-tables.d.ts +141 -17
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'
|
|
@@ -81,7 +82,10 @@ export interface IndexedTable {
|
|
|
81
82
|
readonly list: string
|
|
82
83
|
/** A child table's parent table. */
|
|
83
84
|
readonly parent: string
|
|
85
|
+
/** `read` is `never` for a table read with `'any'` (`readsAny`). */
|
|
84
86
|
readonly actions: { readonly read: string; readonly insert: string; readonly update: string; readonly delete: string }
|
|
87
|
+
/** An application table read with `'any'`: every `app` endpoint, job and webhook reads it, no public procedure or scoped site. */
|
|
88
|
+
readonly readsAny: boolean
|
|
85
89
|
/** The actions besides `read` with which an `app` endpoint lists every row (`actions.appRead`). */
|
|
86
90
|
readonly appRead: string
|
|
87
91
|
/** The actions with which an `app` endpoint only counts every row (`actions.appCount`). */
|
|
@@ -103,6 +107,8 @@ export interface IndexedTable {
|
|
|
103
107
|
readonly versioned: boolean
|
|
104
108
|
/** A queue table: its rows are claimed and finished through `row.ref`. */
|
|
105
109
|
readonly queued: boolean
|
|
110
|
+
/** A view: read like its scope's tables, written by no site (ADR-0072). */
|
|
111
|
+
readonly view: boolean
|
|
106
112
|
}
|
|
107
113
|
|
|
108
114
|
type IndexedTableOf<DB, P, T extends keyof P & keyof DB & string, V = unknown> = {
|
|
@@ -116,6 +122,7 @@ type IndexedTableOf<DB, P, T extends keyof P & keyof DB & string, V = unknown> =
|
|
|
116
122
|
readonly update: ActionOf<P, T, 'update'>
|
|
117
123
|
readonly delete: ActionOf<P, T, 'delete'>
|
|
118
124
|
}
|
|
125
|
+
readonly readsAny: ReadsAny<P, T>
|
|
119
126
|
readonly appRead: AppActionsOf<P, T, 'appRead'>
|
|
120
127
|
readonly appCount: AppActionsOf<P, T, 'appCount'>
|
|
121
128
|
readonly row: WithFiles<RowType<DB[T]>, FileColumnsOf<P, T>>
|
|
@@ -128,6 +135,7 @@ type IndexedTableOf<DB, P, T extends keyof P & keyof DB & string, V = unknown> =
|
|
|
128
135
|
readonly reach: ReachOf<P, T, V>
|
|
129
136
|
readonly versioned: PolicyOf<P, T> extends { readonly version: string } ? true : false
|
|
130
137
|
readonly queued: PolicyOf<P, T> extends { readonly queue: object } ? true : false
|
|
138
|
+
readonly view: PolicyOf<P, T> extends { readonly view: true } ? true : false
|
|
131
139
|
}
|
|
132
140
|
|
|
133
141
|
type OwnerTable<P, D> = { [K in keyof P]: P[K] extends { readonly scope: { readonly dimension: D } } ? K : never }[keyof P]
|
|
@@ -159,6 +167,14 @@ type ChainDimensions<P, T, V> = PolicyOf<P, T> extends { readonly scope: infer S
|
|
|
159
167
|
/** The dimensions whose `Within` lists table `T`: every dimension of its chain, and a top-level table's own. */
|
|
160
168
|
type ReachOf<P, T, V> = [ContainerOf<P, T>] extends [never] ? DimensionOf<P, T> | ChainDimensions<P, T, V> : ChainDimensions<P, T, V>
|
|
161
169
|
|
|
170
|
+
/**
|
|
171
|
+
* The dimensions outside its chain whose `Within` reads table `T` through a foreign key (`readThrough`); a child view's
|
|
172
|
+
* are its parent's, as it is read through its parent row.
|
|
173
|
+
*/
|
|
174
|
+
type ReadThroughOf<P, T> = PolicyOf<P, T> extends { readonly readThrough: infer R } ? keyof R & string
|
|
175
|
+
: PolicyOf<P, T> extends { readonly view: true; readonly scope: { readonly child: infer Parent extends string } } ? ReadThroughOf<P, Parent>
|
|
176
|
+
: never
|
|
177
|
+
|
|
162
178
|
/**
|
|
163
179
|
* A row as reads return it: the generated interface itself where no column has a separate write type, so a
|
|
164
180
|
* diagnostic names it (`DealsTable`), and Kysely's `Selectable` of it otherwise.
|
|
@@ -187,8 +203,11 @@ export type DbIndex<DB, P, V = unknown> = {
|
|
|
187
203
|
readonly children: { readonly [T in ChildTableName<P> as ParentOf<P, T>]: T }
|
|
188
204
|
/** Dimension → the dimension tables listed in it: those it contains, and a top-level table its own. */
|
|
189
205
|
readonly listed: { readonly [T in DimensionTableName<P> as ListDimensionOf<P, T>]: T }
|
|
190
|
-
/**
|
|
191
|
-
|
|
206
|
+
/**
|
|
207
|
+
* Dimension → the tables an `over` of it lists: each whose containment chain holds it, a top-level table its own, and
|
|
208
|
+
* each that reads through it (`readThrough`).
|
|
209
|
+
*/
|
|
210
|
+
readonly within: { readonly [T in DimensionTableName<P> | ChildTableName<P> as ReachOf<P, T, V> | ReadThroughOf<P, T>]: T }
|
|
192
211
|
readonly references: ReferenceTableName<P>
|
|
193
212
|
/** The application tables: an `app` endpoint reads and writes them. */
|
|
194
213
|
readonly applications: ApplicationTableName<P>
|
|
@@ -240,7 +259,7 @@ type InsertValuesOf<E extends IndexedTable> = { [C in keyof E['insert']]: E['ins
|
|
|
240
259
|
|
|
241
260
|
/** `onConflict` of an insert: the columns of a unique index the migrations create, and what a conflict does. */
|
|
242
261
|
export interface InsertConflict<E extends IndexedTable, M extends 'nothing' | 'update'> {
|
|
243
|
-
readonly onConflict: { readonly columns: readonly
|
|
262
|
+
readonly onConflict: { readonly columns: readonly ComparableColumn<E['row']>[]; readonly do: M }
|
|
244
263
|
}
|
|
245
264
|
|
|
246
265
|
/**
|
|
@@ -300,11 +319,30 @@ type NamedActions<E extends IndexedTable> = (E['actions'][keyof E['actions']] |
|
|
|
300
319
|
|
|
301
320
|
type Names<E extends IndexedTable, A extends string> = [A] extends [NamedActions<E>] ? true : false
|
|
302
321
|
|
|
322
|
+
/**
|
|
323
|
+
* Whether an action of `A` is one of `Allowed`. An `app` capability of several actions, an `anyOf` endpoint's, allows
|
|
324
|
+
* what any of them allows, as one role holding them; the runtime refuses an operation none of the held ones allows.
|
|
325
|
+
*/
|
|
326
|
+
type AnyAllowed<A extends string, Allowed> = [Extract<A, Allowed>] extends [never] ? false : true
|
|
327
|
+
|
|
328
|
+
/** `U`'s members as one object: the columns any action of an `app` capability updates, with their values. */
|
|
329
|
+
type Merged<U> = (U extends unknown ? (value: U) => void : never) extends (value: infer I) => void ? I : never
|
|
330
|
+
|
|
303
331
|
type Operation = 'insert' | 'update' | 'delete'
|
|
304
332
|
|
|
305
|
-
/**
|
|
306
|
-
|
|
307
|
-
|
|
333
|
+
/**
|
|
334
|
+
* `Member` in a mutation whose action is the table's `Op` action (`Allowed`, any action of an `app` capability's);
|
|
335
|
+
* otherwise the message that says which fix applies.
|
|
336
|
+
*/
|
|
337
|
+
type Write<
|
|
338
|
+
T extends string,
|
|
339
|
+
E extends IndexedTable,
|
|
340
|
+
Op extends Operation,
|
|
341
|
+
A extends string,
|
|
342
|
+
K extends DataKind,
|
|
343
|
+
Member,
|
|
344
|
+
Allowed extends boolean = [A] extends [E['actions'][Op]] ? true : false,
|
|
345
|
+
> = K extends 'mutation' ? Allowed extends true ? Member
|
|
308
346
|
: Unavailable<`${Op} on ${T} needs the action '${E['actions'][Op] & string}', and this endpoint holds '${A}'`>
|
|
309
347
|
: K extends 'job' ? Unavailable<'a job writes inside ctx.transaction((tx) => …): use tx.db there'>
|
|
310
348
|
: Unavailable<`${Op extends 'delete' ? 'remove' : Op} writes, so it runs only in a mutation: declare the endpoint with r.mutation`>
|
|
@@ -317,17 +355,26 @@ type UpdateRefusal<T extends string, E extends IndexedTable, A extends string> =
|
|
|
317
355
|
|
|
318
356
|
/**
|
|
319
357
|
* `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.
|
|
358
|
+
* action; `Allowed`, any action of an `app` capability's); otherwise the message that says which fix applies.
|
|
321
359
|
*/
|
|
322
|
-
type Update<
|
|
323
|
-
|
|
360
|
+
type Update<
|
|
361
|
+
T extends string,
|
|
362
|
+
E extends IndexedTable,
|
|
363
|
+
A extends string,
|
|
364
|
+
K extends DataKind,
|
|
365
|
+
Member,
|
|
366
|
+
Allowed extends boolean = [A] extends [keyof E['writes']] ? true : false,
|
|
367
|
+
> = K extends 'mutation' ? Allowed extends true ? Member : Unavailable<UpdateRefusal<T, E, A>>
|
|
324
368
|
: K extends 'job' ? Unavailable<'a job writes inside ctx.transaction((tx) => …): use tx.db there'>
|
|
325
369
|
: Unavailable<'update writes, so it runs only in a mutation: declare the endpoint with r.mutation'>
|
|
326
370
|
|
|
327
371
|
/** An update's values: any non-empty subset of the columns a capability of `A` updates; an empty one is refused. */
|
|
328
372
|
type UpdateValuesOf<E extends IndexedTable, A extends string> = E['writes'][A & keyof E['writes']]
|
|
329
373
|
|
|
330
|
-
|
|
374
|
+
/** An `app` capability's update values: the columns any of its actions updates, one action's as `UpdateValuesOf`. */
|
|
375
|
+
type AppUpdateValuesOf<E extends IndexedTable, A extends string> = Merged<UpdateValuesOf<E, A>>
|
|
376
|
+
|
|
377
|
+
type Read<T extends string, E extends IndexedTable, A extends string, Member, Allowed extends boolean = Names<E, A>> = Allowed extends true ? Member
|
|
331
378
|
: Unavailable<`${T} does not name the action '${A}': read it from an endpoint whose action ${T} names, or declare unscoped`>
|
|
332
379
|
|
|
333
380
|
/** One page of a keyset list; `next` is the cursor `after` takes, `null` on the last page. */
|
|
@@ -336,7 +383,7 @@ export interface ListPage<Row> {
|
|
|
336
383
|
readonly next: string | null
|
|
337
384
|
}
|
|
338
385
|
|
|
339
|
-
export type OrderTerm<Row> = { [C in
|
|
386
|
+
export type OrderTerm<Row> = { [C in ComparableColumn<Row>]: readonly [C, 'asc' | 'desc'] }[ComparableColumn<Row>]
|
|
340
387
|
|
|
341
388
|
export interface ListOptions<Row> {
|
|
342
389
|
readonly where?: readonly ConditionTuple<Row>[]
|
|
@@ -386,19 +433,21 @@ export interface UnownedTarget<D extends string> {
|
|
|
386
433
|
readonly remove: Unavailable<`no table owns the dimension ${D}, so ctx.db.target has no row of its own`>
|
|
387
434
|
}
|
|
388
435
|
|
|
389
|
-
/** A child table of the target: its rows are addressed by their own key under the target. */
|
|
436
|
+
/** A child table of the target: its rows are addressed by their own key under the target; a view's are only read. */
|
|
390
437
|
export interface ChildRows<T extends string, E extends IndexedTable, A extends string, K extends DataKind> {
|
|
391
438
|
readonly list: Read<T, E, A, (options?: ListOptions<RowFor<E, A>>) => Promise<ListPage<RowFor<E, A>>>>
|
|
392
439
|
readonly get: Read<T, E, A, (key: string) => Promise<RowFor<E, A>>>
|
|
393
|
-
readonly insert:
|
|
394
|
-
|
|
440
|
+
readonly insert: E['view'] extends true ? Unavailable<`${T} is a view, which no site writes: write the tables it reads`>
|
|
441
|
+
: Write<T, E, 'insert', A, K, InsertMethod<E>>
|
|
442
|
+
readonly update: E['view'] extends true ? Unavailable<`${T} is a view, which no site writes: write the tables it reads`> : Update<
|
|
395
443
|
T,
|
|
396
444
|
E,
|
|
397
445
|
A,
|
|
398
446
|
K,
|
|
399
447
|
(key: string, values: { [C in keyof UpdateValuesOf<E, A>]: UpdateValuesOf<E, A>[C] }, ...version: VersionArguments<E>) => Promise<Saved<E>>
|
|
400
448
|
>
|
|
401
|
-
readonly remove:
|
|
449
|
+
readonly remove: E['view'] extends true ? Unavailable<`${T} is a view, which no site writes: write the tables it reads`>
|
|
450
|
+
: Write<T, E, 'delete', A, K, (key: string, ...version: VersionArguments<E>) => Promise<void>>
|
|
402
451
|
}
|
|
403
452
|
|
|
404
453
|
/** A dimension table the target contains: listed in it, and inserted into it by a mutation of its insert action. */
|
|
@@ -444,31 +493,52 @@ export type WithinTables<Ix extends AnyDbIndex, D extends string, A extends stri
|
|
|
444
493
|
readonly [T in WithinIn<Ix, D>]: WithinRows<T, Ix['tables'][T], A, K>
|
|
445
494
|
}
|
|
446
495
|
|
|
447
|
-
/**
|
|
496
|
+
/**
|
|
497
|
+
* `Member` where `ctx.db.app` of action `A` lists the application table: by its read action, or any action for `'any'`;
|
|
498
|
+
* an action of `A`'s does for an `anyOf` endpoint.
|
|
499
|
+
*/
|
|
500
|
+
type AppList<T extends string, E extends IndexedTable, A extends string, Member> = [E['readsAny']] extends [true] ? Member
|
|
501
|
+
: AnyAllowed<A, E['actions']['read']> extends true ? Member
|
|
502
|
+
: Unavailable<ListRefusal<T, E, A>>
|
|
503
|
+
|
|
504
|
+
/** `Member` where `ctx.db.app` of action `A` (any of them) reads a row of the application table by its key. */
|
|
505
|
+
type AppGet<T extends string, E extends IndexedTable, A extends string, Member> = [E['readsAny']] extends [true] ? Member
|
|
506
|
+
: Read<T, E, A, Member, AnyAllowed<A, NamedActions<E>>>
|
|
507
|
+
|
|
508
|
+
/** `Member` where a public procedure of action `A` reads the application table: under its read action, never `'any'`. */
|
|
509
|
+
type PublicRead<T extends string, E extends IndexedTable, A extends string, Member> = [E['readsAny']] extends [true] ? Unavailable<
|
|
510
|
+
`a public procedure does not read ${T}: it is read with 'any', which an app endpoint, a job and a webhook hold`
|
|
511
|
+
>
|
|
512
|
+
: A extends E['actions']['read'] ? Member
|
|
513
|
+
: Unavailable<ListRefusal<T, E, A>>
|
|
514
|
+
|
|
515
|
+
/**
|
|
516
|
+
* An application table under the capability of an `app` endpoint of action `A`, in a site of kind `K`; an `anyOf`
|
|
517
|
+
* endpoint's `A` is its listed actions, and an operation any of them allows is typed. A column with its own read action
|
|
518
|
+
* stays hidden there (`RowFor`): which listed actions the caller holds is known only at run time.
|
|
519
|
+
*/
|
|
448
520
|
export interface ApplicationRows<T extends string, E extends IndexedTable, A extends string, K extends DataKind> {
|
|
449
|
-
readonly list:
|
|
450
|
-
|
|
451
|
-
readonly
|
|
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>>
|
|
521
|
+
readonly list: AppList<T, E, A, (options?: ListOptions<RowFor<E, A>>) => Promise<ListPage<RowFor<E, A>>>>
|
|
522
|
+
readonly count: AppList<T, E, A, (options?: CountOptions<RowFor<E, A>>) => Promise<number>>
|
|
523
|
+
readonly aggregate: AppList<T, E, A, <const S extends AggregateSpec<RowFor<E, A>>>(spec: S) => Promise<AggregateRow<RowFor<E, A>, S>[]>>
|
|
455
524
|
/** One row by its key; a column that declares another read action than `A` is hidden. */
|
|
456
|
-
readonly get:
|
|
525
|
+
readonly get: AppGet<T, E, A, (key: string) => Promise<RowFor<E, A>>>
|
|
457
526
|
/** `SELECT … FOR UPDATE` of one row by its key: a precondition read that no concurrent write overtakes. */
|
|
458
|
-
readonly getForUpdate: K extends 'mutation' ?
|
|
527
|
+
readonly getForUpdate: K extends 'mutation' ? AppGet<T, E, A, (key: string) => Promise<RowFor<E, A>>>
|
|
459
528
|
: K extends 'job' ? Unavailable<'a job writes inside ctx.transaction((tx) => …): use tx.db there'>
|
|
460
529
|
: Unavailable<'getForUpdate locks the row, so it runs only in a mutation: declare the endpoint with r.mutation'>
|
|
461
530
|
/** Leases due rows of a queue table: a job's drain, before its transactions. */
|
|
462
531
|
readonly claim: Claim<T, E, A, K>
|
|
463
|
-
readonly insert: Write<T, E, 'insert', A, K, InsertMethod<E>>
|
|
532
|
+
readonly insert: Write<T, E, 'insert', A, K, InsertMethod<E>, AnyAllowed<A, E['actions']['insert']>>
|
|
464
533
|
readonly update: Update<
|
|
465
534
|
T,
|
|
466
535
|
E,
|
|
467
536
|
A,
|
|
468
537
|
K,
|
|
469
|
-
(key: string, values: { [C in keyof
|
|
538
|
+
(key: string, values: { [C in keyof AppUpdateValuesOf<E, A>]: AppUpdateValuesOf<E, A>[C] }, ...version: VersionArguments<E>) => Promise<Saved<E>>,
|
|
539
|
+
AnyAllowed<A, keyof E['writes']>
|
|
470
540
|
>
|
|
471
|
-
readonly remove: Write<T, E, 'delete', A, K, (key: string, ...version: VersionArguments<E>) => Promise<void>>
|
|
541
|
+
readonly remove: Write<T, E, 'delete', A, K, (key: string, ...version: VersionArguments<E>) => Promise<void>, AnyAllowed<A, E['actions']['delete']>>
|
|
472
542
|
}
|
|
473
543
|
|
|
474
544
|
/**
|
|
@@ -492,10 +562,10 @@ type PublicVersioned<T extends string, E extends IndexedTable, Member> = E['vers
|
|
|
492
562
|
* under `expectVersion`; never locked or claimed.
|
|
493
563
|
*/
|
|
494
564
|
export interface PublicApplicationRows<T extends string, E extends IndexedTable, A extends string, K extends DataKind> {
|
|
495
|
-
readonly list:
|
|
496
|
-
readonly count:
|
|
497
|
-
readonly aggregate:
|
|
498
|
-
readonly get:
|
|
565
|
+
readonly list: PublicRead<T, E, A, (options?: ListOptions<RowFor<E, A>>) => Promise<ListPage<RowFor<E, A>>>>
|
|
566
|
+
readonly count: PublicRead<T, E, A, (options?: CountOptions<RowFor<E, A>>) => Promise<number>>
|
|
567
|
+
readonly aggregate: PublicRead<T, E, A, <const S extends AggregateSpec<RowFor<E, A>>>(spec: S) => Promise<AggregateRow<RowFor<E, A>, S>[]>>
|
|
568
|
+
readonly get: PublicRead<T, E, A, (key: string) => Promise<RowFor<E, A>>>
|
|
499
569
|
readonly getForUpdate: PublicRefusal<'lock'>
|
|
500
570
|
readonly claim: PublicRefusal<'claim'>
|
|
501
571
|
/** By the id a verified signed token of the request carries; answers only the new version. */
|
|
@@ -529,15 +599,17 @@ export type PublicApplicationTables<Ix extends AnyDbIndex, A extends string, K e
|
|
|
529
599
|
* (`RowFor`), or only how many rows match. A count-only action has no `aggregate`: a group or a minimum is a value.
|
|
530
600
|
*/
|
|
531
601
|
export interface AppRecordRows<T extends string, E extends IndexedTable, A extends string> {
|
|
532
|
-
readonly list:
|
|
602
|
+
readonly list: AnyAllowed<A, E['actions']['read'] | E['appRead']> extends true
|
|
603
|
+
? (options?: ListOptions<RowFor<E, A>>) => Promise<ListPage<RowFor<E, A>>>
|
|
533
604
|
: Unavailable<
|
|
534
605
|
`a list of ${T} needs its read action '${E['actions']['read'] & string}' or an action of its appRead, and this endpoint holds '${A}'`
|
|
535
606
|
>
|
|
536
|
-
readonly count:
|
|
607
|
+
readonly count: AnyAllowed<A, E['actions']['read'] | E['appRead'] | E['appCount']> extends true
|
|
608
|
+
? (options?: CountOptions<RowFor<E, A>>) => Promise<number>
|
|
537
609
|
: Unavailable<
|
|
538
610
|
`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
611
|
>
|
|
540
|
-
readonly aggregate:
|
|
612
|
+
readonly aggregate: AnyAllowed<A, E['actions']['read'] | E['appRead']> extends true
|
|
541
613
|
? <const S extends AggregateSpec<RowFor<E, A>>>(spec: S) => Promise<AggregateRow<RowFor<E, A>, S>[]>
|
|
542
614
|
: Unavailable<
|
|
543
615
|
`an aggregate of ${T} needs its read action '${
|
|
@@ -581,7 +653,10 @@ export interface ScopedApplicationRows<E extends IndexedTable> {
|
|
|
581
653
|
* read-only; any other is the message that names its read action.
|
|
582
654
|
*/
|
|
583
655
|
export type ScopedApplicationTables<Ix extends AnyDbIndex, A extends string> = {
|
|
584
|
-
readonly [T in Ix['applications']]: [
|
|
656
|
+
readonly [T in Ix['applications']]: [Ix['tables'][T]['readsAny']] extends [true] ? Unavailable<
|
|
657
|
+
`a scoped endpoint does not read ${T}: it is read with 'any', which an app endpoint, a job and a webhook hold`
|
|
658
|
+
>
|
|
659
|
+
: [A] extends [Ix['tables'][T]['actions']['read']] ? ScopedApplicationRows<Ix['tables'][T]>
|
|
585
660
|
: Unavailable<
|
|
586
661
|
`a scoped endpoint reads ${T} with its read action '${Ix['tables'][T]['actions']['read'] & string}', and this endpoint holds '${A}'`
|
|
587
662
|
>
|
|
@@ -603,19 +678,22 @@ export type UnscopedMode = 'read' | 'write'
|
|
|
603
678
|
export type UnscopedDeclaration<Table extends string, Mode extends UnscopedMode> = { readonly [T in Table]?: Mode }
|
|
604
679
|
|
|
605
680
|
/**
|
|
606
|
-
* The tables of `U` whose mode may be `'write'` that no site writes: a reference table. A union or optional
|
|
607
|
-
* admits `'write'` counts, because the declaration it types may hold it.
|
|
681
|
+
* The tables of `U` whose mode may be `'write'` that no site writes: a reference table or a view. A union or optional
|
|
682
|
+
* mode that admits `'write'` counts, because the declaration it types may hold it.
|
|
608
683
|
*/
|
|
609
684
|
type ReadOnlyWritten<Ix extends AnyDbIndex, U> = {
|
|
610
|
-
[T in keyof U & TableName<Ix>]: 'write' extends U[T]
|
|
685
|
+
[T in keyof U & TableName<Ix>]: 'write' extends U[T]
|
|
686
|
+
? Ix['tables'][T]['mode'] extends 'reference' ? T : Ix['tables'][T]['view'] extends true ? T : never
|
|
687
|
+
: never
|
|
611
688
|
}[keyof U & TableName<Ix>]
|
|
612
689
|
|
|
613
|
-
type ReadOnlyMessage<T extends string> =
|
|
614
|
-
`${T} is a
|
|
690
|
+
type ReadOnlyMessage<T extends string, E extends IndexedTable> = E['view'] extends true
|
|
691
|
+
? `${T} is a view, which no site writes: its rows come from the tables it reads, so declare unscoped: { ${T}: 'read' }`
|
|
692
|
+
: `${T} is a reference table, which no site writes: its rows come from a migration, so declare unscoped: { ${T}: 'read' }`
|
|
615
693
|
|
|
616
694
|
/** An `unscoped` declaration's `'write'` of a reference table, refused at its own line. */
|
|
617
695
|
export type ReadOnlyUnscoped<Ix extends AnyDbIndex, U> = [ReadOnlyWritten<Ix, U>] extends [never] ? unknown
|
|
618
|
-
: { readonly [T in ReadOnlyWritten<Ix, U>]: Unavailable<ReadOnlyMessage<T>> }
|
|
696
|
+
: { readonly [T in ReadOnlyWritten<Ix, U>]: Unavailable<ReadOnlyMessage<T, Ix['tables'][T]>> }
|
|
619
697
|
|
|
620
698
|
type KeylessMessage<T extends string> = `${T} is a reference table, which has no key: read it with list`
|
|
621
699
|
|
|
@@ -686,8 +764,8 @@ type RefFragment<Ix extends AnyDbIndex, D extends string, A extends string> = {
|
|
|
686
764
|
}[TableName<Ix>]
|
|
687
765
|
|
|
688
766
|
/**
|
|
689
|
-
* What a `Within` writes on a `'write'` site: the
|
|
690
|
-
* write action included (the statement's
|
|
767
|
+
* What a `Within` writes on a `'write'` site: the table of a fragment of `use: 'update'` or `'delete'` whose action it
|
|
768
|
+
* holds, a column's own write action included (the statement's columns are the approver's to review, not checked).
|
|
691
769
|
*/
|
|
692
770
|
type WithinWrites<E extends IndexedTable, M extends 'read' | 'write'> = M extends 'write'
|
|
693
771
|
? E['actions']['update'] | E['actions']['delete'] | (keyof E['writes'] & string)
|
|
@@ -702,7 +780,8 @@ type WithinFragment<Ix extends AnyDbIndex, D extends string, A extends string, M
|
|
|
702
780
|
|
|
703
781
|
/** The tables a fragment of an `app` capability of `A` reaches: as `ctx.db.app` lists them for that action. */
|
|
704
782
|
type AppFragment<Ix extends AnyDbIndex, A extends string> = {
|
|
705
|
-
[T in TableName<Ix>]: Ix['tables'][T]['mode'] extends 'app'
|
|
783
|
+
[T in TableName<Ix>]: Ix['tables'][T]['mode'] extends 'app'
|
|
784
|
+
? ([Ix['tables'][T]['readsAny']] extends [true] ? T : A extends NamedActions<Ix['tables'][T]> ? T : never)
|
|
706
785
|
: Ix['tables'][T]['mode'] extends 'dimension' | 'child' ? (A extends Ix['tables'][T]['actions']['read'] | Ix['tables'][T]['appRead'] ? T : never)
|
|
707
786
|
: never
|
|
708
787
|
}[TableName<Ix>]
|