@mistriwork/engine 1.0.0-rc.73 → 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.
@@ -429,6 +429,11 @@ export class HttpAdapter {
429
429
  return group;
430
430
  const seen = new Set();
431
431
  for (const condition of group) {
432
+ // A column tested against another table's rows (`in: { from }`) is a subquery no API is asked.
433
+ if (condition.within)
434
+ throw new Refusal(`${resource.name} comes from an API, so "${condition.column}" cannot be tested against ${condition.within.table}'s rows.`, {
435
+ columns: [condition.column],
436
+ });
432
437
  const declared = resource.filters[condition.column];
433
438
  const op = condition.op;
434
439
  if (!declared || (op !== '=' && op !== 'in') || !declared.ops.includes(op)) {
@@ -8,7 +8,7 @@
8
8
  * connect, or the leased writer inside a scoped transaction.
9
9
  */
10
10
  import { isProxy } from 'node:util/types';
11
- import { inlineRows } from '../shared/anyRows.js';
11
+ import { inlineRows, inlineWithin } from '../shared/anyRows.js';
12
12
  import { asksAboutRows } from '../shared/filter.js';
13
13
  import { qualifiedTable } from '../shared/sqlRelation.js';
14
14
  import { POSTGRES_STYLE, buildWhere, clampLimit, clampOffset, Refusal, WriteConflict, TransactionOperations, queueRecordLock, recordLockFilter, TransactionOutcomeUnknown, countStatement, countEachStatement, countEachRows, countsTogether, orderClause, lanesStatement, withoutLaneRow, refuseUpdateMatches, } from './adapter.js';
@@ -529,7 +529,9 @@ export class PostgresAdapter {
529
529
  return buildWhere(filter, POSTGRES_STYLE);
530
530
  const schema = await this.schema().catch(() => undefined);
531
531
  const inline = schema && inlineRows(schema, id, qualified, POSTGRES_STYLE, (child) => qualifiedTable(schema, child.id, POSTGRES_STYLE));
532
- return buildWhere(filter, POSTGRES_STYLE, 1, undefined, undefined, undefined, undefined, false, undefined, false, inline);
532
+ /* Another table's keys (`in`) are read as this table is, from its own relation: the same bound. */
533
+ const within = schema && inlineWithin(schema, POSTGRES_STYLE, (other) => qualifiedTable(schema, other.id, POSTGRES_STYLE));
534
+ return buildWhere(filter, POSTGRES_STYLE, 1, undefined, undefined, undefined, undefined, false, undefined, false, inline, within);
533
535
  }
534
536
  async queryOn(request, db, read = false) {
535
537
  const { schema, name } = read ? await this.readTable(request.table) : await this.assertTable(request.table, db);
@@ -815,11 +817,11 @@ export class PostgresAdapter {
815
817
  return { found, meta, applied };
816
818
  }
817
819
  /** Several rows of one table added by one statement inside a transaction, each refused as `insert` refuses one (postgresBatchWrites). */
818
- async insertManyNow(requests, write) {
820
+ async insertManyNow(requests, write, scoped = false) {
819
821
  const plans = [];
820
822
  for (const request of requests)
821
823
  plans.push(await this.insertPlan(request, write));
822
- return insertPlanned(plans, write);
824
+ return insertPlanned(plans, write, scoped);
823
825
  }
824
826
  /** Several rows of one table changed by one statement inside a transaction, each refused as `update` refuses one. */
825
827
  async updateManyNow(requests, write) {
@@ -1489,6 +1491,9 @@ export class PostgresAdapter {
1489
1491
  insert: (r) => this.insertNow(r, write, write, true),
1490
1492
  remove: (r) => this.removeNow(r, write, write),
1491
1493
  update: (r) => this.updateNow(r, write, write),
1494
+ // The row scope proves a batch's rows as it proves one row's, around the one statement (scopedTransaction.ts).
1495
+ insertMany: (rs) => this.insertManyNow(rs, write, true),
1496
+ updateMany: (rs) => this.updateManyNow(rs, write),
1492
1497
  },
1493
1498
  // Filed under the table's id, as the rows sent ahead are; a read asked in its turn is sent as it was asked.
1494
1499
  (request) => {
@@ -1509,6 +1514,7 @@ export class PostgresAdapter {
1509
1514
  insert: writes.insert,
1510
1515
  remove: writes.remove,
1511
1516
  update: writes.update,
1517
+ ...(writes.insertMany && writes.updateMany ? { insertMany: writes.insertMany, updateMany: writes.updateMany } : {}),
1512
1518
  }, scopedReads(false), scopedReads(true), {
1513
1519
  target: lock('UPDATE'),
1514
1520
  parent: lock('SHARE'),
@@ -1559,6 +1565,12 @@ export class PostgresAdapter {
1559
1565
  insert: (r) => invoke(r, (r) => records.insert(r)),
1560
1566
  remove: (r) => invoke(r, (r) => records.remove(r)),
1561
1567
  update: (r) => invoke(r, (r) => records.update(r)),
1568
+ ...(records.insertMany && records.updateMany
1569
+ ? {
1570
+ insertMany: (rs) => invoke(rs, (rs) => records.insertMany(rs)),
1571
+ updateMany: (rs) => invoke(rs, (rs) => records.updateMany(rs)),
1572
+ }
1573
+ : {}),
1562
1574
  // Beneath the row scope: the flows' command tells only of its own target, which it read through the scope.
1563
1575
  ...(writes.tell ? { tell: (r) => invoke(r, (r) => writes.tell(r)) } : {}),
1564
1576
  ...(writes.noted ? { noted: (r) => invoke(r, (r) => writes.noted(r)) } : {}),
@@ -19,7 +19,7 @@ export interface UpdatePlan extends InsertPlan {
19
19
  * `INSERT … RETURNING *` for each run of rows naming the same columns, its rows matched back to the
20
20
  * requests that wrote them (batchedRows).
21
21
  */
22
- export declare function insertPlanned(plans: readonly InsertPlan[], write: PgQuery): Promise<InsertResult[]>;
22
+ export declare function insertPlanned(plans: readonly InsertPlan[], write: PgQuery, scoped?: boolean): Promise<InsertResult[]>;
23
23
  /**
24
24
  * Rows changed inside a transaction, each as the adapter's single update changes one: each row's own values and
25
25
  * `expected`, one row a key. A `CASE` a column picks each row's value, the WHERE matches each key with its own
@@ -7,7 +7,7 @@ import { buildUpdateGuard } from './scoped/updateGuard.js';
7
7
  * `INSERT … RETURNING *` for each run of rows naming the same columns, its rows matched back to the
8
8
  * requests that wrote them (batchedRows).
9
9
  */
10
- export async function insertPlanned(plans, write) {
10
+ export async function insertPlanned(plans, write, scoped = false) {
11
11
  sameTable(plans.map((plan) => `${plan.found.schema}.${plan.found.name}`));
12
12
  const found = plans[0].found;
13
13
  const pk = plans[0].meta.filter((c) => c.is_pk).map((c) => c.name);
@@ -26,6 +26,11 @@ export async function insertPlanned(plans, write) {
26
26
  ' RETURNING *';
27
27
  const result = await write.query(text, params);
28
28
  batchedRows(chunk.map((plan) => plan.applied), result.rows).forEach((stored, index) => {
29
+ /* A scope re-reads each stored row by its key, as the single insert's does (postgres.ts insertNow): a key that
30
+ comes back as no text or number (a bytea's label) proves nothing, and is refused before it stands in. */
31
+ if (scoped &&
32
+ pk.some((column) => !(typeof stored[column] === 'string' || (typeof stored[column] === 'number' && Number.isFinite(stored[column])))))
33
+ throw new Refusal('The stored key cannot be checked in this row scope.');
29
34
  const row = toJsonSafe(stored);
30
35
  const key = {};
31
36
  for (const c of pk)
@@ -13,9 +13,12 @@ export function scopedCommandInvoker(operations, scoped, command, runtime, verif
13
13
  return (input, work, read = false) => {
14
14
  let captured = input;
15
15
  let invalid;
16
+ /* A batch (insertMany, updateMany) is its rows' requests, each captured and judged as it would be alone: one row's
17
+ bounds, not one bound over the whole batch. */
18
+ const batch = Array.isArray(input);
16
19
  if (scoped) {
17
20
  try {
18
- captured = snapshotScopedData(input);
21
+ captured = (batch ? input.map((request) => snapshotScopedData(request)) : snapshotScopedData(input));
19
22
  }
20
23
  catch (error) {
21
24
  invalid = { error };
@@ -30,11 +33,7 @@ export function scopedCommandInvoker(operations, scoped, command, runtime, verif
30
33
  verify();
31
34
  if (command &&
32
35
  scoped &&
33
- captured &&
34
- typeof captured === 'object' &&
35
- 'table' in captured &&
36
- typeof captured.table === 'string' &&
37
- runtime(captured.table)) {
36
+ (batch ? captured : [captured]).some((request) => request && typeof request === 'object' && 'table' in request && typeof request.table === 'string' && runtime(request.table))) {
38
37
  throw new Refusal('Reserved resources are runtime-only.');
39
38
  }
40
39
  const result = await work(captured);
@@ -6,7 +6,7 @@ import { ScopedRelations, snapshotRowScope } from '../../shared/rowScope.js';
6
6
  import { compileView, compileViewCount } from '../../shared/view.js';
7
7
  import { compileBreakdown, compileBreakdownCount } from '../../shared/breakdown.js';
8
8
  import { compileAggregate } from '../../shared/aggregate.js';
9
- import { childRows } from '../../shared/anyRows.js';
9
+ import { childRows, plannedWithin, withinRows } from '../../shared/anyRows.js';
10
10
  import { readExactly, summed } from '../../shared/decimals.js';
11
11
  import { RequestFault } from '../../shared/refusal.js';
12
12
  import { storedTextOf, storedTextSelect } from './storedText.js';
@@ -118,8 +118,18 @@ references = false) {
118
118
  child is a relation of this statement, planned here with its values bound ahead of the outer filter's, so the
119
119
  EXISTS carries none. A child the reader's catalog does not hold is refused (ScopedRelations.table). */
120
120
  const planned = new Map();
121
+ /* Another table's keys (`in`) are read through the reader's own scope too: the same relation a direct read of that
122
+ table is (its tenant, its row policy), narrowed by the test's own conditions, planned here so its values bind
123
+ ahead of the outer filter's and the subquery carries none. A table the reader's catalog does not hold is
124
+ refused (ScopedRelations.table). */
125
+ const subqueries = new Map();
121
126
  for (const group of filter?.groups ?? []) {
122
127
  for (const c of group) {
128
+ if (c.within !== undefined && !subqueries.has(c.within)) {
129
+ const { table: other, key, type } = withinRows(schema, c.within);
130
+ const source = c.within.filter?.groups.length ? relations.filtered(other.id, c.within.filter) : relations.table(other.id);
131
+ subqueries.set(c.within, plannedWithin(style, source, key, type));
132
+ }
123
133
  if (c.any === undefined || planned.has(c.any))
124
134
  continue;
125
135
  const { child, join } = childRows(schema, id, c.any, style);
@@ -135,7 +145,13 @@ references = false) {
135
145
  throw new RequestFault('This read cannot ask about the rows that point at it.');
136
146
  })()
137
147
  : undefined;
138
- const where = buildWhere(filter, style, prefix.params.length + 1, column, undefined, undefined, exists, references, typeIn(table));
148
+ const within = subqueries.size
149
+ ? (rows) => subqueries.get(rows) ??
150
+ (() => {
151
+ throw new RequestFault("This read cannot compare a column with another table's rows.");
152
+ })()
153
+ : undefined;
154
+ const where = buildWhere(filter, style, prefix.params.length + 1, column, undefined, undefined, exists, references, typeIn(table), false, undefined, within);
139
155
  return { table, column, relation, prefix, where, params: [...prefix.params, ...where.params] };
140
156
  };
141
157
  /* A SQLite sum that cannot be exact says so in its cell (decimals.ts); PostgreSQL's numeric always is. */
@@ -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, row) => {
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) ?? []), row]);
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: 2, offset: 0, ...(texts ? { storedText: true } : {}) });
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) => (check.exists ? counted < 1 : counted !== 1);
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 sameChecks = (asked, checks) => asked.length === checks.length &&
577
- asked.every((guess, index) => {
578
- const check = checks[index];
579
- if ('refused' in guess || 'refused' in check)
580
- return 'refused' in guess && 'refused' in check && guess.refused === check.refused;
581
- return guess.fk === check.fk && guess.values.every((value, at) => sameBound(value, check.values[at], bindsText(check)));
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
- const statement = (sent, table, row) => sent.catch((error) => {
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
- if (!columns || !uniqueConflictSeen(rule, columns, row ?? {}, typeof error.errcode === 'number'))
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
- async insert(request) {
684
- const table = target(request.table, { kind: 'insert' });
685
- const afterwards = proof && (await proof.before(table, { kind: 'insert' }, proof.images.insert(table, request.values)));
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;
@@ -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
- return buildWhere(filter, ANSI_STYLE, 1, undefined, undefined, undefined, undefined, false, undefined, false, inline);
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);
@@ -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);
@@ -122,6 +122,28 @@ export function assertStructuredReadFilter(filter, field) {
122
122
  assertStructuredReadFilter(any.filter, 'column');
123
123
  continue;
124
124
  }
125
+ /* The key of another table's rows (`customer_id in (customer where …)`, Condition.within): a command's read, one
126
+ level deep, its own conditions plain comparisons judged by the same rule. The read's scope plans that table
127
+ (scopedReads.ts), so a tenant's subquery keys only its own rows. */
128
+ if (field === 'column' && condition.within !== undefined) {
129
+ const within = condition.within;
130
+ if (Object.keys(condition).some((key) => !['column', 'op', 'within'].includes(key)) ||
131
+ typeof condition.column !== 'string' ||
132
+ !condition.column.length ||
133
+ condition.op !== 'in' ||
134
+ !within ||
135
+ typeof within !== 'object' ||
136
+ Array.isArray(within) ||
137
+ Object.keys(within).some((key) => key !== 'table' && key !== 'filter') ||
138
+ typeof within.table !== 'string' ||
139
+ !within.table.length)
140
+ fail();
141
+ assertStructuredReadFilter(within.filter, 'column');
142
+ const inner = within.filter;
143
+ if (inner?.groups.some((group) => group.some((nested) => nested.within !== undefined)))
144
+ fail();
145
+ continue;
146
+ }
125
147
  if (Object.keys(condition).some((key) => ![field, 'op', 'value', 'collation', ...(other ? ['other'] : [])].includes(key)) ||
126
148
  typeof condition[field] !== 'string' ||
127
149
  !condition[field].length)
@@ -61,9 +61,9 @@ function aggregateBreakdown(schema, request) {
61
61
  };
62
62
  });
63
63
  const groups = (request.filter?.groups ?? []).map((group) => group.map((condition) => {
64
- /* A comparison with another column, or rows that point here (`any`), reads past this table's own values:
64
+ /* A comparison with another column, rows that point here (`any`), or another table's rows (`within`), reads past this table's own values:
65
65
  neither is a condition a command's where writes (compileWhere), and neither is taken here. */
66
- if (condition.any !== undefined || condition.other !== undefined)
66
+ if (condition.any !== undefined || condition.other !== undefined || condition.within !== undefined)
67
67
  throw new RequestFault("An aggregate's where compares the table's own columns with values.");
68
68
  return {
69
69
  path: column(condition.column, 'where'),
@@ -1,4 +1,4 @@
1
- import type { AnyRows, InlineRows, SqlStyle } from './filter.js';
1
+ import type { AnyRows, InlineRows, SqlStyle, WithinPlan, WithinRows } from './filter.js';
2
2
  import { type DatabaseSchema, type TableMeta } from './schema.js';
3
3
  interface ChildRows {
4
4
  readonly child: TableMeta;
@@ -16,4 +16,22 @@ export declare function childRows(schema: DatabaseSchema, parentId: string, rows
16
16
  * child is read from its own relation, as the parent is, with no scope between.
17
17
  */
18
18
  export declare function inlineRows(schema: DatabaseSchema, parentId: string, parent: string, style: SqlStyle, relation: (table: TableMeta) => string): (rows: AnyRows) => InlineRows;
19
+ /**
20
+ * The table an `in` names (`Condition.within`), and the key its subquery selects. Refused, never guessed, when the
21
+ * table is not one the catalog holds, has no key of one column to select, or its conditions say more than its own
22
+ * columns compared with values (no second `in`, no `any`, no column on the right, no resolved test, no path): one
23
+ * level only. The raw and the scoped read check it here, so the two cannot drift on what they refuse.
24
+ */
25
+ export declare function withinRows(schema: DatabaseSchema, rows: WithinRows): {
26
+ readonly table: TableMeta;
27
+ readonly key: string;
28
+ readonly type: string;
29
+ };
30
+ /**
31
+ * `buildWhere`'s `within` for a raw read: the other table is read from its own relation, as the table asked about
32
+ * is, with no scope between, and its conditions bound in the statement's order.
33
+ */
34
+ export declare function inlineWithin(schema: DatabaseSchema, style: SqlStyle, relation: (table: TableMeta) => string): (rows: WithinRows) => WithinPlan;
35
+ /** The subquery of an `in` read from a relation already planned (scoped, its conditions bound there): it binds nothing. */
36
+ export declare function plannedWithin(style: SqlStyle, source: string, key: string, type: string): WithinPlan;
19
37
  export {};
@@ -67,3 +67,52 @@ export function inlineRows(schema, parentId, parent, style, relation) {
67
67
  };
68
68
  };
69
69
  }
70
+ /** The alias another table's rows stand under inside an `in`'s subquery. */
71
+ const WITHIN = '_tw_in';
72
+ const refusedWithin = () => new RequestFault("This read cannot compare a column with another table's rows.");
73
+ /**
74
+ * The table an `in` names (`Condition.within`), and the key its subquery selects. Refused, never guessed, when the
75
+ * table is not one the catalog holds, has no key of one column to select, or its conditions say more than its own
76
+ * columns compared with values (no second `in`, no `any`, no column on the right, no resolved test, no path): one
77
+ * level only. The raw and the scoped read check it here, so the two cannot drift on what they refuse.
78
+ */
79
+ export function withinRows(schema, rows) {
80
+ if (!rows || typeof rows !== 'object' || typeof rows.table !== 'string')
81
+ throw refusedWithin();
82
+ const table = findTable(schema, rows.table);
83
+ if (!table)
84
+ throw new RequestFault('Unknown table in an "in" test.');
85
+ const keys = table.columns.filter((column) => column.primaryKey);
86
+ if (keys.length !== 1)
87
+ throw new RequestFault(`An "in" test selects a table's key, and ${table.name} has no key of exactly one column.`);
88
+ for (const group of rows.filter?.groups ?? []) {
89
+ for (const c of group) {
90
+ if (c.any !== undefined || c.within !== undefined || c.other !== undefined || c.via !== undefined || c.equality !== undefined)
91
+ throw new RequestFault('An "in" test reads one level: its rows are judged by their own columns, never by another "in" or "any".');
92
+ if (typeof c.column !== 'string' || !table.columns.some((column) => column.name === c.column))
93
+ throw new RequestFault('Unknown column in an "in" test.');
94
+ }
95
+ }
96
+ return { table, key: keys[0].name, type: keys[0].type };
97
+ }
98
+ /**
99
+ * `buildWhere`'s `within` for a raw read: the other table is read from its own relation, as the table asked about
100
+ * is, with no scope between, and its conditions bound in the statement's order.
101
+ */
102
+ export function inlineWithin(schema, style, relation) {
103
+ return (rows) => {
104
+ const { table, key, type } = withinRows(schema, rows);
105
+ const alias = style.quote(WITHIN);
106
+ return {
107
+ select: `SELECT ${alias}.${style.quote(key)} FROM ${relation(table)} ${alias}`,
108
+ column: (name) => `${alias}.${style.quote(name)}`,
109
+ types: (name) => table.columns.find((column) => column.name === name)?.type,
110
+ key: type,
111
+ };
112
+ };
113
+ }
114
+ /** The subquery of an `in` read from a relation already planned (scoped, its conditions bound there): it binds nothing. */
115
+ export function plannedWithin(style, source, key, type) {
116
+ const alias = style.quote(WITHIN);
117
+ return { select: `SELECT ${alias}.${style.quote(key)} FROM ${source} ${alias}`, key: type };
118
+ }
@@ -32,6 +32,13 @@ export interface Condition {
32
32
  * `op` is `isNotNull` for `any`, `isNull` for `no`.
33
33
  */
34
34
  any?: AnyRows;
35
+ /**
36
+ * The column's value is the key of a row of another table that matches: `customer_id in (customer where region =
37
+ * west)`. Written as `column IN (SELECT key FROM …)` over the other table's relation, so a row scope on that table
38
+ * narrows it as it narrows a list of its rows. `op` is `in` and `value` is absent. Only a builder that knows how to
39
+ * read that table (`buildWhere`'s `within`) compiles it; everywhere else it is refused, never dropped.
40
+ */
41
+ within?: WithinRows;
35
42
  /**
36
43
  * Compare as a PostgreSQL foreign key compares a reference with its parent's key: by its own operator, named with
37
44
  * its schema, the value cast to the referencing column's type and each side to the operator's, as the database's
@@ -63,7 +70,30 @@ export interface InlineRows {
63
70
  /** A child column's catalog type, where the style names its operators by schema. */
64
71
  readonly types?: (name: string) => string | undefined;
65
72
  }
66
- /** Whether a filter asks about the rows that point at the one read (`any`/`no`) anywhere. */
73
+ /**
74
+ * The rows of a `within` condition (`Condition.within`): the table whose key the column is compared with, by its
75
+ * name in the read's namespace, and which of its rows count — conditions over its own columns compared with values.
76
+ */
77
+ export interface WithinRows {
78
+ table: string;
79
+ filter?: Filter;
80
+ }
81
+ /**
82
+ * A `within` condition's rows as a builder resolved them (`buildWhere`'s `within`): the subquery that reads their key,
83
+ * `SELECT alias.key FROM relation alias`. Where `column` is given, the rows' own conditions are bound by `buildWhere`
84
+ * after it, in the statement's order; absent, the select carries them already (planned with the read's scope) and
85
+ * binds nothing here.
86
+ */
87
+ export interface WithinPlan {
88
+ readonly select: string;
89
+ /** One of the rows' own columns, qualified; refuses a name the table does not hold. */
90
+ readonly column?: (name: string) => string;
91
+ /** A column's catalog type, where the style names its operators by schema. */
92
+ readonly types?: (name: string) => string | undefined;
93
+ /** The key's catalog type: with the compared column's, whether the comparison names the built-in operator. */
94
+ readonly key?: string;
95
+ }
96
+ /** Whether a filter asks about other rows — those that point at the one read (`any`/`no`), or another table's (`in`) — anywhere. */
67
97
  export declare const asksAboutRows: (filter: Filter | undefined) => boolean;
68
98
  /**
69
99
  * A filter whose `any`/`no` children are named in another namespace: a layer that renames the tables of a request
@@ -292,5 +322,7 @@ types?: (name: string) => string | undefined,
292
322
  via?: boolean,
293
323
  /** How an `any` condition is written when its child's values are bound here, in order, with the rest of the
294
324
  filter: a read that resolves the child itself (anyRows.ts). Absent, and without `exists`, one is refused. */
295
- inline?: (rows: AnyRows) => InlineRows): SqlFragment;
325
+ inline?: (rows: AnyRows) => InlineRows,
326
+ /** How a `within` condition's subquery is read (`Condition.within`): its relation, scoped where the read is. Absent, one is refused. */
327
+ within?: (rows: WithinRows) => WithinPlan): SqlFragment;
296
328
  export {};
@@ -5,8 +5,8 @@
5
5
  */
6
6
  import { LIKE_ESCAPE, likePattern } from './like.js';
7
7
  import { RequestFault } from './refusal.js';
8
- /** Whether a filter asks about the rows that point at the one read (`any`/`no`) anywhere. */
9
- export const asksAboutRows = (filter) => filter?.groups.some((group) => group.some((condition) => condition.any !== undefined)) === true;
8
+ /** Whether a filter asks about other rows — those that point at the one read (`any`/`no`), or another table's (`in`) — anywhere. */
9
+ export const asksAboutRows = (filter) => filter?.groups.some((group) => group.some((condition) => condition.any !== undefined || condition.within !== undefined)) === true;
10
10
  /**
11
11
  * A filter whose `any`/`no` children are named in another namespace: a layer that renames the tables of a request
12
12
  * (an App's resources to the database's, a loader's local names to the catalog's) renames the child as it renames the
@@ -18,6 +18,9 @@ export function renamedAnyRows(filter, rename) {
18
18
  return {
19
19
  ...filter,
20
20
  groups: filter.groups.map((group) => group.map((condition) => {
21
+ /* An `in`'s table is a table of the same read too. */
22
+ if (condition.within !== undefined)
23
+ return { ...condition, within: { ...condition.within, table: rename(condition.within.table) } };
21
24
  if (condition.any === undefined)
22
25
  return condition;
23
26
  const { via } = condition.any;
@@ -242,7 +245,9 @@ types,
242
245
  via = false,
243
246
  /** How an `any` condition is written when its child's values are bound here, in order, with the rest of the
244
247
  filter: a read that resolves the child itself (anyRows.ts). Absent, and without `exists`, one is refused. */
245
- inline) {
248
+ inline,
249
+ /** How a `within` condition's subquery is read (`Condition.within`): its relation, scoped where the read is. Absent, one is refused. */
250
+ within) {
246
251
  if (!filter?.groups.length)
247
252
  return { text: '', params: [] };
248
253
  const params = [];
@@ -287,7 +292,7 @@ inline) {
287
292
  values are bound where their relation is planned (rowScope.ts `filtered`),
288
293
  so the EXISTS carries no parameter here and the numbering is untouched. */
289
294
  if (c.any !== undefined) {
290
- if ((!exists && !inline) || typeof c.any !== 'object' || c.any === null)
295
+ if ((!exists && !inline) || typeof c.any !== 'object' || c.any === null || c.within !== undefined)
291
296
  throw new RequestFault('This read cannot ask about the rows that point at it.');
292
297
  let text;
293
298
  if (exists)
@@ -309,6 +314,28 @@ inline) {
309
314
  /* `no task …` is the same rows asked the other way: none of them matches. */
310
315
  return c.op === 'isNull' ? `NOT ${text}` : text;
311
316
  }
317
+ /* Before the column is written by an operator: another table's keys are no value. One level only — the rows'
318
+ own conditions are compiled with no hooks, so an `in` or an `any` inside them is refused, never dropped. */
319
+ if (c.within !== undefined) {
320
+ if (!within ||
321
+ typeof c.within !== 'object' ||
322
+ c.within === null ||
323
+ c.op !== 'in' ||
324
+ c.other !== undefined ||
325
+ c.equality !== undefined)
326
+ throw new RequestFault("This read cannot compare a column with another table's rows.");
327
+ const plan = within(c.within);
328
+ let narrowed = '';
329
+ if (plan.column && c.within.filter?.groups.length) {
330
+ const inner = buildWhere(c.within.filter, style, index, plan.column, undefined, undefined, undefined, false, plan.types);
331
+ params.push(...inner.params);
332
+ index += inner.params.length;
333
+ narrowed = inner.text;
334
+ }
335
+ const col = column(c.column, c);
336
+ const typed = builtin(c.column) && plan.key !== undefined && style.builtin?.(plan.key);
337
+ return typed ? `${col} ${operator('=')} ANY (${plan.select}${narrowed})` : `${col} IN (${plan.select}${narrowed})`;
338
+ }
312
339
  /* Before the column is written: a record's tags are no column. */
313
340
  if (c.op === 'has') {
314
341
  if (!membership || c.other !== undefined)
@@ -424,6 +451,7 @@ function sameCondition(a, b) {
424
451
  a.collation !== b.collation ||
425
452
  a.other !== b.other ||
426
453
  a.any !== b.any ||
454
+ a.within !== b.within ||
427
455
  a.equality !== b.equality ||
428
456
  a.via?.text !== b.via?.text)
429
457
  return false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mistriwork/engine",
3
- "version": "1.0.0-rc.73",
3
+ "version": "1.0.0-rc.74",
4
4
  "description": "The engine both products stand on: the adapters (SQLite, Postgres), the catalog, the query language, keyset pages, the HTTP kit and the shared rules. No React.",
5
5
  "homepage": "https://mistriwork.dev",
6
6
  "bugs": {