@fabrika/db 0.9.0-dev.10 → 0.9.0-dev.12
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 +5 -5
- package/src/codegen-sqlite.ts +8 -5
- package/src/codegen.ts +64 -27
- package/src/index.ts +2 -0
- package/src/orm-types.ts +18 -10
- package/src/orm.ts +19 -5
- package/src/policy.ts +58 -9
- package/src/store.ts +4 -0
- package/src/tables.ts +3 -0
- package/src/testing-shared.ts +166 -42
- package/src/testing-sqlite.ts +8 -2
- package/src/testing.ts +8 -2
- package/src/vocabulary-tables.ts +88 -17
- package/types/src/codegen.d.ts +24 -11
- package/types/src/index.d.ts +1 -1
- package/types/src/orm-types.d.ts +14 -9
- package/types/src/orm.d.ts +5 -0
- package/types/src/policy.d.ts +6 -2
- package/types/src/tables.d.ts +1 -1
- package/types/src/testing-shared.d.ts +29 -10
- package/types/src/testing-sqlite.d.ts +1 -1
- package/types/src/testing.d.ts +1 -1
- package/types/src/vocabulary-tables.d.ts +82 -16
package/types/src/orm-types.d.ts
CHANGED
|
@@ -101,6 +101,8 @@ export interface IndexedTable {
|
|
|
101
101
|
readonly versioned: boolean;
|
|
102
102
|
/** A queue table: its rows are claimed and finished through `row.ref`. */
|
|
103
103
|
readonly queued: boolean;
|
|
104
|
+
/** A view: read like its scope's tables, written by no site (ADR-0072). */
|
|
105
|
+
readonly view: boolean;
|
|
104
106
|
}
|
|
105
107
|
type IndexedTableOf<DB, P, T extends keyof P & keyof DB & string, V = unknown> = {
|
|
106
108
|
readonly mode: ModeOf<P, T>;
|
|
@@ -131,6 +133,9 @@ type IndexedTableOf<DB, P, T extends keyof P & keyof DB & string, V = unknown> =
|
|
|
131
133
|
readonly queued: PolicyOf<P, T> extends {
|
|
132
134
|
readonly queue: object;
|
|
133
135
|
} ? true : false;
|
|
136
|
+
readonly view: PolicyOf<P, T> extends {
|
|
137
|
+
readonly view: true;
|
|
138
|
+
} ? true : false;
|
|
134
139
|
};
|
|
135
140
|
type OwnerTable<P, D> = {
|
|
136
141
|
[K in keyof P]: P[K] extends {
|
|
@@ -379,15 +384,15 @@ export interface UnownedTarget<D extends string> {
|
|
|
379
384
|
readonly update: Unavailable<`no table owns the dimension ${D}, so ctx.db.target has no row of its own`>;
|
|
380
385
|
readonly remove: Unavailable<`no table owns the dimension ${D}, so ctx.db.target has no row of its own`>;
|
|
381
386
|
}
|
|
382
|
-
/** A child table of the target: its rows are addressed by their own key under the target. */
|
|
387
|
+
/** A child table of the target: its rows are addressed by their own key under the target; a view's are only read. */
|
|
383
388
|
export interface ChildRows<T extends string, E extends IndexedTable, A extends string, K extends DataKind> {
|
|
384
389
|
readonly list: Read<T, E, A, (options?: ListOptions<RowFor<E, A>>) => Promise<ListPage<RowFor<E, A>>>>;
|
|
385
390
|
readonly get: Read<T, E, A, (key: string) => Promise<RowFor<E, A>>>;
|
|
386
|
-
readonly insert: Write<T, E, 'insert', A, K, InsertMethod<E>>;
|
|
387
|
-
readonly update: Update<T, E, A, K, (key: string, values: {
|
|
391
|
+
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>>;
|
|
392
|
+
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: {
|
|
388
393
|
[C in keyof UpdateValuesOf<E, A>]: UpdateValuesOf<E, A>[C];
|
|
389
394
|
}, ...version: VersionArguments<E>) => Promise<Saved<E>>>;
|
|
390
|
-
readonly remove: Write<T, E, 'delete', A, K, (key: string, ...version: VersionArguments<E>) => Promise<void>>;
|
|
395
|
+
readonly remove: E['view'] extends true ? Unavailable<`${T} is a view, which no site writes: write the tables it reads`> : Write<T, E, 'delete', A, K, (key: string, ...version: VersionArguments<E>) => Promise<void>>;
|
|
391
396
|
}
|
|
392
397
|
/** A dimension table the target contains: listed in it, and inserted into it by a mutation of its insert action. */
|
|
393
398
|
export interface ContainedRows<T extends string, E extends IndexedTable, A extends string, K extends DataKind> {
|
|
@@ -543,16 +548,16 @@ export type UnscopedDeclaration<Table extends string, Mode extends UnscopedMode>
|
|
|
543
548
|
readonly [T in Table]?: Mode;
|
|
544
549
|
};
|
|
545
550
|
/**
|
|
546
|
-
* The tables of `U` whose mode may be `'write'` that no site writes: a reference table. A union or optional
|
|
547
|
-
* admits `'write'` counts, because the declaration it types may hold it.
|
|
551
|
+
* The tables of `U` whose mode may be `'write'` that no site writes: a reference table or a view. A union or optional
|
|
552
|
+
* mode that admits `'write'` counts, because the declaration it types may hold it.
|
|
548
553
|
*/
|
|
549
554
|
type ReadOnlyWritten<Ix extends AnyDbIndex, U> = {
|
|
550
|
-
[T in keyof U & TableName<Ix>]: 'write' extends U[T] ? Ix['tables'][T]['mode'] extends 'reference' ? T : never : never;
|
|
555
|
+
[T in keyof U & TableName<Ix>]: 'write' extends U[T] ? Ix['tables'][T]['mode'] extends 'reference' ? T : Ix['tables'][T]['view'] extends true ? T : never : never;
|
|
551
556
|
}[keyof U & TableName<Ix>];
|
|
552
|
-
type ReadOnlyMessage<T extends string> = `${T} is a reference table, which no site writes: its rows come from a migration, so declare unscoped: { ${T}: 'read' }`;
|
|
557
|
+
type ReadOnlyMessage<T extends string, E extends IndexedTable> = E['view'] extends true ? `${T} is a view, which no site writes: its rows come from the tables it reads, so declare unscoped: { ${T}: 'read' }` : `${T} is a reference table, which no site writes: its rows come from a migration, so declare unscoped: { ${T}: 'read' }`;
|
|
553
558
|
/** An `unscoped` declaration's `'write'` of a reference table, refused at its own line. */
|
|
554
559
|
export type ReadOnlyUnscoped<Ix extends AnyDbIndex, U> = [ReadOnlyWritten<Ix, U>] extends [never] ? unknown : {
|
|
555
|
-
readonly [T in ReadOnlyWritten<Ix, U>]: Unavailable<ReadOnlyMessage<T>>;
|
|
560
|
+
readonly [T in ReadOnlyWritten<Ix, U>]: Unavailable<ReadOnlyMessage<T, Ix['tables'][T]>>;
|
|
556
561
|
};
|
|
557
562
|
type KeylessMessage<T extends string> = `${T} is a reference table, which has no key: read it with list`;
|
|
558
563
|
/** A table a site declared `unscoped`: its scope predicate is `true`, and `'read'` writes nothing. */
|
package/types/src/orm.d.ts
CHANGED
|
@@ -80,3 +80,8 @@ export interface DataLayerParts {
|
|
|
80
80
|
fenced<T>(runtime: DataLayerRuntime, work: (session: Session) => Promise<T>): Promise<T>;
|
|
81
81
|
}
|
|
82
82
|
export declare function dataAccess(parts: DataLayerParts): DataAccess;
|
|
83
|
+
/**
|
|
84
|
+
* When a claim that leased rows re-arms its job, by the claim's own clock `now`: at once after a full claim, else
|
|
85
|
+
* at the next due row it would take but no sooner than a second after it, or never when it would take none.
|
|
86
|
+
*/
|
|
87
|
+
export declare function claimRearmAt(now: Date, full: boolean, next: Date | null): Date | null;
|
package/types/src/policy.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import type { Insertable, Selectable, SelectType, Updateable } from 'kysely';
|
|
|
2
2
|
import { type NameSet } from './builtins';
|
|
3
3
|
import type { Unavailable } from './orm-types';
|
|
4
4
|
export type { RowOf } from './orm-types';
|
|
5
|
-
import type { ColumnTypes, DefineVocabularyPolicies, TableVocabulary } from './vocabulary-tables';
|
|
5
|
+
import type { ColumnTypes, ColumnViews, DefineVocabularyPolicies, TableVocabulary } from './vocabulary-tables';
|
|
6
6
|
export interface DimensionScope<Col extends string> {
|
|
7
7
|
readonly dimension: string;
|
|
8
8
|
readonly key: Col;
|
|
@@ -136,6 +136,8 @@ export interface TableColumns<Col extends string = string, Table extends string
|
|
|
136
136
|
readonly booleans?: readonly Col[];
|
|
137
137
|
/** The JSON columns (`jsonb`, or declared `JSONB` on SQLite), written as a JSON value and never compared; absent when none. */
|
|
138
138
|
readonly json?: readonly Col[];
|
|
139
|
+
/** A view: read-only, with no foreign key; absent for a table. */
|
|
140
|
+
readonly view?: true;
|
|
139
141
|
}
|
|
140
142
|
/** The columns and foreign keys of every table, generated next to the schema types. */
|
|
141
143
|
export type ColumnList<DB> = {
|
|
@@ -212,6 +214,8 @@ export interface TableModel {
|
|
|
212
214
|
readonly json: NameSet;
|
|
213
215
|
/** The table's foreign keys, from the column list; a containment column must be one to its container's key. */
|
|
214
216
|
readonly foreignKeys: readonly ForeignKey[];
|
|
217
|
+
/** A view: a child or reference table no operation but a read reaches, whose containment only its SQL states. */
|
|
218
|
+
readonly view: boolean;
|
|
215
219
|
/**
|
|
216
220
|
* The containment links this table carries a column for, nearest first: a dimension table's container and
|
|
217
221
|
* the container's own chain; a child's parent and the parent's chain. Every predicate of a Ref covers all.
|
|
@@ -238,7 +242,7 @@ export declare function modelOf(models: TableModels, table: string): TableModel
|
|
|
238
242
|
*/
|
|
239
243
|
export declare function defineTables<const V extends TableVocabulary, C extends object>(vocabulary: V, columns: C): VocabularyTables<V, C>;
|
|
240
244
|
/** `defineTables(vocabulary, columns)` answers the policies' builder, or names the fix for an untyped column list. */
|
|
241
|
-
export type VocabularyTables<V, C> = [ColumnTypes<C>] extends [never] ? Unavailable<'the column list carries no row types: import columns from the generated src/db/schema.generated.ts (fabrika db codegen)'> : DefineVocabularyPolicies<ColumnTypes<C>, V
|
|
245
|
+
export type VocabularyTables<V, C> = [ColumnTypes<C>] extends [never] ? Unavailable<'the column list carries no row types: import columns from the generated src/db/schema.generated.ts (fabrika db codegen)'> : DefineVocabularyPolicies<ColumnTypes<C>, V, ColumnViews<C>>;
|
|
242
246
|
/**
|
|
243
247
|
* The same policies read from values nobody type-checked: `src/db/tables.ts` and its vocabulary as the static reader
|
|
244
248
|
* has them from a commit. Every shape `defineTables` refuses at runtime is refused here, through the same parse.
|
package/types/src/tables.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ export { PolicyError } from './errors';
|
|
|
7
7
|
export type { FileReference } from './file-references';
|
|
8
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';
|
|
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
|
-
export type { ColumnTypes, DefineVocabularyPolicies, NormalizedPolicies, TableVocabulary, TypedColumnList, VocabularyAction, VocabularyChildScope, VocabularyContainer, VocabularyContainerOf, VocabularyDimension, VocabularyDimensionScope, VocabularyPolicyMap, VocabularyTablePolicy, } from './vocabulary-tables';
|
|
10
|
+
export type { ColumnTypes, ColumnViews, DefineVocabularyPolicies, NormalizedPolicies, TableVocabulary, TypedColumnList, VocabularyAction, VocabularyChildScope, VocabularyContainer, VocabularyContainerOf, VocabularyDimension, VocabularyDimensionScope, VocabularyPolicyMap, VocabularyTablePolicy, VocabularyViewPolicy, } from './vocabulary-tables';
|
|
11
11
|
/**
|
|
12
12
|
* The plan of seeding one row of each application table `rows` names (table → column → value), as an endpoint access
|
|
13
13
|
* case seeds its `appRows`: the order, the foreign keys that link one given row to another, and every refusal.
|
|
@@ -32,6 +32,11 @@ export interface WorldRow {
|
|
|
32
32
|
* Each occurs in no other fixture, so one found in an answer to another scope is a leaked row.
|
|
33
33
|
*/
|
|
34
34
|
readonly markers: readonly string[];
|
|
35
|
+
/**
|
|
36
|
+
* The values of the text columns of a row the table already held under a known scope value (`seedWorlds`' `scopes`),
|
|
37
|
+
* which no fixture wrote and other rows may share; empty for a row the fixture inserted.
|
|
38
|
+
*/
|
|
39
|
+
readonly stored: readonly string[];
|
|
35
40
|
}
|
|
36
41
|
/** One seeded row of an application table, which lies in no dimension. */
|
|
37
42
|
export interface ApplicationRow {
|
|
@@ -56,6 +61,8 @@ export interface SeededApplicationRows {
|
|
|
56
61
|
export interface SeededRow {
|
|
57
62
|
readonly key: string;
|
|
58
63
|
readonly markers: readonly string[];
|
|
64
|
+
/** Deletes the row, the rows its foreign keys made the fixture insert, and every row that refers to them. */
|
|
65
|
+
readonly remove: () => Promise<void>;
|
|
59
66
|
}
|
|
60
67
|
/**
|
|
61
68
|
* What the isolation fixture cannot seed for a schema: a CHECK its values fail, a required foreign key to a scoped table
|
|
@@ -75,11 +82,20 @@ export interface IsolationWorlds {
|
|
|
75
82
|
readonly own: WorldRow;
|
|
76
83
|
readonly other: WorldRow;
|
|
77
84
|
/**
|
|
78
|
-
* Deletes both rows of the table
|
|
79
|
-
*
|
|
85
|
+
* Deletes both rows of the table it inserted, every container row it inserted when it was given known scope values,
|
|
86
|
+
* and every row that refers to them, however deep; the other containers stay. A case whose rows hold pinned values or
|
|
87
|
+
* known scope values removes them, so the next case can seed the same values again under a unique index.
|
|
80
88
|
*/
|
|
81
89
|
readonly remove: () => Promise<void>;
|
|
82
90
|
}
|
|
91
|
+
/**
|
|
92
|
+
* Known values of dimensions, by dimension: the own world's and the other world's, which differ. A dimension whose values
|
|
93
|
+
* are a fixed set (an enum the input names it by) takes these instead of fresh ones.
|
|
94
|
+
*/
|
|
95
|
+
export type ScopeValues = Readonly<Record<string, {
|
|
96
|
+
readonly own: string;
|
|
97
|
+
readonly other: string;
|
|
98
|
+
}>>;
|
|
83
99
|
/** What a test kit seeds through, over its migrated schema. */
|
|
84
100
|
export interface TestSeeding {
|
|
85
101
|
/**
|
|
@@ -88,9 +104,12 @@ export interface TestSeeding {
|
|
|
88
104
|
* marker, so a seed of one, or of a column that places the row, throws. `pins` (column → value) give a text column of
|
|
89
105
|
* `table` a known value instead of its marker: the value in the own row, and `<value>-other` in the other row, which
|
|
90
106
|
* counts among the other row's markers, so a leak of it is still found. A pin of a column that places the row, or of
|
|
91
|
-
* no text column, throws.
|
|
107
|
+
* no text column, throws. `scopes` place each world in known values of a dimension instead of fresh ones: a row of
|
|
108
|
+
* the table that owns it with that key is used as it is stored (a migration wrote it), its containers' values with it,
|
|
109
|
+
* and is inserted otherwise. A stored row of `table` itself takes no `values` or `pins`: they throw. A seed that throws
|
|
110
|
+
* removes what it inserted.
|
|
92
111
|
*/
|
|
93
|
-
seedWorlds<DB, P>(tables: Tables<DB, P>, table: string, values?: Readonly<Record<string, SampleValue>>, pins?: Readonly<Record<string, string
|
|
112
|
+
seedWorlds<DB, P>(tables: Tables<DB, P>, table: string, values?: Readonly<Record<string, SampleValue>>, pins?: Readonly<Record<string, string>>, scopes?: ScopeValues): Promise<IsolationWorlds>;
|
|
94
113
|
/**
|
|
95
114
|
* Seeds one row of the application table `table`: its key, version and creator stamped, `values` (column → value)
|
|
96
115
|
* as given, a text column too, every other column sampled. A value for a stamped column or a file column throws.
|
|
@@ -105,11 +124,11 @@ export interface TestSeeding {
|
|
|
105
124
|
*/
|
|
106
125
|
seedApplicationRows<DB, P>(tables: Tables<DB, P>, rows: Readonly<Record<string, Readonly<Record<string, SampleValue>>>>): Promise<SeededApplicationRows>;
|
|
107
126
|
/**
|
|
108
|
-
* Seeds one row of `table`, a child or dimension table that lies in the object
|
|
109
|
-
* seeded
|
|
110
|
-
* the markers it holds.
|
|
127
|
+
* Seeds one row of `table`, a child or dimension table that lies in the object `under`'s chain starts at (a row
|
|
128
|
+
* `seedWorlds` seeded, or a value of its chain with the values outside it), placed in that chain: the row a fixture
|
|
129
|
+
* names by id, such as a note's comment. Answers its key and the markers it holds.
|
|
111
130
|
*/
|
|
112
|
-
seedUnder<DB, P>(tables: Tables<DB, P>, table: string, under: WorldRow): Promise<SeededRow>;
|
|
131
|
+
seedUnder<DB, P>(tables: Tables<DB, P>, table: string, under: Pick<WorldRow, 'chain'>): Promise<SeededRow>;
|
|
113
132
|
/**
|
|
114
133
|
* Values for the policy's insert columns of `table` that its schema accepts: every required one, sampled; a file
|
|
115
134
|
* column a seeded file. `beside` is a row `seedWorlds` seeded that the inserted row will lie beside, in the same
|
|
@@ -206,8 +225,8 @@ export interface SeedingBackend {
|
|
|
206
225
|
* (`checkLimit`, `foreignKeyLimit`, `uniqueValueLimit`).
|
|
207
226
|
*/
|
|
208
227
|
insert(table: string, values: ReadonlyMap<string, SampleValue>): Promise<Readonly<Record<string, unknown>>>;
|
|
209
|
-
/** One row of `table
|
|
210
|
-
firstRow(table: string, columns: readonly string[]): Promise<Readonly<Record<string, unknown>> | undefined>;
|
|
228
|
+
/** One row of `table` that holds `where` (column → value; every row when empty), of `columns`, or `undefined` when none does. */
|
|
229
|
+
firstRow(table: string, columns: readonly string[], where?: ReadonlyMap<string, SampleValue>): Promise<Readonly<Record<string, unknown>> | undefined>;
|
|
211
230
|
insertFile(file: SeededFile): Promise<void>;
|
|
212
231
|
/**
|
|
213
232
|
* Deletes `rows` (each found by its `match`; one already gone is skipped) and every row that refers to one of them,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { TableProbe } from './store';
|
|
2
2
|
import { type TestDatabase } from './testing-shared';
|
|
3
3
|
export { ChildTable, defineChildStore, defineStore, DimensionTable, type TableProbe } from './store';
|
|
4
|
-
export { type ApplicationRow, type IsolationWorlds, type SampleValue, SEEDED_OBJECT_VERSION, type SeededApplicationRows, type SeededRow, SeedLimitError, type TestDatabase, type WorldRow, } from './testing-shared';
|
|
4
|
+
export { type ApplicationRow, type IsolationWorlds, type SampleValue, type ScopeValues, SEEDED_OBJECT_VERSION, type SeededApplicationRows, type SeededRow, SeedLimitError, type TestDatabase, type WorldRow, } from './testing-shared';
|
|
5
5
|
export interface TestDatabaseOptions {
|
|
6
6
|
/** The application's `db/migrations/` directory. */
|
|
7
7
|
readonly migrations: string | URL;
|
package/types/src/testing.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { TableProbe } from './store';
|
|
2
2
|
import { type TestDatabase } from './testing-shared';
|
|
3
3
|
export { ChildTable, defineChildStore, defineStore, DimensionTable, type TableProbe } from './store';
|
|
4
|
-
export { type ApplicationRow, type IsolationWorlds, type SampleValue, SEEDED_OBJECT_VERSION, type SeededApplicationRows, type SeededRow, SeedLimitError, type TestDatabase, type WorldRow, } from './testing-shared';
|
|
4
|
+
export { type ApplicationRow, type IsolationWorlds, type SampleValue, type ScopeValues, SEEDED_OBJECT_VERSION, type SeededApplicationRows, type SeededRow, SeedLimitError, type TestDatabase, type WorldRow, } from './testing-shared';
|
|
5
5
|
export declare const TEST_POSTGRES_URL = "FABRIKA_TEST_POSTGRES_URL";
|
|
6
6
|
/** Set to `1` by the platform build: a missing test database is then a failure, not a skip. */
|
|
7
7
|
export declare const TEST_POSTGRES_REQUIRED = "FABRIKA_TEST_POSTGRES_REQUIRED";
|
|
@@ -1,16 +1,27 @@
|
|
|
1
|
-
import type { ColumnList, ColumnPolicies, FilePolicies, RecordActions, ReferencePolicy, TableActions, Tables, VersionColumn } from './policy';
|
|
1
|
+
import type { ColumnList, ColumnPolicies, FilePolicies, RecordActions, ReferencePolicy, TableAction, TableActions, Tables, VersionColumn } from './policy';
|
|
2
2
|
declare const databaseType: unique symbol;
|
|
3
|
+
declare const viewNames: unique symbol;
|
|
3
4
|
/**
|
|
4
|
-
* The generated column list (`src/db/schema.generated.ts`), which also carries the schema's row types
|
|
5
|
-
* `defineTables(vocabulary, columns)` takes no type argument. The
|
|
5
|
+
* The generated column list (`src/db/schema.generated.ts`), which also carries the schema's row types and the names of
|
|
6
|
+
* its views, so that `defineTables(vocabulary, columns)` takes no type argument. The brands are type-only: no value
|
|
7
|
+
* has either property.
|
|
6
8
|
*/
|
|
7
|
-
export type TypedColumnList<DB> = ColumnList<DB> & {
|
|
9
|
+
export type TypedColumnList<DB, Views extends keyof DB & string = never> = ColumnList<DB> & {
|
|
8
10
|
readonly [databaseType]?: DB;
|
|
11
|
+
readonly [viewNames]?: {
|
|
12
|
+
readonly views: Views;
|
|
13
|
+
};
|
|
9
14
|
};
|
|
10
15
|
/** The row types a typed column list carries; `never` for a list without them. */
|
|
11
16
|
export type ColumnTypes<C> = typeof databaseType extends keyof C ? C extends {
|
|
12
17
|
readonly [databaseType]?: infer DB;
|
|
13
18
|
} ? Exclude<DB, undefined> : never : never;
|
|
19
|
+
/** The views a typed column list names; `never` for a list without views. */
|
|
20
|
+
export type ColumnViews<C> = typeof viewNames extends keyof C ? C extends {
|
|
21
|
+
readonly [viewNames]?: {
|
|
22
|
+
readonly views: infer Views extends string;
|
|
23
|
+
};
|
|
24
|
+
} ? Views : never : never;
|
|
14
25
|
/**
|
|
15
26
|
* What `defineTables(vocabulary, columns)` reads of `@fabrika/app/contract`'s `defineVocabulary` result: the action
|
|
16
27
|
* and dimension names, and each dimension's container. `@fabrika/db` does not import `@fabrika/app`.
|
|
@@ -134,6 +145,52 @@ type AuthoredDimensionPolicy<Col extends string, A extends string, V, Ver extend
|
|
|
134
145
|
type AuthoredChildPolicy<Table extends string, Col extends string, A extends string, Ver extends Col, S, P> = WritableFields<Col, A, Ver, RecordActions<A>> & {
|
|
135
146
|
readonly scope: ChildScopes<Table, Col, S, P>;
|
|
136
147
|
};
|
|
148
|
+
/** What a view's policy holds besides its scope: read actions only, no write list and no stamped column. */
|
|
149
|
+
type ViewIsReadOnly = 'a view is never written: its actions are read, appRead and appCount';
|
|
150
|
+
/** The write actions a view's policy may not name, each typed as the message: a literal is not checked for excess keys. */
|
|
151
|
+
interface NoWriteActions {
|
|
152
|
+
readonly insert?: ViewIsReadOnly;
|
|
153
|
+
readonly update?: ViewIsReadOnly;
|
|
154
|
+
readonly delete?: ViewIsReadOnly;
|
|
155
|
+
}
|
|
156
|
+
interface ReadOnlyFields<Col extends string, A extends string> {
|
|
157
|
+
readonly actions: {
|
|
158
|
+
readonly read: TableAction<A>;
|
|
159
|
+
readonly appRead?: readonly A[];
|
|
160
|
+
readonly appCount?: readonly A[];
|
|
161
|
+
} & NoWriteActions;
|
|
162
|
+
readonly insert?: never;
|
|
163
|
+
readonly update?: never;
|
|
164
|
+
readonly version?: never;
|
|
165
|
+
readonly createdBy?: never;
|
|
166
|
+
readonly queue?: never;
|
|
167
|
+
readonly files?: never;
|
|
168
|
+
readonly columns?: {
|
|
169
|
+
readonly [C in Col]?: {
|
|
170
|
+
readonly read: A;
|
|
171
|
+
readonly write?: never;
|
|
172
|
+
};
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
/** A view read as a child table: its parent's `Ref` and `Within` read it; nothing writes it (ADR-0072). */
|
|
176
|
+
type AuthoredChildViewPolicy<Table extends string, Col extends string, A extends string, S, P> = ReadOnlyFields<Col, A> & {
|
|
177
|
+
readonly scope: ChildScopes<Table, Col, S, P>;
|
|
178
|
+
};
|
|
179
|
+
/** A view's policy as written: a read-only child, or a reference table every site reads. */
|
|
180
|
+
export type VocabularyViewPolicy<Table extends string, Col extends string, A extends string, S = unknown, P = unknown> = S extends {
|
|
181
|
+
readonly child: unknown;
|
|
182
|
+
} ? AuthoredChildViewPolicy<Table, Col, A, S, P> : S extends {
|
|
183
|
+
readonly reference: unknown;
|
|
184
|
+
} ? ReferenceViewPolicy<Col, A> : AuthoredChildViewPolicy<Table, Col, A, S, P> | ReferenceViewPolicy<Col, A>;
|
|
185
|
+
type ReferenceViewPolicy<Col extends string, A extends string> = ReferencePolicy<Col, A> & {
|
|
186
|
+
readonly actions: NoWriteActions;
|
|
187
|
+
readonly columns?: {
|
|
188
|
+
readonly [C in Col]?: {
|
|
189
|
+
readonly read: A;
|
|
190
|
+
readonly write?: never;
|
|
191
|
+
};
|
|
192
|
+
};
|
|
193
|
+
};
|
|
137
194
|
/** An application table's scope: why its rows belong to no dimension; `key` defaults to `id`. */
|
|
138
195
|
type AuthoredApplicationPolicy<Col extends string, A extends string, Ver extends Col> = WritableFields<Col, A, Ver> & {
|
|
139
196
|
readonly scope: {
|
|
@@ -158,13 +215,18 @@ type StatedScope<P, T> = T extends keyof P ? P[T] extends {
|
|
|
158
215
|
} ? S : unknown : unknown;
|
|
159
216
|
/** The policy of table `T` with nothing stated: every form, every dimension's scope, any parent. */
|
|
160
217
|
type AnyTablePolicy<DB, V, T extends keyof DB & string> = VocabularyTablePolicy<keyof DB & string, keyof DB[T] & string, VocabularyAction<V>, V, VersionColumn<DB[T]>>;
|
|
218
|
+
/** The policy of view `T` with nothing stated: both forms, any parent. */
|
|
219
|
+
type AnyViewPolicy<DB, V, T extends keyof DB & string> = VocabularyViewPolicy<keyof DB & string, keyof DB[T] & string, VocabularyAction<V>>;
|
|
161
220
|
/**
|
|
162
|
-
* Every table of the schema, and no other, maps to a policy; a
|
|
163
|
-
* `P` is the policies as written, which picks each table's form by the scope it
|
|
164
|
-
* map's own annotation states nothing narrower, so it is checked against every
|
|
221
|
+
* Every table of the schema, and no other, maps to a policy; a view `Views` names may (ADR-0072). A misspelt action or
|
|
222
|
+
* dimension fails at its own line. `P` is the policies as written, which picks each table's form by the scope it
|
|
223
|
+
* states; a policy written under this map's own annotation states nothing narrower, so it is checked against every
|
|
224
|
+
* form.
|
|
165
225
|
*/
|
|
166
|
-
export type VocabularyPolicyMap<DB, V, P = unknown> = {
|
|
167
|
-
readonly [T in keyof DB & string]: AnyTablePolicy<DB, V, T> extends (T extends keyof P ? P[T] : unknown) ? AnyTablePolicy<DB, V, T> : VocabularyTablePolicy<keyof DB & string, keyof DB[T] & string, VocabularyAction<V>, V, VersionColumn<DB[T]>, StatedScope<P, T>, P>;
|
|
226
|
+
export type VocabularyPolicyMap<DB, V, P = unknown, Views extends string = never> = {
|
|
227
|
+
readonly [T in Exclude<keyof DB & string, Views>]: AnyTablePolicy<DB, V, T> extends (T extends keyof P ? P[T] : unknown) ? AnyTablePolicy<DB, V, T> : VocabularyTablePolicy<keyof DB & string, keyof DB[T] & string, VocabularyAction<V>, V, VersionColumn<DB[T]>, StatedScope<P, T>, P>;
|
|
228
|
+
} & {
|
|
229
|
+
readonly [T in Views & keyof DB & string]?: AnyViewPolicy<DB, V, T> extends (T extends keyof P ? P[T] : unknown) ? AnyViewPolicy<DB, V, T> : VocabularyViewPolicy<keyof DB & string, keyof DB[T] & string, VocabularyAction<V>, StatedScope<P, T>, P>;
|
|
168
230
|
};
|
|
169
231
|
type Flat<T> = {
|
|
170
232
|
[K in keyof T]: T[K];
|
|
@@ -207,19 +269,23 @@ type NormalScope<S, V, P> = S extends {
|
|
|
207
269
|
readonly app: R;
|
|
208
270
|
readonly key: KeyOf<S>;
|
|
209
271
|
} : S;
|
|
272
|
+
/** A view's normalized policy says so, which the handle types read: no site writes it. */
|
|
273
|
+
type ViewMark<T, Views> = T extends Views ? {
|
|
274
|
+
readonly view: true;
|
|
275
|
+
} : unknown;
|
|
210
276
|
/**
|
|
211
277
|
* The policies as the layer models them: every dimension table's key and container, and every child's key and
|
|
212
|
-
* column, stated as the vocabulary and the conventions derive them.
|
|
278
|
+
* column, stated as the vocabulary and the conventions derive them; each view marked.
|
|
213
279
|
*/
|
|
214
|
-
export type NormalizedPolicies<P, V> = {
|
|
280
|
+
export type NormalizedPolicies<P, V, Views = never> = {
|
|
215
281
|
readonly [T in keyof P]: P[T] extends {
|
|
216
282
|
readonly scope: infer S;
|
|
217
|
-
} ? Flat<Omit<P[T], 'scope'> & {
|
|
283
|
+
} ? Flat<Omit<P[T], 'scope' | 'view'> & {
|
|
218
284
|
readonly scope: NormalScope<S, V, P>;
|
|
219
|
-
}
|
|
285
|
+
} & ViewMark<T, Views>> : P[T];
|
|
220
286
|
};
|
|
221
|
-
/** Takes the policies of exactly the tables of the schema, written in the vocabulary form. */
|
|
222
|
-
export type DefineVocabularyPolicies<DB, V> = <const P extends VocabularyPolicyMap<DB, V, P>>(policies: P & {
|
|
287
|
+
/** Takes the policies of exactly the tables of the schema, and of any of its views, written in the vocabulary form. */
|
|
288
|
+
export type DefineVocabularyPolicies<DB, V, Views extends string = never> = <const P extends VocabularyPolicyMap<DB, V, P, Views>>(policies: P & {
|
|
223
289
|
readonly [K in Exclude<keyof P, keyof DB>]: never;
|
|
224
|
-
}) => Tables<DB, NormalizedPolicies<P, V>>;
|
|
290
|
+
}) => Tables<DB, NormalizedPolicies<P, V, Views>>;
|
|
225
291
|
export {};
|