@fabrika/db 0.9.0-dev.2 → 0.9.0-dev.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +35 -4
- package/src/admissions.ts +78 -12
- package/src/application-rows.ts +108 -0
- package/src/authority.ts +560 -0
- package/src/builtins.ts +21 -0
- package/src/capability.ts +56 -2
- package/src/codegen-sqlite.ts +194 -0
- package/src/codegen.ts +229 -40
- package/src/condition-nodes.ts +119 -0
- package/src/conditions.ts +683 -0
- package/src/database.ts +227 -61
- package/src/description.ts +37 -8
- package/src/errors.ts +6 -4
- package/src/file-columns.ts +56 -11
- package/src/framework-tables.ts +28 -8
- package/src/framework.ts +14 -5
- package/src/guard.ts +373 -15
- package/src/index.ts +13 -1
- package/src/job-leases.ts +212 -55
- package/src/job-wakes.ts +450 -0
- package/src/json.ts +124 -0
- package/src/links.ts +8 -1
- package/src/migrations-sqlite.ts +163 -0
- package/src/migrations.ts +25 -0
- package/src/nodes.ts +84 -14
- package/src/orm-types.ts +124 -45
- package/src/orm.ts +283 -133
- package/src/policy.ts +1851 -119
- package/src/postgres.ts +26 -0
- package/src/registry.ts +52 -3
- package/src/sql.ts +247 -476
- package/src/sqlite-bun.ts +149 -0
- package/src/sqlite-migrate.ts +242 -0
- package/src/sqlite.ts +269 -0
- package/src/store.ts +2290 -239
- package/src/stored-files.ts +188 -52
- package/src/tables.ts +164 -3
- package/src/temporal.ts +122 -9
- package/src/testing-shared.ts +1348 -0
- package/src/testing-sqlite.ts +685 -0
- package/src/testing.ts +310 -494
- package/src/vocabulary-tables.ts +124 -18
- package/types/src/application-rows.d.ts +58 -0
- package/types/src/authority.d.ts +147 -0
- package/types/src/builtins.d.ts +2 -0
- package/types/src/capability.d.ts +55 -2
- package/types/src/codegen-sqlite.d.ts +13 -0
- package/types/src/codegen.d.ts +79 -5
- package/types/src/condition-nodes.d.ts +9 -0
- package/types/src/conditions.d.ts +151 -0
- package/types/src/database.d.ts +81 -20
- package/types/src/description.d.ts +26 -8
- package/types/src/errors.d.ts +5 -4
- package/types/src/file-columns.d.ts +6 -4
- package/types/src/framework.d.ts +11 -5
- package/types/src/guard.d.ts +24 -0
- package/types/src/index.d.ts +4 -3
- package/types/src/job-leases.d.ts +34 -5
- package/types/src/job-wakes.d.ts +101 -0
- package/types/src/json.d.ts +18 -0
- package/types/src/migrations-sqlite.d.ts +4 -0
- package/types/src/migrations.d.ts +1 -0
- package/types/src/nodes.d.ts +17 -2
- package/types/src/orm-types.d.ts +91 -42
- package/types/src/orm.d.ts +10 -0
- package/types/src/policy.d.ts +332 -6
- package/types/src/postgres.d.ts +12 -0
- package/types/src/registry.d.ts +37 -3
- package/types/src/sql.d.ts +29 -21
- package/types/src/sqlite-bun.d.ts +21 -0
- package/types/src/sqlite-migrate.d.ts +28 -0
- package/types/src/sqlite.d.ts +24 -0
- package/types/src/store.d.ts +143 -34
- package/types/src/stored-files.d.ts +6 -2
- package/types/src/tables.d.ts +17 -1
- package/types/src/temporal.d.ts +24 -0
- package/types/src/testing-shared.d.ts +290 -0
- package/types/src/testing-sqlite.d.ts +37 -0
- package/types/src/testing.d.ts +2 -93
- package/types/src/vocabulary-tables.d.ts +141 -17
package/types/src/policy.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
import { READ_ANY } from '@fabrika/auth-core';
|
|
1
2
|
import type { Insertable, Selectable, SelectType, Updateable } from 'kysely';
|
|
2
3
|
import { type NameSet } from './builtins';
|
|
4
|
+
import { type ConditionModel, type ConditionPolicy } from './conditions';
|
|
3
5
|
import type { Unavailable } from './orm-types';
|
|
4
6
|
export type { RowOf } from './orm-types';
|
|
5
|
-
import type { ColumnTypes, DefineVocabularyPolicies, TableVocabulary } from './vocabulary-tables';
|
|
7
|
+
import type { ColumnTypes, ColumnViews, DefineVocabularyPolicies, TableVocabulary } from './vocabulary-tables';
|
|
6
8
|
export interface DimensionScope<Col extends string> {
|
|
7
9
|
readonly dimension: string;
|
|
8
10
|
readonly key: Col;
|
|
@@ -27,12 +29,17 @@ export interface ApplicationScope<Col extends string> {
|
|
|
27
29
|
}
|
|
28
30
|
/** One action, or a list of them: a table that staff and a public procedure both read names both actions. */
|
|
29
31
|
export type TableAction<A extends string> = A | readonly A[];
|
|
32
|
+
export { READ_ANY };
|
|
30
33
|
export interface TableActions<A extends string> {
|
|
31
34
|
readonly read: TableAction<A>;
|
|
32
35
|
readonly insert?: TableAction<A>;
|
|
33
36
|
readonly update?: TableAction<A>;
|
|
34
37
|
readonly delete?: TableAction<A>;
|
|
35
38
|
}
|
|
39
|
+
/** An application table's actions: its read may be `'any'` instead of its actions (`READ_ANY`). */
|
|
40
|
+
export interface ApplicationActions<A extends string> extends Omit<TableActions<A>, 'read'> {
|
|
41
|
+
readonly read: TableAction<A> | typeof READ_ANY;
|
|
42
|
+
}
|
|
36
43
|
/**
|
|
37
44
|
* A queue table's columns (ADR-0060's drain): `due` is when a row is next claimed, `null` once it is done; `attempts`
|
|
38
45
|
* counts its claims, and a row with `maxAttempts` (default 10) of them is no longer claimed.
|
|
@@ -65,6 +72,119 @@ interface WritablePolicy<Col extends string, A extends string, V extends Col, Ac
|
|
|
65
72
|
* file reference. The table's actions are the file's authority.
|
|
66
73
|
*/
|
|
67
74
|
readonly files?: FilePolicies<Col>;
|
|
75
|
+
/** The relations this table's rows confer (`relations` of the vocabulary): each row holds one principal at one node. */
|
|
76
|
+
readonly confers?: {
|
|
77
|
+
readonly [relation: string]: ConfersPolicy<Col>;
|
|
78
|
+
};
|
|
79
|
+
/** Conditions that narrow an action of this table: a row the condition does not admit is out of that action's reach. */
|
|
80
|
+
readonly when?: {
|
|
81
|
+
readonly [action in A]?: ConditionPolicy<Col>;
|
|
82
|
+
};
|
|
83
|
+
/**
|
|
84
|
+
* Relation → action → a condition on the rows that relation reaches for that action: it narrows the relation's terms
|
|
85
|
+
* only, never an IAM grant, a member reveal or a path grant of the action.
|
|
86
|
+
*/
|
|
87
|
+
readonly relationWhen?: {
|
|
88
|
+
readonly [relation: string]: {
|
|
89
|
+
readonly [action in A]?: ConditionPolicy<Col>;
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
/** Relation of this table's own dimension, or of one it reads through → the ancestor node a row the relation reaches reveals. */
|
|
93
|
+
readonly reveals?: {
|
|
94
|
+
readonly [relation: string]: RevealsPolicy<Col, A>;
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* Dimension outside the chain → the foreign key column a read reaches a row through: a `Within` of the dimension, or a
|
|
98
|
+
* relation at it, reads the row, and every write follows containment only. No update writes the column. Or
|
|
99
|
+
* `<nearest container column>.<its column>`: the row is read through its nearest container's own read path.
|
|
100
|
+
*/
|
|
101
|
+
readonly readThrough?: {
|
|
102
|
+
readonly [dimension: string]: Col | `${Col}.${string}`;
|
|
103
|
+
};
|
|
104
|
+
/**
|
|
105
|
+
* Dimension of `readThrough` → the actions a `Ref` held through that path holds on the table's rows, writes included:
|
|
106
|
+
* a grant of one of them at the node the path names, or above it, reaches the row for that action. Whoever inserts
|
|
107
|
+
* the path's column must hold every listed action at the node it names.
|
|
108
|
+
*/
|
|
109
|
+
readonly writeThrough?: {
|
|
110
|
+
readonly [dimension: string]: readonly A[];
|
|
111
|
+
};
|
|
112
|
+
/**
|
|
113
|
+
* This table's own dimension, or one it reads through (`readThrough`) → the ancestor node a row reveals to its
|
|
114
|
+
* members: a grant of one of `actions` at the row's own node, or at the node its path names, holds that action at its
|
|
115
|
+
* node in `to` too, while `when` admits the row.
|
|
116
|
+
*/
|
|
117
|
+
readonly memberReveals?: {
|
|
118
|
+
readonly [dimension: string]: MemberRevealsPolicy<Col, A>;
|
|
119
|
+
};
|
|
120
|
+
/**
|
|
121
|
+
* Relation → an access view that lists the rows the relation reaches: the escape path for a rule no structured
|
|
122
|
+
* declaration expresses. The view's SQL decides; the layer binds only the caller's principal and the nodes it holds
|
|
123
|
+
* the relation inside, never a handler value.
|
|
124
|
+
*/
|
|
125
|
+
readonly access?: {
|
|
126
|
+
readonly [relation: string]: AccessPolicy;
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
/** An access view: `row` names this table's key; `principal` is compared to the caller, `member` to the nodes it holds the relation inside. */
|
|
130
|
+
export interface AccessPolicy {
|
|
131
|
+
readonly view: string;
|
|
132
|
+
readonly row: string;
|
|
133
|
+
readonly principal?: string;
|
|
134
|
+
readonly member?: string;
|
|
135
|
+
}
|
|
136
|
+
/** What a row reveals to a caller with a grant at its node: its node in `to`, for `actions`, while `when` admits the row. */
|
|
137
|
+
export interface MemberRevealsPolicy<Col extends string, A extends string> {
|
|
138
|
+
readonly to: string;
|
|
139
|
+
readonly actions: readonly A[];
|
|
140
|
+
/** A condition on the revealing row, under the same path rule as an action's `when`. */
|
|
141
|
+
readonly when?: ConditionPolicy<Col>;
|
|
142
|
+
/** Through a path, on a table with an insert action: the writer picks the node, and `self` lets it pick its own. */
|
|
143
|
+
readonly assign?: {
|
|
144
|
+
readonly self?: true;
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* A row of the table that a relation reaches reveals its node in `to`, an ancestor dimension of its chain: the relation
|
|
149
|
+
* then holds `actions` (those of them its roles list in `via`) there, while `when` admits the row.
|
|
150
|
+
*/
|
|
151
|
+
export interface RevealsPolicy<Col extends string, A extends string> {
|
|
152
|
+
readonly to: string;
|
|
153
|
+
readonly actions: readonly A[];
|
|
154
|
+
/** A condition on the revealing row, under the same path rule as an action's `when`. */
|
|
155
|
+
readonly when?: ConditionPolicy<Col>;
|
|
156
|
+
/**
|
|
157
|
+
* Required on a reveal matched through `readThrough` of a table with an insert action: the row's writer picks the node,
|
|
158
|
+
* so an insert delegates. Without `self`, an insert that names a node where its writer holds the relation is refused.
|
|
159
|
+
*/
|
|
160
|
+
readonly assign?: {
|
|
161
|
+
readonly self?: true;
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* A relation source: each row says that the principal in `principal` stands in the relation to the node `node` holds,
|
|
166
|
+
* until `expires` (none: no deadline) and while `when` admits the row. Writing a row is delegation.
|
|
167
|
+
*/
|
|
168
|
+
export interface ConfersPolicy<Col extends string> {
|
|
169
|
+
/**
|
|
170
|
+
* The column that holds the principal, or `<column>.<principal column>`: one foreign key of the table into the key of
|
|
171
|
+
* the table whose column holds it. The table's `createdBy` names the row's writer (it needs `assign.self`).
|
|
172
|
+
*/
|
|
173
|
+
readonly principal: Col | `${Col}.${string}`;
|
|
174
|
+
/** The column that names the node; by default the table's column for the relation's dimension (its key or its chain). */
|
|
175
|
+
readonly node?: Col;
|
|
176
|
+
/** A timestamp column: the row confers nothing once it passed. */
|
|
177
|
+
readonly expires?: Col;
|
|
178
|
+
/** A condition on the row's own columns (no foreign-key path): `{ revoked_at: { isNull: true } }`. */
|
|
179
|
+
readonly when?: ConditionPolicy<Col>;
|
|
180
|
+
/**
|
|
181
|
+
* Inserting a row delegates the relation's actions to its principal, bounded by the inserting Ref's node: at most
|
|
182
|
+
* `maxDays` ahead (which needs `expires`), and to the inserting principal itself only with `self`.
|
|
183
|
+
*/
|
|
184
|
+
readonly assign?: {
|
|
185
|
+
readonly maxDays?: number;
|
|
186
|
+
readonly self?: true;
|
|
187
|
+
};
|
|
68
188
|
}
|
|
69
189
|
/** A table's file columns: column → the name of a file kind of the contract (`c.files`). */
|
|
70
190
|
export type FilePolicies<Col extends string> = {
|
|
@@ -96,7 +216,7 @@ export interface ChildPolicy<Table extends string, Col extends string, A extends
|
|
|
96
216
|
readonly scope: ChildScope<Table, Col>;
|
|
97
217
|
}
|
|
98
218
|
/** An application table: written like a dimension table, by an `app` endpoint whose action is the operation's. */
|
|
99
|
-
export interface ApplicationPolicy<Col extends string, A extends string, V extends Col = Col> extends WritablePolicy<Col, A, V
|
|
219
|
+
export interface ApplicationPolicy<Col extends string, A extends string, V extends Col = Col> extends WritablePolicy<Col, A, V, ApplicationActions<A>> {
|
|
100
220
|
readonly scope: ApplicationScope<Col>;
|
|
101
221
|
}
|
|
102
222
|
interface ReadOnlyPolicy<Col extends string, A extends string> {
|
|
@@ -132,6 +252,17 @@ export interface TableColumns<Col extends string = string, Table extends string
|
|
|
132
252
|
readonly dates?: readonly Col[];
|
|
133
253
|
/** The `timestamp` and `timestamptz` columns, which a write takes as a `Date` or strict ISO 8601 text; absent when none. */
|
|
134
254
|
readonly timestamps?: readonly Col[];
|
|
255
|
+
/** On SQLite, the columns declared `BOOLEAN`, stored as 0 or 1 and read as a boolean; absent when none. */
|
|
256
|
+
readonly booleans?: readonly Col[];
|
|
257
|
+
/** The JSON columns (`jsonb`, or declared `JSONB` on SQLite), written as a JSON value and never compared; absent when none. */
|
|
258
|
+
readonly json?: readonly Col[];
|
|
259
|
+
/**
|
|
260
|
+
* The columns declared NOT NULL; absent when none. A foreign key that holds a row's tenant needs its ancestor columns
|
|
261
|
+
* among them: under MATCH SIMPLE a null in one skips the whole key.
|
|
262
|
+
*/
|
|
263
|
+
readonly notNull?: readonly Col[];
|
|
264
|
+
/** A view: read-only, with no foreign key; absent for a table. */
|
|
265
|
+
readonly view?: true;
|
|
135
266
|
}
|
|
136
267
|
/** The columns and foreign keys of every table, generated next to the schema types. */
|
|
137
268
|
export type ColumnList<DB> = {
|
|
@@ -160,9 +291,17 @@ export type ScopeModel = {
|
|
|
160
291
|
readonly reason: string;
|
|
161
292
|
readonly key: string;
|
|
162
293
|
};
|
|
163
|
-
/**
|
|
294
|
+
/**
|
|
295
|
+
* Each operation's actions: any one of them allows it. `read` names at least one, unless `readAny`; an empty list
|
|
296
|
+
* allows no operation.
|
|
297
|
+
*/
|
|
164
298
|
export interface ActionModel {
|
|
165
299
|
readonly read: readonly string[];
|
|
300
|
+
/**
|
|
301
|
+
* An application table read with `'any'`: every `app` endpoint, job and webhook reads it, whatever its action; never a
|
|
302
|
+
* public procedure or a scoped site. `read` is empty then, and a `when` narrows only the writes of its action.
|
|
303
|
+
*/
|
|
304
|
+
readonly readAny: boolean;
|
|
166
305
|
readonly insert: readonly string[];
|
|
167
306
|
readonly update: readonly string[];
|
|
168
307
|
readonly delete: readonly string[];
|
|
@@ -202,13 +341,137 @@ export interface TableModel {
|
|
|
202
341
|
readonly dates: NameSet;
|
|
203
342
|
/** The `timestamp` and `timestamptz` columns: a write's text must be strict ISO 8601 with an offset. */
|
|
204
343
|
readonly timestamps: NameSet;
|
|
344
|
+
/** On SQLite, the `BOOLEAN` columns, stored as 0 or 1. */
|
|
345
|
+
readonly booleans: NameSet;
|
|
346
|
+
/** The JSON columns: a write binds a JSON value's text, and no condition, order or aggregate names one. */
|
|
347
|
+
readonly json: NameSet;
|
|
348
|
+
/** The columns the column list declares NOT NULL. */
|
|
349
|
+
readonly notNull: NameSet;
|
|
205
350
|
/** The table's foreign keys, from the column list; a containment column must be one to its container's key. */
|
|
206
351
|
readonly foreignKeys: readonly ForeignKey[];
|
|
352
|
+
/** A view: a child or reference table no operation but a read reaches, whose containment only its SQL states. */
|
|
353
|
+
readonly view: boolean;
|
|
207
354
|
/**
|
|
208
355
|
* The containment links this table carries a column for, nearest first: a dimension table's container and
|
|
209
356
|
* the container's own chain; a child's parent and the parent's chain. Every predicate of a Ref covers all.
|
|
210
357
|
*/
|
|
211
358
|
readonly chain: readonly Container[];
|
|
359
|
+
/** The relations this table's rows confer, each with its columns, its condition and its delegation bound. */
|
|
360
|
+
readonly confers: readonly RelationSource[];
|
|
361
|
+
/** The condition each action of the table holds a row under; an action without one is not narrowed. */
|
|
362
|
+
readonly when: Readonly<Record<string, ConditionModel>>;
|
|
363
|
+
/** Relation → action → the condition the rows that relation reaches hold for that action under; ANDed onto its terms only. */
|
|
364
|
+
readonly relationWhen: Readonly<Record<string, Readonly<Record<string, ConditionModel>>>>;
|
|
365
|
+
/** The ancestor nodes this table's rows reveal to a relation of its own dimension or of one it reads through. */
|
|
366
|
+
readonly reveals: readonly RevealModel[];
|
|
367
|
+
/** The secondary read paths: a dimension outside the chain, by the column that holds the row's node in it. */
|
|
368
|
+
readonly readThrough: readonly SecondaryPath[];
|
|
369
|
+
/** The ancestor nodes this table's rows reveal to a grant at their own node. */
|
|
370
|
+
readonly memberReveals: readonly MemberRevealModel[];
|
|
371
|
+
/** The access views of the relations whose reach a view's SQL decides. */
|
|
372
|
+
readonly access: readonly AccessModel[];
|
|
373
|
+
}
|
|
374
|
+
/** A relation's access view over this table: `T.<key> in (select V.<row> from V where <bindings>)`. */
|
|
375
|
+
export interface AccessModel {
|
|
376
|
+
readonly relation: string;
|
|
377
|
+
/** The relation's dimension: a hold inside a node of it alone binds `member`. */
|
|
378
|
+
readonly dimension: string;
|
|
379
|
+
readonly view: string;
|
|
380
|
+
readonly row: string;
|
|
381
|
+
readonly principal: string | undefined;
|
|
382
|
+
readonly member: string | undefined;
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* A read path to a dimension outside the chain: `column` holds a key of `table`, which owns `dimension` and lies in
|
|
386
|
+
* `chain`. The column's foreign key covers every ancestor the two chains share, so the node lies in the row's own tenant.
|
|
387
|
+
*/
|
|
388
|
+
export interface SecondaryPath {
|
|
389
|
+
readonly dimension: string;
|
|
390
|
+
readonly column: string;
|
|
391
|
+
readonly table: string;
|
|
392
|
+
readonly key: string;
|
|
393
|
+
readonly chain: readonly Container[];
|
|
394
|
+
/**
|
|
395
|
+
* The nearest container whose own path this is: the row's `column` of it, its `table` and `key`. `column` above is
|
|
396
|
+
* then the container's column, and the path is read as `T.<via column> in (select C.<key> from C where …)`.
|
|
397
|
+
*/
|
|
398
|
+
readonly via: ContainerHop | undefined;
|
|
399
|
+
/** The actions a `Ref` held through the path holds on the table's rows (`writeThrough`); a `Within` reads through every path. */
|
|
400
|
+
readonly writes: readonly string[];
|
|
401
|
+
}
|
|
402
|
+
/** The row's nearest containment column, and the table and key it names. */
|
|
403
|
+
export interface ContainerHop {
|
|
404
|
+
readonly column: string;
|
|
405
|
+
readonly table: string;
|
|
406
|
+
readonly key: string;
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* A row of `table` that `relation` reaches reveals its node in `dimension`, held in `column`: the relation holds
|
|
410
|
+
* `actions` at that node while `when` admits the row.
|
|
411
|
+
*/
|
|
412
|
+
export interface RevealModel {
|
|
413
|
+
readonly relation: string;
|
|
414
|
+
readonly table: string;
|
|
415
|
+
/** The table's key, which the relation's nodes match unless the reveal is matched through `path`. */
|
|
416
|
+
readonly key: string;
|
|
417
|
+
/**
|
|
418
|
+
* The secondary path whose node, or an ancestor of it, the relation's nodes match. A reveal matched through one holds
|
|
419
|
+
* a `Ref` of an action only where the path does (`writeThrough`).
|
|
420
|
+
*/
|
|
421
|
+
readonly path: SecondaryPath | undefined;
|
|
422
|
+
/** The bound on an insert's delegation through `path`: `undefined` when no handler inserts a row of the table. */
|
|
423
|
+
readonly assign: {
|
|
424
|
+
readonly self: boolean;
|
|
425
|
+
} | undefined;
|
|
426
|
+
readonly dimension: string;
|
|
427
|
+
readonly column: string;
|
|
428
|
+
readonly actions: readonly string[];
|
|
429
|
+
readonly when: ConditionModel | undefined;
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* A row of `table`, a node of `source` (its own dimension, keyed by `key`), reveals its node in `dimension`, held in
|
|
433
|
+
* `column`: a grant of one of `actions` at the row's node holds the action there too, for a `Within`'s reads, while
|
|
434
|
+
* `when` admits the row. Only a grant at the node itself counts, never one at a container of it: an insert would
|
|
435
|
+
* otherwise reveal the ancestor to the container's members at once.
|
|
436
|
+
*/
|
|
437
|
+
export interface MemberRevealModel {
|
|
438
|
+
readonly source: string;
|
|
439
|
+
readonly table: string;
|
|
440
|
+
/** The column a grant's nodes match: the table's key, or `path`'s column. */
|
|
441
|
+
readonly key: string;
|
|
442
|
+
/** The secondary path whose node a grant names; a `Ref` holds an action through it only where the path does. */
|
|
443
|
+
readonly path: SecondaryPath | undefined;
|
|
444
|
+
/** The bound on an insert's pick of the node through `path`: `undefined` when no handler inserts a row of the table. */
|
|
445
|
+
readonly assign: {
|
|
446
|
+
readonly self: boolean;
|
|
447
|
+
} | undefined;
|
|
448
|
+
readonly dimension: string;
|
|
449
|
+
readonly column: string;
|
|
450
|
+
readonly actions: readonly string[];
|
|
451
|
+
readonly when: ConditionModel | undefined;
|
|
452
|
+
}
|
|
453
|
+
/** One relation's source table: what a relation term reads, and what a write of its rows is bounded by. */
|
|
454
|
+
export interface RelationSource {
|
|
455
|
+
readonly relation: string;
|
|
456
|
+
readonly dimension: string;
|
|
457
|
+
readonly table: string;
|
|
458
|
+
/** The source's column that decides the principal: it holds it, or it references the row of `principalPath` that does. */
|
|
459
|
+
readonly principal: string;
|
|
460
|
+
readonly principalPath: PrincipalPath | undefined;
|
|
461
|
+
readonly node: string;
|
|
462
|
+
readonly expires: string | undefined;
|
|
463
|
+
readonly when: ConditionModel | undefined;
|
|
464
|
+
/** `undefined` when the table has no insert action, so no handler writes a row of it. */
|
|
465
|
+
readonly assign: {
|
|
466
|
+
readonly maxDays: number | undefined;
|
|
467
|
+
readonly self: boolean;
|
|
468
|
+
} | undefined;
|
|
469
|
+
}
|
|
470
|
+
/** `S.<principal>` references `key` of `table`, whose `column` holds the principal the source row names. */
|
|
471
|
+
export interface PrincipalPath {
|
|
472
|
+
readonly table: string;
|
|
473
|
+
readonly key: string;
|
|
474
|
+
readonly column: string;
|
|
212
475
|
}
|
|
213
476
|
/** Every table's model by name; read it through `modelOf`, never by a plain property access. */
|
|
214
477
|
export type TableModels = Readonly<Record<string, TableModel>>;
|
|
@@ -230,7 +493,7 @@ export declare function modelOf(models: TableModels, table: string): TableModel
|
|
|
230
493
|
*/
|
|
231
494
|
export declare function defineTables<const V extends TableVocabulary, C extends object>(vocabulary: V, columns: C): VocabularyTables<V, C>;
|
|
232
495
|
/** `defineTables(vocabulary, columns)` answers the policies' builder, or names the fix for an untyped column list. */
|
|
233
|
-
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
|
|
496
|
+
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>>;
|
|
234
497
|
/**
|
|
235
498
|
* The same policies read from values nobody type-checked: `src/db/tables.ts` and its vocabulary as the static reader
|
|
236
499
|
* has them from a commit. Every shape `defineTables` refuses at runtime is refused here, through the same parse.
|
|
@@ -241,6 +504,11 @@ export declare function readTables(columns: unknown, policies: unknown, vocabula
|
|
|
241
504
|
* columns that declare it as their own write action for any other; none for an action that writes nothing here.
|
|
242
505
|
*/
|
|
243
506
|
export declare function updatableBy(model: TableModel, action: string): NameSet;
|
|
507
|
+
/**
|
|
508
|
+
* The columns any of `actions` updates in `model`, as one role holding them: the union of `updatableBy` over them.
|
|
509
|
+
* One action's set is `updatableBy`'s own.
|
|
510
|
+
*/
|
|
511
|
+
export declare function updatableByAny(model: TableModel, actions: readonly string[]): NameSet;
|
|
244
512
|
/** Whether the table names `action`: one of its table actions, or a column's own write action. */
|
|
245
513
|
export declare function namesAction(model: TableModel, action: string): boolean;
|
|
246
514
|
/**
|
|
@@ -248,21 +516,79 @@ export declare function namesAction(model: TableModel, action: string): boolean;
|
|
|
248
516
|
* one. A capability reads its own row, so a write `Ref` sees the columns of its action and none of another's.
|
|
249
517
|
*/
|
|
250
518
|
export declare function visibleFor(model: TableModel, action: string): readonly string[];
|
|
519
|
+
/**
|
|
520
|
+
* The actions whose reach one read answers: an `app` capability's held actions that admit the operation, each row
|
|
521
|
+
* passing where one of them admits it (`conditionedAny`), and whether their `when` narrows the rows (`narrowed`), which
|
|
522
|
+
* it does not in a table read with `'any'`.
|
|
523
|
+
*/
|
|
524
|
+
export interface ReadReach {
|
|
525
|
+
readonly actions: readonly string[];
|
|
526
|
+
readonly narrowed: boolean;
|
|
527
|
+
}
|
|
528
|
+
/** The reach of a capability of one action, whose own condition narrows its rows. */
|
|
529
|
+
export declare function soleReach(action: string): ReadReach;
|
|
530
|
+
/**
|
|
531
|
+
* Whether every row a read of `reach` answers is a row `action` reads alone: it is one of the reaching actions, and either
|
|
532
|
+
* no `when` of its own narrows it or it alone reaches the rows. A column of its own is shown only then, so several held
|
|
533
|
+
* actions never put one action's column on another action's rows.
|
|
534
|
+
*/
|
|
535
|
+
export declare function readsAsAction(model: TableModel, action: string, reach: ReadReach): boolean;
|
|
536
|
+
/**
|
|
537
|
+
* The columns a read of `reach` answers, the union of what each reaching action reads alone: every one that declares no
|
|
538
|
+
* read action of its own, and one that does where that action reads every answered row (`readsAsAction`).
|
|
539
|
+
*/
|
|
540
|
+
export declare function visibleThrough(model: TableModel, reach: ReadReach): readonly string[];
|
|
251
541
|
/** Every action that updates a column of `model`: its update action and each column's own write action. */
|
|
252
542
|
export declare function updateActions(model: TableModel): readonly string[];
|
|
253
543
|
/** Whether `names` holds `name`: an operation's actions, the one way a policy check reads a list. */
|
|
254
544
|
export declare function includes(names: readonly string[], name: unknown): boolean;
|
|
545
|
+
/**
|
|
546
|
+
* A row of the table hands out access that its writer places: it confers a relation, reveals through a path, or reveals
|
|
547
|
+
* its ancestor to the grants at its node. Such a row is written only through the handles, and never moves.
|
|
548
|
+
*/
|
|
549
|
+
export declare function delegates(model: TableModel): boolean;
|
|
550
|
+
/**
|
|
551
|
+
* The tables whose rows hand out access, which no raw statement writes, lowercased as the tag compares names: a relation
|
|
552
|
+
* source, a path reveal or a path column that holds actions (`writeThrough`), whose writer the layer bounds; a
|
|
553
|
+
* member-revealing table, whose key the layer generates so no write revives a node a stale grant still names; and the
|
|
554
|
+
* tables a principal path or a relation's condition reads, whose columns decide whom a source row names and what it
|
|
555
|
+
* confers.
|
|
556
|
+
*/
|
|
557
|
+
export declare function accessTablesOf(models: TableModels): readonly string[];
|
|
558
|
+
/** A path column whose insert hands out the listed actions at the node it names: the bound its writer is held to. */
|
|
559
|
+
export interface PathWriter {
|
|
560
|
+
readonly path: SecondaryPath;
|
|
561
|
+
/** The path's own `writes` and those of every table that reads the same dimension through this table's column. */
|
|
562
|
+
readonly actions: readonly string[];
|
|
563
|
+
}
|
|
564
|
+
/**
|
|
565
|
+
* The columns of `model` whose insert hands out actions through a secondary path: its own paths that hold actions, and
|
|
566
|
+
* its paths that a table below follows through it (`'<link>.<column>'`) with actions of its own. Such a table is written
|
|
567
|
+
* only through the handles, so no write sets the column past the bound.
|
|
568
|
+
*/
|
|
569
|
+
export declare function pathWriters(models: Readonly<Record<string, TableModel>>, model: TableModel): readonly PathWriter[];
|
|
570
|
+
/** The column of `table` that places a row at a node of `dimension`: its key for its own dimension, else a chain link. */
|
|
571
|
+
export declare function nodeColumnOf(table: Pick<TableModel, 'scope' | 'chain'>, dimension: string): string | undefined;
|
|
255
572
|
type PolicyOf<P, T> = T extends keyof P ? P[T] : never;
|
|
256
573
|
type ScopeOf<P, T> = PolicyOf<P, T> extends {
|
|
257
574
|
readonly scope: infer S;
|
|
258
575
|
} ? S : never;
|
|
259
576
|
export type Operation = 'read' | 'insert' | 'update' | 'delete';
|
|
260
|
-
/**
|
|
577
|
+
/**
|
|
578
|
+
* The action, or the union of the actions, of operation `Op` on table `T`; `never` when the policy names none, and for
|
|
579
|
+
* the read of a table read with `'any'` (`ReadsAny`).
|
|
580
|
+
*/
|
|
261
581
|
export type ActionOf<P, T, Op extends Operation> = PolicyOf<P, T> extends {
|
|
262
582
|
readonly actions: infer Acts;
|
|
263
583
|
} ? Acts extends {
|
|
264
584
|
readonly [K in Op]: infer A;
|
|
265
|
-
} ? A extends string ? A : A extends readonly (infer L extends string)[] ? L : never : never : never;
|
|
585
|
+
} ? A extends typeof READ_ANY ? never : A extends string ? A : A extends readonly (infer L extends string)[] ? L : never : never : never;
|
|
586
|
+
/** Whether table `T` is read with `'any'`: by every `app` endpoint, job and webhook, whatever its action. */
|
|
587
|
+
export type ReadsAny<P, T> = PolicyOf<P, T> extends {
|
|
588
|
+
readonly actions: {
|
|
589
|
+
readonly read: typeof READ_ANY;
|
|
590
|
+
};
|
|
591
|
+
} ? true : false;
|
|
266
592
|
export type DimensionOf<P, T> = ScopeOf<P, T> extends {
|
|
267
593
|
readonly dimension: infer D extends string;
|
|
268
594
|
} ? D : never;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Database } from './database';
|
|
2
|
+
export interface PostgresOptions {
|
|
3
|
+
readonly url: string;
|
|
4
|
+
/** The application's schema (`app_<id>`), set as the connection's `search_path`. */
|
|
5
|
+
readonly schema: string;
|
|
6
|
+
readonly max?: number;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Over `Bun.SQL` with `prepare: false`: Bun otherwise keeps one prepared statement per distinct SQL text per
|
|
10
|
+
* connection, and `in` lists of different lengths make that grow without bound.
|
|
11
|
+
*/
|
|
12
|
+
export declare function openPostgres(options: PostgresOptions): Database;
|
package/types/src/registry.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import type { Admissions } from './admissions';
|
|
2
|
+
import type { ContainerGrant, RelationHold } from './capability';
|
|
2
3
|
import type { Session } from './database';
|
|
3
4
|
import type { WebhookTables } from './framework-tables';
|
|
4
5
|
import type { JobLeases } from './job-leases';
|
|
6
|
+
import type { JobWakes } from './job-wakes';
|
|
5
7
|
import type { DataAccess } from './orm';
|
|
6
8
|
import type { DataLayerRuntime, TableContext, TableProbe } from './store';
|
|
7
9
|
import type { StoredFiles } from './stored-files';
|
|
@@ -10,20 +12,52 @@ import type { StoredFiles } from './stored-files';
|
|
|
10
12
|
* the session to read in (a job's transaction, which sees the object it just wrote); a statement of its own otherwise.
|
|
11
13
|
*/
|
|
12
14
|
export type Locator = (dimension: string, id: string, using?: Session) => Promise<ReadonlyMap<string, string> | null>;
|
|
15
|
+
/** Whether an active row of `relation`'s source names `principal` at `node`: what a Ref held through it is minted on. */
|
|
16
|
+
export type RelationCheck = (relation: string, node: string, principal: string) => Promise<boolean>;
|
|
17
|
+
/**
|
|
18
|
+
* Whether a row `relation` reaches for `principal` reveals `node` of `dimension` now: what a Ref held through a reveal
|
|
19
|
+
* is minted on.
|
|
20
|
+
*/
|
|
21
|
+
export type RevealCheck = (relation: string, dimension: string, node: string, principal: string, action: string) => Promise<boolean>;
|
|
22
|
+
/**
|
|
23
|
+
* Of `relations` and `paths`, the caller's relations and grants of `action` that reach the object `id` of `dimension`
|
|
24
|
+
* through a secondary path that holds the action for a Ref (`writeThrough`), or through a relation row or reveal: what a
|
|
25
|
+
* Ref held through a path is minted on, and carries.
|
|
26
|
+
*/
|
|
27
|
+
export type PathCheck = (dimension: string, id: string, action: string, principal: string, relations: readonly RelationHold[], paths: readonly ContainerGrant[]) => Promise<{
|
|
28
|
+
readonly relations: readonly RelationHold[];
|
|
29
|
+
readonly paths: readonly ContainerGrant[];
|
|
30
|
+
}>;
|
|
31
|
+
/**
|
|
32
|
+
* Of `values`, the caller's grants of `action` at nodes of `revealing`, those whose row reveals `node` of `dimension` to
|
|
33
|
+
* its members now: what a Ref held through member reveals is minted on, and carries.
|
|
34
|
+
*/
|
|
35
|
+
export type MemberRevealCheck = (revealing: string, values: readonly string[], dimension: string, node: string, action: string) => Promise<readonly string[]>;
|
|
13
36
|
/** What the framework writes itself: its own `fabrika_*` tables. Usable once bound. */
|
|
14
37
|
export interface FrameworkTables {
|
|
15
38
|
readonly webhooks: WebhookTables;
|
|
16
39
|
readonly jobs: JobLeases;
|
|
40
|
+
/** A job's durable pending work (`fabrika_wakes`), which follows its lease, and the scheduler's next wake over both. */
|
|
41
|
+
readonly wakes: JobWakes;
|
|
17
42
|
/** A procedure's admission buckets, the challenge tokens it accepted and its byte budget. */
|
|
18
43
|
readonly admissions: Admissions;
|
|
19
44
|
/** Every stored file (`fabrika_files`): staged before its object, claimed at its first reference, swept unreferenced. */
|
|
20
45
|
readonly files: StoredFiles;
|
|
21
46
|
}
|
|
47
|
+
/** A connection of the layer and its locator. Only its holder can `release` it, which lets the layer be bound again. */
|
|
48
|
+
export interface ProvisionalBinding {
|
|
49
|
+
readonly locate: Locator;
|
|
50
|
+
readonly relates: RelationCheck;
|
|
51
|
+
readonly reveals: RevealCheck;
|
|
52
|
+
readonly holdsThrough: PathCheck;
|
|
53
|
+
readonly revealsToMembers: MemberRevealCheck;
|
|
54
|
+
release(): void;
|
|
55
|
+
}
|
|
22
56
|
export interface FrameworkSide {
|
|
23
|
-
/** Connects the layer once and answers its
|
|
24
|
-
bind(runtime: DataLayerRuntime):
|
|
57
|
+
/** Connects the layer once and answers its binding; a call while one is connected throws. */
|
|
58
|
+
bind(runtime: DataLayerRuntime): ProvisionalBinding;
|
|
25
59
|
readonly framework: FrameworkTables;
|
|
26
|
-
/** The untyped handle over one table that `@fabrika/db/testing` hands the isolation
|
|
60
|
+
/** The untyped handle over one table that `@fabrika/db/testing` hands the policy isolation kit. */
|
|
27
61
|
probe(table: string): TableProbe;
|
|
28
62
|
/** One table's context, for the framework's own store builders (`defineStore`, `@fabrika/db/testing`). */
|
|
29
63
|
tableContext(table: string, mode: 'dimension' | 'child'): TableContext;
|
package/types/src/sql.d.ts
CHANGED
|
@@ -2,8 +2,15 @@ import { z } from 'zod/v4';
|
|
|
2
2
|
import { type Session } from './database';
|
|
3
3
|
/** What a site declares: `'read'` runs queries only, `'write'` also `INSERT`, `UPDATE` and `DELETE`. */
|
|
4
4
|
export type SqlMode = 'read' | 'write';
|
|
5
|
-
/** A value an interpolation binds: a scalar, a `Date` (bound as ISO text), or a list of strings or numbers
|
|
5
|
+
/** A value an interpolation binds: a scalar, a `Date` (bound as ISO text), or a list of strings or numbers. */
|
|
6
6
|
export type SqlValue = string | number | boolean | bigint | Date | null | readonly (string | number)[];
|
|
7
|
+
/** The database a statement is classified for: Postgres in the application's schema, or a Durable Object's SQLite. */
|
|
8
|
+
export type SqlDatabase = {
|
|
9
|
+
readonly dialect: 'postgres';
|
|
10
|
+
readonly schema: string;
|
|
11
|
+
} | {
|
|
12
|
+
readonly dialect: 'sqlite';
|
|
13
|
+
};
|
|
7
14
|
/** One test of a capability's scope on a table: the column holds the value, or one of the list. */
|
|
8
15
|
export interface ScopeTest {
|
|
9
16
|
readonly column: string;
|
|
@@ -11,21 +18,20 @@ export interface ScopeTest {
|
|
|
11
18
|
}
|
|
12
19
|
/** A capability's predicate on one table: every row (`'all'`), or an OR of AND groups; no group matches nothing. */
|
|
13
20
|
export type ScopeTerms = 'all' | readonly (readonly ScopeTest[])[];
|
|
14
|
-
/** What a fragment's table is to its statement: read, or the target of
|
|
21
|
+
/** What a fragment's table is to its statement, as its author states it: read, or the target of an UPDATE or DELETE. */
|
|
15
22
|
export type ScopeUse = 'read' | 'update' | 'delete';
|
|
23
|
+
/** The scope of `capability` on `table` for `use`, as the data layer predicates its handles; throws when it does not reach it. */
|
|
24
|
+
export type ScopeOf = (capability: unknown, table: string, use: ScopeUse) => ScopeTerms;
|
|
25
|
+
/** The third argument of `ctx.sql.scope`: the table's alias in the statement, and what the statement does with it. */
|
|
26
|
+
export interface SqlScopeOptions {
|
|
27
|
+
/** The table's name in the statement; the table's own name by default. */
|
|
28
|
+
readonly alias?: string;
|
|
29
|
+
/** `'update'` or `'delete'` where the statement writes the table, which needs that operation's action; `'read'` by default. */
|
|
30
|
+
readonly use?: ScopeUse;
|
|
31
|
+
}
|
|
16
32
|
/**
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* certainty), and `undefined` before a statement is known.
|
|
20
|
-
*/
|
|
21
|
-
export type ScopeOf = (capability: unknown, table: string, use: ScopeUse, assigned?: readonly string[]) => ScopeTerms;
|
|
22
|
-
/** The operation a write statement performs on one of its targets. */
|
|
23
|
-
export type WriteOperation = 'insert' | 'update' | 'delete';
|
|
24
|
-
/** Throws for a table no statement may write with `operation`: a reference table. */
|
|
25
|
-
export type WriteTarget = (table: string, operation: WriteOperation) => void;
|
|
26
|
-
/**
|
|
27
|
-
* `ctx.sql.scope(capability, table, alias?)`: the capability's whole predicate on one table, as SQL with bound values.
|
|
28
|
-
* A fragment is a frozen object with no prototype; what it holds lives in this module's registry, read through
|
|
33
|
+
* `ctx.sql.scope(capability, table, { alias, use })`: the capability's whole predicate on one table, as SQL with bound
|
|
34
|
+
* values. A fragment is a frozen object with no prototype; what it holds lives in this module's registry, read through
|
|
29
35
|
* methods captured at load, so no object an application builds or changes is ever taken for one.
|
|
30
36
|
*/
|
|
31
37
|
export interface SqlScope {
|
|
@@ -34,7 +40,7 @@ export interface SqlScope {
|
|
|
34
40
|
/** `ctx.sql(Row)`: the tag for rows of `Row`, and `scope` for the capability's predicate inside its text. */
|
|
35
41
|
export interface SqlTag {
|
|
36
42
|
<S extends z.core.$ZodType>(row: S): (text: TemplateStringsArray, ...values: readonly (SqlValue | SqlScope)[]) => Promise<z.output<S>[]>;
|
|
37
|
-
scope(capability: unknown, table: string,
|
|
43
|
+
scope(capability: unknown, table: string, options?: SqlScopeOptions): SqlScope;
|
|
38
44
|
}
|
|
39
45
|
/** The site a tag is made for: its mode, its binding to the invocation, and the session its statements run in. */
|
|
40
46
|
export interface SqlSite {
|
|
@@ -46,14 +52,16 @@ export interface SqlSite {
|
|
|
46
52
|
fail(): void;
|
|
47
53
|
}
|
|
48
54
|
/**
|
|
49
|
-
* The SQL `parts` make, with
|
|
50
|
-
* one statement, any statement but a query (and `INSERT`/`UPDATE`/`DELETE` for
|
|
51
|
-
*
|
|
52
|
-
*
|
|
55
|
+
* The SQL `parts` make, with a bound placeholder for each value, or a `QueryRefusedError` that names what is refused.
|
|
56
|
+
* On both dialects: more than one statement, any statement but a query (and `INSERT`/`UPDATE`/`DELETE` for
|
|
57
|
+
* `'write'`), a `'read'` statement that writes or locks, a value placed where it would not be bound, and a `fabrika_*`
|
|
58
|
+
* name. On Postgres also a nested statement of another kind, `SELECT … INTO`, a `U&` name, a `pg_*` name, a qualified
|
|
59
|
+
* name outside `schema` and a function outside the reviewed built-ins; on SQLite also a `sqlite_*` name, and a
|
|
60
|
+
* parameter the text writes itself. workerd's authorizer bounds SQLite's functions, PRAGMAs and DDL.
|
|
53
61
|
*/
|
|
54
|
-
export declare function classifySql(parts: readonly string[], mode: SqlMode,
|
|
62
|
+
export declare function classifySql(parts: readonly string[], mode: SqlMode, database: SqlDatabase, sources?: readonly string[]): string;
|
|
55
63
|
/**
|
|
56
64
|
* `ctx.sql` of one site. Every call first checks that the work running now is the site's invocation, then classifies
|
|
57
65
|
* the text, then binds the values; only then does it reach the session, so a refused statement sends nothing.
|
|
58
66
|
*/
|
|
59
|
-
export declare function sqlTag(site: SqlSite,
|
|
67
|
+
export declare function sqlTag(site: SqlSite, database: SqlDatabase, termsOf: ScopeOf, sources: readonly string[]): SqlTag;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Database, type SQLQueryBindings } from 'bun:sqlite';
|
|
2
|
+
import type { SqliteStorage } from './sqlite';
|
|
3
|
+
export interface BunSqliteStorage extends SqliteStorage {
|
|
4
|
+
/** Runs `fn` in one transaction of its own, as a Durable Object's `storage.transactionSync` does. */
|
|
5
|
+
transactionSync<T>(fn: () => T): T;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Runs each statement of `query` in turn and answers the rows of the last one, as a Durable Object's `sql.exec` does.
|
|
9
|
+
* bun:sqlite's `prepare` compiles only the first statement of a text, and its column count cannot tell a migration from a
|
|
10
|
+
* read (`ALTER TABLE … ADD COLUMN … CHECK (…)` reports one column), so the text is split at the end of each compiled
|
|
11
|
+
* statement. `bindings` belong to a text of one statement. Refuses what workerd's SQLite refuses: more than 100 result
|
|
12
|
+
* columns or parameters in one statement.
|
|
13
|
+
*/
|
|
14
|
+
export declare function execStatements(database: Database, query: string, bindings?: readonly SQLQueryBindings[]): Record<string, unknown>[];
|
|
15
|
+
/**
|
|
16
|
+
* `database` (a fresh `:memory:` one by default) with foreign keys enforced, as a Durable Object enforces them. One
|
|
17
|
+
* `transaction` runs at a time; one started inside a callback nests in a savepoint, as workerd nests it, and its
|
|
18
|
+
* siblings wait for each other. A statement outside one runs at once. A text of several statements runs each of them and
|
|
19
|
+
* answers the rows of the last (`execStatements`).
|
|
20
|
+
*/
|
|
21
|
+
export declare function bunSqliteStorage(database?: Database): BunSqliteStorage;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/** What the runner needs of the database; a Durable Object's `storage.sql.exec` and `storage.transactionSync` fit it. */
|
|
2
|
+
export interface SqliteMigrationTarget {
|
|
3
|
+
/** Runs `query`, which may hold several statements, and answers the rows of the last one. */
|
|
4
|
+
exec(query: string, ...bindings: (string | number | null)[]): {
|
|
5
|
+
toArray(): Record<string, unknown>[];
|
|
6
|
+
};
|
|
7
|
+
/** Runs `fn` in one transaction: commits when it returns, rolls back when it throws. */
|
|
8
|
+
transactionSync<T>(fn: () => T): T;
|
|
9
|
+
}
|
|
10
|
+
export interface SqliteMigrationBundle {
|
|
11
|
+
readonly bundle: string;
|
|
12
|
+
readonly migrations: readonly {
|
|
13
|
+
readonly name: string;
|
|
14
|
+
readonly sql: string;
|
|
15
|
+
}[];
|
|
16
|
+
}
|
|
17
|
+
export declare class SqliteMigrationError extends Error {
|
|
18
|
+
readonly name = "SqliteMigrationError";
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Applies every migration the ledger does not record, each in its own transaction together with its ledger row, so a
|
|
22
|
+
* failing one leaves nothing of itself and keeps the ones before it. A bundle's digest is recorded in the transaction
|
|
23
|
+
* of its last file, or alone when nothing of it was pending; while every bundle matches its digest, the ledger is not
|
|
24
|
+
* read. Answers the identities it applied.
|
|
25
|
+
*/
|
|
26
|
+
export declare function migrateSqlite(target: SqliteMigrationTarget, bundles: readonly SqliteMigrationBundle[]): Promise<{
|
|
27
|
+
readonly applied: readonly string[];
|
|
28
|
+
}>;
|