@mistriwork/engine 1.0.0-rc.10 → 1.0.0-rc.11
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/dist/adapters/adapter.d.ts +33 -0
- package/dist/adapters/postgres.d.ts +21 -16
- package/dist/adapters/postgres.js +174 -223
- package/dist/adapters/postgresCatalog.d.ts +12 -1
- package/dist/adapters/postgresCatalog.js +57 -23
- package/dist/adapters/postgresCatalogDigest.js +40 -34
- package/dist/adapters/postgresCatalogRows.d.ts +2 -0
- package/dist/adapters/postgresWriteAhead.d.ts +47 -0
- package/dist/adapters/postgresWriteAhead.js +209 -0
- package/dist/adapters/scoped/keyRows.js +9 -3
- package/dist/adapters/scoped/keyTypes.d.ts +34 -0
- package/dist/adapters/scoped/keyTypes.js +66 -0
- package/dist/adapters/scoped/postgresScopeCatalog.d.ts +18 -0
- package/dist/adapters/scoped/postgresScopeCatalog.js +353 -13
- package/dist/adapters/scoped/postgresTriggers.d.ts +137 -0
- package/dist/adapters/scoped/postgresTriggers.js +1545 -0
- package/dist/adapters/scoped/scopeTriggers.d.ts +122 -0
- package/dist/adapters/scoped/scopeTriggers.js +131 -0
- package/dist/adapters/scoped/scopedReads.d.ts +3 -1
- package/dist/adapters/scoped/scopedReads.js +23 -5
- package/dist/adapters/scoped/scopedTransaction.d.ts +76 -6
- package/dist/adapters/scoped/scopedTransaction.js +278 -34
- package/dist/adapters/scoped/sqliteScopeCatalog.d.ts +17 -1
- package/dist/adapters/scoped/sqliteScopeCatalog.js +197 -18
- package/dist/adapters/scoped/sqliteTriggerSession.d.ts +64 -0
- package/dist/adapters/scoped/sqliteTriggerSession.js +245 -0
- package/dist/adapters/scoped/sqliteTriggers.d.ts +20 -0
- package/dist/adapters/scoped/sqliteTriggers.js +822 -0
- package/dist/adapters/scoped/storedText.d.ts +45 -0
- package/dist/adapters/scoped/storedText.js +61 -0
- package/dist/adapters/scoped/triggerEffects.d.ts +98 -0
- package/dist/adapters/scoped/triggerEffects.js +189 -0
- package/dist/adapters/scoped/triggerProof.d.ts +124 -0
- package/dist/adapters/scoped/triggerProof.js +366 -0
- package/dist/adapters/snapshotRead.d.ts +8 -0
- package/dist/adapters/snapshotRead.js +37 -0
- package/dist/adapters/sqlite.d.ts +13 -2
- package/dist/adapters/sqlite.js +139 -16
- package/dist/adapters/sqliteDecimals.d.ts +2 -2
- package/dist/adapters/sqliteDecimals.js +2 -8
- package/dist/adapters/storeHost.d.ts +4 -1
- package/dist/adapters/writeCapture.d.ts +7 -0
- package/dist/server/cli/args.d.ts +6 -0
- package/dist/server/cli/args.js +6 -0
- package/dist/server/columnGrants.d.ts +3 -0
- package/dist/server/columnGrants.js +12 -1
- package/dist/server/config/profile.d.ts +123 -0
- package/dist/server/config/profile.js +236 -0
- package/dist/server/connections.d.ts +5 -0
- package/dist/server/connections.js +11 -1
- package/dist/server/hiddenColumns.js +6 -0
- package/dist/server/http/refusalReport.js +16 -4
- package/dist/server/migrate/cli.d.ts +50 -3
- package/dist/server/migrate/cli.js +199 -14
- package/dist/server/migrate/runner.js +5 -1
- package/dist/server/shapeonly.js +5 -0
- package/dist/shared/aggregate.d.ts +49 -0
- package/dist/shared/aggregate.js +107 -0
- package/dist/shared/breakdown.js +17 -4
- package/dist/shared/decimals.d.ts +37 -0
- package/dist/shared/decimals.js +148 -0
- package/dist/shared/displayDefaults.js +15 -1
- package/dist/shared/filter.d.ts +22 -1
- package/dist/shared/filter.js +36 -9
- package/dist/shared/money.d.ts +9 -0
- package/dist/shared/money.js +17 -0
- package/dist/shared/pageModel.d.ts +1 -1
- package/dist/shared/refusal.d.ts +16 -0
- package/dist/shared/refusal.js +40 -0
- package/dist/shared/schema.d.ts +6 -0
- package/dist/shared/scopeCatalog.js +4 -0
- package/dist/shared/servedApp.d.ts +7 -2
- package/package.json +5 -1
|
@@ -1,7 +1,63 @@
|
|
|
1
1
|
import { Refusal } from '../adapter.js';
|
|
2
|
+
import { buildWhere } from '../../shared/filter.js';
|
|
2
3
|
import { recordRefusalCause } from '../../shared/refusal.js';
|
|
4
|
+
import { coerceForColumn, columnKind } from '../../shared/schema.js';
|
|
5
|
+
import { blockingTriggers, firedBy, sayRuns, sayTriggers, sayWrite } from './scopeTriggers.js';
|
|
6
|
+
import { detachStoredText, overlaid, storedText, storedTextOf } from './storedText.js';
|
|
7
|
+
/** A foreign key's name in ScopeCatalog.references: its table, name and columns, each side. */
|
|
8
|
+
export const referenceKey = (fk) => JSON.stringify([fk.from.table, fk.name, fk.from.columns, fk.to.table, fk.to.columns]);
|
|
9
|
+
/** Whether a failure is the database's foreign key refusing a write: SQLite's SQLITE_CONSTRAINT_FOREIGNKEY, PostgreSQL's 23503. */
|
|
10
|
+
export const isForeignKeyViolation = (error) => {
|
|
11
|
+
const { code, errcode } = (error ?? {});
|
|
12
|
+
return errcode === 787 || code === '23503';
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* A foreign key's refusal of a scoped write, said as a hidden parent's is: the database's own error says whether the
|
|
16
|
+
* row it points at exists (and, on PostgreSQL, its key, a default's part of it included), which the principal's rows
|
|
17
|
+
* may hide. Whichever statement it came from, at the statement or at COMMIT for a deferred one, it is the one
|
|
18
|
+
* refusal; what the operator reads names the constraint and nothing of the row.
|
|
19
|
+
*/
|
|
20
|
+
export const foreignKeyRefusal = (error) => {
|
|
21
|
+
const constraint = error?.constraint;
|
|
22
|
+
return recordRefusalCause(new ScopeRefusal('This operation is not available in the current row scope.'), `The database's foreign key${typeof constraint === 'string' ? ` ${JSON.stringify(constraint)}` : ''} refused the write: a row it points at is not in the principal's rows, or does not exist.`);
|
|
23
|
+
};
|
|
3
24
|
/** A table's triggers as a refusal names them: `the trigger "audit_order"`, `the triggers "a", "b"`. */
|
|
4
25
|
export const triggerNames = (names) => `the trigger${names.length === 1 ? '' : 's'} ${names.map((name) => JSON.stringify(name)).join(', ')}`;
|
|
26
|
+
/* The trigger vocabulary, for `mistri check` (commandTriggerChecks.ts), which says the same refusal ahead of time. */
|
|
27
|
+
export { blocksEveryWrite, sayBlocked, sayRuns, sayTriggers } from './scopeTriggers.js';
|
|
28
|
+
/** The keyed-effect judge (triggerEffects.ts), loaded where it is used: a process that never meets a named trigger never loads it. */
|
|
29
|
+
export const loadTriggerEffects = () => import('./triggerEffects.js');
|
|
30
|
+
/**
|
|
31
|
+
* The count of rows of `fk.from.table` that reference the `fk.to.table` row
|
|
32
|
+
* `key` names, as the database's own foreign key matches them, and so as its
|
|
33
|
+
* cascade would reach them.
|
|
34
|
+
*
|
|
35
|
+
* A foreign key compares a reference as the parent key compares: SQLite
|
|
36
|
+
* applies the parent column's affinity and collation to the child's value
|
|
37
|
+
* (an untyped child column holding the number 7 references the text key
|
|
38
|
+
* '7'), PostgreSQL the parent's collation where the child's is the default.
|
|
39
|
+
* The child counted by its own column (`child.col = ?`) compares as the child
|
|
40
|
+
* does, and missed such a reference, which the delete's cascade then reached.
|
|
41
|
+
* Here each parent column is the left side of a join, where both engines take
|
|
42
|
+
* its affinity and collation as the foreign key does; where two collations
|
|
43
|
+
* conflict, PostgreSQL refuses the count, and the delete with it.
|
|
44
|
+
*/
|
|
45
|
+
export function referencesStatement(schema, fk, key, style) {
|
|
46
|
+
const relation = (id) => {
|
|
47
|
+
const table = schema.tables.find((table) => table.id === id);
|
|
48
|
+
if (!table)
|
|
49
|
+
throw new Refusal('This operation is not available in the current row scope.');
|
|
50
|
+
return `${style.quote(table.schema)}.${style.quote(table.name)}`;
|
|
51
|
+
};
|
|
52
|
+
const on = fk.to.columns.map((column, index) => `p.${style.quote(column)} = c.${style.quote(fk.from.columns[index])}`).join(' AND ');
|
|
53
|
+
const where = buildWhere(key, style, 1, (name) => `p.${style.quote(name)}`);
|
|
54
|
+
if (!where.text)
|
|
55
|
+
throw new Refusal('This operation is not available in the current row scope.');
|
|
56
|
+
return {
|
|
57
|
+
text: `SELECT COUNT(*) AS n FROM ${relation(fk.from.table)} AS c JOIN ${relation(fk.to.table)} AS p ON ${on}${where.text}`,
|
|
58
|
+
params: where.params,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
5
61
|
/** The tables where `scope` is exactly `bound`, and the bound admits rows at all: nothing in it is hidden there. */
|
|
6
62
|
export function wholeWithin(scope, bound) {
|
|
7
63
|
return new Set(Object.keys(bound).filter((table) => bound[table] !== false && Object.hasOwn(scope, table) && JSON.stringify(scope[table]) === JSON.stringify(bound[table])));
|
|
@@ -27,6 +83,21 @@ const keyText = (key) => Object.entries(key)
|
|
|
27
83
|
.map(([column, value]) => `${column} ${JSON.stringify(value)}`)
|
|
28
84
|
.join(', ');
|
|
29
85
|
const comparable = (value) => typeof value === 'string' || (typeof value === 'number' && Number.isFinite(value));
|
|
86
|
+
/* A value for a number column as the write sends it: "" is NULL, "7.0" is 7 (sqlite.ts and postgres.ts coerce every
|
|
87
|
+
value by its column's type before binding it). Only numbers: a parent check binds what it is given, and a number
|
|
88
|
+
is where the given text and the written value part. */
|
|
89
|
+
const asWritten = (type, value) => type !== undefined && columnKind(type) === 'number' && typeof value === 'string' ? coerceForColumn(type, value) : value;
|
|
90
|
+
/* A value a PostgreSQL reference compared through its foreign key's equality (Condition.equality) is bound as text and
|
|
91
|
+
read by the column's type: a boolean as its word. Every other type a reference may be (keyTypes.ts) the driver reads
|
|
92
|
+
as a string or a number already, which is its text. */
|
|
93
|
+
const asBound = (value) => (typeof value === 'boolean' ? String(value) : value);
|
|
94
|
+
/* Two values a check binds name the same parent: identical, or, where both are bound as text read by the column's type
|
|
95
|
+
(Condition.equality), the same text — a given 7 and the stored "7" are the one parameter. */
|
|
96
|
+
const sameBound = (a, b, equality) => a === b ||
|
|
97
|
+
(equality &&
|
|
98
|
+
(typeof a === 'string' || typeof a === 'number') &&
|
|
99
|
+
(typeof b === 'string' || typeof b === 'number') &&
|
|
100
|
+
String(a) === String(b));
|
|
30
101
|
/** `scope` with each child rule's key conditions on a reference to a table in `made` extended by those rows' keys. */
|
|
31
102
|
function withOwnRows(schema, scope, made) {
|
|
32
103
|
const out = { ...scope };
|
|
@@ -66,7 +137,7 @@ function withOwnRows(schema, scope, made) {
|
|
|
66
137
|
/** Runs INSIDE a single queued operation. The supplied guards retain actual
|
|
67
138
|
* stored scalar types, not display conversions (a blob label is not a key).
|
|
68
139
|
* This capability deliberately refuses side effects it cannot bound. */
|
|
69
|
-
export function createScopedTransaction(catalog, records, reads, guards, locks, bound, own) {
|
|
140
|
+
export function createScopedTransaction(catalog, records, reads, guards, locks, bound, own, triggers) {
|
|
70
141
|
/* The rows this transaction inserted inside the principal's rows, by table, and the reads that count them in
|
|
71
142
|
(ScopeOwnRows). Rebuilt on each such insert; until the first, the reads are the ones resolved for the request. */
|
|
72
143
|
const made = new Map();
|
|
@@ -78,13 +149,29 @@ export function createScopedTransaction(catalog, records, reads, guards, locks,
|
|
|
78
149
|
reads = own.reads(scope, false);
|
|
79
150
|
guards = own.reads(scope, true);
|
|
80
151
|
};
|
|
81
|
-
const target = (id) => {
|
|
152
|
+
const target = (id, write) => {
|
|
82
153
|
const table = catalog.schema.tables.find((table) => table.id === id);
|
|
83
154
|
if (!table)
|
|
84
155
|
return refuse(`"${id}" is not in this write's footprint: a command writes only the tables it declares.`);
|
|
85
|
-
if (
|
|
86
|
-
|
|
87
|
-
|
|
156
|
+
if (catalog.hierarchy?.has(id))
|
|
157
|
+
return refuse(`"${id}" is part of a partition or inheritance hierarchy, which writes under a row policy do not support yet.`);
|
|
158
|
+
const triggers = catalog.writable.has(id) ? undefined : catalog.triggered?.get(id);
|
|
159
|
+
if (triggers) {
|
|
160
|
+
/* A trigger does what no row policy bounds, so a scoped write fires none but those proven to validate their own
|
|
161
|
+
row. Decided per write: an update of a column no `UPDATE OF` names fires nothing. The refusal names the
|
|
162
|
+
trigger, or the owner is left guessing why a table they declared written is not. */
|
|
163
|
+
const foldCase = catalog.schema.dialect === 'sqlite';
|
|
164
|
+
// A trigger the App names and the proof accepts runs, its effects proven per write (triggerProof.ts).
|
|
165
|
+
const blocking = blockingTriggers(triggers, write, foldCase)
|
|
166
|
+
.filter((trigger) => !proof?.accepts(id, trigger))
|
|
167
|
+
.map((trigger) => ({ ...trigger, why: proof?.unaccepted(id, trigger) ?? trigger.why }));
|
|
168
|
+
if (blocking.length)
|
|
169
|
+
return refuse(`"${id}" cannot be written under a row policy: ${sayWrite(write)} ${sayRuns(blocking)} ${sayTriggers(blocking)}, whose effects the policy cannot bound. ` +
|
|
170
|
+
`A trigger that only checks NEW and OLD and raises does not block a write; mistri check names each command one blocks.`);
|
|
171
|
+
if (firedBy(triggers, write, foldCase).length)
|
|
172
|
+
locks?.relied?.(table);
|
|
173
|
+
}
|
|
174
|
+
else if (!catalog.writable.has(id)) {
|
|
88
175
|
const by = catalog.blockedBy?.get(id);
|
|
89
176
|
return refuse(by
|
|
90
177
|
? `"${id}" cannot be written under a row policy: it has ${by}, whose writes the policy cannot bound.`
|
|
@@ -92,6 +179,9 @@ export function createScopedTransaction(catalog, records, reads, guards, locks,
|
|
|
92
179
|
}
|
|
93
180
|
if (!table.columns.some((column) => column.primaryKey))
|
|
94
181
|
return refuse(`"${id}" has no primary key, so a scoped write cannot prove which row it changed.`);
|
|
182
|
+
const unkeyed = catalog.unkeyed?.get(id);
|
|
183
|
+
if (unkeyed)
|
|
184
|
+
return refuse(`${unkeyed}.`);
|
|
95
185
|
return table;
|
|
96
186
|
};
|
|
97
187
|
const keyFilter = (table, key) => {
|
|
@@ -106,9 +196,16 @@ export function createScopedTransaction(catalog, records, reads, guards, locks,
|
|
|
106
196
|
ends, so what the scope checks is what the write will change. Where the
|
|
107
197
|
writer sends together, the read leaves right behind the lock, in its
|
|
108
198
|
flight; the server runs it only once the lock is granted. */
|
|
199
|
+
/* A row by its key, with its reference columns' stored text beside it where the engine reads one (storedText.ts):
|
|
200
|
+
every check that takes a value from a stored row binds that text, never the driver's reading of it. */
|
|
201
|
+
const readStored = async (table, filter, within) => {
|
|
202
|
+
const texts = storedTextOf(catalog.schema, table);
|
|
203
|
+
const result = await within.query({ table: table.id, filter, limit: 2, offset: 0, ...(texts ? { storedText: true } : {}) });
|
|
204
|
+
return { ...result, rows: result.rows.map((row) => detachStoredText(texts, row)) };
|
|
205
|
+
};
|
|
109
206
|
const lockedRead = async (table, filter, within) => {
|
|
110
207
|
const locked = locks?.target(table, filter);
|
|
111
|
-
const read = () =>
|
|
208
|
+
const read = () => readStored(table, filter, within);
|
|
112
209
|
if (!locks?.together) {
|
|
113
210
|
await locked;
|
|
114
211
|
return read();
|
|
@@ -138,21 +235,86 @@ export function createScopedTransaction(catalog, records, reads, guards, locks,
|
|
|
138
235
|
|
|
139
236
|
Each reference is checked by its parent's lock, then that parent's count
|
|
140
237
|
under the principal's rows; a reference that cannot be checked ends the
|
|
141
|
-
list, refused there, so the checks before it still speak first.
|
|
142
|
-
|
|
238
|
+
list, refused there, so the checks before it still speak first.
|
|
239
|
+
|
|
240
|
+
The references a write names are checked before it is sent, not only
|
|
241
|
+
after: the database's own foreign key answers a parent that does not
|
|
242
|
+
exist with its error, at the statement, while a parent that exists and
|
|
243
|
+
is hidden passed the statement and was refused after it. The two
|
|
244
|
+
answers told a hidden row from a missing one. Checked first, both are
|
|
245
|
+
the one refusal below. `given` keeps to the references whose every
|
|
246
|
+
column the write names; the rest (a default, a value a trigger sets)
|
|
247
|
+
are checked after the statement as before.
|
|
248
|
+
|
|
249
|
+
A reference is checked as the database's foreign key compares it
|
|
250
|
+
(ScopeCatalog.references), with the value the write sends: a check that
|
|
251
|
+
compared otherwise found one parent where the foreign key found
|
|
252
|
+
another, a hidden one (a char(3) value against a text key on
|
|
253
|
+
PostgreSQL, whose foreign key drops its padding; a REAL column's 7.0
|
|
254
|
+
against a TEXT key on SQLite). A value the write sends as NULL ("" for
|
|
255
|
+
a number) points at nothing. And a row that names itself as its own
|
|
256
|
+
parent by the key it gives (`{id: 1, parent_id: 1}`) is that parent:
|
|
257
|
+
it exists once the statement has run, and is checked after it. */
|
|
258
|
+
const comparisonOf = (fk) => {
|
|
259
|
+
const said = catalog.references?.get(referenceKey(fk));
|
|
260
|
+
if (said)
|
|
261
|
+
return said;
|
|
262
|
+
if (catalog.references)
|
|
263
|
+
return {
|
|
264
|
+
refused: `"${fk.from.table}".${fk.from.columns.join('+')} has a foreign key whose comparison was not read with the catalog.`,
|
|
265
|
+
};
|
|
266
|
+
const child = catalog.schema.tables.find((table) => table.id === fk.from.table);
|
|
267
|
+
return { columns: fk.from.columns.map((name) => ({ type: child?.columns.find((column) => column.name === name)?.type ?? '' })) };
|
|
268
|
+
};
|
|
269
|
+
/* A reference with a generated column (the catalog marks one unwritable: PostgreSQL's attgenerated, SQLite's
|
|
270
|
+
table_xinfo hidden 2 or 3) moves with whatever column its expression reads, which no update names: an update of
|
|
271
|
+
`x` re-pointed `k GENERATED ALWAYS AS (x + 1) REFERENCES p` at a hidden parent, checked by nobody. So every update
|
|
272
|
+
touches it, and it is checked after the statement, from the row as stored; before the statement its value is not
|
|
273
|
+
known (what the row held is not what the update leaves), so a check of what a write gives leaves it out. */
|
|
274
|
+
const generated = (table, name) => table.columns.find((column) => column.name === name)?.writable === false;
|
|
275
|
+
const generatedIn = (table, fk) => fk.from.columns.some((name) => generated(table, name));
|
|
276
|
+
const parentChecks = (table, row, set, given = false) => {
|
|
143
277
|
const checks = [];
|
|
144
|
-
for (const fk of catalog.schema.foreignKeys.filter((fk) => fk.from.table === table.id &&
|
|
145
|
-
|
|
278
|
+
for (const fk of catalog.schema.foreignKeys.filter((fk) => fk.from.table === table.id &&
|
|
279
|
+
(!set || fk.from.columns.some((column) => set.has(column)) || (!given && generatedIn(table, fk))) &&
|
|
280
|
+
(!given || (!generatedIn(table, fk) && fk.from.columns.every((column) => Object.hasOwn(row, column) && row[column] !== undefined))))) {
|
|
281
|
+
const compared = comparisonOf(fk);
|
|
282
|
+
// A value the row holds as stored is bound as the text it was read as (storedText.ts); one given, as given.
|
|
283
|
+
const texts = storedText(row);
|
|
284
|
+
const values = fk.from.columns.map((column, index) => {
|
|
285
|
+
const compares = 'columns' in compared ? compared.columns[index] : undefined;
|
|
286
|
+
if (compares?.equality && texts && Object.hasOwn(texts, column) && Object.hasOwn(row, column))
|
|
287
|
+
return texts[column];
|
|
288
|
+
const value = asWritten(compares?.type, row[column]);
|
|
289
|
+
return compares?.equality ? asBound(value) : value;
|
|
290
|
+
});
|
|
146
291
|
if (values.some((value) => value === null))
|
|
147
292
|
continue;
|
|
148
|
-
if (
|
|
149
|
-
checks.push({ refused:
|
|
293
|
+
if ('refused' in compared) {
|
|
294
|
+
checks.push({ refused: compared.refused });
|
|
150
295
|
break;
|
|
151
296
|
}
|
|
297
|
+
const unsupported = values.findIndex((value) => !comparable(value));
|
|
298
|
+
if (unsupported !== -1) {
|
|
299
|
+
checks.push({
|
|
300
|
+
refused: `"${table.id}".${fk.from.columns[unsupported]} holds a value of a type a scoped write does not support for a reference: a check could not bind it as the foreign key compares it.`,
|
|
301
|
+
});
|
|
302
|
+
break;
|
|
303
|
+
}
|
|
304
|
+
if (given &&
|
|
305
|
+
fk.to.table === table.id &&
|
|
306
|
+
fk.to.columns.every((column, index) => Object.hasOwn(row, column) &&
|
|
307
|
+
asWritten(table.columns.find((candidate) => candidate.name === column)?.type, row[column]) === values[index]))
|
|
308
|
+
continue;
|
|
152
309
|
// Parent columns stay on the SQL left-hand side so SQLite's parent
|
|
153
310
|
// affinity and collation, rather than JavaScript equality, decide.
|
|
154
311
|
const filter = {
|
|
155
|
-
groups: [
|
|
312
|
+
groups: [
|
|
313
|
+
fk.to.columns.map((column, index) => {
|
|
314
|
+
const equality = compared.columns[index]?.equality;
|
|
315
|
+
return { column, op: '=', value: values[index], ...(equality ? { equality } : {}) };
|
|
316
|
+
}),
|
|
317
|
+
],
|
|
156
318
|
};
|
|
157
319
|
const parent = catalog.schema.tables.find((table) => table.id === fk.to.table);
|
|
158
320
|
if (!parent) {
|
|
@@ -165,6 +327,8 @@ export function createScopedTransaction(catalog, records, reads, guards, locks,
|
|
|
165
327
|
}
|
|
166
328
|
return checks;
|
|
167
329
|
};
|
|
330
|
+
/** Whether a check binds every value as text read by its column's type (Condition.equality): PostgreSQL's. */
|
|
331
|
+
const bindsText = (check) => check.filter.groups[0].every((condition) => condition.equality);
|
|
168
332
|
const unseen = (check) => `"${check.fk.from.table}".${check.fk.from.columns.join('+')} points at the "${check.fk.to.table}" row ${keyText(Object.fromEntries(check.fk.to.columns.map((column, index) => [column, check.values[index]])))}, which the principal's rows do not include (or the command does not declare "${check.fk.to.table}" read).`;
|
|
169
333
|
/** The checks sent at once (locks.together): each parent's lock, then its count, every answer heard. */
|
|
170
334
|
const ask = (checks) => checks.map((check) => {
|
|
@@ -184,10 +348,14 @@ export function createScopedTransaction(catalog, records, reads, guards, locks,
|
|
|
184
348
|
const check = checks[index];
|
|
185
349
|
if ('refused' in guess || 'refused' in check)
|
|
186
350
|
return 'refused' in guess && 'refused' in check && guess.refused === check.refused;
|
|
187
|
-
return guess.fk === check.fk && guess.values.every((value, at) => value
|
|
351
|
+
return guess.fk === check.fk && guess.values.every((value, at) => sameBound(value, check.values[at], bindsText(check)));
|
|
188
352
|
});
|
|
189
|
-
|
|
190
|
-
|
|
353
|
+
/* References this transaction has proven: each parent locked and counted inside the principal's rows, which holds
|
|
354
|
+
until COMMIT (the lock), so the same reference is not asked again after the write that made it. */
|
|
355
|
+
const proven = [];
|
|
356
|
+
const unproven = (checks) => checks.filter((check) => 'refused' in check ||
|
|
357
|
+
!proven.some((done) => done.fk === check.fk && done.values.every((value, at) => sameBound(value, check.values[at], bindsText(check)))));
|
|
358
|
+
const verify = async (checks, guessed) => {
|
|
191
359
|
if (!locks?.together) {
|
|
192
360
|
for (const check of checks) {
|
|
193
361
|
if ('refused' in check)
|
|
@@ -196,6 +364,7 @@ export function createScopedTransaction(catalog, records, reads, guards, locks,
|
|
|
196
364
|
await locks?.parent(check.parent, check.filter);
|
|
197
365
|
if ((await guards.count(check.fk.to.table, check.filter)) !== 1)
|
|
198
366
|
return refuse(unseen(check));
|
|
367
|
+
proven.push(check);
|
|
199
368
|
}
|
|
200
369
|
return;
|
|
201
370
|
}
|
|
@@ -208,14 +377,74 @@ export function createScopedTransaction(catalog, records, reads, guards, locks,
|
|
|
208
377
|
await check.locked;
|
|
209
378
|
if ((await check.counted) !== 1)
|
|
210
379
|
return refuse(unseen(check));
|
|
380
|
+
proven.push(check);
|
|
381
|
+
}
|
|
382
|
+
};
|
|
383
|
+
const parents = async (table, row, set, guessed, given = false) => verify(unproven(parentChecks(table, row, set, given)), guessed);
|
|
384
|
+
/**
|
|
385
|
+
* Before a write: the references it names, checked as `parents` checks them. Where the writer sends together, the
|
|
386
|
+
* checks leave ahead of the write, in its flight, and `write` is sent behind them before any is judged: the server
|
|
387
|
+
* runs them first, and a refused one is said whatever the write then answered (a foreign key's error included).
|
|
388
|
+
*/
|
|
389
|
+
const parentsFirst = async (checks, write) => {
|
|
390
|
+
if (!locks?.together) {
|
|
391
|
+
await verify(checks);
|
|
392
|
+
return write();
|
|
211
393
|
}
|
|
394
|
+
const asked = ask(checks);
|
|
395
|
+
const sent = write();
|
|
396
|
+
await Promise.allSettled([sent, ...asked.flatMap((check) => ('refused' in check ? [] : [check.locked, check.counted]))]);
|
|
397
|
+
await verify(checks, asked);
|
|
398
|
+
return sent;
|
|
399
|
+
};
|
|
400
|
+
/* Triggers the App names: each write that fires one proves its effects' keys first and the rest right after the
|
|
401
|
+
statement, with the transaction's own locks and guards (triggerProof.ts). */
|
|
402
|
+
const proof = triggers
|
|
403
|
+
? triggers.prove(triggers.acceptance, {
|
|
404
|
+
tables: catalog.schema.tables,
|
|
405
|
+
foreignKeys: catalog.schema.foreignKeys,
|
|
406
|
+
triggered: catalog.triggered,
|
|
407
|
+
foldCase: catalog.schema.dialect === 'sqlite',
|
|
408
|
+
scope: triggers.scope,
|
|
409
|
+
guards: () => guards,
|
|
410
|
+
lockedRead,
|
|
411
|
+
read: (table, filter) => readStored(table, filter, guards),
|
|
412
|
+
parents: (table, row, set) => parents(table, row, set),
|
|
413
|
+
parentsBefore: (table, row, set) => parents(table, row, set, undefined, true),
|
|
414
|
+
...(locks ? { holdParent: (table, filter) => locks.parent(table, filter) } : {}),
|
|
415
|
+
...(locks?.relied ? { relied: (table) => locks.relied(table) } : {}),
|
|
416
|
+
...(triggers.watch ? { watch: triggers.watch } : {}),
|
|
417
|
+
refuse,
|
|
418
|
+
})
|
|
419
|
+
: undefined;
|
|
420
|
+
/* The statements themselves. A foreign key that refuses one answers as a hidden parent does (foreignKeyRefusal):
|
|
421
|
+
the checks before a write compare what it names, and this is everything they cannot see — a default's part of a
|
|
422
|
+
reference, a value a trigger writes, a deferred check. */
|
|
423
|
+
const statement = (sent) => sent.catch((error) => {
|
|
424
|
+
throw isForeignKeyViolation(error) ? foreignKeyRefusal(error) : error;
|
|
425
|
+
});
|
|
426
|
+
const insertRow = (request) => statement(records.insert(request));
|
|
427
|
+
const updateRow = (request) => statement(records.update(request));
|
|
428
|
+
const afterward = async (afterwards, images) => {
|
|
429
|
+
for (const next of afterwards ?? [])
|
|
430
|
+
await next(images);
|
|
212
431
|
};
|
|
213
432
|
return Object.freeze({
|
|
214
433
|
query: (request) => reads.query(request),
|
|
215
434
|
count: (table, filter) => reads.count(table, filter),
|
|
435
|
+
/* Under the principal's rows, as `count` reads: never the writer's own unscoped aggregate. */
|
|
436
|
+
...(reads.aggregate
|
|
437
|
+
? {
|
|
438
|
+
aggregate: (request) => reads.aggregate ? reads.aggregate(request) : refuse('This scope cannot aggregate.'),
|
|
439
|
+
}
|
|
440
|
+
: {}),
|
|
216
441
|
async insert(request) {
|
|
217
|
-
const table = target(request.table);
|
|
218
|
-
const
|
|
442
|
+
const table = target(request.table, { kind: 'insert' });
|
|
443
|
+
const afterwards = proof && (await proof.before(table, { kind: 'insert' }, proof.images.insert(table, request.values)));
|
|
444
|
+
const given = unproven(parentChecks(table, request.values ?? {}, undefined, true));
|
|
445
|
+
if (!locks?.together)
|
|
446
|
+
await verify(given);
|
|
447
|
+
const result = locks?.together ? await parentsFirst(given, () => insertRow(request)) : await insertRow(request);
|
|
219
448
|
const filter = keyFilter(table, result.key);
|
|
220
449
|
/* The new row locked and read back, and where the writer sends together
|
|
221
450
|
its references checked in the same flight, from the row the INSERT
|
|
@@ -223,21 +452,24 @@ export function createScopedTransaction(catalog, records, reads, guards, locks,
|
|
|
223
452
|
those). No other transaction can see this row yet, so its lock is
|
|
224
453
|
granted at once and the read answers what a count before it would. */
|
|
225
454
|
const read = lockedRead(table, filter, guards);
|
|
226
|
-
const guessed = locks?.together && result.row ? ask(parentChecks(table, result.row)) : undefined;
|
|
455
|
+
const guessed = locks?.together && result.row ? ask(unproven(parentChecks(table, result.row))) : undefined;
|
|
227
456
|
read.catch(() => { });
|
|
228
457
|
const found = await read;
|
|
229
458
|
// Inside the bound and outside the principal's rows: the command's own row, answered by its key and what it wrote.
|
|
230
459
|
if (bound && found.rows.length !== 1) {
|
|
231
|
-
|
|
460
|
+
const within = await stored(table, result.key, bound.reads);
|
|
461
|
+
await parents(table, within, undefined, guessed);
|
|
462
|
+
await afterward(afterwards, { new: within, old: null });
|
|
232
463
|
return { key: result.key, applied: result.applied, sql: result.sql };
|
|
233
464
|
}
|
|
234
465
|
const row = one(table, result.key, found);
|
|
235
466
|
await parents(table, row, undefined, guessed);
|
|
467
|
+
await afterward(afterwards, { new: row, old: null });
|
|
236
468
|
madeBy(table, row);
|
|
237
469
|
return result;
|
|
238
470
|
},
|
|
239
471
|
async update(request) {
|
|
240
|
-
const table = target(request.table);
|
|
472
|
+
const table = target(request.table, { kind: 'update', set: Object.keys(request.values ?? {}) });
|
|
241
473
|
const protectedColumns = new Set([
|
|
242
474
|
...table.columns.filter((column) => column.primaryKey).map((column) => column.name),
|
|
243
475
|
...catalog.schema.foreignKeys.filter((fk) => fk.to.table === table.id).flatMap((fk) => fk.to.columns),
|
|
@@ -247,32 +479,44 @@ export function createScopedTransaction(catalog, records, reads, guards, locks,
|
|
|
247
479
|
return refuse(`A scoped update may not change "${table.id}".${moved.join(', ')}: a key, or a column another table references.`);
|
|
248
480
|
const before = await stored(table, request.key);
|
|
249
481
|
const set = new Set(Object.keys(request.values ?? {}));
|
|
482
|
+
const afterwards = proof && (await proof.before(table, { kind: 'update', set: [...set] }, proof.images.update(table, before, request.values)));
|
|
483
|
+
// What the update sets is given; every other column of a reference it sets in part is as stored, bound as its text.
|
|
484
|
+
const given = unproven(parentChecks(table, overlaid(before, request.values), set, true));
|
|
485
|
+
if (!locks?.together)
|
|
486
|
+
await verify(given);
|
|
250
487
|
/* The row read again after the write, sent the moment the write is: it
|
|
251
488
|
reads what the write wrote, and leaves in the write's round trip, with
|
|
252
|
-
the references the write sets checked
|
|
253
|
-
them. A
|
|
254
|
-
the
|
|
489
|
+
the references the write sets checked ahead of it, as the row will
|
|
490
|
+
hold them. A reference refused is the answer whatever the write said;
|
|
491
|
+
otherwise a write that fails aborts the read too, and the write's
|
|
492
|
+
failure is the one thrown. */
|
|
255
493
|
const after = {};
|
|
256
494
|
const withdraw = locks?.behind?.(table, () => {
|
|
257
495
|
after.row = stored(table, request.key);
|
|
258
496
|
after.row.catch(() => { });
|
|
259
|
-
if (locks.together)
|
|
260
|
-
after.guessed = ask(parentChecks(table, { ...before, ...request.values }, set));
|
|
261
497
|
});
|
|
262
498
|
let result;
|
|
263
499
|
try {
|
|
264
|
-
|
|
500
|
+
// The references it sets leave ahead of it, in its flight (parentsFirst).
|
|
501
|
+
result = locks?.together ? await parentsFirst(given, () => updateRow(request)) : await updateRow(request);
|
|
265
502
|
}
|
|
266
503
|
finally {
|
|
267
504
|
withdraw?.();
|
|
268
505
|
}
|
|
269
506
|
if (result.affected !== 1)
|
|
270
507
|
return refuse(`The update of "${table.id}" changed ${result.affected} rows, not one.`);
|
|
271
|
-
|
|
508
|
+
const now = await (after.row ?? stored(table, request.key));
|
|
509
|
+
/* A key or a referenced column the database generates moves with the columns its expression reads, which the
|
|
510
|
+
names above cannot catch: one that moved is refused, whatever a cascade or the foreign key made of it. */
|
|
511
|
+
const shifted = [...protectedColumns].filter((column) => generated(table, column) && JSON.stringify(before[column]) !== JSON.stringify(now[column]));
|
|
512
|
+
if (shifted.length)
|
|
513
|
+
return refuse(`A scoped update may not change "${table.id}".${shifted.join(', ')}: a key, or a column another table references, generated from the columns it sets.`);
|
|
514
|
+
await parents(table, now, set);
|
|
515
|
+
await afterward(afterwards, { new: now, old: before });
|
|
272
516
|
return result;
|
|
273
517
|
},
|
|
274
518
|
async remove(request) {
|
|
275
|
-
const table = target(request.table);
|
|
519
|
+
const table = target(request.table, { kind: 'delete' });
|
|
276
520
|
const children = catalog.schema.foreignKeys.filter((fk) => fk.to.table === table.id);
|
|
277
521
|
const hidden = children.find((fk) => !bound?.whole.has(fk.from.table));
|
|
278
522
|
if (hidden)
|
|
@@ -288,15 +532,15 @@ export function createScopedTransaction(catalog, records, reads, guards, locks,
|
|
|
288
532
|
continue;
|
|
289
533
|
if (!values.every(comparable))
|
|
290
534
|
return refuse(`"${table.id}".${fk.to.columns.join('+')} holds a value that is not a text or a number.`);
|
|
291
|
-
const
|
|
292
|
-
|
|
293
|
-
};
|
|
294
|
-
if ((await records.count(fk.from.table, filter)) !== 0)
|
|
535
|
+
const key = { groups: [Object.entries(request.key).map(([column, value]) => ({ column, op: '=', value }))] };
|
|
536
|
+
if ((await bound?.references(fk, key)) !== 0)
|
|
295
537
|
return refuse(`A scoped command may not delete the "${table.id}" row ${keyText(request.key)}: "${fk.from.table}" still references it.`);
|
|
296
538
|
}
|
|
297
|
-
const
|
|
539
|
+
const afterwards = proof && (await proof.before(table, { kind: 'delete' }, proof.images.delete(row)));
|
|
540
|
+
const result = await statement(records.remove(request));
|
|
298
541
|
if (result.affected !== 1)
|
|
299
542
|
return refuse(`The delete from "${table.id}" removed ${result.affected} rows, not one.`);
|
|
543
|
+
await afterward(afterwards, { new: null, old: row });
|
|
300
544
|
return result;
|
|
301
545
|
},
|
|
302
546
|
});
|
|
@@ -1,12 +1,28 @@
|
|
|
1
1
|
import type { DatabaseSync } from 'node:sqlite';
|
|
2
2
|
import { type DatabaseSchema } from '../../shared/schema.js';
|
|
3
3
|
import { type ScopeCatalog } from './scopedTransaction.js';
|
|
4
|
+
import type { EffectCatalog, resolveTriggerEffects } from './triggerEffects.js';
|
|
4
5
|
export type SqliteScopeCatalog = ScopeCatalog;
|
|
5
6
|
/** Call only on the writer inside BEGIN IMMEDIATE. No cached/public catalog,
|
|
6
7
|
* estimates or data scans: the lease's physical metadata is the authority.
|
|
7
8
|
* A non-writable table remains in the graph so hidden dependents cannot vanish.
|
|
8
9
|
*/
|
|
9
|
-
export declare function readSqliteScopeCatalog(db: DatabaseSync): SqliteScopeCatalog;
|
|
10
|
+
export declare function readSqliteScopeCatalog(db: DatabaseSync, resolve?: typeof resolveTriggerEffects): SqliteScopeCatalog;
|
|
11
|
+
/**
|
|
12
|
+
* How a key equality in a trigger compares on SQLite (triggerEffects.ts): each column's affinity, which decides what
|
|
13
|
+
* a value is converted to before it is compared, and its collation, which `NEW.code = code` takes from the column on
|
|
14
|
+
* its left; and the primary key as a key only where its index compares each column under that column's own
|
|
15
|
+
* collation (`PRIMARY KEY (code COLLATE NOCASE)` on a BINARY column is unique under one and found under the other).
|
|
16
|
+
* And the conflict clause a table declares other than ABORT, which a trigger's write runs under when the statement
|
|
17
|
+
* that fired it is a delete (triggerEffects.ts). Read where effects are judged, inside the writer's BEGIN IMMEDIATE.
|
|
18
|
+
*/
|
|
19
|
+
export declare function sqliteKeyFacts(db: DatabaseSync): Pick<EffectCatalog, 'keys' | 'comparison' | 'conflicts'>;
|
|
20
|
+
/**
|
|
21
|
+
* For `mistri check`: the tables a scoped write cannot target for what they are rather than their triggers — a
|
|
22
|
+
* virtual table and its shadow tables, whose module writes them (scanSqliteScopeCatalog keeps them out of
|
|
23
|
+
* `writable`) — read on the reader, so check refuses an effect on one as the write does.
|
|
24
|
+
*/
|
|
25
|
+
export declare function sqliteBlockedTables(db: DatabaseSync): ReadonlyMap<string, string>;
|
|
10
26
|
/** Metadata-only read attestation inside the caller's owned snapshot. Exposes
|
|
11
27
|
* no writer eligibility. Keep every non-engine table and dependency, including
|
|
12
28
|
* private/unmapped objects, so an unknown business edge cannot disappear. */
|