@fabrika/db 0.9.0-dev.1 → 0.9.0-dev.10
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 +44 -12
- package/src/admissions.ts +78 -12
- package/src/application-rows.ts +108 -0
- package/src/capability.ts +5 -0
- package/src/codegen-sqlite.ts +191 -0
- package/src/codegen.ts +170 -28
- package/src/database.ts +227 -61
- package/src/description.ts +34 -8
- package/src/errors.ts +6 -4
- package/src/file-columns.ts +36 -9
- package/src/framework-tables.ts +28 -8
- package/src/framework.ts +14 -5
- package/src/guard.ts +8 -0
- package/src/index.ts +10 -1
- package/src/job-leases.ts +210 -54
- 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 +64 -9
- package/src/orm-types.ts +5 -5
- package/src/orm.ts +94 -37
- package/src/policy.ts +17 -2
- package/src/postgres.ts +26 -0
- package/src/registry.ts +11 -2
- package/src/sql.ts +230 -477
- package/src/sqlite-bun.ts +149 -0
- package/src/sqlite-migrate.ts +242 -0
- package/src/sqlite.ts +224 -0
- package/src/store.ts +385 -103
- package/src/stored-files.ts +188 -52
- package/src/tables.ts +16 -0
- package/src/temporal.ts +122 -9
- package/src/testing-shared.ts +878 -0
- package/src/testing-sqlite.ts +570 -0
- package/src/testing.ts +255 -482
- package/types/src/admissions.d.ts +21 -0
- package/types/src/application-rows.d.ts +58 -0
- package/types/src/builtins.d.ts +42 -0
- package/types/src/capability.d.ts +119 -0
- package/types/src/codegen-sqlite.d.ts +13 -0
- package/types/src/codegen.d.ts +113 -0
- package/types/src/database.d.ts +185 -0
- package/types/src/description.d.ts +124 -0
- package/types/src/errors.d.ts +98 -0
- package/types/src/file-columns.d.ts +39 -0
- package/types/src/file-references.d.ts +27 -0
- package/types/src/framework-tables.d.ts +59 -0
- package/types/src/framework.d.ts +38 -0
- package/types/src/guard.d.ts +23 -0
- package/types/src/identifier.d.ts +4 -0
- package/types/src/index.d.ts +11 -0
- package/types/src/job-leases.d.ts +112 -0
- package/types/src/job-wakes.d.ts +101 -0
- package/types/src/json.d.ts +18 -0
- package/types/src/links.d.ts +13 -0
- package/types/src/migrations-sqlite.d.ts +4 -0
- package/types/src/migrations.d.ts +26 -0
- package/types/src/nodes.d.ts +73 -0
- package/types/src/orm-types.d.ts +638 -0
- package/types/src/orm.d.ts +82 -0
- package/types/src/policy.d.ts +358 -0
- package/types/src/postgres.d.ts +12 -0
- package/types/src/production.d.ts +5 -0
- package/types/src/registry.d.ts +43 -0
- package/types/src/sql.d.ts +67 -0
- 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 +351 -0
- package/types/src/stored-files.d.ts +102 -0
- package/types/src/tables.d.ts +21 -0
- package/types/src/temporal.d.ts +36 -0
- package/types/src/testing-shared.d.ts +250 -0
- package/types/src/testing-sqlite.d.ts +22 -0
- package/types/src/testing.d.ts +35 -0
- package/types/src/vocabulary-tables.d.ts +225 -0
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { type NameSet } from './builtins';
|
|
2
|
+
export type Primitive = string | number | bigint | boolean | Date;
|
|
3
|
+
export type ComparisonOperator = '=' | '<>' | '<' | '<=' | '>' | '>=';
|
|
4
|
+
export type PatternOperator = 'like' | 'ilike' | 'not like' | 'not ilike';
|
|
5
|
+
export type ListOperator = 'in' | 'not in';
|
|
6
|
+
export type NullOperator = 'is' | 'is not';
|
|
7
|
+
export type AllowedOperator = ComparisonOperator | PatternOperator | ListOperator | NullOperator;
|
|
8
|
+
export interface Compare {
|
|
9
|
+
readonly kind: 'compare';
|
|
10
|
+
readonly column: string;
|
|
11
|
+
readonly operator: AllowedOperator;
|
|
12
|
+
readonly value: Primitive | readonly Primitive[] | null;
|
|
13
|
+
}
|
|
14
|
+
/** A case-insensitive substring match; the text's `%`, `_` and `\` match literally. */
|
|
15
|
+
export interface Contains {
|
|
16
|
+
readonly kind: 'contains';
|
|
17
|
+
readonly column: string;
|
|
18
|
+
readonly text: string;
|
|
19
|
+
}
|
|
20
|
+
export interface AnyOf {
|
|
21
|
+
readonly kind: 'any';
|
|
22
|
+
readonly conditions: readonly (Compare | Contains)[];
|
|
23
|
+
}
|
|
24
|
+
export type Condition = Compare | Contains | AnyOf;
|
|
25
|
+
/** What a select may name: the visible columns of its table and of the children it may join. */
|
|
26
|
+
export interface TableShape {
|
|
27
|
+
readonly table: string;
|
|
28
|
+
readonly visible: NameSet;
|
|
29
|
+
readonly joinable: Readonly<Record<string, JoinShape>>;
|
|
30
|
+
/** The `date` columns, which a read answers as `YYYY-MM-DD` text. */
|
|
31
|
+
readonly dates: NameSet;
|
|
32
|
+
/** The JSON columns, which a query selects but never compares or orders by. */
|
|
33
|
+
readonly json: NameSet;
|
|
34
|
+
}
|
|
35
|
+
export interface JoinShape {
|
|
36
|
+
readonly shape: TableShape;
|
|
37
|
+
/** The child's read actions: a select of the parent joins it only with one of them. */
|
|
38
|
+
readonly readActions: readonly string[];
|
|
39
|
+
/** The composite foreign key: child column = parent column, for each pair. */
|
|
40
|
+
readonly on: readonly {
|
|
41
|
+
readonly childColumn: string;
|
|
42
|
+
readonly parentColumn: string;
|
|
43
|
+
}[];
|
|
44
|
+
}
|
|
45
|
+
export interface JoinState {
|
|
46
|
+
readonly join: JoinShape;
|
|
47
|
+
readonly select: SelectState;
|
|
48
|
+
}
|
|
49
|
+
export interface SelectState {
|
|
50
|
+
/** Identifies the description a callback received, so it cannot return one from elsewhere. */
|
|
51
|
+
readonly origin: object;
|
|
52
|
+
readonly table: string;
|
|
53
|
+
readonly columns: readonly string[] | undefined;
|
|
54
|
+
readonly conditions: readonly Condition[];
|
|
55
|
+
readonly orderBy: readonly {
|
|
56
|
+
readonly column: string;
|
|
57
|
+
readonly direction: 'asc' | 'desc';
|
|
58
|
+
}[];
|
|
59
|
+
readonly limit: number | undefined;
|
|
60
|
+
readonly offset: number | undefined;
|
|
61
|
+
readonly joins: readonly JoinState[];
|
|
62
|
+
}
|
|
63
|
+
type Comparable<V> = Exclude<V, null | undefined>;
|
|
64
|
+
type Columns<Row> = keyof Row & string;
|
|
65
|
+
/** The columns a condition or an order may name: every column but a JSON one, whose read type is `unknown`. */
|
|
66
|
+
export type ComparableColumn<Row> = {
|
|
67
|
+
[K in Columns<Row>]: unknown extends Row[K] ? never : K;
|
|
68
|
+
}[Columns<Row>];
|
|
69
|
+
type StringColumn<Row> = {
|
|
70
|
+
[K in Columns<Row>]: Comparable<Row[K]> extends string ? K : never;
|
|
71
|
+
}[Columns<Row>];
|
|
72
|
+
type NullableColumn<Row> = {
|
|
73
|
+
[K in ComparableColumn<Row>]: null extends Row[K] ? K : never;
|
|
74
|
+
}[ComparableColumn<Row>];
|
|
75
|
+
/**
|
|
76
|
+
* A `[column, operator, value]` condition on `Row`'s columns. A null test (`is`, `is not`) takes a nullable column only;
|
|
77
|
+
* the runtime knows no column's nullability, so an untyped condition that tests a column never null runs and matches no
|
|
78
|
+
* row (`is`) or every row (`is not`).
|
|
79
|
+
*/
|
|
80
|
+
export type ConditionTuple<Row> = {
|
|
81
|
+
[K in ComparableColumn<Row>]: readonly [K, ComparisonOperator, Comparable<Row[K]>] | readonly [K, ListOperator, readonly Comparable<Row[K]>[]];
|
|
82
|
+
}[ComparableColumn<Row>] | {
|
|
83
|
+
[K in StringColumn<Row>]: readonly [K, PatternOperator | 'contains', string];
|
|
84
|
+
}[StringColumn<Row>] | {
|
|
85
|
+
[K in NullableColumn<Row>]: readonly [K, NullOperator, null];
|
|
86
|
+
}[NullableColumn<Row>];
|
|
87
|
+
type Prefixed<C extends string, J> = {
|
|
88
|
+
[K in keyof J & string as `${C}.${K}`]: J[K];
|
|
89
|
+
};
|
|
90
|
+
export type NoColumns = Record<never, never>;
|
|
91
|
+
/**
|
|
92
|
+
* The only query surface a store callback sees: a frozen description, not a Kysely builder. There is nothing to
|
|
93
|
+
* execute, no raw SQL entry point, and no way to name a table other than this one and its declared children.
|
|
94
|
+
*/
|
|
95
|
+
export declare class ScopedSelect<Row, Selected, Children> {
|
|
96
|
+
#private;
|
|
97
|
+
constructor(token: symbol, state: SelectState, shape: TableShape);
|
|
98
|
+
/** The columns to return; without it, every visible column. */
|
|
99
|
+
columns<const C extends Columns<Row>>(columns: readonly C[]): ScopedSelect<Row, Pick<Row, C>, Children>;
|
|
100
|
+
where<C extends ComparableColumn<Row>>(column: C, operator: ComparisonOperator, value: Comparable<Row[C]>): ScopedSelect<Row, Selected, Children>;
|
|
101
|
+
where<C extends ComparableColumn<Row>>(column: C, operator: ListOperator, values: readonly Comparable<Row[C]>[]): ScopedSelect<Row, Selected, Children>;
|
|
102
|
+
where<C extends StringColumn<Row>>(column: C, operator: PatternOperator, pattern: string): ScopedSelect<Row, Selected, Children>;
|
|
103
|
+
where<C extends NullableColumn<Row>>(column: C, operator: NullOperator, value: null): ScopedSelect<Row, Selected, Children>;
|
|
104
|
+
/** One OR group of conditions on this table's columns. */
|
|
105
|
+
whereAny(conditions: readonly ConditionTuple<Row>[]): ScopedSelect<Row, Selected, Children>;
|
|
106
|
+
/** A case-insensitive substring match in which `%` and `_` in the text match themselves. */
|
|
107
|
+
contains(column: StringColumn<Row>, text: string): ScopedSelect<Row, Selected, Children>;
|
|
108
|
+
orderBy(column: ComparableColumn<Row>, direction?: 'asc' | 'desc'): ScopedSelect<Row, Selected, Children>;
|
|
109
|
+
limit(limit: number): ScopedSelect<Row, Selected, Children>;
|
|
110
|
+
offset(offset: number): ScopedSelect<Row, Selected, Children>;
|
|
111
|
+
/** An inner join to a declared child; its columns come back prefixed (`note_comments.body`). */
|
|
112
|
+
join<C extends keyof Children & string, J>(child: C, build: (q: ScopedSelect<Children[C], Children[C], NoColumns>) => ScopedSelect<Children[C], J, NoColumns>): ScopedSelect<Row, Selected & Prefixed<C, J>, Children>;
|
|
113
|
+
}
|
|
114
|
+
/** A column the shape lets a query name; a hidden one is refused like a missing one. */
|
|
115
|
+
export declare function columnOf(shape: TableShape, name: unknown): string;
|
|
116
|
+
/** A column the shape lets a query compare, order or aggregate by: `columnOf`, and never a JSON column. */
|
|
117
|
+
export declare function comparableColumnOf(shape: TableShape, name: unknown, use: string): string;
|
|
118
|
+
/** One `[column, operator, value]` condition over a caller's values, checked before anything reads it further. */
|
|
119
|
+
export declare function parseCondition(shape: TableShape, column: unknown, operator: unknown, value: unknown): Compare | Contains;
|
|
120
|
+
export declare function startSelect<Row, Children>(shape: TableShape): ScopedSelect<Row, Row, Children>;
|
|
121
|
+
/** The description a callback returned; it must derive from the one it received. */
|
|
122
|
+
export declare function finishSelect<Row, Children>(built: unknown, start: ScopedSelect<Row, Row, Children>): SelectState;
|
|
123
|
+
export declare function isPrimitive(value: unknown): value is Primitive;
|
|
124
|
+
export {};
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Why the layer refused a capability, as a closed catalog: a server log names the reason and its fixed text, never the
|
|
3
|
+
* message, which can name a table, a dimension or a value.
|
|
4
|
+
*/
|
|
5
|
+
export declare const CAPABILITY_ERRORS: Readonly<{
|
|
6
|
+
wrong_capability: "the operation takes another capability than it was given: another kind (a Ref, a Within or an app capability) or dimension";
|
|
7
|
+
wrong_action: "the capability holds another action than the table names for the operation";
|
|
8
|
+
out_of_reach: "the table does not lie in the capability's dimension, or the capability does not reach it";
|
|
9
|
+
unenforceable: "the table carries no column for a dimension of the capability's chain, so the layer cannot enforce it";
|
|
10
|
+
undeclared_operation: "the table's policy declares no action for the operation, or handlers do not write the table";
|
|
11
|
+
table_kind: "the operation needs another kind of table (a dimension, a child or an application table)";
|
|
12
|
+
within_size: "a Within carries more scope values than one statement binds";
|
|
13
|
+
layer_unbound: "the data layer was used before the framework connected it at boot, or connected a second time";
|
|
14
|
+
}>;
|
|
15
|
+
export type CapabilityErrorReason = keyof typeof CAPABILITY_ERRORS;
|
|
16
|
+
/** A capability of the wrong kind, dimension or action for the operation, or the layer used outside its binding. */
|
|
17
|
+
export declare class CapabilityError extends Error {
|
|
18
|
+
name: string;
|
|
19
|
+
/** What a log may say; the message is for the author's own run. */
|
|
20
|
+
readonly reason: CapabilityErrorReason;
|
|
21
|
+
constructor(reason: CapabilityErrorReason, message: string);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Why the layer refused, as a closed catalog: a server log names the reason and its fixed text, never the message,
|
|
25
|
+
* which can quote a value the request sent. Each text is one string literal, however long: a concatenation keeps the
|
|
26
|
+
* whole catalog in an SPA bundle that imports this module (`web/__tests__/bundle.test.ts`).
|
|
27
|
+
*/
|
|
28
|
+
export declare const QUERY_REFUSALS: {
|
|
29
|
+
readonly invalid_argument: "a data-layer call took an argument of another shape than its type declares";
|
|
30
|
+
readonly invalid_value: "a value cannot be bound: undefined, a function, symbol or object of a kind the database does not bind, a list item other than a string or finite number, a date or timestamp that is neither a valid Date nor well-formed text or lies out of range, a non-finite number, or on SQLite a bigint outside the safe integer range";
|
|
31
|
+
readonly invalid_name: "a configured name (application, schema, table or file column) breaks its naming rule";
|
|
32
|
+
readonly unknown_column: "a column the table does not declare, or one this operation may not set";
|
|
33
|
+
readonly unknown_table: "a table src/db/tables.ts does not declare, or one no store, join or locator reaches";
|
|
34
|
+
readonly framework_table: "a framework table or a catalog name: application data reaches only the application's own tables";
|
|
35
|
+
readonly row_address: "a row is addressed otherwise than its table declares (its Ref, its parent Ref and key, or its key)";
|
|
36
|
+
readonly version_required: "the table has a version column, so update and remove take expectVersion";
|
|
37
|
+
readonly read_only: "a read-only request, query or table was asked to write or lock a row";
|
|
38
|
+
readonly ended: "the transaction, invocation or handle has ended or belongs to other work";
|
|
39
|
+
readonly statement_failed: "a statement of this transaction failed, so nothing it wrote commits";
|
|
40
|
+
readonly unexpected_row: "the database answered a row in another shape than its migration declares";
|
|
41
|
+
readonly statement_shape: "the compiled statement has a form the guard does not admit";
|
|
42
|
+
readonly scope_predicate: "the scope predicate is missing or is not a top-level conjunct";
|
|
43
|
+
readonly list_page: "a list limit, cursor or order outside what a page admits";
|
|
44
|
+
readonly undeclared_reach: "the site reaches data it did not declare (sql or unscoped, or their mode)";
|
|
45
|
+
readonly sql_template: "ctx.sql was not called as one tagged template whose values are interpolated whole";
|
|
46
|
+
readonly sql_statement: "ctx.sql runs one statement of an admitted kind, and none inside another";
|
|
47
|
+
readonly sql_read_only: "sql: 'read' runs queries only; declare sql: 'write' on a mutation, a job or a webhook";
|
|
48
|
+
readonly sql_name: "ctx.sql names a framework (fabrika_*), catalog (pg_*, sqlite_*) or foreign-schema object, or writes a name in U& form";
|
|
49
|
+
readonly sql_function: "ctx.sql calls a function that is not a reviewed built-in";
|
|
50
|
+
readonly sql_outside_transaction: "a job runs ctx.sql only inside ctx.transaction((tx) => …), as tx.sql";
|
|
51
|
+
readonly sql_scope: "ctx.sql.scope(…) is interpolated at code level in a statement of its own invocation and site";
|
|
52
|
+
readonly file_scan: "a scan verdict outside the scan job's lease";
|
|
53
|
+
readonly claim_site: "claim leases rows in a transaction of its own: a job calls it on ctx.db, never inside ctx.transaction or a request";
|
|
54
|
+
readonly on_conflict: "an insert's onConflict names no column of the table, a column the insert leaves empty, or another action";
|
|
55
|
+
readonly public_reach: "a public procedure updates, removes, locks or claims no row, and an insert's conflict writes nothing";
|
|
56
|
+
readonly file_value: "a file column takes a file reference this request holds and may read (an incoming file, a put, or a read under the table's read action) of the column's kind, or null";
|
|
57
|
+
readonly file_scope: "a file is referenced from another top-level scope than the one its first reference set";
|
|
58
|
+
};
|
|
59
|
+
export type QueryRefusal = keyof typeof QUERY_REFUSALS;
|
|
60
|
+
/** A query description or an operation tree outside what the layer allows. */
|
|
61
|
+
export declare class QueryRefusedError extends Error {
|
|
62
|
+
name: string;
|
|
63
|
+
/** What a log may say; the message is for the author's own run and may quote a value. */
|
|
64
|
+
readonly reason: QueryRefusal;
|
|
65
|
+
constructor(reason: QueryRefusal, message: string);
|
|
66
|
+
}
|
|
67
|
+
/** A `ctx.sql` row its row schema refuses: the handler's bug. The message names the column, never its value. */
|
|
68
|
+
export declare class SqlRowError extends Error {
|
|
69
|
+
name: string;
|
|
70
|
+
}
|
|
71
|
+
/** A table policy that cannot be enforced as written. */
|
|
72
|
+
export declare class PolicyError extends Error {
|
|
73
|
+
name: string;
|
|
74
|
+
}
|
|
75
|
+
/** The addressed object does not exist in the capability's scope (HTTP 404). */
|
|
76
|
+
export declare class NotFoundError extends Error {
|
|
77
|
+
name: string;
|
|
78
|
+
}
|
|
79
|
+
/** The object exists, but its version is not the one the caller expected (HTTP 409). */
|
|
80
|
+
export declare class ConflictError extends Error {
|
|
81
|
+
name: string;
|
|
82
|
+
}
|
|
83
|
+
/** A job attempt wrote after it lost its lease (timed out, taken over or expired); nothing was written. */
|
|
84
|
+
export declare class LeaseLostError extends Error {
|
|
85
|
+
name: string;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* A statement the database refused or could not run, as the layer reports it: its SQLSTATE only. The driver's message,
|
|
89
|
+
* detail and hint can quote the whole failing row, columns the capability may not read included, so none is kept.
|
|
90
|
+
*/
|
|
91
|
+
export declare class DatabaseError extends Error {
|
|
92
|
+
name: string;
|
|
93
|
+
/** The five-character SQLSTATE, or `null` when the statement never reached the database (a lost connection). */
|
|
94
|
+
readonly sqlState: string | null;
|
|
95
|
+
constructor(sqlState: string | null);
|
|
96
|
+
}
|
|
97
|
+
/** The driver's failure as a `DatabaseError`; the driver's error object goes no further. */
|
|
98
|
+
export declare function databaseError(error: unknown): DatabaseError;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { type NameSet } from './builtins';
|
|
2
|
+
import { type Session } from './database';
|
|
3
|
+
import type { Primitive } from './description';
|
|
4
|
+
import type { TableModel } from './policy';
|
|
5
|
+
/** Where a site's file references belong, and what claiming a pending one needs. */
|
|
6
|
+
export interface FileBinding {
|
|
7
|
+
/** The work a reference of this site belongs to: one request, or one job attempt. */
|
|
8
|
+
readonly holder: object;
|
|
9
|
+
/** The id a pending file must carry to be claimed here: the request (or attempt) that staged it. */
|
|
10
|
+
readonly request: string;
|
|
11
|
+
/** How long a pending file may wait for its first reference. */
|
|
12
|
+
readonly graceMs: number;
|
|
13
|
+
/** Whether files of `kind` wait for a clean scan: a claimed one is `quarantined` until then. */
|
|
14
|
+
scans(kind: string): boolean;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Puts a file reference in each file column of `rows`, read with `action` in `session`: one statement loads every
|
|
18
|
+
* file's metadata. Without a binding (a store's own runner) the columns keep their ids. `held` (a queue claim's) adds
|
|
19
|
+
* the actions the site holds over the whole application: a reference is readable when one of them reads the table.
|
|
20
|
+
*/
|
|
21
|
+
export declare function answerFiles<R>(model: TableModel, binding: FileBinding | undefined, session: Session, rows: R[], action: string, held?: NameSet): Promise<R[]>;
|
|
22
|
+
/**
|
|
23
|
+
* A write's value for a file column: `null`, or the id of a file reference of the column's kind. A string id, a copied
|
|
24
|
+
* object or a reference of another kind is refused; whether the site holds it is checked where the write runs.
|
|
25
|
+
*/
|
|
26
|
+
export declare function fileValue(model: TableModel, column: string, value: unknown): string | null;
|
|
27
|
+
/**
|
|
28
|
+
* The top-level scope a row of `model` lies in, from the values it is written with: `<dimension>:<value>`, or `null` for
|
|
29
|
+
* an application table, which sets no scope.
|
|
30
|
+
*/
|
|
31
|
+
export declare function rowScope(model: TableModel, values: ReadonlyMap<string, Primitive | null>): string | null | undefined;
|
|
32
|
+
/** The top-level scope of the row whose key is `key`, read in `session`: what an update's file references are stamped with. */
|
|
33
|
+
export declare function storedRowScope(model: TableModel, session: Session, key: string, keyValue: string): Promise<string | null>;
|
|
34
|
+
/**
|
|
35
|
+
* The file references of a write, in `session` (its transaction), after its statement: each file is locked `for share`
|
|
36
|
+
* while its state is checked, a pending one this request staged within the grace is claimed, and the first reference
|
|
37
|
+
* from a dimensional table stamps its scope; a reference from another top-level scope is refused.
|
|
38
|
+
*/
|
|
39
|
+
export declare function referenceFiles(model: TableModel, binding: FileBinding | undefined, session: Session, values: ReadonlyMap<string, Primitive | null>, scope: () => Promise<string | null>): Promise<void>;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { StoredFileMeta, StoredFileStatus } from './stored-files';
|
|
2
|
+
/** What a handler sees of a stored file: its metadata, never its object. */
|
|
3
|
+
export interface FileReference {
|
|
4
|
+
readonly id: string;
|
|
5
|
+
readonly name: string | null;
|
|
6
|
+
readonly type: string;
|
|
7
|
+
readonly size: number;
|
|
8
|
+
readonly status: StoredFileStatus;
|
|
9
|
+
}
|
|
10
|
+
/** Whose a reference is, and what it lets that work do. */
|
|
11
|
+
export interface FileHolding {
|
|
12
|
+
readonly id: string;
|
|
13
|
+
readonly kind: string;
|
|
14
|
+
/** The work that holds it: one request, or one job attempt. */
|
|
15
|
+
readonly holder: object;
|
|
16
|
+
/** Read under an action that reads the file (or received, or put): it may be written into a column, served and opened. */
|
|
17
|
+
readonly readable: boolean;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* A reference to a file for `holder`; only `@fabrika/db` and the framework mint one. A `readable` one also lets its
|
|
21
|
+
* holder write the file into a column, serve it and open it, through any reference to the same file.
|
|
22
|
+
*/
|
|
23
|
+
export declare function fileReference(meta: StoredFileMeta, holder: object, readable: boolean): FileReference;
|
|
24
|
+
/** What a reference holds, or `undefined` for any other value: a structural copy, a string id or a forged object. */
|
|
25
|
+
export declare function fileHoldingOf(value: unknown): FileHolding | undefined;
|
|
26
|
+
/** Whether `holder` received, put, or read under a read action the file `id`. */
|
|
27
|
+
export declare function holdsFile(holder: object, id: string): boolean;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { type Session } from './database';
|
|
2
|
+
import type { DataLayerRuntime } from './store';
|
|
3
|
+
/** One webhook binding: the scope value it reaches, the provider account it accepts, and its signing secret. */
|
|
4
|
+
export interface WebhookBinding {
|
|
5
|
+
readonly id: string;
|
|
6
|
+
/** The webhook mount's name. */
|
|
7
|
+
readonly webhook: string;
|
|
8
|
+
readonly scope: {
|
|
9
|
+
readonly type: string;
|
|
10
|
+
readonly value: string;
|
|
11
|
+
};
|
|
12
|
+
/** The provider account: for GitHub, `installation:<id>` or `repository:<id>`, which a delivery must name. */
|
|
13
|
+
readonly account: string;
|
|
14
|
+
readonly secret: string;
|
|
15
|
+
}
|
|
16
|
+
export interface NewWebhookBinding extends WebhookBinding {
|
|
17
|
+
readonly createdBy: string;
|
|
18
|
+
readonly createdAt: number;
|
|
19
|
+
}
|
|
20
|
+
export interface WebhookTables {
|
|
21
|
+
/** Records a binding, in `using` when given: the transaction of the procedure that binds. */
|
|
22
|
+
createBinding(binding: NewWebhookBinding, using?: Session): Promise<void>;
|
|
23
|
+
/** The binding `id` of `webhook`, or `null`. */
|
|
24
|
+
binding(webhook: string, id: string): Promise<WebhookBinding | null>;
|
|
25
|
+
/**
|
|
26
|
+
* Replaces the secret of the binding when it belongs to `webhook` and `scope`, in `using` when given: a provider that
|
|
27
|
+
* issues its own signing secret shows it only after the binding's URL is registered. `false` when there was none.
|
|
28
|
+
*/
|
|
29
|
+
replaceSecret(webhook: string, id: string, scope: {
|
|
30
|
+
readonly type: string;
|
|
31
|
+
readonly value: string;
|
|
32
|
+
}, secret: string, using?: Session): Promise<boolean>;
|
|
33
|
+
/** Removes the binding when it belongs to `webhook` and `scope`, in `using` when given; `false` when there was none. */
|
|
34
|
+
removeBinding(webhook: string, id: string, scope: {
|
|
35
|
+
readonly type: string;
|
|
36
|
+
readonly value: string;
|
|
37
|
+
}, using?: Session): Promise<boolean>;
|
|
38
|
+
/**
|
|
39
|
+
* Claims a delivery key for `leaseMs`: `null` when the binding completed it within the retention window, or another
|
|
40
|
+
* copy holds an unexpired claim. A claim whose lease expired is taken over. Keys of the binding older than
|
|
41
|
+
* `retainedSince` are dropped first, so the table stays bounded.
|
|
42
|
+
*/
|
|
43
|
+
claimDelivery(bindingId: string, deliveryKey: string, now: number, leaseMs: number, retainedSince: number): Promise<DeliveryClaim | null>;
|
|
44
|
+
/**
|
|
45
|
+
* Completes a claimed delivery in `using`, the transaction its handler wrote in, so the work and the completion
|
|
46
|
+
* commit together; `false` when another copy took the claim over meanwhile, and that transaction must not commit.
|
|
47
|
+
* Without `using` (a handler with no transaction) it completes on its own.
|
|
48
|
+
*/
|
|
49
|
+
completeDelivery(claim: DeliveryClaim, using?: Session): Promise<boolean>;
|
|
50
|
+
/** Gives a claim back after its handler failed, so the provider's redelivery runs it again at once. */
|
|
51
|
+
releaseDelivery(claim: DeliveryClaim): Promise<void>;
|
|
52
|
+
}
|
|
53
|
+
/** One copy's claim on a delivery: its lease end fences the completion against a copy that took the claim over. */
|
|
54
|
+
export interface DeliveryClaim {
|
|
55
|
+
readonly bindingId: string;
|
|
56
|
+
readonly key: string;
|
|
57
|
+
readonly leaseUntil: number;
|
|
58
|
+
}
|
|
59
|
+
export declare function webhookTables(runtime: () => DataLayerRuntime): WebhookTables;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { DataAccess } from './orm';
|
|
2
|
+
import { type FrameworkTables, type Locator, type ProvisionalBinding } from './registry';
|
|
3
|
+
import type { DataLayerRuntime } from './store';
|
|
4
|
+
export type { Admissions } from './admissions';
|
|
5
|
+
export type { LeaseFence } from './capability';
|
|
6
|
+
export type { InvocationTransaction, Session } from './database';
|
|
7
|
+
export { CAPABILITY_ERRORS, CapabilityError, type CapabilityErrorReason, LeaseLostError, QUERY_REFUSALS, type QueryRefusal, QueryRefusedError, } from './errors';
|
|
8
|
+
export type { FileBinding } from './file-columns';
|
|
9
|
+
export { type FileHolding, fileHoldingOf, type FileReference, fileReference, holdsFile } from './file-references';
|
|
10
|
+
export type { DeliveryClaim, NewWebhookBinding, WebhookBinding, WebhookTables } from './framework-tables';
|
|
11
|
+
export type { JobLeases, JobLeaseState, JobOutcome, LeaseChange, LeaseProjection, OverdueClaim } from './job-leases';
|
|
12
|
+
export type { FinishWakes, JobWakes, NewWake, Wake, WakeReason } from './job-wakes';
|
|
13
|
+
export type { DataAccess, DataHandles, DataSite } from './orm';
|
|
14
|
+
export type { FrameworkTables, Locator, ProvisionalBinding } from './registry';
|
|
15
|
+
export type { SqlMode, SqlSite, SqlTag, SqlValue } from './sql';
|
|
16
|
+
export type { FileColumnRef, QuarantinedStoredFile, ServedFile, StagedFile, StoredFileMeta, StoredFiles, StoredFileStatus } from './stored-files';
|
|
17
|
+
/** Whether `value` is an object `scoped(tables)` created, rather than a look-alike with the same fields. */
|
|
18
|
+
export declare function isScopedDatabase(value: unknown): boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Connects the layer `scoped(tables)` created to `runtime`, once, and answers its locator, which nothing else can
|
|
21
|
+
* reach. Throws for any other value and for a layer that is already connected.
|
|
22
|
+
*/
|
|
23
|
+
export declare function bindScopedDatabase(value: unknown, runtime: DataLayerRuntime): Locator;
|
|
24
|
+
/**
|
|
25
|
+
* Connects the layer like `bindScopedDatabase`, and answers with its locator the `release` that disconnects it again:
|
|
26
|
+
* a boot that fails releases the layer, so a later boot of the same application binds it. Throws as that does.
|
|
27
|
+
*/
|
|
28
|
+
export declare function bindScopedDatabaseProvisionally(value: unknown, runtime: DataLayerRuntime): ProvisionalBinding;
|
|
29
|
+
/**
|
|
30
|
+
* The framework's own writes over the layer `scoped(tables)` created: the webhook tables, the job leases and wakes, and
|
|
31
|
+
* the admission counters. Each operation needs the layer bound first. Throws for any other value.
|
|
32
|
+
*/
|
|
33
|
+
export declare function frameworkTablesOf(value: unknown): FrameworkTables;
|
|
34
|
+
/**
|
|
35
|
+
* The mini-ORM of the layer `scoped(tables)` created: the invocation transactions and the `ctx.db` handles the
|
|
36
|
+
* framework builds per site. Each operation needs the layer bound first. Throws for any other value.
|
|
37
|
+
*/
|
|
38
|
+
export declare function dataAccessOf(value: unknown): DataAccess;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { OperationNode } from 'kysely';
|
|
2
|
+
export type RootKind = 'SelectQueryNode' | 'InsertQueryNode' | 'UpdateQueryNode' | 'DeleteQueryNode';
|
|
3
|
+
export interface ForeignKeyPair {
|
|
4
|
+
readonly childColumn: string;
|
|
5
|
+
readonly parentColumn: string;
|
|
6
|
+
}
|
|
7
|
+
export interface GuardContext {
|
|
8
|
+
readonly root: RootKind;
|
|
9
|
+
readonly table: string;
|
|
10
|
+
/** The children a select may join, each with the composite foreign key its ON must equal. */
|
|
11
|
+
readonly joins: Readonly<Record<string, readonly ForeignKeyPair[]>>;
|
|
12
|
+
/** The scope predicate; it must be the WHERE itself or its top-level right conjunct. Absent for an insert only. */
|
|
13
|
+
readonly scope: OperationNode | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* A select that locks its rows: `share` is the layer's check of a mutation's destination, `update` a mutation's
|
|
16
|
+
* `getForUpdate` of its own target, `claim` a queue claim's `for update skip locked`. No other lock is admitted.
|
|
17
|
+
*/
|
|
18
|
+
readonly lock?: 'share' | 'update' | 'claim';
|
|
19
|
+
/** An insert that does nothing on a conflict of its listed columns: `on conflict (columns) do nothing`, and no other. */
|
|
20
|
+
readonly onConflict?: 'nothing';
|
|
21
|
+
}
|
|
22
|
+
/** Refuses any operation tree outside the allowlist. Runs on the final tree, immediately before it is compiled. */
|
|
23
|
+
export declare function assertAllowedTree(root: OperationNode, context: GuardContext): void;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** `^[a-z_][a-z0-9_]*$`, at most 63 characters (Postgres truncates longer names). */
|
|
2
|
+
export declare function isPlainIdentifier(value: unknown): value is string;
|
|
3
|
+
/** A plain identifier, or two joined by one dot (`note_comments.body`, the alias of a joined column). */
|
|
4
|
+
export declare function isPlainAlias(value: unknown): value is string;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export type { AppCapability, AppGrant, CapabilityAuthority, CapabilityGrant, ContainerGrant, LeaseFence, Ref, RefGrant, RowGrant, RowRef, Within, WithinGrant, } from './capability';
|
|
2
|
+
export { MAX_WITHIN_VALUES } from './capability';
|
|
3
|
+
export { applicationSchema, Database, type DatabaseEngine, type Executor, type SqlDialect, type TransactionOptions, type TransactionRunner, } from './database';
|
|
4
|
+
export type { AllowedOperator, ComparisonOperator, ConditionTuple, ListOperator, NoColumns, NullOperator, PatternOperator, Primitive, ScopedSelect, } from './description';
|
|
5
|
+
export { CAPABILITY_ERRORS, CapabilityError, type CapabilityErrorReason, DatabaseError, LeaseLostError, PolicyError, QUERY_REFUSALS, type QueryRefusal, QueryRefusedError, } from './errors';
|
|
6
|
+
export type { JsonColumnValue, JsonValue } from './json';
|
|
7
|
+
export type { AnyDbIndex, ChildRows, ContainedRows, CountOptions, DataKind, DbIndex, IndexedTable, ListOptions, ListPage, NoUnscoped, OrderTerm, OwnerRow, ReadOnlyUnscoped, ReferenceRows, ReferenceTables, RowFor, Saved, ScopeTarget, TargetForm, TargetRow, Unavailable, UnownedTarget, UnscopedDeclaration, UnscopedInsert, UnscopedMode, UnscopedRows, UnscopedTables, VersionArguments, WithinRows, WithinTables, } from './orm-types';
|
|
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
|
+
export { type AggregateRow, type AggregateSpec, type ChildListScope, type DataLayer, type DataLayerRuntime, scoped, type ScopedDatabase, } from './store';
|
|
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';
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import type { LeaseFence } from './capability';
|
|
2
|
+
import { type Session, type SqlDialect } from './database';
|
|
3
|
+
import { type FinishWakes } from './job-wakes';
|
|
4
|
+
import type { DataLayerRuntime } from './store';
|
|
5
|
+
export type JobOutcome = 'succeeded' | 'failed';
|
|
6
|
+
export interface JobLeaseState {
|
|
7
|
+
readonly generation: number;
|
|
8
|
+
/** The attempt holding the lease, or `null` when it is free. */
|
|
9
|
+
readonly holder: string | null;
|
|
10
|
+
readonly expiresAt: Date;
|
|
11
|
+
/** The holding attempt's deadline: no write of it commits after this. */
|
|
12
|
+
readonly attemptDeadline: Date;
|
|
13
|
+
/** When the job was first seen in this schema: the baseline of a job that never succeeded. */
|
|
14
|
+
readonly firstSeenAt: Date;
|
|
15
|
+
readonly lastStartedAt: Date | null;
|
|
16
|
+
readonly lastFinishedAt: Date | null;
|
|
17
|
+
readonly lastStatus: JobOutcome | null;
|
|
18
|
+
readonly lastSucceededAt: Date | null;
|
|
19
|
+
readonly lastDurationMs: number | null;
|
|
20
|
+
readonly lastMissedReportedAt: Date | null;
|
|
21
|
+
}
|
|
22
|
+
/** A job overdue by twice its period, claimed for one report. */
|
|
23
|
+
export interface OverdueClaim {
|
|
24
|
+
readonly lastSucceededAt: Date | null;
|
|
25
|
+
readonly firstSeenAt: Date;
|
|
26
|
+
}
|
|
27
|
+
export interface JobLeases {
|
|
28
|
+
/** The database's clock. */
|
|
29
|
+
now(): Promise<Date>;
|
|
30
|
+
/** Records that the job exists (its first-seen time), once; later calls change nothing. */
|
|
31
|
+
register(job: string): Promise<void>;
|
|
32
|
+
/**
|
|
33
|
+
* Frees a lease whose holder never finished before it expired (the run crashed) and records the run as failed.
|
|
34
|
+
* `true` for the one caller that freed it.
|
|
35
|
+
*/
|
|
36
|
+
reapCrashed(job: string): Promise<boolean>;
|
|
37
|
+
/**
|
|
38
|
+
* A free lease for `holder`, under a new generation, for `leaseMs`; its attempt may write for `attemptMs`. `null`
|
|
39
|
+
* while another run holds it.
|
|
40
|
+
*/
|
|
41
|
+
acquire(job: string, holder: string, attemptMs: number, leaseMs: number): Promise<LeaseFence | null>;
|
|
42
|
+
/**
|
|
43
|
+
* `acquire` of a registered job that never waits for a lock: `null` at once while another run holds the lease or an
|
|
44
|
+
* attempt's transaction holds its row. A kick takes the lease only this way, on a connection requests share.
|
|
45
|
+
*/
|
|
46
|
+
tryAcquire(job: string, holder: string, attemptMs: number, leaseMs: number): Promise<LeaseFence | null>;
|
|
47
|
+
/**
|
|
48
|
+
* Hands a live lease to the next attempt under a new generation, so no write of the previous attempt commits from
|
|
49
|
+
* now on. `null` when `fence` no longer holds it.
|
|
50
|
+
*/
|
|
51
|
+
renew(fence: LeaseFence, holder: string, attemptMs: number, leaseMs: number): Promise<LeaseFence | null>;
|
|
52
|
+
/**
|
|
53
|
+
* Frees the lease and records the run's outcome and duration, and in the same transaction applies `wakes` to the wakes
|
|
54
|
+
* the run owned (`job-wakes.ts`). `false` when `fence` no longer held it: then nothing is applied.
|
|
55
|
+
*/
|
|
56
|
+
finish(fence: LeaseFence, outcome: JobOutcome, wakes?: FinishWakes): Promise<boolean>;
|
|
57
|
+
/** Records that a kick arrived: the run that started before it runs the job once more. Never waits on the lease. */
|
|
58
|
+
markPending(job: string): Promise<void>;
|
|
59
|
+
/**
|
|
60
|
+
* Clears the flag `markPending` set; `true` for the one caller that cleared it. A run clears it when it starts (it
|
|
61
|
+
* covers every kick before it) and again after it succeeded, when `true` means one more run.
|
|
62
|
+
*/
|
|
63
|
+
takePending(job: string): Promise<boolean>;
|
|
64
|
+
/**
|
|
65
|
+
* Claims one report of a job that has not succeeded within twice `periodMs`, at most once per period: only the
|
|
66
|
+
* caller that gets the claim reports.
|
|
67
|
+
*/
|
|
68
|
+
claimOverdue(job: string, periodMs: number): Promise<OverdueClaim | null>;
|
|
69
|
+
/**
|
|
70
|
+
* Refuses every further write of the attempt `fence` names and cancels the statements it still has running; the
|
|
71
|
+
* runner calls it when the attempt ends.
|
|
72
|
+
*/
|
|
73
|
+
cancelInFlight(fence: LeaseFence): Promise<void>;
|
|
74
|
+
state(job: string): Promise<JobLeaseState | null>;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* What a lease statement changed: a crashed holder reaped, a lease taken or handed on, or a run finished with what its
|
|
78
|
+
* wakes become (`null` leaves them).
|
|
79
|
+
*/
|
|
80
|
+
export type LeaseChange = {
|
|
81
|
+
readonly kind: 'reaped' | 'acquired' | 'renewed';
|
|
82
|
+
} | {
|
|
83
|
+
readonly kind: 'finished';
|
|
84
|
+
readonly wakes: FinishWakes | null;
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* Runs in the transaction of every lease statement that changed the lease, so a table that follows the lease (the
|
|
88
|
+
* wakes, `job-wakes.ts`) commits with it or not at all. A statement that changed nothing, a lost lease's, runs none.
|
|
89
|
+
*/
|
|
90
|
+
export type LeaseProjection = (transaction: Session, job: string, change: LeaseChange) => Promise<void>;
|
|
91
|
+
/** One framework statement in each dialect's text; a session runs the text of its own. */
|
|
92
|
+
export type DialectTexts = Readonly<Record<SqlDialect, string>>;
|
|
93
|
+
/** SQLite's clock, as a stored timestamp: ISO text, so a comparison of two is a text comparison. */
|
|
94
|
+
export declare const SQLITE_NOW = "strftime('%Y-%m-%dT%H:%M:%fZ', 'now')";
|
|
95
|
+
/** SQLite's clock moved by `ms`, an SQL expression of milliseconds (negative is earlier). */
|
|
96
|
+
export declare function sqliteNowPlus(ms: string): string;
|
|
97
|
+
/** A stored SQLite timestamp (`SQLITE_NOW_FORMAT`) as a `Date`, or `null`; anything else is refused. */
|
|
98
|
+
export declare function sqliteTimestamp(value: unknown, what: string): Date | null;
|
|
99
|
+
/**
|
|
100
|
+
* The database's clock inside `session`'s transaction: a queue claim compares and moves `due` by it, so no process
|
|
101
|
+
* clock decides when a row is due.
|
|
102
|
+
*/
|
|
103
|
+
export declare function databaseNow(session: Session): Promise<Date>;
|
|
104
|
+
/**
|
|
105
|
+
* Runs `work` in a mutation's transaction under `fence`. Before it: the attempt still holds its lease, the lease row
|
|
106
|
+
* stays locked `for share` until the commit (a takeover or a renewal waits), and every statement is bounded by the
|
|
107
|
+
* attempt's remaining time. After it, just before the commit: the attempt still holds the lease within its deadline.
|
|
108
|
+
* Otherwise the transaction rolls back with `LeaseLostError`; so does a statement the deadline or the runner cancelled.
|
|
109
|
+
* On SQLite only the two checks run: no other event reaches the Durable Object while the transaction is open.
|
|
110
|
+
*/
|
|
111
|
+
export declare function fencedTransaction<T>(session: Session, fence: LeaseFence, work: (transaction: Session) => Promise<T>): Promise<T>;
|
|
112
|
+
export declare function jobLeases(runtime: () => DataLayerRuntime, project: LeaseProjection): JobLeases;
|