@fabrika/db 0.9.0-dev.11 → 0.9.0-dev.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +5 -5
- package/src/authority.ts +254 -0
- package/src/builtins.ts +21 -0
- package/src/capability.ts +15 -0
- package/src/codegen-sqlite.ts +8 -5
- package/src/codegen.ts +75 -28
- package/src/condition-nodes.ts +119 -0
- package/src/conditions.ts +683 -0
- package/src/description.ts +3 -0
- package/src/framework.ts +1 -1
- package/src/guard.ts +302 -15
- package/src/index.ts +2 -0
- package/src/nodes.ts +9 -6
- package/src/orm-types.ts +31 -12
- package/src/orm.ts +85 -33
- package/src/policy.ts +1247 -40
- package/src/registry.ts +11 -0
- package/src/sql.ts +22 -3
- package/src/store.ts +744 -55
- package/src/tables.ts +104 -2
- package/src/testing-shared.ts +3 -0
- package/src/vocabulary-tables.ts +110 -17
- package/types/src/authority.d.ts +66 -0
- package/types/src/builtins.d.ts +2 -0
- package/types/src/capability.d.ts +15 -0
- package/types/src/codegen.d.ts +24 -11
- package/types/src/condition-nodes.d.ts +9 -0
- package/types/src/conditions.d.ts +151 -0
- package/types/src/description.d.ts +3 -0
- package/types/src/framework.d.ts +1 -1
- package/types/src/guard.d.ts +16 -0
- package/types/src/index.d.ts +1 -1
- package/types/src/nodes.d.ts +2 -0
- package/types/src/orm-types.d.ts +31 -11
- package/types/src/orm.d.ts +5 -0
- package/types/src/policy.d.ts +197 -2
- package/types/src/registry.d.ts +9 -0
- package/types/src/sql.d.ts +2 -2
- package/types/src/store.d.ts +45 -7
- package/types/src/tables.d.ts +7 -1
- package/types/src/vocabulary-tables.d.ts +117 -16
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import type { ConditionArtifact } from '@fabrika/auth-core';
|
|
2
|
+
import { type NameSet } from './builtins';
|
|
3
|
+
import type { Primitive } from './description';
|
|
4
|
+
/** A literal a condition compares with. `{ now: true }` is the statement's clock, never the text `now`. */
|
|
5
|
+
export type ConditionLiteral = string | number | boolean;
|
|
6
|
+
export interface NowOperand {
|
|
7
|
+
readonly now: true;
|
|
8
|
+
}
|
|
9
|
+
/** The parsed clock operand, compared by identity: no literal, the text `now` included, is ever it. */
|
|
10
|
+
export declare const NOW: NowOperand;
|
|
11
|
+
/** One test of a column: exactly one operator. */
|
|
12
|
+
export interface ColumnTest {
|
|
13
|
+
readonly eq?: ConditionLiteral;
|
|
14
|
+
readonly notEq?: ConditionLiteral;
|
|
15
|
+
readonly in?: readonly ConditionLiteral[];
|
|
16
|
+
readonly notIn?: readonly ConditionLiteral[];
|
|
17
|
+
readonly isNull?: boolean;
|
|
18
|
+
readonly lt?: ConditionLiteral | NowOperand;
|
|
19
|
+
readonly lte?: ConditionLiteral | NowOperand;
|
|
20
|
+
readonly gt?: ConditionLiteral | NowOperand;
|
|
21
|
+
readonly gte?: ConditionLiteral | NowOperand;
|
|
22
|
+
}
|
|
23
|
+
/** `{ some: { table: … } }` or `{ none: { table: … } }` at the end of a path: over the rows naming the row it reaches. */
|
|
24
|
+
export type RowsTest = {
|
|
25
|
+
readonly some: {
|
|
26
|
+
readonly [table: string]: ConditionPolicy;
|
|
27
|
+
};
|
|
28
|
+
} | {
|
|
29
|
+
readonly none: {
|
|
30
|
+
readonly [table: string]: ConditionPolicy;
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* A condition as a policy writes it: `{ column: test, … }` (all of them), `{ 'fk_column.column': test }` along a
|
|
35
|
+
* foreign key, `{ anyOf: […] }`, `{ allOf: […] }`, `{ not: … }`, or `{ some: { table: … } }` and `{ none: { table: … } }`
|
|
36
|
+
* over the rows of `table` whose foreign key names the row; `{ 'fk_column': { some: { table: … } } }` over the rows
|
|
37
|
+
* naming the row a path reaches.
|
|
38
|
+
*/
|
|
39
|
+
export type ConditionPolicy<Col extends string = string> = {
|
|
40
|
+
readonly anyOf: readonly ConditionPolicy<Col>[];
|
|
41
|
+
} | {
|
|
42
|
+
readonly allOf: readonly ConditionPolicy<Col>[];
|
|
43
|
+
} | {
|
|
44
|
+
readonly not: ConditionPolicy<Col>;
|
|
45
|
+
} | {
|
|
46
|
+
readonly some: {
|
|
47
|
+
readonly [table: string]: ConditionPolicy;
|
|
48
|
+
};
|
|
49
|
+
} | {
|
|
50
|
+
readonly none: {
|
|
51
|
+
readonly [table: string]: ConditionPolicy;
|
|
52
|
+
};
|
|
53
|
+
} | {
|
|
54
|
+
readonly [C in Col | `${Col}.${string}`]?: ColumnTest | RowsTest;
|
|
55
|
+
};
|
|
56
|
+
export type ConditionOperator = 'eq' | 'notEq' | 'in' | 'notIn' | 'isNull' | 'lt' | 'lte' | 'gt' | 'gte';
|
|
57
|
+
/** One foreign-key hop of a path: `column` of the table it leaves references `key` of `table`. */
|
|
58
|
+
export interface ConditionHop {
|
|
59
|
+
readonly column: string;
|
|
60
|
+
readonly table: string;
|
|
61
|
+
readonly key: string;
|
|
62
|
+
}
|
|
63
|
+
export type ConditionModel = {
|
|
64
|
+
readonly kind: 'all';
|
|
65
|
+
readonly terms: readonly ConditionModel[];
|
|
66
|
+
} | {
|
|
67
|
+
readonly kind: 'any';
|
|
68
|
+
readonly terms: readonly ConditionModel[];
|
|
69
|
+
} | {
|
|
70
|
+
readonly kind: 'not';
|
|
71
|
+
readonly term: ConditionModel;
|
|
72
|
+
} | {
|
|
73
|
+
/**
|
|
74
|
+
* `key in (select table.column from table where <term>)`, or its negation for `none`, over the row `hops` reach
|
|
75
|
+
* from the condition's own row (none: the row itself).
|
|
76
|
+
*/
|
|
77
|
+
readonly kind: 'some';
|
|
78
|
+
readonly hops: readonly ConditionHop[];
|
|
79
|
+
readonly negated: boolean;
|
|
80
|
+
readonly key: string;
|
|
81
|
+
readonly table: string;
|
|
82
|
+
readonly column: string;
|
|
83
|
+
readonly term: ConditionModel;
|
|
84
|
+
} | {
|
|
85
|
+
readonly kind: 'test';
|
|
86
|
+
readonly hops: readonly ConditionHop[];
|
|
87
|
+
readonly column: string;
|
|
88
|
+
readonly operator: ConditionOperator;
|
|
89
|
+
readonly value: ConditionLiteral | readonly ConditionLiteral[] | boolean | NowOperand;
|
|
90
|
+
};
|
|
91
|
+
/** What the parse reads of each table: its columns, its foreign keys and its typed columns. */
|
|
92
|
+
export interface ConditionTable {
|
|
93
|
+
readonly name: string;
|
|
94
|
+
readonly columns: NameSet;
|
|
95
|
+
readonly foreignKeys: readonly {
|
|
96
|
+
readonly columns: readonly string[];
|
|
97
|
+
readonly table: string;
|
|
98
|
+
readonly references: readonly string[];
|
|
99
|
+
}[];
|
|
100
|
+
readonly timestamps: NameSet;
|
|
101
|
+
readonly json: NameSet;
|
|
102
|
+
/** The columns the column list declares NOT NULL: a path under `not` follows only these. */
|
|
103
|
+
readonly notNull: NameSet;
|
|
104
|
+
}
|
|
105
|
+
/** A path follows at most this many foreign keys: each hop is one more subquery in every statement the condition scopes. */
|
|
106
|
+
export declare const MAX_CONDITION_HOPS = 3;
|
|
107
|
+
/**
|
|
108
|
+
* Whether a path may follow `from.column` to the row of `to` whose `key` it holds. A hop reads its target unscoped, so
|
|
109
|
+
* the rule admits only one that stays where the row is (its own container or parent), stays in its tenant (a foreign
|
|
110
|
+
* key that also holds every ancestor the two tables share), or reaches what every site reads (a reference table).
|
|
111
|
+
* `backward` is a `some` reading the rows of `from` that name a row of `to`: those must stay in that row's tenant, so
|
|
112
|
+
* a reference row, which every tenant's rows name, is no shortcut there.
|
|
113
|
+
*/
|
|
114
|
+
export type HopRule = (from: string, column: string, to: string, key: string, backward: boolean) => boolean;
|
|
115
|
+
/** Parses `input` as a condition over `table`. `hops` is the rule a path's foreign keys are held to. */
|
|
116
|
+
export declare function parseCondition(input: unknown, table: ConditionTable, tables: (name: string) => ConditionTable | undefined, what: string, hops: HopRule): ConditionModel;
|
|
117
|
+
export declare function splitPath(path: string): readonly string[];
|
|
118
|
+
export declare function singleForeignKey(table: ConditionTable, name: string, what: string): ConditionHop;
|
|
119
|
+
/** The tables a condition's subqueries read, each with the column it selects: what the guard admits under it. */
|
|
120
|
+
export declare function conditionReach(condition: ConditionModel, select: (table: string, column: string) => void): void;
|
|
121
|
+
/** A test's single literal; a list is compared only with in or notIn. */
|
|
122
|
+
export declare function scalar(value: ConditionLiteral | readonly ConditionLiteral[] | boolean | NowOperand): ConditionLiteral;
|
|
123
|
+
/**
|
|
124
|
+
* One step of the rows an insert's condition reads: from the rows reached so far, the rows of `table` whose `to` column
|
|
125
|
+
* holds their `from` column. A path's hop goes to the row a foreign key names (`from` the key column, `to` the target's
|
|
126
|
+
* key); a `some` goes back to the rows naming it (`from` the referenced key, `to` the naming column).
|
|
127
|
+
*/
|
|
128
|
+
export interface ReadStep {
|
|
129
|
+
readonly from: string;
|
|
130
|
+
readonly table: string;
|
|
131
|
+
readonly to: string;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Every sequence of steps from an insert's own row to the rows its condition reads, each one no other extends: the rows
|
|
135
|
+
* the insert locks, step by step, before it judges them. Empty when the condition reads the row alone.
|
|
136
|
+
*/
|
|
137
|
+
export declare function readPaths(condition: ConditionModel): readonly (readonly ReadStep[])[];
|
|
138
|
+
/**
|
|
139
|
+
* Why `condition` cannot be judged on an inserted row, or `undefined` when it can. The insert locks every row its
|
|
140
|
+
* condition reads, which holds a column test and a row a `some` found until the commit, but not a row inserted meanwhile:
|
|
141
|
+
* that turns `none`, and a `some` under `not`, false. And no row names the inserted row before it exists.
|
|
142
|
+
*/
|
|
143
|
+
export declare function insertProblem(condition: ConditionModel, table: string): string | undefined;
|
|
144
|
+
/**
|
|
145
|
+
* The value of `condition` on an insert's own row, under SQL's three-valued logic: a NULL compared is unknown, `not`
|
|
146
|
+
* of unknown stays unknown, and only `true` passes. A condition with a path is judged in SQL on the written row instead.
|
|
147
|
+
*/
|
|
148
|
+
export declare function rowSatisfies(condition: ConditionModel, row: ReadonlyMap<string, Primitive | null>, now: Date): boolean;
|
|
149
|
+
export declare function hasOwnCondition(record: Readonly<Record<string, ConditionModel>>, action: string): boolean;
|
|
150
|
+
/** A condition as the artifact records it: the policy's own form, a path written back as `fk_column.column`. */
|
|
151
|
+
export declare function conditionArtifact(condition: ConditionModel): ConditionArtifact;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { OperationNode } from 'kysely';
|
|
1
2
|
import { type NameSet } from './builtins';
|
|
2
3
|
export type Primitive = string | number | bigint | boolean | Date;
|
|
3
4
|
export type ComparisonOperator = '=' | '<>' | '<' | '<=' | '>' | '>=';
|
|
@@ -45,6 +46,8 @@ export interface JoinShape {
|
|
|
45
46
|
export interface JoinState {
|
|
46
47
|
readonly join: JoinShape;
|
|
47
48
|
readonly select: SelectState;
|
|
49
|
+
/** The child's condition for the action the select holds, which the layer adds to the ON; never the builder's. */
|
|
50
|
+
readonly condition?: OperationNode;
|
|
48
51
|
}
|
|
49
52
|
export interface SelectState {
|
|
50
53
|
/** Identifies the description a callback received, so it cannot return one from elsewhere. */
|
package/types/src/framework.d.ts
CHANGED
|
@@ -11,7 +11,7 @@ export type { DeliveryClaim, NewWebhookBinding, WebhookBinding, WebhookTables }
|
|
|
11
11
|
export type { JobLeases, JobLeaseState, JobOutcome, LeaseChange, LeaseProjection, OverdueClaim } from './job-leases';
|
|
12
12
|
export type { FinishWakes, JobWakes, NewWake, Wake, WakeReason } from './job-wakes';
|
|
13
13
|
export type { DataAccess, DataHandles, DataSite } from './orm';
|
|
14
|
-
export type { FrameworkTables, Locator, ProvisionalBinding } from './registry';
|
|
14
|
+
export type { FrameworkTables, Locator, ProvisionalBinding, RelationCheck, RevealCheck } from './registry';
|
|
15
15
|
export type { SqlMode, SqlSite, SqlTag, SqlValue } from './sql';
|
|
16
16
|
export type { FileColumnRef, QuarantinedStoredFile, ServedFile, StagedFile, StoredFileMeta, StoredFiles, StoredFileStatus } from './stored-files';
|
|
17
17
|
/** Whether `value` is an object `scoped(tables)` created, rather than a look-alike with the same fields. */
|
package/types/src/guard.d.ts
CHANGED
|
@@ -18,6 +18,22 @@ export interface GuardContext {
|
|
|
18
18
|
readonly lock?: 'share' | 'update' | 'claim';
|
|
19
19
|
/** An insert that does nothing on a conflict of its listed columns: `on conflict (columns) do nothing`, and no other. */
|
|
20
20
|
readonly onConflict?: 'nothing';
|
|
21
|
+
/**
|
|
22
|
+
* The tables a scope predicate's subqueries may read, each with the columns one may select: a relation's source by
|
|
23
|
+
* its node column, a condition path's target by its key. Without it, a scope holds no subquery.
|
|
24
|
+
*/
|
|
25
|
+
readonly subqueries?: Readonly<Record<string, readonly string[]>>;
|
|
26
|
+
/**
|
|
27
|
+
* A child view's parent: its scope may read the view's rows through `<column> in (select P.<key> from P where <P's
|
|
28
|
+
* reach>)`. Without it, a scope holds no such subquery.
|
|
29
|
+
*/
|
|
30
|
+
readonly through?: ViewParentLink | undefined;
|
|
31
|
+
}
|
|
32
|
+
/** The parent a child view's scope reads its rows through: the view's parent column, the parent table and its key. */
|
|
33
|
+
export interface ViewParentLink {
|
|
34
|
+
readonly column: string;
|
|
35
|
+
readonly table: string;
|
|
36
|
+
readonly key: string;
|
|
21
37
|
}
|
|
22
38
|
/** Refuses any operation tree outside the allowlist. Runs on the final tree, immediately before it is compiled. */
|
|
23
39
|
export declare function assertAllowedTree(root: OperationNode, context: GuardContext): void;
|
package/types/src/index.d.ts
CHANGED
|
@@ -8,4 +8,4 @@ export type { AnyDbIndex, ChildRows, ContainedRows, CountOptions, DataKind, DbIn
|
|
|
8
8
|
export { type ChildPolicy, type ChildScope, type ColumnList, defineTables, type DimensionPolicy, type DimensionScope, type InsertValues, isTables, readTables, type RecordActions, type ReferencePolicy, type ReferenceScope, type RowOf, type TableActions, type TableInsert, Tables, type TableUpdate, type UpdateValues, type VersionColumn, type VocabularyTables, } from './policy';
|
|
9
9
|
export { type AggregateRow, type AggregateSpec, type ChildListScope, type DataLayer, type DataLayerRuntime, scoped, type ScopedDatabase, } from './store';
|
|
10
10
|
export { type TablePolicies, tablePolicies } from './tables';
|
|
11
|
-
export type { ColumnTypes, DefineVocabularyPolicies, NormalizedPolicies, TableVocabulary, TypedColumnList, VocabularyAction, VocabularyChildScope, VocabularyContainer, VocabularyContainerOf, VocabularyDimension, VocabularyDimensionScope, VocabularyPolicyMap, VocabularyTablePolicy, } from './vocabulary-tables';
|
|
11
|
+
export type { ColumnTypes, ColumnViews, DefineVocabularyPolicies, NormalizedPolicies, TableVocabulary, TypedColumnList, VocabularyAction, VocabularyChildScope, VocabularyContainer, VocabularyContainerOf, VocabularyDimension, VocabularyDimensionScope, VocabularyPolicyMap, VocabularyTablePolicy, VocabularyViewPolicy, } from './vocabulary-tables';
|
package/types/src/nodes.d.ts
CHANGED
|
@@ -8,6 +8,8 @@ export declare function allOf(nodes: readonly OperationNode[]): OperationNode;
|
|
|
8
8
|
/** One node, or the OR of several in a parenthesis: inside an AND, `a and b or c` would read as `(a and b) or c`. */
|
|
9
9
|
export declare function anyOf(nodes: readonly OperationNode[]): OperationNode;
|
|
10
10
|
export declare function inList(table: string, name: string, values: readonly string[]): OperationNode;
|
|
11
|
+
/** `($1, $2, …)`: bound values, as the right operand of an `in`. */
|
|
12
|
+
export declare function valueList(values: readonly string[]): OperationNode;
|
|
11
13
|
/** The scope of a `Within` that is not limited to scope values. */
|
|
12
14
|
export declare function everything(): OperationNode;
|
|
13
15
|
/** LIKE treats `\` as its escape character by default, so the text matches literally. */
|
package/types/src/orm-types.d.ts
CHANGED
|
@@ -101,6 +101,8 @@ export interface IndexedTable {
|
|
|
101
101
|
readonly versioned: boolean;
|
|
102
102
|
/** A queue table: its rows are claimed and finished through `row.ref`. */
|
|
103
103
|
readonly queued: boolean;
|
|
104
|
+
/** A view: read like its scope's tables, written by no site (ADR-0072). */
|
|
105
|
+
readonly view: boolean;
|
|
104
106
|
}
|
|
105
107
|
type IndexedTableOf<DB, P, T extends keyof P & keyof DB & string, V = unknown> = {
|
|
106
108
|
readonly mode: ModeOf<P, T>;
|
|
@@ -131,6 +133,9 @@ type IndexedTableOf<DB, P, T extends keyof P & keyof DB & string, V = unknown> =
|
|
|
131
133
|
readonly queued: PolicyOf<P, T> extends {
|
|
132
134
|
readonly queue: object;
|
|
133
135
|
} ? true : false;
|
|
136
|
+
readonly view: PolicyOf<P, T> extends {
|
|
137
|
+
readonly view: true;
|
|
138
|
+
} ? true : false;
|
|
134
139
|
};
|
|
135
140
|
type OwnerTable<P, D> = {
|
|
136
141
|
[K in keyof P]: P[K] extends {
|
|
@@ -168,6 +173,18 @@ type ChainDimensions<P, T, V> = PolicyOf<P, T> extends {
|
|
|
168
173
|
} ? DimensionOf<P, Parent> | ChainDimensions<P, Parent, V> : never : never;
|
|
169
174
|
/** The dimensions whose `Within` lists table `T`: every dimension of its chain, and a top-level table's own. */
|
|
170
175
|
type ReachOf<P, T, V> = [ContainerOf<P, T>] extends [never] ? DimensionOf<P, T> | ChainDimensions<P, T, V> : ChainDimensions<P, T, V>;
|
|
176
|
+
/**
|
|
177
|
+
* The dimensions outside its chain whose `Within` reads table `T` through a foreign key (`readThrough`); a child view's
|
|
178
|
+
* are its parent's, as it is read through its parent row.
|
|
179
|
+
*/
|
|
180
|
+
type ReadThroughOf<P, T> = PolicyOf<P, T> extends {
|
|
181
|
+
readonly readThrough: infer R;
|
|
182
|
+
} ? keyof R & string : PolicyOf<P, T> extends {
|
|
183
|
+
readonly view: true;
|
|
184
|
+
readonly scope: {
|
|
185
|
+
readonly child: infer Parent extends string;
|
|
186
|
+
};
|
|
187
|
+
} ? ReadThroughOf<P, Parent> : never;
|
|
171
188
|
/**
|
|
172
189
|
* A row as reads return it: the generated interface itself where no column has a separate write type, so a
|
|
173
190
|
* diagnostic names it (`DealsTable`), and Kysely's `Selectable` of it otherwise.
|
|
@@ -209,9 +226,12 @@ export type DbIndex<DB, P, V = unknown> = {
|
|
|
209
226
|
readonly listed: {
|
|
210
227
|
readonly [T in DimensionTableName<P> as ListDimensionOf<P, T>]: T;
|
|
211
228
|
};
|
|
212
|
-
/**
|
|
229
|
+
/**
|
|
230
|
+
* Dimension → the tables an `over` of it lists: each whose containment chain holds it, a top-level table its own, and
|
|
231
|
+
* each that reads through it (`readThrough`).
|
|
232
|
+
*/
|
|
213
233
|
readonly within: {
|
|
214
|
-
readonly [T in DimensionTableName<P> | ChildTableName<P> as ReachOf<P, T, V>]: T;
|
|
234
|
+
readonly [T in DimensionTableName<P> | ChildTableName<P> as ReachOf<P, T, V> | ReadThroughOf<P, T>]: T;
|
|
215
235
|
};
|
|
216
236
|
readonly references: ReferenceTableName<P>;
|
|
217
237
|
/** The application tables: an `app` endpoint reads and writes them. */
|
|
@@ -379,15 +399,15 @@ export interface UnownedTarget<D extends string> {
|
|
|
379
399
|
readonly update: Unavailable<`no table owns the dimension ${D}, so ctx.db.target has no row of its own`>;
|
|
380
400
|
readonly remove: Unavailable<`no table owns the dimension ${D}, so ctx.db.target has no row of its own`>;
|
|
381
401
|
}
|
|
382
|
-
/** A child table of the target: its rows are addressed by their own key under the target. */
|
|
402
|
+
/** A child table of the target: its rows are addressed by their own key under the target; a view's are only read. */
|
|
383
403
|
export interface ChildRows<T extends string, E extends IndexedTable, A extends string, K extends DataKind> {
|
|
384
404
|
readonly list: Read<T, E, A, (options?: ListOptions<RowFor<E, A>>) => Promise<ListPage<RowFor<E, A>>>>;
|
|
385
405
|
readonly get: Read<T, E, A, (key: string) => Promise<RowFor<E, A>>>;
|
|
386
|
-
readonly insert: Write<T, E, 'insert', A, K, InsertMethod<E>>;
|
|
387
|
-
readonly update: Update<T, E, A, K, (key: string, values: {
|
|
406
|
+
readonly insert: E['view'] extends true ? Unavailable<`${T} is a view, which no site writes: write the tables it reads`> : Write<T, E, 'insert', A, K, InsertMethod<E>>;
|
|
407
|
+
readonly update: E['view'] extends true ? Unavailable<`${T} is a view, which no site writes: write the tables it reads`> : Update<T, E, A, K, (key: string, values: {
|
|
388
408
|
[C in keyof UpdateValuesOf<E, A>]: UpdateValuesOf<E, A>[C];
|
|
389
409
|
}, ...version: VersionArguments<E>) => Promise<Saved<E>>>;
|
|
390
|
-
readonly remove: Write<T, E, 'delete', A, K, (key: string, ...version: VersionArguments<E>) => Promise<void>>;
|
|
410
|
+
readonly remove: E['view'] extends true ? Unavailable<`${T} is a view, which no site writes: write the tables it reads`> : Write<T, E, 'delete', A, K, (key: string, ...version: VersionArguments<E>) => Promise<void>>;
|
|
391
411
|
}
|
|
392
412
|
/** A dimension table the target contains: listed in it, and inserted into it by a mutation of its insert action. */
|
|
393
413
|
export interface ContainedRows<T extends string, E extends IndexedTable, A extends string, K extends DataKind> {
|
|
@@ -543,16 +563,16 @@ export type UnscopedDeclaration<Table extends string, Mode extends UnscopedMode>
|
|
|
543
563
|
readonly [T in Table]?: Mode;
|
|
544
564
|
};
|
|
545
565
|
/**
|
|
546
|
-
* The tables of `U` whose mode may be `'write'` that no site writes: a reference table. A union or optional
|
|
547
|
-
* admits `'write'` counts, because the declaration it types may hold it.
|
|
566
|
+
* The tables of `U` whose mode may be `'write'` that no site writes: a reference table or a view. A union or optional
|
|
567
|
+
* mode that admits `'write'` counts, because the declaration it types may hold it.
|
|
548
568
|
*/
|
|
549
569
|
type ReadOnlyWritten<Ix extends AnyDbIndex, U> = {
|
|
550
|
-
[T in keyof U & TableName<Ix>]: 'write' extends U[T] ? Ix['tables'][T]['mode'] extends 'reference' ? T : never : never;
|
|
570
|
+
[T in keyof U & TableName<Ix>]: 'write' extends U[T] ? Ix['tables'][T]['mode'] extends 'reference' ? T : Ix['tables'][T]['view'] extends true ? T : never : never;
|
|
551
571
|
}[keyof U & TableName<Ix>];
|
|
552
|
-
type ReadOnlyMessage<T extends string> = `${T} is a reference table, which no site writes: its rows come from a migration, so declare unscoped: { ${T}: 'read' }`;
|
|
572
|
+
type ReadOnlyMessage<T extends string, E extends IndexedTable> = E['view'] extends true ? `${T} is a view, which no site writes: its rows come from the tables it reads, so declare unscoped: { ${T}: 'read' }` : `${T} is a reference table, which no site writes: its rows come from a migration, so declare unscoped: { ${T}: 'read' }`;
|
|
553
573
|
/** An `unscoped` declaration's `'write'` of a reference table, refused at its own line. */
|
|
554
574
|
export type ReadOnlyUnscoped<Ix extends AnyDbIndex, U> = [ReadOnlyWritten<Ix, U>] extends [never] ? unknown : {
|
|
555
|
-
readonly [T in ReadOnlyWritten<Ix, U>]: Unavailable<ReadOnlyMessage<T>>;
|
|
575
|
+
readonly [T in ReadOnlyWritten<Ix, U>]: Unavailable<ReadOnlyMessage<T, Ix['tables'][T]>>;
|
|
556
576
|
};
|
|
557
577
|
type KeylessMessage<T extends string> = `${T} is a reference table, which has no key: read it with list`;
|
|
558
578
|
/** A table a site declared `unscoped`: its scope predicate is `true`, and `'read'` writes nothing. */
|
package/types/src/orm.d.ts
CHANGED
|
@@ -80,3 +80,8 @@ export interface DataLayerParts {
|
|
|
80
80
|
fenced<T>(runtime: DataLayerRuntime, work: (session: Session) => Promise<T>): Promise<T>;
|
|
81
81
|
}
|
|
82
82
|
export declare function dataAccess(parts: DataLayerParts): DataAccess;
|
|
83
|
+
/**
|
|
84
|
+
* When a claim that leased rows re-arms its job, by the claim's own clock `now`: at once after a full claim, else
|
|
85
|
+
* at the next due row it would take but no sooner than a second after it, or never when it would take none.
|
|
86
|
+
*/
|
|
87
|
+
export declare function claimRearmAt(now: Date, full: boolean, next: Date | null): Date | null;
|
package/types/src/policy.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import type { Insertable, Selectable, SelectType, Updateable } from 'kysely';
|
|
2
2
|
import { type NameSet } from './builtins';
|
|
3
|
+
import { type ConditionModel, type ConditionPolicy } from './conditions';
|
|
3
4
|
import type { Unavailable } from './orm-types';
|
|
4
5
|
export type { RowOf } from './orm-types';
|
|
5
|
-
import type { ColumnTypes, DefineVocabularyPolicies, TableVocabulary } from './vocabulary-tables';
|
|
6
|
+
import type { ColumnTypes, ColumnViews, DefineVocabularyPolicies, TableVocabulary } from './vocabulary-tables';
|
|
6
7
|
export interface DimensionScope<Col extends string> {
|
|
7
8
|
readonly dimension: string;
|
|
8
9
|
readonly key: Col;
|
|
@@ -65,6 +66,82 @@ interface WritablePolicy<Col extends string, A extends string, V extends Col, Ac
|
|
|
65
66
|
* file reference. The table's actions are the file's authority.
|
|
66
67
|
*/
|
|
67
68
|
readonly files?: FilePolicies<Col>;
|
|
69
|
+
/** The relations this table's rows confer (`relations` of the vocabulary): each row holds one principal at one node. */
|
|
70
|
+
readonly confers?: {
|
|
71
|
+
readonly [relation: string]: ConfersPolicy<Col>;
|
|
72
|
+
};
|
|
73
|
+
/** Conditions that narrow an action of this table: a row the condition does not admit is out of that action's reach. */
|
|
74
|
+
readonly when?: {
|
|
75
|
+
readonly [action in A]?: ConditionPolicy<Col>;
|
|
76
|
+
};
|
|
77
|
+
/** Relation of this table's own dimension, or of one it reads through → the ancestor node a row the relation reaches reveals. */
|
|
78
|
+
readonly reveals?: {
|
|
79
|
+
readonly [relation: string]: RevealsPolicy<Col, A>;
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* Dimension outside the chain → the foreign key column a read reaches a row through: a `Within` of the dimension, or a
|
|
83
|
+
* relation at it, reads the row, and every write follows containment only. No update writes the column. Or
|
|
84
|
+
* `<nearest container column>.<its column>`: the row is read through its nearest container's own read path.
|
|
85
|
+
*/
|
|
86
|
+
readonly readThrough?: {
|
|
87
|
+
readonly [dimension: string]: Col | `${Col}.${string}`;
|
|
88
|
+
};
|
|
89
|
+
/**
|
|
90
|
+
* This table's own dimension → the ancestor node a row reveals to its members: a grant of one of `actions` at the
|
|
91
|
+
* row's own node holds that action at its node in `to` too, for a `Within`'s reads, while `when` admits the row.
|
|
92
|
+
*/
|
|
93
|
+
readonly memberReveals?: {
|
|
94
|
+
readonly [dimension: string]: MemberRevealsPolicy<Col, A>;
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
/** What a row reveals to a caller with a grant at its node: its node in `to`, for `actions`, while `when` admits the row. */
|
|
98
|
+
export interface MemberRevealsPolicy<Col extends string, A extends string> {
|
|
99
|
+
readonly to: string;
|
|
100
|
+
readonly actions: readonly A[];
|
|
101
|
+
/** A condition on the revealing row, under the same path rule as an action's `when`. */
|
|
102
|
+
readonly when?: ConditionPolicy<Col>;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* A row of the table that a relation reaches reveals its node in `to`, an ancestor dimension of its chain: the relation
|
|
106
|
+
* then holds `actions` (those of them its roles list in `via`) there, while `when` admits the row.
|
|
107
|
+
*/
|
|
108
|
+
export interface RevealsPolicy<Col extends string, A extends string> {
|
|
109
|
+
readonly to: string;
|
|
110
|
+
readonly actions: readonly A[];
|
|
111
|
+
/** A condition on the revealing row, under the same path rule as an action's `when`. */
|
|
112
|
+
readonly when?: ConditionPolicy<Col>;
|
|
113
|
+
/**
|
|
114
|
+
* Required on a reveal matched through `readThrough` of a table with an insert action: the row's writer picks the node,
|
|
115
|
+
* so an insert delegates. Without `self`, an insert that names a node where its writer holds the relation is refused.
|
|
116
|
+
*/
|
|
117
|
+
readonly assign?: {
|
|
118
|
+
readonly self?: true;
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* A relation source: each row says that the principal in `principal` stands in the relation to the node `node` holds,
|
|
123
|
+
* until `expires` (none: no deadline) and while `when` admits the row. Writing a row is delegation.
|
|
124
|
+
*/
|
|
125
|
+
export interface ConfersPolicy<Col extends string> {
|
|
126
|
+
/**
|
|
127
|
+
* The column that holds the principal, or `<column>.<principal column>`: one foreign key of the table into the key of
|
|
128
|
+
* the table whose column holds it. The table's `createdBy` names the row's writer (it needs `assign.self`).
|
|
129
|
+
*/
|
|
130
|
+
readonly principal: Col | `${Col}.${string}`;
|
|
131
|
+
/** The column that names the node; by default the table's column for the relation's dimension (its key or its chain). */
|
|
132
|
+
readonly node?: Col;
|
|
133
|
+
/** A timestamp column: the row confers nothing once it passed. */
|
|
134
|
+
readonly expires?: Col;
|
|
135
|
+
/** A condition on the row's own columns (no foreign-key path): `{ revoked_at: { isNull: true } }`. */
|
|
136
|
+
readonly when?: ConditionPolicy<Col>;
|
|
137
|
+
/**
|
|
138
|
+
* Inserting a row delegates the relation's actions to its principal, bounded by the inserting Ref's node: at most
|
|
139
|
+
* `maxDays` ahead (which needs `expires`), and to the inserting principal itself only with `self`.
|
|
140
|
+
*/
|
|
141
|
+
readonly assign?: {
|
|
142
|
+
readonly maxDays?: number;
|
|
143
|
+
readonly self?: true;
|
|
144
|
+
};
|
|
68
145
|
}
|
|
69
146
|
/** A table's file columns: column → the name of a file kind of the contract (`c.files`). */
|
|
70
147
|
export type FilePolicies<Col extends string> = {
|
|
@@ -136,6 +213,13 @@ export interface TableColumns<Col extends string = string, Table extends string
|
|
|
136
213
|
readonly booleans?: readonly Col[];
|
|
137
214
|
/** The JSON columns (`jsonb`, or declared `JSONB` on SQLite), written as a JSON value and never compared; absent when none. */
|
|
138
215
|
readonly json?: readonly Col[];
|
|
216
|
+
/**
|
|
217
|
+
* The columns declared NOT NULL; absent when none. A foreign key that holds a row's tenant needs its ancestor columns
|
|
218
|
+
* among them: under MATCH SIMPLE a null in one skips the whole key.
|
|
219
|
+
*/
|
|
220
|
+
readonly notNull?: readonly Col[];
|
|
221
|
+
/** A view: read-only, with no foreign key; absent for a table. */
|
|
222
|
+
readonly view?: true;
|
|
139
223
|
}
|
|
140
224
|
/** The columns and foreign keys of every table, generated next to the schema types. */
|
|
141
225
|
export type ColumnList<DB> = {
|
|
@@ -210,13 +294,110 @@ export interface TableModel {
|
|
|
210
294
|
readonly booleans: NameSet;
|
|
211
295
|
/** The JSON columns: a write binds a JSON value's text, and no condition, order or aggregate names one. */
|
|
212
296
|
readonly json: NameSet;
|
|
297
|
+
/** The columns the column list declares NOT NULL. */
|
|
298
|
+
readonly notNull: NameSet;
|
|
213
299
|
/** The table's foreign keys, from the column list; a containment column must be one to its container's key. */
|
|
214
300
|
readonly foreignKeys: readonly ForeignKey[];
|
|
301
|
+
/** A view: a child or reference table no operation but a read reaches, whose containment only its SQL states. */
|
|
302
|
+
readonly view: boolean;
|
|
215
303
|
/**
|
|
216
304
|
* The containment links this table carries a column for, nearest first: a dimension table's container and
|
|
217
305
|
* the container's own chain; a child's parent and the parent's chain. Every predicate of a Ref covers all.
|
|
218
306
|
*/
|
|
219
307
|
readonly chain: readonly Container[];
|
|
308
|
+
/** The relations this table's rows confer, each with its columns, its condition and its delegation bound. */
|
|
309
|
+
readonly confers: readonly RelationSource[];
|
|
310
|
+
/** The condition each action of the table holds a row under; an action without one is not narrowed. */
|
|
311
|
+
readonly when: Readonly<Record<string, ConditionModel>>;
|
|
312
|
+
/** The ancestor nodes this table's rows reveal to a relation of its own dimension or of one it reads through. */
|
|
313
|
+
readonly reveals: readonly RevealModel[];
|
|
314
|
+
/** The secondary read paths: a dimension outside the chain, by the column that holds the row's node in it. */
|
|
315
|
+
readonly readThrough: readonly SecondaryPath[];
|
|
316
|
+
/** The ancestor nodes this table's rows reveal to a grant at their own node. */
|
|
317
|
+
readonly memberReveals: readonly MemberRevealModel[];
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* A read path to a dimension outside the chain: `column` holds a key of `table`, which owns `dimension` and lies in
|
|
321
|
+
* `chain`. The column's foreign key covers every ancestor the two chains share, so the node lies in the row's own tenant.
|
|
322
|
+
*/
|
|
323
|
+
export interface SecondaryPath {
|
|
324
|
+
readonly dimension: string;
|
|
325
|
+
readonly column: string;
|
|
326
|
+
readonly table: string;
|
|
327
|
+
readonly key: string;
|
|
328
|
+
readonly chain: readonly Container[];
|
|
329
|
+
/**
|
|
330
|
+
* The nearest container whose own path this is: the row's `column` of it, its `table` and `key`. `column` above is
|
|
331
|
+
* then the container's column, and the path is read as `T.<via column> in (select C.<key> from C where …)`.
|
|
332
|
+
*/
|
|
333
|
+
readonly via: ContainerHop | undefined;
|
|
334
|
+
}
|
|
335
|
+
/** The row's nearest containment column, and the table and key it names. */
|
|
336
|
+
export interface ContainerHop {
|
|
337
|
+
readonly column: string;
|
|
338
|
+
readonly table: string;
|
|
339
|
+
readonly key: string;
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* A row of `table` that `relation` reaches reveals its node in `dimension`, held in `column`: the relation holds
|
|
343
|
+
* `actions` at that node while `when` admits the row.
|
|
344
|
+
*/
|
|
345
|
+
export interface RevealModel {
|
|
346
|
+
readonly relation: string;
|
|
347
|
+
readonly table: string;
|
|
348
|
+
/** The table's key, which the relation's nodes match unless the reveal is matched through `path`. */
|
|
349
|
+
readonly key: string;
|
|
350
|
+
/**
|
|
351
|
+
* The secondary path whose node, or an ancestor of it, the relation's nodes match. A reveal matched through one holds
|
|
352
|
+
* its actions for a `Within`'s reads only, as the path itself does.
|
|
353
|
+
*/
|
|
354
|
+
readonly path: SecondaryPath | undefined;
|
|
355
|
+
/** The bound on an insert's delegation through `path`: `undefined` when no handler inserts a row of the table. */
|
|
356
|
+
readonly assign: {
|
|
357
|
+
readonly self: boolean;
|
|
358
|
+
} | undefined;
|
|
359
|
+
readonly dimension: string;
|
|
360
|
+
readonly column: string;
|
|
361
|
+
readonly actions: readonly string[];
|
|
362
|
+
readonly when: ConditionModel | undefined;
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* A row of `table`, a node of `source` (its own dimension, keyed by `key`), reveals its node in `dimension`, held in
|
|
366
|
+
* `column`: a grant of one of `actions` at the row's node holds the action there too, for a `Within`'s reads, while
|
|
367
|
+
* `when` admits the row. Only a grant at the node itself counts, never one at a container of it: an insert would
|
|
368
|
+
* otherwise reveal the ancestor to the container's members at once.
|
|
369
|
+
*/
|
|
370
|
+
export interface MemberRevealModel {
|
|
371
|
+
readonly source: string;
|
|
372
|
+
readonly table: string;
|
|
373
|
+
readonly key: string;
|
|
374
|
+
readonly dimension: string;
|
|
375
|
+
readonly column: string;
|
|
376
|
+
readonly actions: readonly string[];
|
|
377
|
+
readonly when: ConditionModel | undefined;
|
|
378
|
+
}
|
|
379
|
+
/** One relation's source table: what a relation term reads, and what a write of its rows is bounded by. */
|
|
380
|
+
export interface RelationSource {
|
|
381
|
+
readonly relation: string;
|
|
382
|
+
readonly dimension: string;
|
|
383
|
+
readonly table: string;
|
|
384
|
+
/** The source's column that decides the principal: it holds it, or it references the row of `principalPath` that does. */
|
|
385
|
+
readonly principal: string;
|
|
386
|
+
readonly principalPath: PrincipalPath | undefined;
|
|
387
|
+
readonly node: string;
|
|
388
|
+
readonly expires: string | undefined;
|
|
389
|
+
readonly when: ConditionModel | undefined;
|
|
390
|
+
/** `undefined` when the table has no insert action, so no handler writes a row of it. */
|
|
391
|
+
readonly assign: {
|
|
392
|
+
readonly maxDays: number | undefined;
|
|
393
|
+
readonly self: boolean;
|
|
394
|
+
} | undefined;
|
|
395
|
+
}
|
|
396
|
+
/** `S.<principal>` references `key` of `table`, whose `column` holds the principal the source row names. */
|
|
397
|
+
export interface PrincipalPath {
|
|
398
|
+
readonly table: string;
|
|
399
|
+
readonly key: string;
|
|
400
|
+
readonly column: string;
|
|
220
401
|
}
|
|
221
402
|
/** Every table's model by name; read it through `modelOf`, never by a plain property access. */
|
|
222
403
|
export type TableModels = Readonly<Record<string, TableModel>>;
|
|
@@ -238,7 +419,7 @@ export declare function modelOf(models: TableModels, table: string): TableModel
|
|
|
238
419
|
*/
|
|
239
420
|
export declare function defineTables<const V extends TableVocabulary, C extends object>(vocabulary: V, columns: C): VocabularyTables<V, C>;
|
|
240
421
|
/** `defineTables(vocabulary, columns)` answers the policies' builder, or names the fix for an untyped column list. */
|
|
241
|
-
export type VocabularyTables<V, C> = [ColumnTypes<C>] extends [never] ? Unavailable<'the column list carries no row types: import columns from the generated src/db/schema.generated.ts (fabrika db codegen)'> : DefineVocabularyPolicies<ColumnTypes<C>, V
|
|
422
|
+
export type VocabularyTables<V, C> = [ColumnTypes<C>] extends [never] ? Unavailable<'the column list carries no row types: import columns from the generated src/db/schema.generated.ts (fabrika db codegen)'> : DefineVocabularyPolicies<ColumnTypes<C>, V, ColumnViews<C>>;
|
|
242
423
|
/**
|
|
243
424
|
* The same policies read from values nobody type-checked: `src/db/tables.ts` and its vocabulary as the static reader
|
|
244
425
|
* has them from a commit. Every shape `defineTables` refuses at runtime is refused here, through the same parse.
|
|
@@ -260,6 +441,20 @@ export declare function visibleFor(model: TableModel, action: string): readonly
|
|
|
260
441
|
export declare function updateActions(model: TableModel): readonly string[];
|
|
261
442
|
/** Whether `names` holds `name`: an operation's actions, the one way a policy check reads a list. */
|
|
262
443
|
export declare function includes(names: readonly string[], name: unknown): boolean;
|
|
444
|
+
/**
|
|
445
|
+
* A row of the table hands out access that its writer places: it confers a relation, reveals through a path, or reveals
|
|
446
|
+
* its ancestor to the grants at its node. Such a row is written only through the handles, and never moves.
|
|
447
|
+
*/
|
|
448
|
+
export declare function delegates(model: TableModel): boolean;
|
|
449
|
+
/**
|
|
450
|
+
* The tables whose rows hand out access, which no raw statement writes, lowercased as the tag compares names: a relation
|
|
451
|
+
* source or a path reveal, whose writer the layer bounds; a member-revealing table, whose key the layer generates so no
|
|
452
|
+
* write revives a node a stale grant still names; and the tables a principal path or a relation's condition reads, whose
|
|
453
|
+
* columns decide whom a source row names and what it confers.
|
|
454
|
+
*/
|
|
455
|
+
export declare function accessTablesOf(models: TableModels): readonly string[];
|
|
456
|
+
/** The column of `table` that places a row at a node of `dimension`: its key for its own dimension, else a chain link. */
|
|
457
|
+
export declare function nodeColumnOf(table: Pick<TableModel, 'scope' | 'chain'>, dimension: string): string | undefined;
|
|
263
458
|
type PolicyOf<P, T> = T extends keyof P ? P[T] : never;
|
|
264
459
|
type ScopeOf<P, T> = PolicyOf<P, T> extends {
|
|
265
460
|
readonly scope: infer S;
|
package/types/src/registry.d.ts
CHANGED
|
@@ -11,6 +11,13 @@ import type { StoredFiles } from './stored-files';
|
|
|
11
11
|
* the session to read in (a job's transaction, which sees the object it just wrote); a statement of its own otherwise.
|
|
12
12
|
*/
|
|
13
13
|
export type Locator = (dimension: string, id: string, using?: Session) => Promise<ReadonlyMap<string, string> | null>;
|
|
14
|
+
/** Whether an active row of `relation`'s source names `principal` at `node`: what a Ref held through it is minted on. */
|
|
15
|
+
export type RelationCheck = (relation: string, node: string, principal: string) => Promise<boolean>;
|
|
16
|
+
/**
|
|
17
|
+
* Whether a row `relation` reaches for `principal` reveals `node` of `dimension` now: what a Ref held through a reveal
|
|
18
|
+
* is minted on.
|
|
19
|
+
*/
|
|
20
|
+
export type RevealCheck = (relation: string, dimension: string, node: string, principal: string) => Promise<boolean>;
|
|
14
21
|
/** What the framework writes itself: its own `fabrika_*` tables. Usable once bound. */
|
|
15
22
|
export interface FrameworkTables {
|
|
16
23
|
readonly webhooks: WebhookTables;
|
|
@@ -25,6 +32,8 @@ export interface FrameworkTables {
|
|
|
25
32
|
/** A connection of the layer and its locator. Only its holder can `release` it, which lets the layer be bound again. */
|
|
26
33
|
export interface ProvisionalBinding {
|
|
27
34
|
readonly locate: Locator;
|
|
35
|
+
readonly relates: RelationCheck;
|
|
36
|
+
readonly reveals: RevealCheck;
|
|
28
37
|
release(): void;
|
|
29
38
|
}
|
|
30
39
|
export interface FrameworkSide {
|
package/types/src/sql.d.ts
CHANGED
|
@@ -59,9 +59,9 @@ export interface SqlSite {
|
|
|
59
59
|
* name outside `schema` and a function outside the reviewed built-ins; on SQLite also a `sqlite_*` name, and a
|
|
60
60
|
* parameter the text writes itself. workerd's authorizer bounds SQLite's functions, PRAGMAs and DDL.
|
|
61
61
|
*/
|
|
62
|
-
export declare function classifySql(parts: readonly string[], mode: SqlMode, database: SqlDatabase): string;
|
|
62
|
+
export declare function classifySql(parts: readonly string[], mode: SqlMode, database: SqlDatabase, sources?: readonly string[]): string;
|
|
63
63
|
/**
|
|
64
64
|
* `ctx.sql` of one site. Every call first checks that the work running now is the site's invocation, then classifies
|
|
65
65
|
* the text, then binds the values; only then does it reach the session, so a refused statement sends nothing.
|
|
66
66
|
*/
|
|
67
|
-
export declare function sqlTag(site: SqlSite, database: SqlDatabase, termsOf: ScopeOf): SqlTag;
|
|
67
|
+
export declare function sqlTag(site: SqlSite, database: SqlDatabase, termsOf: ScopeOf, sources: readonly string[]): SqlTag;
|