@fabrika/db 0.9.0-dev.2 → 0.9.0-dev.20
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
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
import type { Database } from './database';
|
|
2
|
+
import type { Primitive } from './description';
|
|
3
|
+
import { type Tables } from './policy';
|
|
4
|
+
import type { TableProbe } from './store';
|
|
5
|
+
/** A value a test row holds in a column; `null` for a nullable column the fixture left empty. */
|
|
6
|
+
export type SampleValue = Primitive | null;
|
|
7
|
+
/**
|
|
8
|
+
* A seeded value as a backend binds it: a JSON column's value as the JSON text the layer would write for it, so a
|
|
9
|
+
* seeded row and the kits' cases that write the same value through the layer hold the same JSON.
|
|
10
|
+
*/
|
|
11
|
+
export declare function seedBinding(columns: readonly SeedColumn[], table: string, name: string, value: SampleValue): SampleValue;
|
|
12
|
+
/** One seeded row and where it lies. */
|
|
13
|
+
export interface WorldRow {
|
|
14
|
+
/** The row's key: the dimension value of a dimension table's row, a child's own key. */
|
|
15
|
+
readonly key: string;
|
|
16
|
+
/** A child row's parent key; `null` for a dimension table. */
|
|
17
|
+
readonly parent: string | null;
|
|
18
|
+
/** The value of the grant dimension the row lies in (`IsolationWorlds.dimension`). */
|
|
19
|
+
readonly scope: string;
|
|
20
|
+
/**
|
|
21
|
+
* Every dimension value the row lies in: a dimension table's own first, then each container, innermost first. A
|
|
22
|
+
* share link's chain is this list.
|
|
23
|
+
*/
|
|
24
|
+
readonly chain: readonly {
|
|
25
|
+
readonly type: string;
|
|
26
|
+
readonly value: string;
|
|
27
|
+
}[];
|
|
28
|
+
/** The row's values of the policy's update columns: as seeded, or as stored where the database filled them. */
|
|
29
|
+
readonly updates: Readonly<Record<string, SampleValue>>;
|
|
30
|
+
/**
|
|
31
|
+
* The distinct value written into every text column the fixture filled itself, in this row and in its containers.
|
|
32
|
+
* Each occurs in no other fixture, so one found in an answer to another scope is a leaked row.
|
|
33
|
+
*/
|
|
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[];
|
|
40
|
+
}
|
|
41
|
+
/** One seeded row of an application table, which lies in no dimension. */
|
|
42
|
+
export interface ApplicationRow {
|
|
43
|
+
readonly key: string;
|
|
44
|
+
/** The row's values of the policy's update columns: as seeded, or as stored where the database filled them. */
|
|
45
|
+
readonly updates: Readonly<Record<string, SampleValue>>;
|
|
46
|
+
/** The distinct value written into every text column the fixture filled itself. */
|
|
47
|
+
readonly markers: readonly string[];
|
|
48
|
+
}
|
|
49
|
+
/** Rows of application tables seeded together for one case, and their removal once the case is over. */
|
|
50
|
+
export interface SeededApplicationRows {
|
|
51
|
+
/** Each table's row key, by table. */
|
|
52
|
+
readonly keys: Readonly<Record<string, string>>;
|
|
53
|
+
/**
|
|
54
|
+
* Deletes the rows, the rows their required foreign keys and file columns made the fixture insert, and every row
|
|
55
|
+
* that refers to one of them, however deep (what the case's handler wrote about them), in one transaction, so rows
|
|
56
|
+
* that refer to each other go together. A row already gone is skipped.
|
|
57
|
+
*/
|
|
58
|
+
remove(): Promise<void>;
|
|
59
|
+
}
|
|
60
|
+
/** A row seeded under another: its key, and the distinct value written into every text column the fixture filled. */
|
|
61
|
+
export interface SeededRow {
|
|
62
|
+
readonly key: string;
|
|
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>;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* What the isolation fixture cannot seed for a schema: a CHECK or a unique index no row it tries passes, a required
|
|
69
|
+
* foreign key whose referenced row it cannot seed. It is a limit of the generator, named with the constraint or the
|
|
70
|
+
* column, never a failure of the handler or the policy a case is about.
|
|
71
|
+
*/
|
|
72
|
+
export declare class SeedLimitError extends Error {
|
|
73
|
+
readonly name = "SeedLimitError";
|
|
74
|
+
}
|
|
75
|
+
/** The constraint a seeded row failed, as a backend reads it off the database's error. */
|
|
76
|
+
export interface Violation {
|
|
77
|
+
readonly kind: 'check' | 'unique' | 'foreign';
|
|
78
|
+
/** The constraint's name, or for an unnamed CHECK on SQLite its expression, as the database reports it. */
|
|
79
|
+
readonly name: string;
|
|
80
|
+
/** The columns the database names (SQLite's unique error); `undefined` where the seeding looks them up by `name`. */
|
|
81
|
+
readonly columns?: readonly string[];
|
|
82
|
+
}
|
|
83
|
+
/** A seed limit of a row the database refused, with the constraint it failed: the seeding tries another row. */
|
|
84
|
+
export declare class ConstraintLimitError extends SeedLimitError {
|
|
85
|
+
readonly violation: Violation;
|
|
86
|
+
constructor(message: string, violation: Violation);
|
|
87
|
+
}
|
|
88
|
+
/** One row of a table in each of two containment chains that share no value. */
|
|
89
|
+
export interface IsolationWorlds {
|
|
90
|
+
/**
|
|
91
|
+
* The dimension a caller's grants are scoped by: a dimension table's container, or its own dimension when it has
|
|
92
|
+
* none; a child table's parent's.
|
|
93
|
+
*/
|
|
94
|
+
readonly dimension: string;
|
|
95
|
+
readonly own: WorldRow;
|
|
96
|
+
readonly other: WorldRow;
|
|
97
|
+
/**
|
|
98
|
+
* Deletes both rows of the table it inserted, every container row it inserted when it was given known scope values,
|
|
99
|
+
* and every row that refers to them, however deep; the other containers stay. A case whose rows hold pinned values or
|
|
100
|
+
* known scope values removes them, so the next case can seed the same values again under a unique index.
|
|
101
|
+
*/
|
|
102
|
+
readonly remove: () => Promise<void>;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Known values of dimensions, by dimension: the own world's and the other world's, which differ. A dimension whose values
|
|
106
|
+
* are a fixed set (an enum the input names it by) takes these instead of fresh ones.
|
|
107
|
+
*/
|
|
108
|
+
export type ScopeValues = Readonly<Record<string, {
|
|
109
|
+
readonly own: string;
|
|
110
|
+
readonly other: string;
|
|
111
|
+
}>>;
|
|
112
|
+
/** What a test kit seeds through, over its migrated schema. */
|
|
113
|
+
export interface TestSeeding {
|
|
114
|
+
/**
|
|
115
|
+
* Seeds one row of `table` in each of two fresh chains, with every container row a table owns. `values` (column →
|
|
116
|
+
* value) are written into both rows of `table`: the state an endpoint's precondition needs. A text column keeps its
|
|
117
|
+
* marker, so a seed of one, or of a column that places the row, throws. `pins` (column → value) give a text column of
|
|
118
|
+
* `table` a known value instead of its marker: the value in the own row, and `<value>-other` in the other row, which
|
|
119
|
+
* 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
|
|
120
|
+
* no text column, throws. `scopes` place each world in known values of a dimension instead of fresh ones: a row of
|
|
121
|
+
* the table that owns it with that key is used as it is stored (a migration wrote it), its containers' values with it,
|
|
122
|
+
* and is inserted otherwise. A stored row of `table` itself takes no `values` or `pins`: they throw. A seed that throws
|
|
123
|
+
* removes what it inserted.
|
|
124
|
+
*/
|
|
125
|
+
seedWorlds<DB, P>(tables: Tables<DB, P>, table: string, values?: Readonly<Record<string, SampleValue>>, pins?: Readonly<Record<string, string>>, scopes?: ScopeValues): Promise<IsolationWorlds>;
|
|
126
|
+
/**
|
|
127
|
+
* Seeds one row of the application table `table`: its key, version and creator stamped, `values` (column → value)
|
|
128
|
+
* as given, a text column too, every other column sampled. A value for a stamped column or a file column throws.
|
|
129
|
+
*/
|
|
130
|
+
seedRow<DB, P>(tables: Tables<DB, P>, table: string, values?: Readonly<Record<string, SampleValue>>): Promise<ApplicationRow>;
|
|
131
|
+
/**
|
|
132
|
+
* Seeds one row of each application table `rows` names (table → column → value), as `seedRow` does, in foreign-key
|
|
133
|
+
* order: a foreign key from one of them to another holds that row's key unless the values give it. Every table's
|
|
134
|
+
* values are checked before the first insert, and a table that fails removes the rows seeded before it. `remove`
|
|
135
|
+
* deletes them, the rows a required foreign key of theirs made it insert, and every row that refers to any of them,
|
|
136
|
+
* so the next case starts without them.
|
|
137
|
+
*/
|
|
138
|
+
seedApplicationRows<DB, P>(tables: Tables<DB, P>, rows: Readonly<Record<string, Readonly<Record<string, SampleValue>>>>): Promise<SeededApplicationRows>;
|
|
139
|
+
/**
|
|
140
|
+
* Seeds one row of `table`, a child or dimension table that lies in the object `under`'s chain starts at (a row
|
|
141
|
+
* `seedWorlds` seeded, or a value of its chain with the values outside it), placed in that chain: the row a fixture
|
|
142
|
+
* names by id, such as a note's comment. Answers its key and the markers it holds.
|
|
143
|
+
*/
|
|
144
|
+
seedUnder<DB, P>(tables: Tables<DB, P>, table: string, under: Pick<WorldRow, 'chain'>): Promise<SeededRow>;
|
|
145
|
+
/**
|
|
146
|
+
* Values for the policy's insert columns of `table` that its schema accepts: every required one, sampled; a file
|
|
147
|
+
* column a seeded file. `beside` is a row `seedWorlds` seeded that the inserted row will lie beside, in the same
|
|
148
|
+
* containers. A number column a unique index covers takes the next number above the largest the table holds now in the
|
|
149
|
+
* row's group (the index's other columns as the row will have them, its containers' from `beside`), so insert the
|
|
150
|
+
* values before taking the next sample, or the two share it.
|
|
151
|
+
*/
|
|
152
|
+
sampleInsert<DB, P>(tables: Tables<DB, P>, table: string, beside?: WorldRow): Promise<Readonly<Record<string, SampleValue>>>;
|
|
153
|
+
/**
|
|
154
|
+
* The object key of every file the fixture seeded into a file column, oldest first. Each stands for an empty object,
|
|
155
|
+
* which a test's object store holds when a handler serves or opens the file.
|
|
156
|
+
*/
|
|
157
|
+
seededObjects(): readonly string[];
|
|
158
|
+
}
|
|
159
|
+
export interface TestDatabase extends TestSeeding {
|
|
160
|
+
/** The connection `boot` binds the data layer to: on Postgres the runtime role on the migrated schema; on SQLite the database itself. */
|
|
161
|
+
readonly database: Database;
|
|
162
|
+
/** The migrated schema: `app_test_<hex>` on Postgres, `main` on SQLite. */
|
|
163
|
+
readonly schema: string;
|
|
164
|
+
/** Closes the connections and drops the schema and its roles (on SQLite: closes the in-memory database). */
|
|
165
|
+
close(): Promise<void>;
|
|
166
|
+
}
|
|
167
|
+
/** A column of the migrated schema, as the seeding reads it. */
|
|
168
|
+
export interface SeedColumn {
|
|
169
|
+
readonly table: string;
|
|
170
|
+
readonly name: string;
|
|
171
|
+
/** The type as the database names it, for a message. */
|
|
172
|
+
readonly type: string;
|
|
173
|
+
readonly nullable: boolean;
|
|
174
|
+
/** The database fills it when an insert leaves it out: a default, an identity, a generated column. */
|
|
175
|
+
readonly defaulted: boolean;
|
|
176
|
+
/** A text type that is no enum: where nothing else fills it, the fixture writes a marker into it. */
|
|
177
|
+
readonly text: boolean;
|
|
178
|
+
/** A uuid type: a key or containment column of it holds a random uuid as its scope value. */
|
|
179
|
+
readonly uuid: boolean;
|
|
180
|
+
/** A number type: where a unique index covers it, the fixture writes the next number of the row's group instead of its sample. */
|
|
181
|
+
readonly number: boolean;
|
|
182
|
+
/** A JSON column (`jsonb`, `JSONB`): its value is a JSON value, bound as the layer binds it (`seedBinding`). */
|
|
183
|
+
readonly json: boolean;
|
|
184
|
+
/**
|
|
185
|
+
* A sample of the column's type for a required column the test does not care about; a `SeedLimitError` for a type it
|
|
186
|
+
* cannot fill. `variant` 0 is the plain sample; a higher one differs from the lower ones where the type allows (another
|
|
187
|
+
* day, the other boolean, a larger number), for a unique index whose other columns the row cannot vary.
|
|
188
|
+
*/
|
|
189
|
+
readonly sample: (variant: number) => Primitive;
|
|
190
|
+
}
|
|
191
|
+
/** A CHECK constraint of the schema, by the columns it names. */
|
|
192
|
+
export interface CheckConstraint {
|
|
193
|
+
readonly name: string;
|
|
194
|
+
readonly table: string;
|
|
195
|
+
readonly columns: readonly string[];
|
|
196
|
+
/** The values of a one-column `IN (…)` list the fixture writes in its place, first one first; empty for any other CHECK. */
|
|
197
|
+
readonly values: readonly CheckLiteral[];
|
|
198
|
+
/** Every text and number literal of the expression: what the fixture tries in the columns it names when a row fails it. */
|
|
199
|
+
readonly literals: readonly CheckLiteral[];
|
|
200
|
+
}
|
|
201
|
+
/** A literal of a CHECK expression. */
|
|
202
|
+
export type CheckLiteral = string | number;
|
|
203
|
+
/**
|
|
204
|
+
* A unique index of the schema (a primary key's or a UNIQUE constraint's included), by its key columns. Only an index of
|
|
205
|
+
* plain columns with no predicate is one: the fixture cannot tell a partial index's rows or an expression's values.
|
|
206
|
+
*/
|
|
207
|
+
export interface UniqueIndex {
|
|
208
|
+
readonly name: string;
|
|
209
|
+
readonly table: string;
|
|
210
|
+
readonly columns: readonly string[];
|
|
211
|
+
/** `NULLS NOT DISTINCT`: a null in a key column collides with another row's null there. */
|
|
212
|
+
readonly nullsNotDistinct: boolean;
|
|
213
|
+
}
|
|
214
|
+
/** A live, empty file of `kind`, as the framework's put leaves one once a reference claims it. */
|
|
215
|
+
export interface SeededFile {
|
|
216
|
+
readonly id: string;
|
|
217
|
+
readonly kind: string;
|
|
218
|
+
readonly objectKey: string;
|
|
219
|
+
readonly sha256: string;
|
|
220
|
+
readonly objectVersion: string;
|
|
221
|
+
}
|
|
222
|
+
/** A row the fixture inserted, found again by `match` (column → value): what a removal deletes. */
|
|
223
|
+
export interface InsertedRow {
|
|
224
|
+
readonly table: string;
|
|
225
|
+
readonly match: ReadonlyMap<string, SampleValue>;
|
|
226
|
+
}
|
|
227
|
+
/** How many rows one removal deletes, referring rows included, before it gives up. */
|
|
228
|
+
export declare const MAX_REMOVED_ROWS = 10000;
|
|
229
|
+
/** The seed limit of a removal that reaches more than `MAX_REMOVED_ROWS` rows. */
|
|
230
|
+
export declare function removalLimit(): SeedLimitError;
|
|
231
|
+
/** What a kit gives the seeding: its schema's columns and CHECK constraints, and the statements it runs. */
|
|
232
|
+
export interface SeedingBackend {
|
|
233
|
+
readonly columns: readonly SeedColumn[];
|
|
234
|
+
/** Every CHECK constraint of the schema's tables; read once, on the first column that needs them. */
|
|
235
|
+
checks(): Promise<readonly CheckConstraint[]>;
|
|
236
|
+
/** Every unique index of plain columns and no predicate of the schema's tables; read once, on the first column that needs them. */
|
|
237
|
+
uniqueIndexes(): Promise<readonly UniqueIndex[]>;
|
|
238
|
+
/**
|
|
239
|
+
* The smallest whole number above every value `column` holds in the rows of `table` that match one of `groups`
|
|
240
|
+
* (`groupsWhere`), and at least 1, as the column's type holds it; `undefined` when the type holds no such number, or
|
|
241
|
+
* the largest is no finite number.
|
|
242
|
+
*/
|
|
243
|
+
nextNumber(table: string, column: string, groups: readonly ReadonlyMap<string, SampleValue>[]): Promise<Primitive | undefined>;
|
|
244
|
+
/**
|
|
245
|
+
* Inserts one row and answers it as stored; a CHECK, foreign key or unique index it fails is a `SeedLimitError`
|
|
246
|
+
* (`checkLimit`, `foreignKeyLimit`, `uniqueValueLimit`).
|
|
247
|
+
*/
|
|
248
|
+
insert(table: string, values: ReadonlyMap<string, SampleValue>): Promise<Readonly<Record<string, unknown>>>;
|
|
249
|
+
/** One row of `table` that holds `where` (column → value; every row when empty), of `columns`, or `undefined` when none does. */
|
|
250
|
+
firstRow(table: string, columns: readonly string[], where?: ReadonlyMap<string, SampleValue>): Promise<Readonly<Record<string, unknown>> | undefined>;
|
|
251
|
+
insertFile(file: SeededFile): Promise<void>;
|
|
252
|
+
/**
|
|
253
|
+
* Deletes `rows` (each found by its `match`; one already gone is skipped) and every row that refers to one of them,
|
|
254
|
+
* however deep, in one transaction whose foreign keys are checked once all are gone, so rows that refer to each other
|
|
255
|
+
* go together. No write moves or adds a referring row between finding the rows and deleting them.
|
|
256
|
+
*/
|
|
257
|
+
remove(rows: readonly InsertedRow[]): Promise<void>;
|
|
258
|
+
}
|
|
259
|
+
/** The seed limit of a CHECK a seeded row fails. */
|
|
260
|
+
export declare function checkLimit(constraint: string, table: string): ConstraintLimitError;
|
|
261
|
+
/** The seed limit of a foreign key a seeded row breaks. */
|
|
262
|
+
export declare function foreignKeyLimit(constraint: string, table: string): ConstraintLimitError;
|
|
263
|
+
/** The seed limit of a number column a unique index covers whose type holds no number above the largest of the row's group. */
|
|
264
|
+
export declare function uniqueLimit(index: string, table: string, column: string): SeedLimitError;
|
|
265
|
+
/** The seed limit of a row whose values another row of the table already holds under a unique index. */
|
|
266
|
+
export declare function uniqueValueLimit(index: string, table: string, columns?: readonly string[]): ConstraintLimitError;
|
|
267
|
+
/** The seed limit of a required column whose type the fixture has no sample of. */
|
|
268
|
+
export declare function typeLimit(table: string, column: string, type: string): SeedLimitError;
|
|
269
|
+
/**
|
|
270
|
+
* A condition that matches a row holding every value of one of `groups` (column → value, `null` matched by `IS NULL`; an
|
|
271
|
+
* empty group matches every row, no group none), in the dialect's quoting and placeholders, with the values it binds.
|
|
272
|
+
*/
|
|
273
|
+
export declare function groupsWhere(groups: readonly ReadonlyMap<string, SampleValue>[], quote: (identifier: string) => string, placeholder: (position: number) => string): {
|
|
274
|
+
where: string;
|
|
275
|
+
values: SampleValue[];
|
|
276
|
+
};
|
|
277
|
+
export declare function randomHex(): string;
|
|
278
|
+
/**
|
|
279
|
+
* The object version a seeded file was scanned at: whoever stocks its empty object (`seededObjects`) stores it under
|
|
280
|
+
* this version, so a file of a scanning kind is served as a live one is, by its scanned version.
|
|
281
|
+
*/
|
|
282
|
+
export declare const SEEDED_OBJECT_VERSION = "\"seeded\"";
|
|
283
|
+
/** The isolation fixtures over the schema `backend` describes. */
|
|
284
|
+
export declare function testSeeding(backend: SeedingBackend): TestSeeding;
|
|
285
|
+
/**
|
|
286
|
+
* The untyped handle over one table of the data layer `scoped(tables)` created; it reaches the database once `boot`
|
|
287
|
+
* has bound that layer. Every runtime check of a store applies: kind, dimension, action and the request's authority.
|
|
288
|
+
* `module` is the kit's, which refuses a production process.
|
|
289
|
+
*/
|
|
290
|
+
export declare function probeTable(module: string, layer: unknown, table: string): TableProbe;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { TableProbe } from './store';
|
|
2
|
+
import { type TestDatabase } from './testing-shared';
|
|
3
|
+
export { ChildTable, defineChildStore, defineStore, DimensionTable, type TableProbe } from './store';
|
|
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
|
+
export interface TestDatabaseOptions {
|
|
6
|
+
/** The application's `db/migrations/` directory. */
|
|
7
|
+
readonly migrations: string | URL;
|
|
8
|
+
/** The test file. `@fabrika/db/testing` names it when it skips; this kit never skips. */
|
|
9
|
+
readonly label: string;
|
|
10
|
+
/** A database migrated and seeded once (`testDatabaseTemplate`), which this one starts as a copy of. */
|
|
11
|
+
readonly template?: TestDatabaseTemplate;
|
|
12
|
+
}
|
|
13
|
+
/** A migrated database with its base seed, as an image every `testDatabase({ template })` copies. */
|
|
14
|
+
export interface TestDatabaseTemplate {
|
|
15
|
+
/** The migrations it was migrated with: a database of other migrations does not start from it. */
|
|
16
|
+
readonly migrations: string;
|
|
17
|
+
readonly image: Uint8Array;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Migrates a database once, runs `seed` in it, and keeps it as the image each `testDatabase({ template })` starts as a
|
|
21
|
+
* copy of: in milliseconds, without migrating or seeding again.
|
|
22
|
+
*/
|
|
23
|
+
export declare function testDatabaseTemplate(options: Omit<TestDatabaseOptions, 'template'> & {
|
|
24
|
+
readonly seed?: (database: TestDatabase) => Promise<void>;
|
|
25
|
+
}): Promise<TestDatabaseTemplate>;
|
|
26
|
+
/**
|
|
27
|
+
* A fresh in-memory database with the framework's and the application's migrations applied by the runner a Durable
|
|
28
|
+
* Object boots with (`@fabrika/db/sqlite-migrate`), opened with its engine (`@fabrika/db/sqlite`). Never `null`: it
|
|
29
|
+
* needs no backend, so it never skips; its type is still assignable to `@fabrika/db/testing`'s, so a test
|
|
30
|
+
* that checks for `null` compiles against both kits.
|
|
31
|
+
*/
|
|
32
|
+
export declare function testDatabase(options: TestDatabaseOptions): Promise<TestDatabase>;
|
|
33
|
+
/**
|
|
34
|
+
* The untyped handle over one table of the data layer `scoped(tables)` created; it reaches the database once `boot`
|
|
35
|
+
* has bound that layer. Every runtime check of a store applies: kind, dimension, action and the request's authority.
|
|
36
|
+
*/
|
|
37
|
+
export declare function tableProbe(layer: unknown, table: string): TableProbe;
|
package/types/src/testing.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { Database } from './database';
|
|
2
|
-
import type { Primitive } from './description';
|
|
3
|
-
import { type Tables } from './policy';
|
|
4
1
|
import type { TableProbe } from './store';
|
|
2
|
+
import { type TestDatabase } from './testing-shared';
|
|
5
3
|
export { ChildTable, defineChildStore, defineStore, DimensionTable, type TableProbe } from './store';
|
|
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';
|
|
6
5
|
export declare const TEST_POSTGRES_URL = "FABRIKA_TEST_POSTGRES_URL";
|
|
7
6
|
/** Set to `1` by the platform build: a missing test database is then a failure, not a skip. */
|
|
8
7
|
export declare const TEST_POSTGRES_REQUIRED = "FABRIKA_TEST_POSTGRES_REQUIRED";
|
|
@@ -17,96 +16,6 @@ export interface TestDatabaseOptions {
|
|
|
17
16
|
*/
|
|
18
17
|
readonly connectionWaitMs?: number;
|
|
19
18
|
}
|
|
20
|
-
/** A value a test row holds in a column; `null` for a nullable column the fixture left empty. */
|
|
21
|
-
export type SampleValue = Primitive | null;
|
|
22
|
-
/** One seeded row and where it lies. */
|
|
23
|
-
export interface WorldRow {
|
|
24
|
-
/** The row's key: the dimension value of a dimension table's row, a child's own key. */
|
|
25
|
-
readonly key: string;
|
|
26
|
-
/** A child row's parent key; `null` for a dimension table. */
|
|
27
|
-
readonly parent: string | null;
|
|
28
|
-
/** The value of the grant dimension the row lies in (`IsolationWorlds.dimension`). */
|
|
29
|
-
readonly scope: string;
|
|
30
|
-
/**
|
|
31
|
-
* Every dimension value the row lies in: a dimension table's own first, then each container, innermost first. A
|
|
32
|
-
* share link's chain is this list.
|
|
33
|
-
*/
|
|
34
|
-
readonly chain: readonly {
|
|
35
|
-
readonly type: string;
|
|
36
|
-
readonly value: string;
|
|
37
|
-
}[];
|
|
38
|
-
/** The row's values of the policy's update columns, as seeded. */
|
|
39
|
-
readonly updates: Readonly<Record<string, SampleValue>>;
|
|
40
|
-
/**
|
|
41
|
-
* The distinct value written into every text column the fixture filled itself, in this row and in its containers.
|
|
42
|
-
* Each occurs in no other fixture, so one found in an answer to another scope is a leaked row.
|
|
43
|
-
*/
|
|
44
|
-
readonly markers: readonly string[];
|
|
45
|
-
}
|
|
46
|
-
/** One seeded row of an application table, which lies in no dimension. */
|
|
47
|
-
export interface ApplicationRow {
|
|
48
|
-
readonly key: string;
|
|
49
|
-
/** The row's values of the policy's update columns, as seeded. */
|
|
50
|
-
readonly updates: Readonly<Record<string, SampleValue>>;
|
|
51
|
-
/** The distinct value written into every text column the fixture filled itself. */
|
|
52
|
-
readonly markers: readonly string[];
|
|
53
|
-
}
|
|
54
|
-
/** A row seeded under another: its key, and the distinct value written into every text column the fixture filled. */
|
|
55
|
-
export interface SeededRow {
|
|
56
|
-
readonly key: string;
|
|
57
|
-
readonly markers: readonly string[];
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* What the isolation fixture cannot seed for a schema: a CHECK its values fail, a required foreign key to a scoped table
|
|
61
|
-
* outside the containment chain. It is a limit of the generator, named with the constraint or the column, never a
|
|
62
|
-
* failure of the handler or the policy a case is about.
|
|
63
|
-
*/
|
|
64
|
-
export declare class SeedLimitError extends Error {
|
|
65
|
-
readonly name = "SeedLimitError";
|
|
66
|
-
}
|
|
67
|
-
/** One row of a table in each of two containment chains that share no value. */
|
|
68
|
-
export interface IsolationWorlds {
|
|
69
|
-
/**
|
|
70
|
-
* The dimension a caller's grants are scoped by: a dimension table's container, or its own dimension when it has
|
|
71
|
-
* none; a child table's parent's.
|
|
72
|
-
*/
|
|
73
|
-
readonly dimension: string;
|
|
74
|
-
readonly own: WorldRow;
|
|
75
|
-
readonly other: WorldRow;
|
|
76
|
-
}
|
|
77
|
-
export interface TestDatabase {
|
|
78
|
-
/** The connection `boot` binds the data layer to: the runtime role, on the migrated schema. */
|
|
79
|
-
readonly database: Database;
|
|
80
|
-
readonly schema: string;
|
|
81
|
-
/**
|
|
82
|
-
* Seeds one row of `table` in each of two fresh chains, with every container row a table owns. `values` (column →
|
|
83
|
-
* value) are written into both rows of `table`: the state an endpoint's precondition needs. A text column keeps its
|
|
84
|
-
* marker, so a seed of one, or of a column that places the row, throws.
|
|
85
|
-
*/
|
|
86
|
-
seedWorlds<DB, P>(tables: Tables<DB, P>, table: string, values?: Readonly<Record<string, SampleValue>>): Promise<IsolationWorlds>;
|
|
87
|
-
/** Seeds one row of the application table `table`: its key, version and creator stamped, every other column sampled. */
|
|
88
|
-
seedRow<DB, P>(tables: Tables<DB, P>, table: string): Promise<ApplicationRow>;
|
|
89
|
-
/**
|
|
90
|
-
* Seeds one row of `table`, a child or dimension table that lies in the object of `under` (a row `seedWorlds`
|
|
91
|
-
* seeded), placed in that row's chain: the row a fixture names by id, such as a note's comment. Answers its key and
|
|
92
|
-
* the markers it holds.
|
|
93
|
-
*/
|
|
94
|
-
seedUnder<DB, P>(tables: Tables<DB, P>, table: string, under: WorldRow): Promise<SeededRow>;
|
|
95
|
-
/** Values for the policy's insert columns of `table` that its schema accepts: every required one, sampled; a file column a seeded file. */
|
|
96
|
-
sampleInsert<DB, P>(tables: Tables<DB, P>, table: string): Promise<Readonly<Record<string, SampleValue>>>;
|
|
97
|
-
/**
|
|
98
|
-
* The object key of every file the fixture seeded into a file column, oldest first. Each stands for an empty object,
|
|
99
|
-
* which a test's object store holds when a handler serves or opens the file.
|
|
100
|
-
*/
|
|
101
|
-
seededObjects(): readonly string[];
|
|
102
|
-
/** Closes the connections and drops the schema and its roles. */
|
|
103
|
-
close(): Promise<void>;
|
|
104
|
-
}
|
|
105
|
-
/**
|
|
106
|
-
* The object version a seeded file was scanned at: whoever stocks its empty object (`seededObjects`) stores it under
|
|
107
|
-
* this version, so a file of a scanning kind is served as a live one is, by its scanned version.
|
|
108
|
-
*/
|
|
109
|
-
export declare const SEEDED_OBJECT_VERSION = "\"seeded\"";
|
|
110
19
|
/**
|
|
111
20
|
* A fresh schema with the framework's and the application's migrations applied by the real runner (`@fabrika/platform-node`) as its
|
|
112
21
|
* migration role, connected and seeded as its runtime role, or `null` after the standard skip message when
|
|
@@ -1,16 +1,28 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ConditionPolicy } from './conditions';
|
|
2
|
+
import type { AccessPolicy, ApplicationActions, ColumnList, ColumnPolicies, ConfersPolicy, FilePolicies, MemberRevealsPolicy, RecordActions, ReferencePolicy, RevealsPolicy, TableAction, TableActions, Tables, VersionColumn } from './policy';
|
|
2
3
|
declare const databaseType: unique symbol;
|
|
4
|
+
declare const viewNames: unique symbol;
|
|
3
5
|
/**
|
|
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
|
|
6
|
+
* The generated column list (`src/db/schema.generated.ts`), which also carries the schema's row types and the names of
|
|
7
|
+
* its views, so that `defineTables(vocabulary, columns)` takes no type argument. The brands are type-only: no value
|
|
8
|
+
* has either property.
|
|
6
9
|
*/
|
|
7
|
-
export type TypedColumnList<DB> = ColumnList<DB> & {
|
|
10
|
+
export type TypedColumnList<DB, Views extends keyof DB & string = never> = ColumnList<DB> & {
|
|
8
11
|
readonly [databaseType]?: DB;
|
|
12
|
+
readonly [viewNames]?: {
|
|
13
|
+
readonly views: Views;
|
|
14
|
+
};
|
|
9
15
|
};
|
|
10
16
|
/** The row types a typed column list carries; `never` for a list without them. */
|
|
11
17
|
export type ColumnTypes<C> = typeof databaseType extends keyof C ? C extends {
|
|
12
18
|
readonly [databaseType]?: infer DB;
|
|
13
19
|
} ? Exclude<DB, undefined> : never : never;
|
|
20
|
+
/** The views a typed column list names; `never` for a list without views. */
|
|
21
|
+
export type ColumnViews<C> = typeof viewNames extends keyof C ? C extends {
|
|
22
|
+
readonly [viewNames]?: {
|
|
23
|
+
readonly views: infer Views extends string;
|
|
24
|
+
};
|
|
25
|
+
} ? Views : never : never;
|
|
14
26
|
/**
|
|
15
27
|
* What `defineTables(vocabulary, columns)` reads of `@fabrika/app/contract`'s `defineVocabulary` result: the action
|
|
16
28
|
* and dimension names, and each dimension's container. `@fabrika/db` does not import `@fabrika/app`.
|
|
@@ -24,6 +36,11 @@ export interface TableVocabulary {
|
|
|
24
36
|
readonly in?: string;
|
|
25
37
|
};
|
|
26
38
|
};
|
|
39
|
+
readonly relations?: {
|
|
40
|
+
readonly [relation: string]: {
|
|
41
|
+
readonly dimension: string;
|
|
42
|
+
};
|
|
43
|
+
};
|
|
27
44
|
}
|
|
28
45
|
export type VocabularyAction<V> = V extends {
|
|
29
46
|
readonly actions: infer A;
|
|
@@ -81,6 +98,46 @@ interface WritableFields<Col extends string, A extends string, Ver extends Col,
|
|
|
81
98
|
readonly columns?: ColumnPolicies<Col, A>;
|
|
82
99
|
/** The file columns, each with the file kind it holds (`c.files`): a `uuid` foreign key to `fabrika_files`. */
|
|
83
100
|
readonly files?: FilePolicies<Col>;
|
|
101
|
+
/** Relation → how a row of this table confers it: whom it names, at which node, until when. */
|
|
102
|
+
readonly confers?: {
|
|
103
|
+
readonly [relation: string]: ConfersPolicy<Col>;
|
|
104
|
+
};
|
|
105
|
+
/** Action → the condition a row must meet for it, whoever holds it. */
|
|
106
|
+
readonly when?: {
|
|
107
|
+
readonly [action in A]?: ConditionPolicy<Col>;
|
|
108
|
+
};
|
|
109
|
+
/** Relation → action → the condition a row that relation reaches must meet for it; IAM grants are not narrowed. */
|
|
110
|
+
readonly relationWhen?: {
|
|
111
|
+
readonly [relation: string]: {
|
|
112
|
+
readonly [action in A]?: ConditionPolicy<Col>;
|
|
113
|
+
};
|
|
114
|
+
};
|
|
115
|
+
/** Relation of the table's own dimension → the ancestor node a row it reaches reveals, and for which actions. */
|
|
116
|
+
readonly reveals?: {
|
|
117
|
+
readonly [relation: string]: RevealsPolicy<Col, A>;
|
|
118
|
+
};
|
|
119
|
+
/**
|
|
120
|
+
* Dimension outside the chain → the foreign key column a `Within`'s read reaches a row through, no update writes it; or
|
|
121
|
+
* `<nearest container column>.<its column>`, the nearest container's own path.
|
|
122
|
+
*/
|
|
123
|
+
readonly readThrough?: {
|
|
124
|
+
readonly [dimension: string]: Col | `${Col}.${string}`;
|
|
125
|
+
};
|
|
126
|
+
/**
|
|
127
|
+
* Dimension of `readThrough` → the actions a `Ref` held through that path holds, writes included; whoever inserts the
|
|
128
|
+
* path's column must hold each at the node it names.
|
|
129
|
+
*/
|
|
130
|
+
readonly writeThrough?: {
|
|
131
|
+
readonly [dimension: string]: readonly A[];
|
|
132
|
+
};
|
|
133
|
+
/** The table's own dimension → the ancestor node a row reveals to a grant at its node, for a `Within`'s reads. */
|
|
134
|
+
readonly memberReveals?: {
|
|
135
|
+
readonly [dimension: string]: MemberRevealsPolicy<Col, A>;
|
|
136
|
+
};
|
|
137
|
+
/** Relation → the access view that lists the rows it reaches: the escape path, decided by the view's SQL. */
|
|
138
|
+
readonly access?: {
|
|
139
|
+
readonly [relation: string]: AccessPolicy;
|
|
140
|
+
};
|
|
84
141
|
}
|
|
85
142
|
/**
|
|
86
143
|
* The scope of the dimension the policy states (`S`, its scope as written); for any other name, the dimension alone,
|
|
@@ -134,8 +191,66 @@ type AuthoredDimensionPolicy<Col extends string, A extends string, V, Ver extend
|
|
|
134
191
|
type AuthoredChildPolicy<Table extends string, Col extends string, A extends string, Ver extends Col, S, P> = WritableFields<Col, A, Ver, RecordActions<A>> & {
|
|
135
192
|
readonly scope: ChildScopes<Table, Col, S, P>;
|
|
136
193
|
};
|
|
194
|
+
/** What a view's policy holds besides its scope: read actions only, no write list and no stamped column. */
|
|
195
|
+
type ViewIsReadOnly = 'a view is never written: its actions are read, appRead and appCount';
|
|
196
|
+
/** The write actions a view's policy may not name, each typed as the message: a literal is not checked for excess keys. */
|
|
197
|
+
interface NoWriteActions {
|
|
198
|
+
readonly insert?: ViewIsReadOnly;
|
|
199
|
+
readonly update?: ViewIsReadOnly;
|
|
200
|
+
readonly delete?: ViewIsReadOnly;
|
|
201
|
+
}
|
|
202
|
+
interface ReadOnlyFields<Col extends string, A extends string> {
|
|
203
|
+
readonly actions: {
|
|
204
|
+
readonly read: TableAction<A>;
|
|
205
|
+
readonly appRead?: readonly A[];
|
|
206
|
+
readonly appCount?: readonly A[];
|
|
207
|
+
} & NoWriteActions;
|
|
208
|
+
readonly insert?: never;
|
|
209
|
+
readonly update?: never;
|
|
210
|
+
readonly version?: never;
|
|
211
|
+
readonly createdBy?: never;
|
|
212
|
+
readonly queue?: never;
|
|
213
|
+
readonly files?: never;
|
|
214
|
+
readonly columns?: {
|
|
215
|
+
readonly [C in Col]?: {
|
|
216
|
+
readonly read: A;
|
|
217
|
+
readonly write?: never;
|
|
218
|
+
};
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* A view read as a child table: its parent's `Ref` and `Within` read it; nothing writes it (ADR-0072). Its `when` and
|
|
223
|
+
* `relationWhen` narrow its read actions; a path may start at its parent column, to the parent row.
|
|
224
|
+
*/
|
|
225
|
+
type AuthoredChildViewPolicy<Table extends string, Col extends string, A extends string, S, P> = ReadOnlyFields<Col, A> & {
|
|
226
|
+
readonly scope: ChildScopes<Table, Col, S, P>;
|
|
227
|
+
readonly when?: {
|
|
228
|
+
readonly [action in A]?: ConditionPolicy<Col>;
|
|
229
|
+
};
|
|
230
|
+
/** Relation → action → the condition a row the relation reaches through the parent row must meet for it. */
|
|
231
|
+
readonly relationWhen?: {
|
|
232
|
+
readonly [relation: string]: {
|
|
233
|
+
readonly [action in A]?: ConditionPolicy<Col>;
|
|
234
|
+
};
|
|
235
|
+
};
|
|
236
|
+
};
|
|
237
|
+
/** A view's policy as written: a read-only child, or a reference table every site reads. */
|
|
238
|
+
export type VocabularyViewPolicy<Table extends string, Col extends string, A extends string, S = unknown, P = unknown> = S extends {
|
|
239
|
+
readonly child: unknown;
|
|
240
|
+
} ? AuthoredChildViewPolicy<Table, Col, A, S, P> : S extends {
|
|
241
|
+
readonly reference: unknown;
|
|
242
|
+
} ? ReferenceViewPolicy<Col, A> : AuthoredChildViewPolicy<Table, Col, A, S, P> | ReferenceViewPolicy<Col, A>;
|
|
243
|
+
type ReferenceViewPolicy<Col extends string, A extends string> = ReferencePolicy<Col, A> & {
|
|
244
|
+
readonly actions: NoWriteActions;
|
|
245
|
+
readonly columns?: {
|
|
246
|
+
readonly [C in Col]?: {
|
|
247
|
+
readonly read: A;
|
|
248
|
+
readonly write?: never;
|
|
249
|
+
};
|
|
250
|
+
};
|
|
251
|
+
};
|
|
137
252
|
/** An application table's scope: why its rows belong to no dimension; `key` defaults to `id`. */
|
|
138
|
-
type AuthoredApplicationPolicy<Col extends string, A extends string, Ver extends Col> = WritableFields<Col, A, Ver
|
|
253
|
+
type AuthoredApplicationPolicy<Col extends string, A extends string, Ver extends Col> = WritableFields<Col, A, Ver, ApplicationActions<A>> & {
|
|
139
254
|
readonly scope: {
|
|
140
255
|
readonly app: string;
|
|
141
256
|
} & KeyField<Col>;
|
|
@@ -158,13 +273,18 @@ type StatedScope<P, T> = T extends keyof P ? P[T] extends {
|
|
|
158
273
|
} ? S : unknown : unknown;
|
|
159
274
|
/** The policy of table `T` with nothing stated: every form, every dimension's scope, any parent. */
|
|
160
275
|
type AnyTablePolicy<DB, V, T extends keyof DB & string> = VocabularyTablePolicy<keyof DB & string, keyof DB[T] & string, VocabularyAction<V>, V, VersionColumn<DB[T]>>;
|
|
276
|
+
/** The policy of view `T` with nothing stated: both forms, any parent. */
|
|
277
|
+
type AnyViewPolicy<DB, V, T extends keyof DB & string> = VocabularyViewPolicy<keyof DB & string, keyof DB[T] & string, VocabularyAction<V>>;
|
|
161
278
|
/**
|
|
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
|
|
279
|
+
* Every table of the schema, and no other, maps to a policy; a view `Views` names may (ADR-0072). A misspelt action or
|
|
280
|
+
* dimension fails at its own line. `P` is the policies as written, which picks each table's form by the scope it
|
|
281
|
+
* states; a policy written under this map's own annotation states nothing narrower, so it is checked against every
|
|
282
|
+
* form.
|
|
165
283
|
*/
|
|
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>;
|
|
284
|
+
export type VocabularyPolicyMap<DB, V, P = unknown, Views extends string = never> = {
|
|
285
|
+
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>;
|
|
286
|
+
} & {
|
|
287
|
+
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
288
|
};
|
|
169
289
|
type Flat<T> = {
|
|
170
290
|
[K in keyof T]: T[K];
|
|
@@ -207,19 +327,23 @@ type NormalScope<S, V, P> = S extends {
|
|
|
207
327
|
readonly app: R;
|
|
208
328
|
readonly key: KeyOf<S>;
|
|
209
329
|
} : S;
|
|
330
|
+
/** A view's normalized policy says so, which the handle types read: no site writes it. */
|
|
331
|
+
type ViewMark<T, Views> = T extends Views ? {
|
|
332
|
+
readonly view: true;
|
|
333
|
+
} : unknown;
|
|
210
334
|
/**
|
|
211
335
|
* 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.
|
|
336
|
+
* column, stated as the vocabulary and the conventions derive them; each view marked.
|
|
213
337
|
*/
|
|
214
|
-
export type NormalizedPolicies<P, V> = {
|
|
338
|
+
export type NormalizedPolicies<P, V, Views = never> = {
|
|
215
339
|
readonly [T in keyof P]: P[T] extends {
|
|
216
340
|
readonly scope: infer S;
|
|
217
|
-
} ? Flat<Omit<P[T], 'scope'> & {
|
|
341
|
+
} ? Flat<Omit<P[T], 'scope' | 'view'> & {
|
|
218
342
|
readonly scope: NormalScope<S, V, P>;
|
|
219
|
-
}
|
|
343
|
+
} & ViewMark<T, Views>> : P[T];
|
|
220
344
|
};
|
|
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 & {
|
|
345
|
+
/** Takes the policies of exactly the tables of the schema, and of any of its views, written in the vocabulary form. */
|
|
346
|
+
export type DefineVocabularyPolicies<DB, V, Views extends string = never> = <const P extends VocabularyPolicyMap<DB, V, P, Views>>(policies: P & {
|
|
223
347
|
readonly [K in Exclude<keyof P, keyof DB>]: never;
|
|
224
|
-
}) => Tables<DB, NormalizedPolicies<P, V>>;
|
|
348
|
+
}) => Tables<DB, NormalizedPolicies<P, V, Views>>;
|
|
225
349
|
export {};
|