@mistriwork/engine 1.0.0-rc.72 → 1.0.0-rc.74
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/http/httpAdapter.js +5 -0
- package/dist/adapters/postgres.js +16 -4
- package/dist/adapters/postgresBatchWrites.d.ts +1 -1
- package/dist/adapters/postgresBatchWrites.js +6 -1
- package/dist/adapters/postgresReadSnapshot.js +11 -3
- package/dist/adapters/scoped/scopedCommandOperations.js +5 -6
- package/dist/adapters/scoped/scopedReads.js +18 -2
- package/dist/adapters/scoped/scopedTransaction.js +325 -107
- package/dist/adapters/scoped/triggerProof.js +1 -1
- package/dist/adapters/sqlite.js +4 -2
- package/dist/server/cli/config.d.ts +3 -0
- package/dist/server/cli/config.js +3 -1
- package/dist/server/columnGrants.js +21 -1
- package/dist/server/resourceReadHttp.d.ts +7 -0
- package/dist/server/resourceReadHttp.js +8 -1
- package/dist/server/resourceReadOperations.d.ts +1 -1
- package/dist/server/resourceReadOperations.js +30 -3
- package/dist/shared/aggregate.js +2 -2
- package/dist/shared/anyRows.d.ts +19 -1
- package/dist/shared/anyRows.js +49 -0
- package/dist/shared/filter.d.ts +34 -2
- package/dist/shared/filter.js +32 -4
- package/dist/shared/listEmpty.d.ts +5 -1
- package/dist/shared/listEmpty.js +13 -8
- package/dist/shared/listPresentations.d.ts +8 -5
- package/package.json +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Refusal } from '../adapter.js';
|
|
1
|
+
import { Refusal, oneAtATime, sameTable, } from '../adapter.js';
|
|
2
2
|
import { buildWhere } from '../../shared/filter.js';
|
|
3
3
|
import { authorFault, recordRefusalCause, refusalCause, sayRefusal } from '../../shared/refusal.js';
|
|
4
4
|
import { coerceForColumn, columnKind } from '../../shared/schema.js';
|
|
@@ -264,7 +264,7 @@ function withOwnRows(schema, scope, made) {
|
|
|
264
264
|
return given;
|
|
265
265
|
const condition = given;
|
|
266
266
|
const added = extra.get(condition.column);
|
|
267
|
-
if (!added || condition.other !== undefined || condition.any !== undefined)
|
|
267
|
+
if (!added || condition.other !== undefined || condition.any !== undefined || condition.within !== undefined)
|
|
268
268
|
return condition;
|
|
269
269
|
if (condition.op === '=' && comparable(condition.value))
|
|
270
270
|
return { ...condition, op: 'in', value: [condition.value, ...added] };
|
|
@@ -276,6 +276,8 @@ function withOwnRows(schema, scope, made) {
|
|
|
276
276
|
}
|
|
277
277
|
return out;
|
|
278
278
|
}
|
|
279
|
+
/** The most keys one read of a batch names: a command's batch is at most 1,000 rows (commandDb.ts), so one read. */
|
|
280
|
+
const KEYS_A_READ = 1000;
|
|
279
281
|
/** Runs INSIDE a single queued operation. The supplied guards retain actual
|
|
280
282
|
* stored scalar types, not display conversions (a blob label is not a key).
|
|
281
283
|
* This capability deliberately refuses side effects it cannot bound. */
|
|
@@ -292,10 +294,10 @@ referencesOf) {
|
|
|
292
294
|
/* The rows this transaction inserted inside the principal's rows, by table, and the reads that count them in
|
|
293
295
|
(ScopeOwnRows). Rebuilt on each such insert; until the first, the reads are the ones resolved for the request. */
|
|
294
296
|
const made = new Map();
|
|
295
|
-
const madeBy = (table,
|
|
296
|
-
if (!own)
|
|
297
|
+
const madeBy = (table, ...rows) => {
|
|
298
|
+
if (!own || !rows.length)
|
|
297
299
|
return;
|
|
298
|
-
made.set(table.id, [...(made.get(table.id) ?? []),
|
|
300
|
+
made.set(table.id, [...(made.get(table.id) ?? []), ...rows]);
|
|
299
301
|
const scope = withOwnRows(catalog.schema, own.scope, made);
|
|
300
302
|
reads = own.reads(scope, false);
|
|
301
303
|
guards = own.reads(scope, true);
|
|
@@ -374,14 +376,14 @@ referencesOf) {
|
|
|
374
376
|
flight; the server runs it only once the lock is granted. */
|
|
375
377
|
/* A row by its key, with its reference columns' stored text beside it where the engine reads one (storedText.ts):
|
|
376
378
|
every check that takes a value from a stored row binds that text, never the driver's reading of it. */
|
|
377
|
-
const readStored = async (table, filter, within) => {
|
|
379
|
+
const readStored = async (table, filter, within, limit = 2) => {
|
|
378
380
|
const texts = storedTextOf(catalog.schema, table);
|
|
379
|
-
const result = await within.query({ table: table.id, filter, limit
|
|
381
|
+
const result = await within.query({ table: table.id, filter, limit, offset: 0, ...(texts ? { storedText: true } : {}) });
|
|
380
382
|
return { ...result, rows: result.rows.map((row) => detachStoredText(texts, row)) };
|
|
381
383
|
};
|
|
382
|
-
const lockedRead = async (table, filter, within) => {
|
|
384
|
+
const lockedRead = async (table, filter, within, limit) => {
|
|
383
385
|
const locked = locks?.target(table, filter);
|
|
384
|
-
const read = () => readStored(table, filter, within);
|
|
386
|
+
const read = () => readStored(table, filter, within, limit);
|
|
385
387
|
if (!locks?.together) {
|
|
386
388
|
await locked;
|
|
387
389
|
return read();
|
|
@@ -402,6 +404,31 @@ referencesOf) {
|
|
|
402
404
|
return row;
|
|
403
405
|
};
|
|
404
406
|
const stored = async (table, key, within = guards) => one(table, key, await lockedRead(table, keyFilter(table, key), within));
|
|
407
|
+
/* A batch's rows read as `stored` reads one, a read for up to KEYS_A_READ of them: one key column is one `in`, several
|
|
408
|
+
are a group a key, each key held to what keyFilter asks of one. Found by key as the read spells it; a row a key does
|
|
409
|
+
not find there (a padded char, a folded case) is read again alone (storedFrom), as `stored` reads it. */
|
|
410
|
+
const keyColumns = (table) => table.columns.filter((column) => column.primaryKey).map((column) => column.name);
|
|
411
|
+
const keyId = (table, row) => JSON.stringify(keyColumns(table).map((column) => String(row[column])));
|
|
412
|
+
const keysFilter = (table, keys) => {
|
|
413
|
+
const groups = keys.map((key) => keyFilter(table, key).groups[0]);
|
|
414
|
+
const pk = keyColumns(table);
|
|
415
|
+
if (pk.length !== 1)
|
|
416
|
+
return { groups };
|
|
417
|
+
return { groups: [[{ column: pk[0], op: 'in', value: groups.map((group) => group[0].value) }]] };
|
|
418
|
+
};
|
|
419
|
+
const lockedReads = async (table, keys) => {
|
|
420
|
+
const found = new Map();
|
|
421
|
+
for (let at = 0; at < keys.length; at += KEYS_A_READ) {
|
|
422
|
+
const chunk = keys.slice(at, at + KEYS_A_READ);
|
|
423
|
+
for (const row of (await lockedRead(table, keysFilter(table, chunk), guards, chunk.length + 1)).rows)
|
|
424
|
+
found.set(keyId(table, row), row);
|
|
425
|
+
}
|
|
426
|
+
return found;
|
|
427
|
+
};
|
|
428
|
+
const storedFrom = async (table, key, found) => {
|
|
429
|
+
const row = found.get(keyId(table, key));
|
|
430
|
+
return row ? one(table, key, { rows: [row] }) : stored(table, key);
|
|
431
|
+
};
|
|
405
432
|
/* Every reference a write makes must land on a row the principal reads:
|
|
406
433
|
all of a new row's, and of an updated row only those whose columns the
|
|
407
434
|
update sets. A reference the update leaves as it was is not this write's
|
|
@@ -558,8 +585,47 @@ referencesOf) {
|
|
|
558
585
|
/** Whether a check binds every value as text read by its column's type (Condition.equality): PostgreSQL's. */
|
|
559
586
|
const bindsText = (check) => check.filter.groups[0].every((condition) => condition.equality);
|
|
560
587
|
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).`;
|
|
561
|
-
/** Whether the count of a parent's rows does not prove the reference: a foreign key names one row, a `via` any number of them. */
|
|
562
|
-
const missing = (check, counted) =>
|
|
588
|
+
/** Whether the count of a parent's rows does not prove the reference: a foreign key names one row (one a member), a `via` any number of them. */
|
|
589
|
+
const missing = (check, counted) => check.exists ? counted < 1 : counted !== (check.members?.length ?? 1);
|
|
590
|
+
/* A batch's checks, one asked a foreign key where a row at a time asks one a row: its parents counted in one read
|
|
591
|
+
under the principal's rows, which proves them all where it counts each once (a foreign key names exactly one row,
|
|
592
|
+
so a count of every member is every member found, and found apart). Where it counts fewer — a parent hidden or
|
|
593
|
+
missing, or two values the key compares as one — every check is asked again a check at a time, in row order
|
|
594
|
+
(`whole`, verify), so the refusal said is the one a row at a time says. A `via` stays one check: it counts any
|
|
595
|
+
number of rows. A refused check ends the list, as it ends one row's. */
|
|
596
|
+
const combined = (checks) => {
|
|
597
|
+
const whole = [];
|
|
598
|
+
for (const check of checks) {
|
|
599
|
+
if ('refused' in check) {
|
|
600
|
+
whole.push(check);
|
|
601
|
+
break;
|
|
602
|
+
}
|
|
603
|
+
if (!whole.some((done) => !('refused' in done) && sameCheck(done, check)))
|
|
604
|
+
whole.push(check);
|
|
605
|
+
}
|
|
606
|
+
const out = [];
|
|
607
|
+
const open = new Map();
|
|
608
|
+
for (const check of whole) {
|
|
609
|
+
if ('refused' in check || check.exists) {
|
|
610
|
+
out.push(check);
|
|
611
|
+
continue;
|
|
612
|
+
}
|
|
613
|
+
let members = open.get(check.fk);
|
|
614
|
+
if (!members || members.length >= KEYS_A_READ) {
|
|
615
|
+
open.set(check.fk, (members = []));
|
|
616
|
+
out.push(members);
|
|
617
|
+
}
|
|
618
|
+
members.push(check);
|
|
619
|
+
}
|
|
620
|
+
return out.map((entry) => {
|
|
621
|
+
if (!Array.isArray(entry))
|
|
622
|
+
return 'refused' in entry ? entry : { ...entry, whole };
|
|
623
|
+
const [first] = entry;
|
|
624
|
+
if (entry.length === 1)
|
|
625
|
+
return { ...first, whole };
|
|
626
|
+
return { ...first, filter: { groups: entry.flatMap((member) => member.filter.groups) }, members: entry, whole };
|
|
627
|
+
});
|
|
628
|
+
};
|
|
563
629
|
/** The checks sent at once (locks.together): each parent's lock, then its count, every answer heard. */
|
|
564
630
|
const ask = (checks) => checks.map((check) => {
|
|
565
631
|
if ('refused' in check)
|
|
@@ -573,13 +639,15 @@ referencesOf) {
|
|
|
573
639
|
/* Checks asked ahead of the row they are for (built from what the write
|
|
574
640
|
returned, or from what an update sets), used only where they name exactly
|
|
575
641
|
the references the row as stored holds: the same statements, sent sooner. */
|
|
576
|
-
const
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
642
|
+
const sameCheck = (guess, check) => {
|
|
643
|
+
if ('refused' in guess || 'refused' in check)
|
|
644
|
+
return 'refused' in guess && 'refused' in check && guess.refused === check.refused;
|
|
645
|
+
return (guess.fk === check.fk &&
|
|
646
|
+
guess.values.every((value, at) => sameBound(value, check.values[at], bindsText(check))) &&
|
|
647
|
+
(guess.members?.length ?? 0) === (check.members?.length ?? 0) &&
|
|
648
|
+
(guess.members ?? []).every((member, at) => sameCheck(member, check.members[at])));
|
|
649
|
+
};
|
|
650
|
+
const sameChecks = (asked, checks) => asked.length === checks.length && asked.every((guess, index) => sameCheck(guess, checks[index]));
|
|
583
651
|
/* References this transaction has proven: each parent locked and counted inside the principal's rows, which holds
|
|
584
652
|
until COMMIT (the lock), so the same reference is not asked again after the write that made it. */
|
|
585
653
|
const proven = [];
|
|
@@ -593,8 +661,8 @@ referencesOf) {
|
|
|
593
661
|
// A parent's visibility must hold until commit, not just at this read.
|
|
594
662
|
await locks?.parent(check.parent, check.filter);
|
|
595
663
|
if (missing(check, await guards.count(check.fk.to.table, check.filter)))
|
|
596
|
-
return refuse(unseen(check));
|
|
597
|
-
proven.push(check);
|
|
664
|
+
return check.whole ? verify(unproven(check.whole)) : refuse(unseen(check));
|
|
665
|
+
proven.push(...(check.members ?? [check]));
|
|
598
666
|
}
|
|
599
667
|
return;
|
|
600
668
|
}
|
|
@@ -606,8 +674,8 @@ referencesOf) {
|
|
|
606
674
|
return refuse(check.refused);
|
|
607
675
|
await check.locked;
|
|
608
676
|
if (missing(check, await check.counted))
|
|
609
|
-
return refuse(unseen(check));
|
|
610
|
-
proven.push(check);
|
|
677
|
+
return check.whole ? verify(unproven(check.whole)) : refuse(unseen(check));
|
|
678
|
+
proven.push(...(check.members ?? [check]));
|
|
611
679
|
}
|
|
612
680
|
};
|
|
613
681
|
const parents = async (table, row, set, guessed, given = false) => verify(unproven(parentChecks(table, row, set, given)), guessed);
|
|
@@ -652,25 +720,252 @@ referencesOf) {
|
|
|
652
720
|
reference, a value a trigger writes, a deferred check. */
|
|
653
721
|
/* A unique rule that refuses one names its column, and on PostgreSQL the value, of a row that may be outside the
|
|
654
722
|
principal's rows: that the value is taken there is what the scope keeps. It is let through only where the
|
|
655
|
-
conflicting row is one the principal reads (uniqueConflictSeen); otherwise it is the scope's own refusal.
|
|
656
|
-
|
|
723
|
+
conflicting row is one the principal reads (uniqueConflictSeen); otherwise it is the scope's own refusal. A batch's
|
|
724
|
+
statement does not say which of its rows conflicted, so it is let through only where every row's would be. */
|
|
725
|
+
const statement = (sent, table, rows = [{}]) => sent.catch((error) => {
|
|
657
726
|
if (isForeignKeyViolation(error))
|
|
658
727
|
throw foreignKeyRefusal(error);
|
|
659
728
|
if (isUniqueViolation(error) && table) {
|
|
660
729
|
const columns = uniqueColumns(error, table);
|
|
661
730
|
// With a scope present, a table it does not name has no rows the principal reads (shared/rowScope.ts).
|
|
662
731
|
const rule = scope === undefined ? true : Object.hasOwn(scope, table.id) ? scope[table.id] : false;
|
|
663
|
-
|
|
732
|
+
const sqlite = typeof error.errcode === 'number';
|
|
733
|
+
if (!columns || !rows.every((row) => uniqueConflictSeen(rule, columns, row, sqlite)))
|
|
664
734
|
throw uniqueRefusal(table, columns);
|
|
665
735
|
}
|
|
666
736
|
throw error;
|
|
667
737
|
});
|
|
668
|
-
const insertRow = (table, request) => statement(records.insert(request), table, request.values ?? {});
|
|
669
|
-
const updateRow = (table, request, row) => statement(records.update(request), table, row);
|
|
738
|
+
const insertRow = (table, request) => statement(records.insert(request), table, [request.values ?? {}]);
|
|
739
|
+
const updateRow = (table, request, row) => statement(records.update(request), table, [row]);
|
|
670
740
|
const afterward = async (afterwards, images) => {
|
|
671
741
|
for (const next of afterwards ?? [])
|
|
672
742
|
await next(images);
|
|
673
743
|
};
|
|
744
|
+
/* What a scoped update may not set: a key, or a column another table references, refused by name. Answers the set,
|
|
745
|
+
for the generated ones the update can still move (shiftedRefusal). */
|
|
746
|
+
const unmoved = (table, request) => {
|
|
747
|
+
const protectedColumns = new Set([
|
|
748
|
+
...table.columns.filter((column) => column.primaryKey).map((column) => column.name),
|
|
749
|
+
...catalog.schema.foreignKeys.filter((fk) => fk.to.table === table.id).flatMap((fk) => fk.to.columns),
|
|
750
|
+
]);
|
|
751
|
+
const moved = Object.keys(request.values ?? {}).filter((column) => protectedColumns.has(column));
|
|
752
|
+
if (moved.length)
|
|
753
|
+
return refuse(`A scoped update may not change "${table.id}".${moved.join(', ')}: a key, or a column another table references.`);
|
|
754
|
+
return protectedColumns;
|
|
755
|
+
};
|
|
756
|
+
/* A key or a referenced column the database generates moves with the columns its expression reads, which the
|
|
757
|
+
names above cannot catch: one that moved is refused, whatever a cascade or the foreign key made of it. */
|
|
758
|
+
const shiftedRefusal = (table, protectedColumns, before, now) => {
|
|
759
|
+
const shifted = [...protectedColumns].filter((column) => generated(table, column) && JSON.stringify(before[column]) !== JSON.stringify(now[column]));
|
|
760
|
+
if (shifted.length)
|
|
761
|
+
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.`);
|
|
762
|
+
};
|
|
763
|
+
const insert = async (request) => {
|
|
764
|
+
const table = target(request.table, { kind: 'insert' });
|
|
765
|
+
const afterwards = proof && (await proof.before(table, { kind: 'insert' }, proof.images.insert(table, request.values)));
|
|
766
|
+
const given = unproven(parentChecks(table, request.values ?? {}, undefined, true));
|
|
767
|
+
if (!locks?.together)
|
|
768
|
+
await verify(given);
|
|
769
|
+
const result = locks?.together ? await parentsFirst(given, () => insertRow(table, request)) : await insertRow(table, request);
|
|
770
|
+
const filter = keyFilter(table, result.key);
|
|
771
|
+
/* The new row locked and read back, and where the writer sends together
|
|
772
|
+
its references checked in the same flight, from the row the INSERT
|
|
773
|
+
returned (parents uses them only where the stored row holds exactly
|
|
774
|
+
those). No other transaction can see this row yet, so its lock is
|
|
775
|
+
granted at once and the read answers what a count before it would. */
|
|
776
|
+
const read = lockedRead(table, filter, guards);
|
|
777
|
+
const guessed = locks?.together && result.row ? ask(unproven(parentChecks(table, result.row))) : undefined;
|
|
778
|
+
read.catch(() => { });
|
|
779
|
+
const found = await read;
|
|
780
|
+
// Inside the bound and outside the principal's rows: the command's own row, answered by its key and what it wrote.
|
|
781
|
+
if (bound && found.rows.length !== 1) {
|
|
782
|
+
const within = await stored(table, result.key, bound.reads);
|
|
783
|
+
await parents(table, within, undefined, guessed);
|
|
784
|
+
await afterward(afterwards, { new: within, old: null });
|
|
785
|
+
return { key: result.key, applied: result.applied, sql: result.sql };
|
|
786
|
+
}
|
|
787
|
+
const row = one(table, result.key, found);
|
|
788
|
+
await parents(table, row, undefined, guessed);
|
|
789
|
+
await afterward(afterwards, { new: row, old: null });
|
|
790
|
+
madeBy(table, row);
|
|
791
|
+
return result;
|
|
792
|
+
};
|
|
793
|
+
const update = async (request) => {
|
|
794
|
+
const table = target(request.table, { kind: 'update', set: Object.keys(request.values ?? {}) });
|
|
795
|
+
const protectedColumns = unmoved(table, request);
|
|
796
|
+
const before = await stored(table, request.key);
|
|
797
|
+
const set = new Set(Object.keys(request.values ?? {}));
|
|
798
|
+
const afterwards = proof && (await proof.before(table, { kind: 'update', set: [...set] }, proof.images.update(table, before, request.values)));
|
|
799
|
+
// What the update sets is given; every other column of a reference it sets in part is as stored, bound as its text.
|
|
800
|
+
const written = overlaid(before, request.values);
|
|
801
|
+
const given = unproven(parentChecks(table, written, set, true));
|
|
802
|
+
if (!locks?.together)
|
|
803
|
+
await verify(given);
|
|
804
|
+
/* The row read again after the write, sent the moment the write is: it
|
|
805
|
+
reads what the write wrote, and leaves in the write's round trip, with
|
|
806
|
+
the references the write sets checked ahead of it, as the row will
|
|
807
|
+
hold them. A reference refused is the answer whatever the write said;
|
|
808
|
+
otherwise a write that fails aborts the read too, and the write's
|
|
809
|
+
failure is the one thrown. */
|
|
810
|
+
const after = {};
|
|
811
|
+
const withdraw = locks?.behind?.(table, () => {
|
|
812
|
+
after.row = stored(table, request.key);
|
|
813
|
+
after.row.catch(() => { });
|
|
814
|
+
});
|
|
815
|
+
let result;
|
|
816
|
+
try {
|
|
817
|
+
// The references it sets leave ahead of it, in its flight (parentsFirst).
|
|
818
|
+
result = locks?.together
|
|
819
|
+
? await parentsFirst(given, () => updateRow(table, request, written))
|
|
820
|
+
: await updateRow(table, request, written);
|
|
821
|
+
}
|
|
822
|
+
finally {
|
|
823
|
+
withdraw?.();
|
|
824
|
+
}
|
|
825
|
+
if (result.affected !== 1)
|
|
826
|
+
return refuse(`The update of "${table.id}" changed ${result.affected} rows, not one.`);
|
|
827
|
+
const now = await (after.row ?? stored(table, request.key));
|
|
828
|
+
shiftedRefusal(table, protectedColumns, before, now);
|
|
829
|
+
await parents(table, now, set);
|
|
830
|
+
await afterward(afterwards, { new: now, old: before });
|
|
831
|
+
return result;
|
|
832
|
+
};
|
|
833
|
+
/* A batch keeps every rule a row at a time keeps, and pays its flights once: the references it gives checked before
|
|
834
|
+
its one statement, a foreign key a read (combined), then one locked read of the rows it wrote, key IN (…), and
|
|
835
|
+
their references from the rows as stored. Where a row's rule needs the rows before it written first, the batch is
|
|
836
|
+
written a row at a time (oneAtATime), with the same outcome:
|
|
837
|
+
- a trigger that reads or writes another row (keyed): its proof reads before each row, and a later row's proof
|
|
838
|
+
may read what an earlier row's statement left;
|
|
839
|
+
- a table that references itself, or whose policy reaches its own rows: a row's parent may be a row the batch
|
|
840
|
+
writes before it, which a check sent ahead of the statement does not find;
|
|
841
|
+
- a reference that cannot be checked at all: a row at a time, the rows before it are written and checked first,
|
|
842
|
+
and the refusal said is theirs where they have one. */
|
|
843
|
+
const keyedTrigger = (table, write) => (catalog.triggered?.get(table.id) ?? []).some((trigger) => !trigger.validates && firedBy([trigger], write, catalog.schema.dialect === 'sqlite').length > 0);
|
|
844
|
+
const selfReferenced = (table) => {
|
|
845
|
+
const rule = scope?.[table.id];
|
|
846
|
+
return (catalog.schema.foreignKeys.some((fk) => fk.from.table === table.id && fk.to.table === table.id) ||
|
|
847
|
+
(!!rule &&
|
|
848
|
+
typeof rule === 'object' &&
|
|
849
|
+
rule.groups.some((group) => group.some((condition) => isViaCondition(condition) && condition.via.resource === table.id))));
|
|
850
|
+
};
|
|
851
|
+
const refusedIn = (checks) => checks.some((check) => 'refused' in check);
|
|
852
|
+
const insertMany = async (requests) => {
|
|
853
|
+
const table = target(sameTable(requests.map((request) => request.table)), { kind: 'insert' });
|
|
854
|
+
const given = requests.map((request) => parentChecks(table, request.values ?? {}, undefined, true));
|
|
855
|
+
if (requests.length < 2 || keyedTrigger(table, { kind: 'insert' }) || selfReferenced(table) || given.some(refusedIn))
|
|
856
|
+
return oneAtATime(requests, insert);
|
|
857
|
+
const afterwards = [];
|
|
858
|
+
for (const request of requests)
|
|
859
|
+
afterwards.push(proof && (await proof.before(table, { kind: 'insert' }, proof.images.insert(table, request.values))));
|
|
860
|
+
const checks = combined(unproven(given.flat()));
|
|
861
|
+
if (!locks?.together)
|
|
862
|
+
await verify(checks);
|
|
863
|
+
const write = () => statement(records.insertMany(requests), table, requests.map((request) => request.values ?? {}));
|
|
864
|
+
const results = locks?.together ? await parentsFirst(checks, write) : await write();
|
|
865
|
+
/* The rows locked and read back in one read, and where the writer sends together their references checked in the
|
|
866
|
+
same flight, from the rows the INSERT returned (verify uses them only where the stored rows hold exactly those). */
|
|
867
|
+
const read = lockedReads(table, results.map((result) => result.key));
|
|
868
|
+
const guessed = locks?.together && results.every((result) => result.row)
|
|
869
|
+
? ask(combined(unproven(results.flatMap((result) => parentChecks(table, result.row)))))
|
|
870
|
+
: undefined;
|
|
871
|
+
read.catch(() => { });
|
|
872
|
+
const found = await read;
|
|
873
|
+
const rows = [];
|
|
874
|
+
const outside = new Set();
|
|
875
|
+
for (const [index, result] of results.entries()) {
|
|
876
|
+
const row = found.get(keyId(table, result.key));
|
|
877
|
+
if (row) {
|
|
878
|
+
rows.push(one(table, result.key, { rows: [row] }));
|
|
879
|
+
continue;
|
|
880
|
+
}
|
|
881
|
+
// A key the read spelled otherwise is read again alone, as a row at a time reads it.
|
|
882
|
+
const alone = await lockedRead(table, keyFilter(table, result.key), guards);
|
|
883
|
+
// Inside the bound and outside the principal's rows: the command's own row, answered by its key and what it wrote.
|
|
884
|
+
if (bound && alone.rows.length !== 1) {
|
|
885
|
+
rows.push(await stored(table, result.key, bound.reads));
|
|
886
|
+
outside.add(index);
|
|
887
|
+
}
|
|
888
|
+
else
|
|
889
|
+
rows.push(one(table, result.key, alone));
|
|
890
|
+
}
|
|
891
|
+
await verify(combined(unproven(rows.flatMap((row) => parentChecks(table, row)))), guessed);
|
|
892
|
+
for (const [index, row] of rows.entries())
|
|
893
|
+
await afterward(afterwards[index], { new: row, old: null });
|
|
894
|
+
madeBy(table, ...rows.filter((_, index) => !outside.has(index)));
|
|
895
|
+
return results.map((result, index) => (outside.has(index) ? { key: result.key, applied: result.applied, sql: result.sql } : result));
|
|
896
|
+
};
|
|
897
|
+
/* An update's batch likewise: the rows read before it in one locked read, the statement with the read after it sent
|
|
898
|
+
behind it (locks.behind), every row's references the update sets checked before and after. A row a batch names
|
|
899
|
+
twice, or one a row at a time would refuse before its statement, is written a row at a time. */
|
|
900
|
+
const updateMany = async (requests) => {
|
|
901
|
+
const id = sameTable(requests.map((request) => request.table));
|
|
902
|
+
let table;
|
|
903
|
+
let protectedColumns = new Set();
|
|
904
|
+
const sets = requests.map((request) => new Set(Object.keys(request.values ?? {})));
|
|
905
|
+
try {
|
|
906
|
+
for (const [index, request] of requests.entries()) {
|
|
907
|
+
table = target(id, { kind: 'update', set: [...sets[index]] });
|
|
908
|
+
protectedColumns = unmoved(table, request);
|
|
909
|
+
keyFilter(table, request.key);
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
catch {
|
|
913
|
+
return oneAtATime(requests, update);
|
|
914
|
+
}
|
|
915
|
+
if (!table ||
|
|
916
|
+
requests.length < 2 ||
|
|
917
|
+
new Set(requests.map((request) => keyId(table, request.key))).size !== requests.length ||
|
|
918
|
+
sets.some((set) => keyedTrigger(table, { kind: 'update', set: [...set] })) ||
|
|
919
|
+
selfReferenced(table))
|
|
920
|
+
return oneAtATime(requests, update);
|
|
921
|
+
const at = table;
|
|
922
|
+
const keys = requests.map((request) => request.key);
|
|
923
|
+
const found = await lockedReads(at, keys);
|
|
924
|
+
const befores = [];
|
|
925
|
+
for (const key of keys)
|
|
926
|
+
befores.push(await storedFrom(at, key, found));
|
|
927
|
+
// What each update sets is given; every other column of a reference it sets in part is as stored, bound as its text.
|
|
928
|
+
const written = requests.map((request, index) => overlaid(befores[index], request.values));
|
|
929
|
+
const given = written.map((row, index) => parentChecks(at, row, sets[index], true));
|
|
930
|
+
if (given.some(refusedIn))
|
|
931
|
+
return oneAtATime(requests, update);
|
|
932
|
+
const afterwards = [];
|
|
933
|
+
for (const [index, request] of requests.entries())
|
|
934
|
+
afterwards.push(proof &&
|
|
935
|
+
(await proof.before(at, { kind: 'update', set: [...sets[index]] }, proof.images.update(at, befores[index], request.values))));
|
|
936
|
+
const checks = combined(unproven(given.flat()));
|
|
937
|
+
if (!locks?.together)
|
|
938
|
+
await verify(checks);
|
|
939
|
+
// The rows read again after the write, sent the moment the (first) statement is, in its round trip.
|
|
940
|
+
const after = {};
|
|
941
|
+
const withdraw = locks?.behind?.(at, () => {
|
|
942
|
+
after.read = lockedReads(at, keys);
|
|
943
|
+
after.read.catch(() => { });
|
|
944
|
+
});
|
|
945
|
+
let results;
|
|
946
|
+
try {
|
|
947
|
+
const write = () => statement(records.updateMany(requests), at, written);
|
|
948
|
+
results = locks?.together ? await parentsFirst(checks, write) : await write();
|
|
949
|
+
}
|
|
950
|
+
finally {
|
|
951
|
+
withdraw?.();
|
|
952
|
+
}
|
|
953
|
+
for (const result of results)
|
|
954
|
+
if (result.affected !== 1)
|
|
955
|
+
return refuse(`The update of "${at.id}" changed ${result.affected} rows, not one.`);
|
|
956
|
+
// A batch sent as several statements was read behind the first alone: read again once they all have run.
|
|
957
|
+
const statements = new Set(results.map((result) => result.sql.params));
|
|
958
|
+
const now = statements.size > 1 || !after.read ? await lockedReads(at, keys) : await after.read;
|
|
959
|
+
const rows = [];
|
|
960
|
+
for (const key of keys)
|
|
961
|
+
rows.push(await storedFrom(at, key, now));
|
|
962
|
+
for (const [index, row] of rows.entries())
|
|
963
|
+
shiftedRefusal(at, protectedColumns, befores[index], row);
|
|
964
|
+
await verify(combined(unproven(rows.flatMap((row, index) => parentChecks(at, row, sets[index])))));
|
|
965
|
+
for (const [index, row] of rows.entries())
|
|
966
|
+
await afterward(afterwards[index], { new: row, old: befores[index] });
|
|
967
|
+
return results;
|
|
968
|
+
};
|
|
674
969
|
return Object.freeze({
|
|
675
970
|
query: (request) => reads.query(request),
|
|
676
971
|
count: (table, filter) => reads.count(table, filter),
|
|
@@ -680,86 +975,9 @@ referencesOf) {
|
|
|
680
975
|
aggregate: (request) => reads.aggregate ? reads.aggregate(request) : refuse('This scope cannot aggregate.'),
|
|
681
976
|
}
|
|
682
977
|
: {}),
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
const given = unproven(parentChecks(table, request.values ?? {}, undefined, true));
|
|
687
|
-
if (!locks?.together)
|
|
688
|
-
await verify(given);
|
|
689
|
-
const result = locks?.together ? await parentsFirst(given, () => insertRow(table, request)) : await insertRow(table, request);
|
|
690
|
-
const filter = keyFilter(table, result.key);
|
|
691
|
-
/* The new row locked and read back, and where the writer sends together
|
|
692
|
-
its references checked in the same flight, from the row the INSERT
|
|
693
|
-
returned (parents uses them only where the stored row holds exactly
|
|
694
|
-
those). No other transaction can see this row yet, so its lock is
|
|
695
|
-
granted at once and the read answers what a count before it would. */
|
|
696
|
-
const read = lockedRead(table, filter, guards);
|
|
697
|
-
const guessed = locks?.together && result.row ? ask(unproven(parentChecks(table, result.row))) : undefined;
|
|
698
|
-
read.catch(() => { });
|
|
699
|
-
const found = await read;
|
|
700
|
-
// Inside the bound and outside the principal's rows: the command's own row, answered by its key and what it wrote.
|
|
701
|
-
if (bound && found.rows.length !== 1) {
|
|
702
|
-
const within = await stored(table, result.key, bound.reads);
|
|
703
|
-
await parents(table, within, undefined, guessed);
|
|
704
|
-
await afterward(afterwards, { new: within, old: null });
|
|
705
|
-
return { key: result.key, applied: result.applied, sql: result.sql };
|
|
706
|
-
}
|
|
707
|
-
const row = one(table, result.key, found);
|
|
708
|
-
await parents(table, row, undefined, guessed);
|
|
709
|
-
await afterward(afterwards, { new: row, old: null });
|
|
710
|
-
madeBy(table, row);
|
|
711
|
-
return result;
|
|
712
|
-
},
|
|
713
|
-
async update(request) {
|
|
714
|
-
const table = target(request.table, { kind: 'update', set: Object.keys(request.values ?? {}) });
|
|
715
|
-
const protectedColumns = new Set([
|
|
716
|
-
...table.columns.filter((column) => column.primaryKey).map((column) => column.name),
|
|
717
|
-
...catalog.schema.foreignKeys.filter((fk) => fk.to.table === table.id).flatMap((fk) => fk.to.columns),
|
|
718
|
-
]);
|
|
719
|
-
const moved = Object.keys(request.values ?? {}).filter((column) => protectedColumns.has(column));
|
|
720
|
-
if (moved.length)
|
|
721
|
-
return refuse(`A scoped update may not change "${table.id}".${moved.join(', ')}: a key, or a column another table references.`);
|
|
722
|
-
const before = await stored(table, request.key);
|
|
723
|
-
const set = new Set(Object.keys(request.values ?? {}));
|
|
724
|
-
const afterwards = proof && (await proof.before(table, { kind: 'update', set: [...set] }, proof.images.update(table, before, request.values)));
|
|
725
|
-
// What the update sets is given; every other column of a reference it sets in part is as stored, bound as its text.
|
|
726
|
-
const written = overlaid(before, request.values);
|
|
727
|
-
const given = unproven(parentChecks(table, written, set, true));
|
|
728
|
-
if (!locks?.together)
|
|
729
|
-
await verify(given);
|
|
730
|
-
/* The row read again after the write, sent the moment the write is: it
|
|
731
|
-
reads what the write wrote, and leaves in the write's round trip, with
|
|
732
|
-
the references the write sets checked ahead of it, as the row will
|
|
733
|
-
hold them. A reference refused is the answer whatever the write said;
|
|
734
|
-
otherwise a write that fails aborts the read too, and the write's
|
|
735
|
-
failure is the one thrown. */
|
|
736
|
-
const after = {};
|
|
737
|
-
const withdraw = locks?.behind?.(table, () => {
|
|
738
|
-
after.row = stored(table, request.key);
|
|
739
|
-
after.row.catch(() => { });
|
|
740
|
-
});
|
|
741
|
-
let result;
|
|
742
|
-
try {
|
|
743
|
-
// The references it sets leave ahead of it, in its flight (parentsFirst).
|
|
744
|
-
result = locks?.together
|
|
745
|
-
? await parentsFirst(given, () => updateRow(table, request, written))
|
|
746
|
-
: await updateRow(table, request, written);
|
|
747
|
-
}
|
|
748
|
-
finally {
|
|
749
|
-
withdraw?.();
|
|
750
|
-
}
|
|
751
|
-
if (result.affected !== 1)
|
|
752
|
-
return refuse(`The update of "${table.id}" changed ${result.affected} rows, not one.`);
|
|
753
|
-
const now = await (after.row ?? stored(table, request.key));
|
|
754
|
-
/* A key or a referenced column the database generates moves with the columns its expression reads, which the
|
|
755
|
-
names above cannot catch: one that moved is refused, whatever a cascade or the foreign key made of it. */
|
|
756
|
-
const shifted = [...protectedColumns].filter((column) => generated(table, column) && JSON.stringify(before[column]) !== JSON.stringify(now[column]));
|
|
757
|
-
if (shifted.length)
|
|
758
|
-
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.`);
|
|
759
|
-
await parents(table, now, set);
|
|
760
|
-
await afterward(afterwards, { new: now, old: before });
|
|
761
|
-
return result;
|
|
762
|
-
},
|
|
978
|
+
insert,
|
|
979
|
+
update,
|
|
980
|
+
...(records.insertMany && records.updateMany ? { insertMany, updateMany } : {}),
|
|
763
981
|
async remove(request) {
|
|
764
982
|
const table = target(request.table, { kind: 'delete' });
|
|
765
983
|
const children = catalog.schema.foreignKeys.filter((fk) => fk.to.table === table.id);
|
|
@@ -130,7 +130,7 @@ export function createTriggerProof(acceptance, tools) {
|
|
|
130
130
|
if (given.via !== undefined)
|
|
131
131
|
return UNKNOWN;
|
|
132
132
|
const condition = given;
|
|
133
|
-
if (condition.other !== undefined || condition.any !== undefined || condition.collation !== undefined)
|
|
133
|
+
if (condition.other !== undefined || condition.any !== undefined || condition.within !== undefined || condition.collation !== undefined)
|
|
134
134
|
return UNKNOWN;
|
|
135
135
|
if (!Object.hasOwn(row, condition.column))
|
|
136
136
|
return UNKNOWN;
|
package/dist/adapters/sqlite.js
CHANGED
|
@@ -12,7 +12,7 @@ import { waitForLocks, assertIndependentWrite, writeToFile, prepareGuarded, tran
|
|
|
12
12
|
/* The tenant sources keep their catalog and store-layout checks by these (tenantReadSource.ts, tenantStore.ts). */
|
|
13
13
|
export { cachedStatement, schemaFingerprint } from './sqliteHandles.js';
|
|
14
14
|
import { captureScopeCatalog, mapScopeCatalog } from '../shared/scopeCatalog.js';
|
|
15
|
-
import { inlineRows } from '../shared/anyRows.js';
|
|
15
|
+
import { inlineRows, inlineWithin } from '../shared/anyRows.js';
|
|
16
16
|
import { asksAboutRows } from '../shared/filter.js';
|
|
17
17
|
import { qualifiedTable } from '../shared/sqlRelation.js';
|
|
18
18
|
import { DatabaseSync } from 'node:sqlite';
|
|
@@ -499,7 +499,9 @@ export class SqliteAdapter {
|
|
|
499
499
|
return buildWhere(filter, ANSI_STYLE);
|
|
500
500
|
const schema = await this.schema().catch(() => undefined);
|
|
501
501
|
const inline = schema && inlineRows(schema, id, ANSI_STYLE.quote(relation), ANSI_STYLE, (child) => qualifiedTable(schema, child.id, ANSI_STYLE));
|
|
502
|
-
|
|
502
|
+
/* Another table's keys (`in`) are read as this table is, from its own relation: the same bound. */
|
|
503
|
+
const within = schema && inlineWithin(schema, ANSI_STYLE, (other) => qualifiedTable(schema, other.id, ANSI_STYLE));
|
|
504
|
+
return buildWhere(filter, ANSI_STYLE, 1, undefined, undefined, undefined, undefined, false, undefined, false, inline, within);
|
|
503
505
|
}
|
|
504
506
|
async queryOn(request, db, read = false) {
|
|
505
507
|
const table = read ? this.readTable(request.table) : this.assertTable(request.table, db);
|
|
@@ -41,6 +41,9 @@ export interface StartupConfig {
|
|
|
41
41
|
/** `"presentation": { "<app>": { name, logo, theme, locale } }`, as written: one deployment's name, logo, theme and
|
|
42
42
|
locale for an App, which the App framework checks and applies (mistri app/appPresentation.ts). Never read by the browser. */
|
|
43
43
|
presentation?: unknown;
|
|
44
|
+
/** `"auth": { "<app>": { "store": "…" } }`, as written: where one deployment keeps an App's accounts and sessions,
|
|
45
|
+
which wins over the App's own `auth.store` (mistri app/deploymentAuth.ts). Never read by the browser. */
|
|
46
|
+
auth?: unknown;
|
|
44
47
|
}
|
|
45
48
|
/**
|
|
46
49
|
* Whether the config file a start would read names Apps: read raw, before anything is said of it, so the database
|
|
@@ -182,9 +182,11 @@ export async function readStartupConfig(opts, registry, given) {
|
|
|
182
182
|
}
|
|
183
183
|
/* Where the Apps are served, beside which Apps: a deployment's fact,
|
|
184
184
|
checked with the flags' own rule once the Apps are loaded (main). */
|
|
185
|
-
const { mount, basePath, presentation } = JSON.parse(await readFile(config.path, 'utf8'));
|
|
185
|
+
const { mount, basePath, presentation, auth } = JSON.parse(await readFile(config.path, 'utf8'));
|
|
186
186
|
if (presentation !== undefined)
|
|
187
187
|
read.presentation = presentation;
|
|
188
|
+
if (auth !== undefined)
|
|
189
|
+
read.auth = auth;
|
|
188
190
|
if (mount !== undefined) {
|
|
189
191
|
if (!mount || typeof mount !== 'object' || Array.isArray(mount) || Object.values(mount).some((at) => typeof at !== 'string')) {
|
|
190
192
|
throw new Error(`${config.path}: "mount" must name each App's place by its id, like { "main": "/", "billing": "/billing" }.`);
|
|
@@ -412,10 +412,30 @@ export function resourceChecks(full, access) {
|
|
|
412
412
|
for (const condition of group)
|
|
413
413
|
if (condition.any)
|
|
414
414
|
assertAnyRows({ groups: [[{ ...condition, path: condition.column }]] });
|
|
415
|
-
else
|
|
415
|
+
else {
|
|
416
416
|
assertFields(table, [condition.column], 'read', server);
|
|
417
|
+
if (condition.within !== undefined)
|
|
418
|
+
assertWithin(condition.within, server);
|
|
419
|
+
}
|
|
417
420
|
return filter;
|
|
418
421
|
};
|
|
422
|
+
/* `customer_id in (customer where region = west)` reads another table's rows: that table, the key the subquery
|
|
423
|
+
selects and every column its conditions name must be this reader's, as a read of that table would need. Its scope
|
|
424
|
+
is the statement's (scopedReads.ts), so a row policy on it narrows the keys as it narrows a list of its rows. */
|
|
425
|
+
const assertWithin = (within, server) => {
|
|
426
|
+
if (!within || typeof within !== 'object' || typeof within.table !== 'string')
|
|
427
|
+
throw new AppAccessDenied();
|
|
428
|
+
assertRead(within.table);
|
|
429
|
+
const key = schema.tables.find((item) => item.id === within.table)?.columns.filter((column) => column.primaryKey) ?? [];
|
|
430
|
+
if (key.length !== 1)
|
|
431
|
+
throw new AppAccessDenied();
|
|
432
|
+
assertFields(within.table, [key[0].name], 'read', server);
|
|
433
|
+
for (const inner of within.filter?.groups.flat() ?? []) {
|
|
434
|
+
if (typeof inner.column !== 'string')
|
|
435
|
+
throw new AppAccessDenied();
|
|
436
|
+
assertFields(within.table, [inner.column, ...(inner.other === undefined ? [] : [inner.other])], 'read', server);
|
|
437
|
+
}
|
|
438
|
+
};
|
|
419
439
|
const queryFor = (input) => {
|
|
420
440
|
const server = isServerRequest(input);
|
|
421
441
|
const request = requestData(input);
|
|
@@ -53,6 +53,13 @@ interface ResourceReadHttpOptions {
|
|
|
53
53
|
* by another name.
|
|
54
54
|
*/
|
|
55
55
|
rowsMost?: (table: string) => number | undefined;
|
|
56
|
+
/**
|
|
57
|
+
* Asked just before a route's last wave of reads, where the routes below
|
|
58
|
+
* can say which that is: every read after it leaves in that one wave. A
|
|
59
|
+
* tenant App's bounded request lets its release fence leave with it
|
|
60
|
+
* (TenantReadContext.lastWave). Internal; absent, nothing.
|
|
61
|
+
*/
|
|
62
|
+
lastWave?: () => void;
|
|
56
63
|
}
|
|
57
64
|
interface ResourceReadHttpReply {
|
|
58
65
|
status: number;
|