@fabrika/db 0.9.0-dev.4 → 0.9.0-dev.41
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 +39 -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 +230 -41
- 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 +10 -6
- package/src/file-columns.ts +56 -11
- package/src/framework-tables.ts +28 -8
- package/src/framework.ts +15 -5
- package/src/guard.ts +373 -15
- package/src/index.ts +17 -1
- package/src/job-leases.ts +228 -55
- package/src/job-wakes.ts +600 -0
- package/src/json.ts +124 -0
- package/src/links.ts +8 -1
- package/src/list-cursor.ts +82 -0
- package/src/migrations-sqlite.ts +210 -0
- package/src/migrations.ts +73 -0
- package/src/nodes.ts +84 -14
- package/src/orm-types.ts +206 -71
- package/src/orm.ts +425 -170
- package/src/policy.ts +1979 -141
- package/src/postgres.ts +26 -0
- package/src/registry.ts +55 -3
- package/src/request-receipts.ts +107 -0
- 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 +2292 -239
- package/src/stored-files.ts +188 -52
- package/src/tables.ts +172 -6
- package/src/temporal.ts +122 -9
- package/src/testing-shared.ts +1351 -0
- package/src/testing-sqlite.ts +685 -0
- package/src/testing.ts +310 -494
- package/src/vocabulary-tables.ts +159 -27
- 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 +9 -6
- package/types/src/file-columns.d.ts +6 -4
- package/types/src/framework.d.ts +12 -5
- package/types/src/guard.d.ts +24 -0
- package/types/src/index.d.ts +5 -4
- package/types/src/job-leases.d.ts +40 -5
- package/types/src/job-wakes.d.ts +124 -0
- package/types/src/json.d.ts +18 -0
- package/types/src/list-cursor.d.ts +13 -0
- package/types/src/migrations-sqlite.d.ts +4 -0
- package/types/src/migrations.d.ts +4 -0
- package/types/src/nodes.d.ts +17 -2
- package/types/src/orm-types.d.ts +163 -71
- package/types/src/orm.d.ts +21 -0
- package/types/src/policy.d.ts +351 -23
- package/types/src/postgres.d.ts +12 -0
- package/types/src/registry.d.ts +40 -3
- package/types/src/request-receipts.d.ts +22 -0
- 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 +18 -2
- 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 +169 -22
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,28 +72,141 @@ 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> = {
|
|
71
191
|
readonly [C in Col]?: string;
|
|
72
192
|
};
|
|
73
193
|
/**
|
|
74
|
-
* A column's own actions. `read`: only a capability of
|
|
75
|
-
* without one it falls back to the table's read action. `write`: only a capability of
|
|
76
|
-
*
|
|
194
|
+
* A column's own actions. `read`: only a capability of one of those actions reads, filters or sorts by the column;
|
|
195
|
+
* without one it falls back to the table's read action. `write`: only a capability of one of those actions updates the
|
|
196
|
+
* column, so the column is not in `update`; a listed table update action writes it beside its update list.
|
|
77
197
|
*/
|
|
78
198
|
export type ColumnPolicies<Col extends string, A extends string> = {
|
|
79
199
|
readonly [C in Col]?: ColumnPolicy<A>;
|
|
80
200
|
};
|
|
81
|
-
/** A column's own read
|
|
201
|
+
/** A column's own read actions and its own write actions (each one, or a list as a table operation takes), or both. */
|
|
82
202
|
export interface ColumnPolicy<A extends string> {
|
|
83
|
-
readonly read?: A
|
|
84
|
-
readonly write?: A
|
|
203
|
+
readonly read?: TableAction<A>;
|
|
204
|
+
readonly write?: TableAction<A>;
|
|
85
205
|
}
|
|
86
206
|
/** A table no handler writes (a reference table) declares column read actions only. */
|
|
87
207
|
export type ColumnReadPolicies<Col extends string, A extends string> = {
|
|
88
208
|
readonly [C in Col]?: {
|
|
89
|
-
readonly read: A
|
|
209
|
+
readonly read: TableAction<A>;
|
|
90
210
|
};
|
|
91
211
|
};
|
|
92
212
|
export interface DimensionPolicy<Col extends string, A extends string, V extends Col = Col> extends WritablePolicy<Col, A, V, RecordActions<A>> {
|
|
@@ -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[];
|
|
@@ -194,21 +333,145 @@ export interface TableModel {
|
|
|
194
333
|
readonly queue: QueueModel | undefined;
|
|
195
334
|
/** The file columns, each with its file kind: a reference to `fabrika_files`, written only with a file reference. */
|
|
196
335
|
readonly files: Readonly<Record<string, string>>;
|
|
197
|
-
/** The columns that declare their own read
|
|
198
|
-
readonly columnReads: Readonly<Record<string, string>>;
|
|
199
|
-
/** The columns that declare their own write
|
|
200
|
-
readonly columnWrites: Readonly<Record<string, string>>;
|
|
336
|
+
/** The columns that declare their own read actions, as declared (also those equal to the table's). */
|
|
337
|
+
readonly columnReads: Readonly<Record<string, readonly string[]>>;
|
|
338
|
+
/** The columns that declare their own write actions, as declared: only a capability of one of them updates them. */
|
|
339
|
+
readonly columnWrites: Readonly<Record<string, readonly string[]>>;
|
|
201
340
|
/** The columns of Postgres type `date`, read as `YYYY-MM-DD` text. */
|
|
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,39 +493,104 @@ 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.
|
|
237
500
|
*/
|
|
238
501
|
export declare function readTables(columns: unknown, policies: unknown, vocabulary: unknown): Tables<unknown, unknown>;
|
|
239
502
|
/**
|
|
240
|
-
* The columns a capability of `action` updates in `model`: the update list for
|
|
241
|
-
*
|
|
503
|
+
* The columns a capability of `action` updates in `model`: the update list for a table update action, and every
|
|
504
|
+
* column that lists it among its own write actions; 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
|
/**
|
|
247
|
-
* The columns a capability of `action` reads: every one that declares no read action of its own, or
|
|
248
|
-
* one. A capability reads its own row, so a write `Ref` sees the columns of its action and none of another's.
|
|
515
|
+
* The columns a capability of `action` reads: every one that declares no read action of its own, or one whose own
|
|
516
|
+
* read actions include this one. A capability reads its own row, so a write `Ref` sees the columns of its action and none of another's.
|
|
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[];
|
|
541
|
+
/** Whether one of a column's own read actions reads every row a read of `reach` answers (`readsAsAction`). */
|
|
542
|
+
export declare function readsAsOneOf(model: TableModel, actions: readonly string[], reach: ReadReach): boolean;
|
|
251
543
|
/** Every action that updates a column of `model`: its update action and each column's own write action. */
|
|
252
544
|
export declare function updateActions(model: TableModel): readonly string[];
|
|
253
545
|
/** Whether `names` holds `name`: an operation's actions, the one way a policy check reads a list. */
|
|
254
546
|
export declare function includes(names: readonly string[], name: unknown): boolean;
|
|
547
|
+
/**
|
|
548
|
+
* A row of the table hands out access that its writer places: it confers a relation, reveals through a path, or reveals
|
|
549
|
+
* its ancestor to the grants at its node. Such a row is written only through the handles, and never moves.
|
|
550
|
+
*/
|
|
551
|
+
export declare function delegates(model: TableModel): boolean;
|
|
552
|
+
/**
|
|
553
|
+
* The tables whose rows hand out access, which no raw statement writes, lowercased as the tag compares names: a relation
|
|
554
|
+
* source, a path reveal or a path column that holds actions (`writeThrough`), whose writer the layer bounds; a
|
|
555
|
+
* member-revealing table, whose key the layer generates so no write revives a node a stale grant still names; and the
|
|
556
|
+
* tables a principal path or a relation's condition reads, whose columns decide whom a source row names and what it
|
|
557
|
+
* confers.
|
|
558
|
+
*/
|
|
559
|
+
export declare function accessTablesOf(models: TableModels): readonly string[];
|
|
560
|
+
/** A path column whose insert hands out the listed actions at the node it names: the bound its writer is held to. */
|
|
561
|
+
export interface PathWriter {
|
|
562
|
+
readonly path: SecondaryPath;
|
|
563
|
+
/** The path's own `writes` and those of every table that reads the same dimension through this table's column. */
|
|
564
|
+
readonly actions: readonly string[];
|
|
565
|
+
}
|
|
566
|
+
/**
|
|
567
|
+
* The columns of `model` whose insert hands out actions through a secondary path: its own paths that hold actions, and
|
|
568
|
+
* its paths that a table below follows through it (`'<link>.<column>'`) with actions of its own. Such a table is written
|
|
569
|
+
* only through the handles, so no write sets the column past the bound.
|
|
570
|
+
*/
|
|
571
|
+
export declare function pathWriters(models: Readonly<Record<string, TableModel>>, model: TableModel): readonly PathWriter[];
|
|
572
|
+
/** The column of `table` that places a row at a node of `dimension`: its key for its own dimension, else a chain link. */
|
|
573
|
+
export declare function nodeColumnOf(table: Pick<TableModel, 'scope' | 'chain'>, dimension: string): string | undefined;
|
|
255
574
|
type PolicyOf<P, T> = T extends keyof P ? P[T] : never;
|
|
256
575
|
type ScopeOf<P, T> = PolicyOf<P, T> extends {
|
|
257
576
|
readonly scope: infer S;
|
|
258
577
|
} ? S : never;
|
|
259
578
|
export type Operation = 'read' | 'insert' | 'update' | 'delete';
|
|
260
|
-
/**
|
|
579
|
+
/**
|
|
580
|
+
* The action, or the union of the actions, of operation `Op` on table `T`; `never` when the policy names none, and for
|
|
581
|
+
* the read of a table read with `'any'` (`ReadsAny`).
|
|
582
|
+
*/
|
|
261
583
|
export type ActionOf<P, T, Op extends Operation> = PolicyOf<P, T> extends {
|
|
262
584
|
readonly actions: infer Acts;
|
|
263
585
|
} ? Acts extends {
|
|
264
586
|
readonly [K in Op]: infer A;
|
|
265
|
-
} ? A extends string ? A : A extends readonly (infer L extends string)[] ? L : never : never : never;
|
|
587
|
+
} ? A extends typeof READ_ANY ? never : A extends string ? A : A extends readonly (infer L extends string)[] ? L : never : never : never;
|
|
588
|
+
/** Whether table `T` is read with `'any'`: by every `app` endpoint, job and webhook, whatever its action. */
|
|
589
|
+
export type ReadsAny<P, T> = PolicyOf<P, T> extends {
|
|
590
|
+
readonly actions: {
|
|
591
|
+
readonly read: typeof READ_ANY;
|
|
592
|
+
};
|
|
593
|
+
} ? true : false;
|
|
266
594
|
export type DimensionOf<P, T> = ScopeOf<P, T> extends {
|
|
267
595
|
readonly dimension: infer D extends string;
|
|
268
596
|
} ? D : never;
|
|
@@ -311,11 +639,11 @@ type TableReadAction<P, T> = PolicyOf<P, T> extends {
|
|
|
311
639
|
readonly read: infer A;
|
|
312
640
|
};
|
|
313
641
|
} ? A : ActionOf<P, T, 'read'>;
|
|
314
|
-
/** Columns whose own read
|
|
642
|
+
/** Columns whose own read actions leave out one of the table's: no read by the table's read action returns them. */
|
|
315
643
|
type RestrictedColumns<P, T> = [ColumnPoliciesOf<P, T>] extends [never] ? never : {
|
|
316
644
|
[K in keyof ColumnPoliciesOf<P, T>]: ColumnPoliciesOf<P, T>[K] extends {
|
|
317
645
|
readonly read: infer R;
|
|
318
|
-
} ? ([TableReadAction<P, T>] extends [R] ? never : K) : never;
|
|
646
|
+
} ? ([TableReadAction<P, T>] extends [R extends readonly (infer L extends string)[] ? L : R] ? never : K) : never;
|
|
319
647
|
}[keyof ColumnPoliciesOf<P, T>];
|
|
320
648
|
/** A row of table `T` as a store reads it, by the table's read action. */
|
|
321
649
|
export type PolicyRow<DB, P, T> = T extends keyof DB ? Flat<Omit<Selectable<DB[T]>, RestrictedColumns<P, T>>> : 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,8 +1,11 @@
|
|
|
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';
|
|
8
|
+
import type { RequestReceipts } from './request-receipts';
|
|
6
9
|
import type { DataLayerRuntime, TableContext, TableProbe } from './store';
|
|
7
10
|
import type { StoredFiles } from './stored-files';
|
|
8
11
|
/**
|
|
@@ -10,20 +13,54 @@ import type { StoredFiles } from './stored-files';
|
|
|
10
13
|
* the session to read in (a job's transaction, which sees the object it just wrote); a statement of its own otherwise.
|
|
11
14
|
*/
|
|
12
15
|
export type Locator = (dimension: string, id: string, using?: Session) => Promise<ReadonlyMap<string, string> | null>;
|
|
16
|
+
/** Whether an active row of `relation`'s source names `principal` at `node`: what a Ref held through it is minted on. */
|
|
17
|
+
export type RelationCheck = (relation: string, node: string, principal: string) => Promise<boolean>;
|
|
18
|
+
/**
|
|
19
|
+
* Whether a row `relation` reaches for `principal` reveals `node` of `dimension` now: what a Ref held through a reveal
|
|
20
|
+
* is minted on.
|
|
21
|
+
*/
|
|
22
|
+
export type RevealCheck = (relation: string, dimension: string, node: string, principal: string, action: string) => Promise<boolean>;
|
|
23
|
+
/**
|
|
24
|
+
* Of `relations` and `paths`, the caller's relations and grants of `action` that reach the object `id` of `dimension`
|
|
25
|
+
* through a secondary path that holds the action for a Ref (`writeThrough`), or through a relation row or reveal: what a
|
|
26
|
+
* Ref held through a path is minted on, and carries.
|
|
27
|
+
*/
|
|
28
|
+
export type PathCheck = (dimension: string, id: string, action: string, principal: string, relations: readonly RelationHold[], paths: readonly ContainerGrant[]) => Promise<{
|
|
29
|
+
readonly relations: readonly RelationHold[];
|
|
30
|
+
readonly paths: readonly ContainerGrant[];
|
|
31
|
+
}>;
|
|
32
|
+
/**
|
|
33
|
+
* Of `values`, the caller's grants of `action` at nodes of `revealing`, those whose row reveals `node` of `dimension` to
|
|
34
|
+
* its members now: what a Ref held through member reveals is minted on, and carries.
|
|
35
|
+
*/
|
|
36
|
+
export type MemberRevealCheck = (revealing: string, values: readonly string[], dimension: string, node: string, action: string) => Promise<readonly string[]>;
|
|
13
37
|
/** What the framework writes itself: its own `fabrika_*` tables. Usable once bound. */
|
|
14
38
|
export interface FrameworkTables {
|
|
15
39
|
readonly webhooks: WebhookTables;
|
|
16
40
|
readonly jobs: JobLeases;
|
|
41
|
+
/** A job's durable pending work (`fabrika_wakes`), which follows its lease, and the scheduler's next wake over both. */
|
|
42
|
+
readonly wakes: JobWakes;
|
|
17
43
|
/** A procedure's admission buckets, the challenge tokens it accepted and its byte budget. */
|
|
18
44
|
readonly admissions: Admissions;
|
|
19
45
|
/** Every stored file (`fabrika_files`): staged before its object, claimed at its first reference, swept unreferenced. */
|
|
20
46
|
readonly files: StoredFiles;
|
|
47
|
+
/** The answers of keyed mutations, read and written in the invocation's transaction (ADR-0078). */
|
|
48
|
+
readonly receipts: RequestReceipts;
|
|
49
|
+
}
|
|
50
|
+
/** A connection of the layer and its locator. Only its holder can `release` it, which lets the layer be bound again. */
|
|
51
|
+
export interface ProvisionalBinding {
|
|
52
|
+
readonly locate: Locator;
|
|
53
|
+
readonly relates: RelationCheck;
|
|
54
|
+
readonly reveals: RevealCheck;
|
|
55
|
+
readonly holdsThrough: PathCheck;
|
|
56
|
+
readonly revealsToMembers: MemberRevealCheck;
|
|
57
|
+
release(): void;
|
|
21
58
|
}
|
|
22
59
|
export interface FrameworkSide {
|
|
23
|
-
/** Connects the layer once and answers its
|
|
24
|
-
bind(runtime: DataLayerRuntime):
|
|
60
|
+
/** Connects the layer once and answers its binding; a call while one is connected throws. */
|
|
61
|
+
bind(runtime: DataLayerRuntime): ProvisionalBinding;
|
|
25
62
|
readonly framework: FrameworkTables;
|
|
26
|
-
/** The untyped handle over one table that `@fabrika/db/testing` hands the isolation
|
|
63
|
+
/** The untyped handle over one table that `@fabrika/db/testing` hands the policy isolation kit. */
|
|
27
64
|
probe(table: string): TableProbe;
|
|
28
65
|
/** One table's context, for the framework's own store builders (`defineStore`, `@fabrika/db/testing`). */
|
|
29
66
|
tableContext(table: string, mode: 'dimension' | 'child'): TableContext;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type Session } from './database';
|
|
2
|
+
/** What a receipt records of the request it answers: its operation, the digest of its input, and the output's JSON. */
|
|
3
|
+
export interface RequestReceipt {
|
|
4
|
+
readonly operation: string;
|
|
5
|
+
readonly identity: string;
|
|
6
|
+
readonly output: string;
|
|
7
|
+
}
|
|
8
|
+
export interface RequestReceipts {
|
|
9
|
+
/**
|
|
10
|
+
* The receipt of `principal`'s `key`, read in `session`; `null` when there is none. On Postgres it first takes the
|
|
11
|
+
* key's transaction lock, so a concurrent copy of the request waits for this transaction and then reads its receipt
|
|
12
|
+
* instead of running too; SQLite's one writer already serializes them.
|
|
13
|
+
*/
|
|
14
|
+
find(session: Session, principal: string, key: string): Promise<RequestReceipt | null>;
|
|
15
|
+
/**
|
|
16
|
+
* Records `receipt` under `principal`'s `key` in `session`, created at `now` (epoch ms), after pruning a batch of
|
|
17
|
+
* receipts created before `retainedSince`. `false` when the key already has a receipt: another copy of the request
|
|
18
|
+
* recorded it meanwhile, and this one's transaction must not commit.
|
|
19
|
+
*/
|
|
20
|
+
record(session: Session, principal: string, key: string, receipt: RequestReceipt, now: number, retainedSince: number): Promise<boolean>;
|
|
21
|
+
}
|
|
22
|
+
export declare function requestReceipts(): RequestReceipts;
|