@fabrika/db 0.9.0-dev.23 → 0.9.0-dev.26

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.
@@ -32,15 +32,15 @@ type AppActionsOf<P, T, F extends 'appRead' | 'appCount'> = PolicyOf<P, T> exten
32
32
  readonly [K in F]: readonly (infer A extends string)[];
33
33
  };
34
34
  } ? A : never;
35
- /** Each column that declares its own read action, by column. */
35
+ /** Each column that declares its own read actions, by column: the union of the actions it lists. */
36
36
  type ColumnReadsOf<P, T> = PolicyOf<P, T> extends {
37
37
  readonly columns: infer C;
38
38
  } ? {
39
39
  readonly [K in keyof C as C[K] extends {
40
- readonly read: string;
40
+ readonly read: string | readonly string[];
41
41
  } ? K : never]: C[K] extends {
42
- readonly read: infer A extends string;
43
- } ? A : never;
42
+ readonly read: infer A extends string | readonly string[];
43
+ } ? A extends readonly (infer L extends string)[] ? L : A : never;
44
44
  } : Record<never, never>;
45
45
  type ColumnPoliciesOf<P, T> = PolicyOf<P, T> extends {
46
46
  readonly columns: infer C;
@@ -263,12 +263,14 @@ type ChildrenOf<Ix extends AnyDbIndex, T> = Lookup<Ix['children'], T>;
263
263
  type ListedIn<Ix extends AnyDbIndex, D> = Lookup<Ix['listed'], D>;
264
264
  type WithinIn<Ix extends AnyDbIndex, D> = Lookup<Ix['within'], D>;
265
265
  /**
266
- * The columns a capability of action `A` cannot read: each that declares a read action other than exactly `A`. A
267
- * union `A` (a widened capability type) holds no single action provably, so it reads none of them.
266
+ * The columns a capability of action `A` cannot read: each whose own read actions do not include `A`. A union `A` (a
267
+ * widened capability type) holds no single action provably, so it reads none of them.
268
268
  */
269
269
  type HiddenFor<Reads, A> = {
270
- [C in keyof Reads]: [A] extends [Reads[C]] ? ([Reads[C]] extends [A] ? never : C) : C;
270
+ [C in keyof Reads]: IsSingle<A> extends true ? ([A] extends [Reads[C]] ? never : C) : C;
271
271
  }[keyof Reads];
272
+ /** Whether `A` is one literal, not a union of several. */
273
+ type IsSingle<A, All = A> = [A] extends [never] ? false : A extends unknown ? ([All] extends [A] ? true : false) : never;
272
274
  /**
273
275
  * A row as a capability of action `A` reads, filters and sorts it: a column that declares another read action is
274
276
  * absent. The type keeps the generated row's name, so a read of a hidden column reports
@@ -387,6 +389,11 @@ export interface ListOptions<Row> {
387
389
  readonly limit?: number;
388
390
  readonly after?: string;
389
391
  }
392
+ /** What `all()` takes: a list's filter and order, and no page; it answers every row or refuses past `MAX_ALL_ROWS`. */
393
+ export interface AllOptions<Row> {
394
+ readonly where?: readonly ConditionTuple<Row>[];
395
+ readonly orderBy?: readonly OrderTerm<Row>[];
396
+ }
390
397
  export interface CountOptions<Row> {
391
398
  readonly where?: readonly ConditionTuple<Row>[];
392
399
  }
@@ -418,6 +425,7 @@ export interface UnownedTarget<D extends string> {
418
425
  /** A child table of the target: its rows are addressed by their own key under the target; a view's are only read. */
419
426
  export interface ChildRows<T extends string, E extends IndexedTable, A extends string, K extends DataKind> {
420
427
  readonly list: Read<T, E, A, (options?: ListOptions<RowFor<E, A>>) => Promise<ListPage<RowFor<E, A>>>>;
428
+ readonly all: Read<T, E, A, (options?: AllOptions<RowFor<E, A>>) => Promise<RowFor<E, A>[]>>;
421
429
  readonly get: Read<T, E, A, (key: string) => Promise<RowFor<E, A>>>;
422
430
  readonly insert: E['view'] extends true ? Unavailable<`${T} is a view, which no site writes: write the tables it reads`> : Write<T, E, 'insert', A, K, InsertMethod<E>>;
423
431
  readonly update: E['view'] extends true ? Unavailable<`${T} is a view, which no site writes: write the tables it reads`> : Update<T, E, A, K, (key: string, values: {
@@ -428,6 +436,7 @@ export interface ChildRows<T extends string, E extends IndexedTable, A extends s
428
436
  /** A dimension table the target contains: listed in it, and inserted into it by a mutation of its insert action. */
429
437
  export interface ContainedRows<T extends string, E extends IndexedTable, A extends string, K extends DataKind> {
430
438
  readonly list: Read<T, E, A, (options?: ListOptions<RowFor<E, A>>) => Promise<ListPage<RowFor<E, A>>>>;
439
+ readonly all: Read<T, E, A, (options?: AllOptions<RowFor<E, A>>) => Promise<RowFor<E, A>[]>>;
431
440
  readonly insert: Write<T, E, 'insert', A, K, InsertMethod<E>>;
432
441
  }
433
442
  type SubtreeTables<Ix extends AnyDbIndex, D extends string, A extends string, K extends DataKind, F extends TargetForm> = {
@@ -446,6 +455,7 @@ export type ScopeTarget<Ix extends AnyDbIndex, D extends string, A extends strin
446
455
  export interface WithinRows<T extends string, E extends IndexedTable, A extends string, K extends DataKind> {
447
456
  readonly claim: Claim<T, E, A, K>;
448
457
  readonly list: A extends E['actions']['read'] ? (options?: ListOptions<RowFor<E, A>>) => Promise<ListPage<RowFor<E, A>>> : Unavailable<ListRefusal<T, E, A>>;
458
+ readonly all: A extends E['actions']['read'] ? (options?: AllOptions<RowFor<E, A>>) => Promise<RowFor<E, A>[]> : Unavailable<ListRefusal<T, E, A>>;
449
459
  readonly count: A extends E['actions']['read'] ? (options?: CountOptions<RowFor<E, A>>) => Promise<number> : Unavailable<ListRefusal<T, E, A>>;
450
460
  readonly aggregate: A extends E['actions']['read'] ? <const S extends AggregateSpec<RowFor<E, A>>>(spec: S) => Promise<AggregateRow<RowFor<E, A>, S>[]> : Unavailable<ListRefusal<T, E, A>>;
451
461
  }
@@ -474,6 +484,7 @@ type PublicRead<T extends string, E extends IndexedTable, A extends string, Memb
474
484
  */
475
485
  export interface ApplicationRows<T extends string, E extends IndexedTable, A extends string, K extends DataKind> {
476
486
  readonly list: AppList<T, E, A, (options?: ListOptions<RowFor<E, A>>) => Promise<ListPage<RowFor<E, A>>>>;
487
+ readonly all: AppList<T, E, A, (options?: AllOptions<RowFor<E, A>>) => Promise<RowFor<E, A>[]>>;
477
488
  readonly count: AppList<T, E, A, (options?: CountOptions<RowFor<E, A>>) => Promise<number>>;
478
489
  readonly aggregate: AppList<T, E, A, <const S extends AggregateSpec<RowFor<E, A>>>(spec: S) => Promise<AggregateRow<RowFor<E, A>, S>[]>>;
479
490
  /** One row by its key; a column that declares another read action than `A` is hidden. */
@@ -502,7 +513,7 @@ export interface PublicInsertMethod<E extends IndexedTable> {
502
513
  }
503
514
  type PublicRefusal<Op extends string> = Unavailable<`a public procedure does not ${Op} a row: it inserts, and reads under the table's read action`>;
504
515
  /** A public procedure updates and removes only a versioned row, by an id a verified signed token of its request carries. */
505
- type PublicVersioned<T extends string, E extends IndexedTable, Member> = E['versioned'] extends true ? Member : Unavailable<`a public procedure updates and removes only a versioned row: declare version in the policy of ${T}`>;
516
+ type PublicVersioned<T extends string, E extends IndexedTable, Member> = E['versioned'] extends true ? Member : Unavailable<`a public procedure updates and removes only a versioned row: give ${T} a version column`>;
506
517
  /**
507
518
  * An application table of a public procedure of action `A`, in a site of kind `K`: read under its read action only,
508
519
  * inserted with nothing read back, and updated or removed only by the id a verified signed token of the request carries,
@@ -510,6 +521,7 @@ type PublicVersioned<T extends string, E extends IndexedTable, Member> = E['vers
510
521
  */
511
522
  export interface PublicApplicationRows<T extends string, E extends IndexedTable, A extends string, K extends DataKind> {
512
523
  readonly list: PublicRead<T, E, A, (options?: ListOptions<RowFor<E, A>>) => Promise<ListPage<RowFor<E, A>>>>;
524
+ readonly all: Unavailable<'a public procedure pages its reads: read the table with list'>;
513
525
  readonly count: PublicRead<T, E, A, (options?: CountOptions<RowFor<E, A>>) => Promise<number>>;
514
526
  readonly aggregate: PublicRead<T, E, A, <const S extends AggregateSpec<RowFor<E, A>>>(spec: S) => Promise<AggregateRow<RowFor<E, A>, S>[]>>;
515
527
  readonly get: PublicRead<T, E, A, (key: string) => Promise<RowFor<E, A>>>;
@@ -539,6 +551,7 @@ export type PublicApplicationTables<Ix extends AnyDbIndex, A extends string, K e
539
551
  */
540
552
  export interface AppRecordRows<T extends string, E extends IndexedTable, A extends string> {
541
553
  readonly list: AnyAllowed<A, E['actions']['read'] | E['appRead']> extends true ? (options?: ListOptions<RowFor<E, A>>) => Promise<ListPage<RowFor<E, A>>> : Unavailable<`a list of ${T} needs its read action '${E['actions']['read'] & string}' or an action of its appRead, and this endpoint holds '${A}'`>;
554
+ readonly all: AnyAllowed<A, E['actions']['read'] | E['appRead']> extends true ? (options?: AllOptions<RowFor<E, A>>) => Promise<RowFor<E, A>[]> : Unavailable<`a list of ${T} needs its read action '${E['actions']['read'] & string}' or an action of its appRead, and this endpoint holds '${A}'`>;
542
555
  readonly count: AnyAllowed<A, E['actions']['read'] | E['appRead'] | E['appCount']> extends true ? (options?: CountOptions<RowFor<E, A>>) => Promise<number> : Unavailable<`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}'`>;
543
556
  readonly aggregate: AnyAllowed<A, E['actions']['read'] | E['appRead']> extends true ? <const S extends AggregateSpec<RowFor<E, A>>>(spec: S) => Promise<AggregateRow<RowFor<E, A>, S>[]> : Unavailable<`an aggregate of ${T} needs its read action '${E['actions']['read'] & string}' or an action of its appRead, and this endpoint holds '${A}': an appCount action only counts`>;
544
557
  }
@@ -560,6 +573,7 @@ type TableRow<E extends IndexedTable> = RowFor<E, E['actions']['read']>;
560
573
  */
561
574
  export interface ScopedApplicationRows<E extends IndexedTable> {
562
575
  list(options?: ListOptions<TableRow<E>>): Promise<ListPage<TableRow<E>>>;
576
+ all(options?: AllOptions<TableRow<E>>): Promise<TableRow<E>[]>;
563
577
  count(options?: CountOptions<TableRow<E>>): Promise<number>;
564
578
  aggregate<const S extends AggregateSpec<TableRow<E>>>(spec: S): Promise<AggregateRow<TableRow<E>, S>[]>;
565
579
  /** One row by its key. */
@@ -579,6 +593,7 @@ export type ScopedApplicationTables<Ix extends AnyDbIndex, A extends string> = {
579
593
  /** A reference table: every endpoint reads it, through its read action, and none writes it. */
580
594
  export interface ReferenceRows<E extends IndexedTable> {
581
595
  list(options?: ListOptions<TableRow<E>>): Promise<ListPage<TableRow<E>>>;
596
+ all(options?: AllOptions<TableRow<E>>): Promise<TableRow<E>[]>;
582
597
  count(options?: CountOptions<TableRow<E>>): Promise<number>;
583
598
  }
584
599
  /** `ctx.db.reference`. */
@@ -607,6 +622,7 @@ type KeylessMessage<T extends string> = `${T} is a reference table, which has no
607
622
  /** A table a site declared `unscoped`: its scope predicate is `true`, and `'read'` writes nothing. */
608
623
  export interface UnscopedRows<T extends string, E extends IndexedTable, M extends UnscopedMode, K extends DataKind> {
609
624
  list(options?: ListOptions<TableRow<E>>): Promise<ListPage<TableRow<E>>>;
625
+ all(options?: AllOptions<TableRow<E>>): Promise<TableRow<E>[]>;
610
626
  count(options?: CountOptions<TableRow<E>>): Promise<number>;
611
627
  aggregate<const S extends AggregateSpec<TableRow<E>>>(spec: S): Promise<AggregateRow<TableRow<E>, S>[]>;
612
628
  readonly get: E['mode'] extends 'reference' ? Unavailable<KeylessMessage<T>> : (key: string) => Promise<TableRow<E>>;
@@ -7,6 +7,12 @@ import { type DataLayerRuntime, type TableContext } from './store';
7
7
  /** A list page holds this many rows unless the caller asks for fewer, and never more than `MAX_LIST_LIMIT`. */
8
8
  export declare const DEFAULT_LIST_LIMIT = 50;
9
9
  export declare const MAX_LIST_LIMIT = 500;
10
+ /**
11
+ * The most rows `all()` answers; past it, it refuses instead of truncating. It bounds rows, not bytes: 10 000 rows of
12
+ * ~1 KB, held two or three times over (driver, answer, response), is tens of MB of the one 128 MB Durable Object
13
+ * isolate every concurrent invocation of the environment shares. Wide rows or a growing set page with `list`.
14
+ */
15
+ export declare const MAX_ALL_ROWS = 10000;
10
16
  /** What every handle of one site, in one invocation, is made for. */
11
17
  export interface DataSite {
12
18
  /** A query reads; a mutation also writes and locks; a job reads, and writes only in its fenced transaction. */
@@ -56,6 +62,11 @@ export interface DataHandles {
56
62
  readonly reference: object;
57
63
  /** `ctx.db.unscoped`, or `undefined` for a site that declares none. */
58
64
  readonly unscoped: object | undefined;
65
+ /**
66
+ * At most two rows of `table` whose `column` holds `value`, as an unscoped read answers them: the framework's read of
67
+ * the caller's own row (`ctx.caller` of `@fabrika/app`). No handle of an application reaches it.
68
+ */
69
+ rowsNaming(table: string, column: string, value: string): Promise<readonly Readonly<Record<string, unknown>>[]>;
59
70
  }
60
71
  /** The data side of one bound layer, which only the framework reaches (`@fabrika/db/framework`). */
61
72
  export interface DataAccess {
@@ -191,22 +191,22 @@ export type FilePolicies<Col extends string> = {
191
191
  readonly [C in Col]?: string;
192
192
  };
193
193
  /**
194
- * A column's own actions. `read`: only a capability of exactly that action reads, filters or sorts by the column;
194
+ * A column's own actions. `read`: only a capability of one of those actions reads, filters or sorts by the column;
195
195
  * without one it falls back to the table's read action. `write`: only a capability of that action updates the column,
196
196
  * and the table's update action does not, so the column is not in `update`.
197
197
  */
198
198
  export type ColumnPolicies<Col extends string, A extends string> = {
199
199
  readonly [C in Col]?: ColumnPolicy<A>;
200
200
  };
201
- /** A column's own read action, its own write action, or both. */
201
+ /** A column's own read actions (one, or a list as a table's read takes), its own write action, or both. */
202
202
  export interface ColumnPolicy<A extends string> {
203
- readonly read?: A;
203
+ readonly read?: TableAction<A>;
204
204
  readonly write?: A;
205
205
  }
206
206
  /** A table no handler writes (a reference table) declares column read actions only. */
207
207
  export type ColumnReadPolicies<Col extends string, A extends string> = {
208
208
  readonly [C in Col]?: {
209
- readonly read: A;
209
+ readonly read: TableAction<A>;
210
210
  };
211
211
  };
212
212
  export interface DimensionPolicy<Col extends string, A extends string, V extends Col = Col> extends WritablePolicy<Col, A, V, RecordActions<A>> {
@@ -333,8 +333,8 @@ export interface TableModel {
333
333
  readonly queue: QueueModel | undefined;
334
334
  /** The file columns, each with its file kind: a reference to `fabrika_files`, written only with a file reference. */
335
335
  readonly files: Readonly<Record<string, string>>;
336
- /** The columns that declare their own read action, as declared (also those equal to the table's). */
337
- readonly columnReads: Readonly<Record<string, string>>;
336
+ /** The columns that declare their own read actions, as declared (also those equal to the table's). */
337
+ readonly columnReads: Readonly<Record<string, readonly string[]>>;
338
338
  /** The columns that declare their own write action: only a capability of it updates them. */
339
339
  readonly columnWrites: Readonly<Record<string, string>>;
340
340
  /** The columns of Postgres type `date`, read as `YYYY-MM-DD` text. */
@@ -512,8 +512,8 @@ export declare function updatableByAny(model: TableModel, actions: readonly stri
512
512
  /** Whether the table names `action`: one of its table actions, or a column's own write action. */
513
513
  export declare function namesAction(model: TableModel, action: string): boolean;
514
514
  /**
515
- * The columns a capability of `action` reads: every one that declares no read action of its own, or exactly this
516
- * one. A capability reads its own row, so a write `Ref` sees the columns of its action and none of another's.
515
+ * The columns a capability of `action` reads: every one that declares no read action of its own, or one whose own
516
+ * read actions include this one. A capability reads its own row, so a write `Ref` sees the columns of its action and none of another's.
517
517
  */
518
518
  export declare function visibleFor(model: TableModel, action: string): readonly string[];
519
519
  /**
@@ -538,6 +538,8 @@ export declare function readsAsAction(model: TableModel, action: string, reach:
538
538
  * read action of its own, and one that does where that action reads every answered row (`readsAsAction`).
539
539
  */
540
540
  export declare function visibleThrough(model: TableModel, reach: ReadReach): readonly string[];
541
+ /** Whether one of a column's own read actions reads every row a read of `reach` answers (`readsAsAction`). */
542
+ export declare function readsAsOneOf(model: TableModel, actions: readonly string[], reach: ReadReach): boolean;
541
543
  /** Every action that updates a column of `model`: its update action and each column's own write action. */
542
544
  export declare function updateActions(model: TableModel): readonly string[];
543
545
  /** Whether `names` holds `name`: an operation's actions, the one way a policy check reads a list. */
@@ -637,11 +639,11 @@ type TableReadAction<P, T> = PolicyOf<P, T> extends {
637
639
  readonly read: infer A;
638
640
  };
639
641
  } ? A : ActionOf<P, T, 'read'>;
640
- /** Columns whose own read action is not the table's: no read by the table's read action returns them. */
642
+ /** Columns whose own read actions leave out one of the table's: no read by the table's read action returns them. */
641
643
  type RestrictedColumns<P, T> = [ColumnPoliciesOf<P, T>] extends [never] ? never : {
642
644
  [K in keyof ColumnPoliciesOf<P, T>]: ColumnPoliciesOf<P, T>[K] extends {
643
645
  readonly read: infer R;
644
- } ? ([TableReadAction<P, T>] extends [R] ? never : K) : never;
646
+ } ? ([TableReadAction<P, T>] extends [R extends readonly (infer L extends string)[] ? L : R] ? never : K) : never;
645
647
  }[keyof ColumnPoliciesOf<P, T>];
646
648
  /** A row of table `T` as a store reads it, by the table's read action. */
647
649
  export type PolicyRow<DB, P, T> = T extends keyof DB ? Flat<Omit<Selectable<DB[T]>, RestrictedColumns<P, T>>> : never;
@@ -5,6 +5,7 @@ import type { WebhookTables } from './framework-tables';
5
5
  import type { JobLeases } from './job-leases';
6
6
  import type { JobWakes } from './job-wakes';
7
7
  import type { DataAccess } from './orm';
8
+ import type { RequestReceipts } from './request-receipts';
8
9
  import type { DataLayerRuntime, TableContext, TableProbe } from './store';
9
10
  import type { StoredFiles } from './stored-files';
10
11
  /**
@@ -43,6 +44,8 @@ export interface FrameworkTables {
43
44
  readonly admissions: Admissions;
44
45
  /** Every stored file (`fabrika_files`): staged before its object, claimed at its first reference, swept unreferenced. */
45
46
  readonly files: StoredFiles;
47
+ /** The answers of keyed mutations, read and written in the invocation's transaction (ADR-0078). */
48
+ readonly receipts: RequestReceipts;
46
49
  }
47
50
  /** A connection of the layer and its locator. Only its holder can `release` it, which lets the layer be bound again. */
48
51
  export interface ProvisionalBinding {
@@ -0,0 +1,22 @@
1
+ import { type Session } from './database';
2
+ /** What a receipt records of the request it answers: its operation, the digest of its input, and the output's JSON. */
3
+ export interface RequestReceipt {
4
+ readonly operation: string;
5
+ readonly identity: string;
6
+ readonly output: string;
7
+ }
8
+ export interface RequestReceipts {
9
+ /**
10
+ * The receipt of `principal`'s `key`, read in `session`; `null` when there is none. On Postgres it first takes the
11
+ * key's transaction lock, so a concurrent copy of the request waits for this transaction and then reads its receipt
12
+ * instead of running too; SQLite's one writer already serializes them.
13
+ */
14
+ find(session: Session, principal: string, key: string): Promise<RequestReceipt | null>;
15
+ /**
16
+ * Records `receipt` under `principal`'s `key` in `session`, created at `now` (epoch ms), after pruning a batch of
17
+ * receipts created before `retainedSince`. `false` when the key already has a receipt: another copy of the request
18
+ * recorded it meanwhile, and this one's transaction must not commit.
19
+ */
20
+ record(session: Session, principal: string, key: string, receipt: RequestReceipt, now: number, retainedSince: number): Promise<boolean>;
21
+ }
22
+ export declare function requestReceipts(): RequestReceipts;
@@ -5,7 +5,7 @@ export { applicationRowRefusalText } from './application-rows';
5
5
  export type { ApplicationRowRefusal, ApplicationRowsPlan } from './application-rows';
6
6
  export { PolicyError } from './errors';
7
7
  export type { FileReference } from './file-references';
8
- export type { AnyDbIndex, ApplicationRows, ApplicationTables, AppRecordRows, ChildRows, ClaimedRow, ClaimedRowHandle, ClaimOptions, ContainedRows, CountOptions, DataKind, DbIndex, FragmentTable, IndexedTable, InsertConflict, InsertMethod, ListOptions, ListPage, NoUnscoped, OrderTerm, OwnerRow, PublicApplicationRows, PublicApplicationTables, PublicInsertMethod, ReadOnlyUnscoped, ReferenceRows, ReferenceTables, RowFor, RowOf, Saved, ScopedApplicationRows, ScopedApplicationTables, ScopeTarget, TableNameOf, TargetForm, TargetRow, Unavailable, UnownedTarget, UnscopedDeclaration, UnscopedInsert, UnscopedMode, UnscopedRows, UnscopedTables, VersionArguments, WithinRows, WithinTables, } from './orm-types';
8
+ export type { AllOptions, AnyDbIndex, ApplicationRows, ApplicationTables, AppRecordRows, ChildRows, ClaimedRow, ClaimedRowHandle, ClaimOptions, ContainedRows, CountOptions, DataKind, DbIndex, FragmentTable, IndexedTable, InsertConflict, InsertMethod, ListOptions, ListPage, NoUnscoped, OrderTerm, OwnerRow, PublicApplicationRows, PublicApplicationTables, PublicInsertMethod, ReadOnlyUnscoped, ReferenceRows, ReferenceTables, RowFor, RowOf, Saved, ScopedApplicationRows, ScopedApplicationTables, ScopeTarget, TableNameOf, TargetForm, TargetRow, Unavailable, UnownedTarget, UnscopedDeclaration, UnscopedInsert, UnscopedMode, UnscopedRows, UnscopedTables, VersionArguments, WithinRows, WithinTables, } from './orm-types';
9
9
  export { type ApplicationPolicy, type ApplicationScope, type ChildPolicy, type ChildScope, type ColumnList, defineTables, type DimensionPolicy, type DimensionScope, type FilePolicies, isTables, type QueuePolicy, readTables, type RecordActions, type ReferencePolicy, type ReferenceScope, type TableAction, type TableActions, type TableInsert, Tables, type TableUpdate, type VersionColumn, type VocabularyTables, } from './policy';
10
10
  export type { ColumnTypes, ColumnViews, DefineVocabularyPolicies, NormalizedPolicies, TableVocabulary, TypedColumnList, VocabularyAction, VocabularyChildScope, VocabularyContainer, VocabularyContainerOf, VocabularyDimension, VocabularyDimensionScope, VocabularyPolicyMap, VocabularyTablePolicy, VocabularyViewPolicy, } from './vocabulary-tables';
11
11
  /**
@@ -89,11 +89,20 @@ export type VocabularyChildScope<Table extends string, Col extends string> = {
89
89
  readonly column?: Col;
90
90
  readonly inherit?: readonly Col[];
91
91
  } & KeyField<Col>;
92
- interface WritableFields<Col extends string, A extends string, Ver extends Col, Acts = TableActions<A>> {
92
+ /** A written table whose `version` column is not an INTEGER names its version column: `version` would be it otherwise. */
93
+ interface NamesItsVersionColumn<Ver extends string> {
94
+ readonly version: Ver;
95
+ }
96
+ /** `version` defaults to the `version` column, so it is required only where that column holds no number. */
97
+ type VersionField<Col extends string, Ver extends Col> = 'version' extends Col ? 'version' extends Ver ? {
98
+ readonly version?: Ver;
99
+ } : NamesItsVersionColumn<Ver> : {
100
+ readonly version?: Ver;
101
+ };
102
+ interface WritableFields<Col extends string, A extends string, Acts = TableActions<A>> {
93
103
  readonly actions: Acts;
94
104
  readonly insert?: readonly Col[];
95
105
  readonly update?: readonly Col[];
96
- readonly version?: Ver;
97
106
  readonly createdBy?: Col;
98
107
  readonly columns?: ColumnPolicies<Col, A>;
99
108
  /** The file columns, each with the file kind it holds (`c.files`): a `uuid` foreign key to `fabrika_files`. */
@@ -185,10 +194,10 @@ type ChildScopes<Table extends string, Col extends string, S, P> = unknown exten
185
194
  } ? T : never;
186
195
  }[keyof P] & string;
187
196
  } : ChildTableScope<Parent, StatedDimensionOf<P, Parent>, Col> : VocabularyChildScope<Table, Col>;
188
- type AuthoredDimensionPolicy<Col extends string, A extends string, V, Ver extends Col, S> = WritableFields<Col, A, Ver, RecordActions<A>> & {
197
+ type AuthoredDimensionPolicy<Col extends string, A extends string, V, Ver extends Col, S> = WritableFields<Col, A, RecordActions<A>> & VersionField<Col, Ver> & {
189
198
  readonly scope: DimensionScopes<V, Col, S>;
190
199
  };
191
- type AuthoredChildPolicy<Table extends string, Col extends string, A extends string, Ver extends Col, S, P> = WritableFields<Col, A, Ver, RecordActions<A>> & {
200
+ type AuthoredChildPolicy<Table extends string, Col extends string, A extends string, Ver extends Col, S, P> = WritableFields<Col, A, RecordActions<A>> & VersionField<Col, Ver> & {
192
201
  readonly scope: ChildScopes<Table, Col, S, P>;
193
202
  };
194
203
  /** What a view's policy holds besides its scope: read actions only, no write list and no stamped column. */
@@ -213,7 +222,7 @@ interface ReadOnlyFields<Col extends string, A extends string> {
213
222
  readonly files?: never;
214
223
  readonly columns?: {
215
224
  readonly [C in Col]?: {
216
- readonly read: A;
225
+ readonly read: TableAction<A>;
217
226
  readonly write?: never;
218
227
  };
219
228
  };
@@ -244,13 +253,13 @@ type ReferenceViewPolicy<Col extends string, A extends string> = ReferencePolicy
244
253
  readonly actions: NoWriteActions;
245
254
  readonly columns?: {
246
255
  readonly [C in Col]?: {
247
- readonly read: A;
256
+ readonly read: TableAction<A>;
248
257
  readonly write?: never;
249
258
  };
250
259
  };
251
260
  };
252
261
  /** An application table's scope: why its rows belong to no dimension; `key` defaults to `id`. */
253
- type AuthoredApplicationPolicy<Col extends string, A extends string, Ver extends Col> = WritableFields<Col, A, Ver, ApplicationActions<A>> & {
262
+ type AuthoredApplicationPolicy<Col extends string, A extends string, Ver extends Col> = WritableFields<Col, A, ApplicationActions<A>> & VersionField<Col, Ver> & {
254
263
  readonly scope: {
255
264
  readonly app: string;
256
265
  } & KeyField<Col>;
@@ -331,19 +340,33 @@ type NormalScope<S, V, P> = S extends {
331
340
  type ViewMark<T, Views> = T extends Views ? {
332
341
  readonly view: true;
333
342
  } : unknown;
343
+ /** A written table's `version` and `created_by` columns are its version and creator unless its policy names others. */
344
+ type Conventions<Row, Pol> = (Pol extends {
345
+ readonly version: string;
346
+ } ? unknown : 'version' extends keyof Row ? {
347
+ readonly version: 'version';
348
+ } : unknown) & (Pol extends {
349
+ readonly createdBy: string;
350
+ } ? unknown : 'created_by' extends keyof Row ? {
351
+ readonly createdBy: 'created_by';
352
+ } : unknown);
353
+ /** Only a dimension, child or application table is written; a view and a reference table get no convention. */
354
+ type WrittenConventions<DB, T, Pol, S, Views> = T extends Views ? unknown : S extends {
355
+ readonly reference: unknown;
356
+ } ? unknown : Conventions<T extends keyof DB ? DB[T] : never, Pol>;
334
357
  /**
335
- * The policies as the layer models them: every dimension table's key and container, and every child's key and
336
- * column, stated as the vocabulary and the conventions derive them; each view marked.
358
+ * The policies as the layer models them: every dimension table's key and container, every child's key and column,
359
+ * and a written table's version and creator, stated as the vocabulary and the conventions derive them; each view marked.
337
360
  */
338
- export type NormalizedPolicies<P, V, Views = never> = {
361
+ export type NormalizedPolicies<DB, P, V, Views = never> = {
339
362
  readonly [T in keyof P]: P[T] extends {
340
363
  readonly scope: infer S;
341
364
  } ? Flat<Omit<P[T], 'scope' | 'view'> & {
342
365
  readonly scope: NormalScope<S, V, P>;
343
- } & ViewMark<T, Views>> : P[T];
366
+ } & ViewMark<T, Views> & WrittenConventions<DB, T, P[T], S, Views>> : P[T];
344
367
  };
345
368
  /** Takes the policies of exactly the tables of the schema, and of any of its views, written in the vocabulary form. */
346
369
  export type DefineVocabularyPolicies<DB, V, Views extends string = never> = <const P extends VocabularyPolicyMap<DB, V, P, Views>>(policies: P & {
347
370
  readonly [K in Exclude<keyof P, keyof DB>]: never;
348
- }) => Tables<DB, NormalizedPolicies<P, V, Views>>;
371
+ }) => Tables<DB, NormalizedPolicies<DB, P, V, Views>>;
349
372
  export {};