@mistriwork/engine 1.0.0-rc.10 → 1.0.0-rc.12

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.
Files changed (93) hide show
  1. package/dist/adapters/adapter.d.ts +33 -0
  2. package/dist/adapters/postgres.d.ts +21 -16
  3. package/dist/adapters/postgres.js +199 -223
  4. package/dist/adapters/postgresCatalog.d.ts +12 -1
  5. package/dist/adapters/postgresCatalog.js +57 -23
  6. package/dist/adapters/postgresCatalogDigest.js +40 -34
  7. package/dist/adapters/postgresCatalogRows.d.ts +2 -0
  8. package/dist/adapters/postgresWriteAhead.d.ts +47 -0
  9. package/dist/adapters/postgresWriteAhead.js +209 -0
  10. package/dist/adapters/scoped/keyRows.js +9 -3
  11. package/dist/adapters/scoped/keyTypes.d.ts +34 -0
  12. package/dist/adapters/scoped/keyTypes.js +66 -0
  13. package/dist/adapters/scoped/postgresScopeCatalog.d.ts +40 -1
  14. package/dist/adapters/scoped/postgresScopeCatalog.js +417 -13
  15. package/dist/adapters/scoped/postgresTriggers.d.ts +137 -0
  16. package/dist/adapters/scoped/postgresTriggers.js +1545 -0
  17. package/dist/adapters/scoped/scopeTriggers.d.ts +122 -0
  18. package/dist/adapters/scoped/scopeTriggers.js +131 -0
  19. package/dist/adapters/scoped/scopedReads.d.ts +3 -1
  20. package/dist/adapters/scoped/scopedReads.js +28 -8
  21. package/dist/adapters/scoped/scopedTransaction.d.ts +95 -6
  22. package/dist/adapters/scoped/scopedTransaction.js +308 -34
  23. package/dist/adapters/scoped/sqliteScopeCatalog.d.ts +17 -1
  24. package/dist/adapters/scoped/sqliteScopeCatalog.js +197 -18
  25. package/dist/adapters/scoped/sqliteTriggerSession.d.ts +64 -0
  26. package/dist/adapters/scoped/sqliteTriggerSession.js +245 -0
  27. package/dist/adapters/scoped/sqliteTriggers.d.ts +20 -0
  28. package/dist/adapters/scoped/sqliteTriggers.js +822 -0
  29. package/dist/adapters/scoped/storedText.d.ts +45 -0
  30. package/dist/adapters/scoped/storedText.js +61 -0
  31. package/dist/adapters/scoped/triggerEffects.d.ts +98 -0
  32. package/dist/adapters/scoped/triggerEffects.js +189 -0
  33. package/dist/adapters/scoped/triggerProof.d.ts +124 -0
  34. package/dist/adapters/scoped/triggerProof.js +366 -0
  35. package/dist/adapters/snapshotRead.d.ts +8 -0
  36. package/dist/adapters/snapshotRead.js +37 -0
  37. package/dist/adapters/sqlite.d.ts +13 -2
  38. package/dist/adapters/sqlite.js +139 -16
  39. package/dist/adapters/sqliteDecimals.d.ts +2 -2
  40. package/dist/adapters/sqliteDecimals.js +2 -8
  41. package/dist/adapters/storeHost.d.ts +4 -1
  42. package/dist/adapters/writeCapture.d.ts +7 -0
  43. package/dist/server/cli/args.d.ts +6 -0
  44. package/dist/server/cli/args.js +6 -0
  45. package/dist/server/columnGrants.d.ts +3 -0
  46. package/dist/server/columnGrants.js +12 -1
  47. package/dist/server/config/profile.d.ts +123 -0
  48. package/dist/server/config/profile.js +236 -0
  49. package/dist/server/connections.d.ts +5 -0
  50. package/dist/server/connections.js +11 -1
  51. package/dist/server/hiddenColumns.js +6 -0
  52. package/dist/server/http/refusalReport.js +16 -4
  53. package/dist/server/http/router.d.ts +11 -22
  54. package/dist/server/http/router.js +0 -15
  55. package/dist/server/migrate/cli.d.ts +50 -3
  56. package/dist/server/migrate/cli.js +199 -14
  57. package/dist/server/migrate/runner.js +5 -1
  58. package/dist/server/shapeonly.js +5 -0
  59. package/dist/shared/addressParams.d.ts +2 -0
  60. package/dist/shared/addressParams.js +2 -0
  61. package/dist/shared/aggregate.d.ts +49 -0
  62. package/dist/shared/aggregate.js +107 -0
  63. package/dist/shared/breakdown.js +19 -6
  64. package/dist/shared/decimals.d.ts +37 -0
  65. package/dist/shared/decimals.js +148 -0
  66. package/dist/shared/displayDefaults.js +15 -1
  67. package/dist/shared/filter.d.ts +39 -1
  68. package/dist/shared/filter.js +96 -15
  69. package/dist/shared/listPresentations.d.ts +2 -0
  70. package/dist/shared/listQuestion.d.ts +3 -1
  71. package/dist/shared/money.d.ts +9 -0
  72. package/dist/shared/money.js +17 -0
  73. package/dist/shared/moneyEntry.d.ts +6 -3
  74. package/dist/shared/moneyEntry.js +14 -7
  75. package/dist/shared/pageModel.d.ts +1 -1
  76. package/dist/shared/recordview.d.ts +1 -1
  77. package/dist/shared/refusal.d.ts +16 -0
  78. package/dist/shared/refusal.js +40 -0
  79. package/dist/shared/rowScope.js +12 -4
  80. package/dist/shared/schema.d.ts +6 -0
  81. package/dist/shared/schematext.d.ts +22 -6
  82. package/dist/shared/schematext.js +18 -2
  83. package/dist/shared/scopeCatalog.js +4 -0
  84. package/dist/shared/servedApp.d.ts +15 -4
  85. package/dist/shared/view.js +4 -4
  86. package/dist/shared/viewLabels.d.ts +1 -0
  87. package/dist/shared/viewLabels.js +8 -2
  88. package/dist/shared/viewPaths.d.ts +3 -0
  89. package/dist/shared/viewPaths.js +8 -0
  90. package/package.json +5 -2
  91. package/schema/model.d.ts +1 -1
  92. package/dist/shared/order.d.ts +0 -59
  93. package/dist/shared/order.js +0 -133
@@ -13,11 +13,14 @@ import { captureScopeCatalog, mapScopeCatalog } from '../shared/scopeCatalog.js'
13
13
  import { DatabaseSync } from 'node:sqlite';
14
14
  import { isPromise, isProxy } from 'node:util/types';
15
15
  import { ANSI_STYLE, buildWhere, clampLimit, clampOffset, boundStatement, looksWrappable, dedupe, refuseStacked, trimStatement, Refusal, WriteConflict, TransactionOutcomeUnknown, Serial, TransactionOperations, queueRecordLock, recordLockFilter, refuseAnalyze, countStatement, countEachStatement, countEachRows, countsTogether, orderClause, } from './adapter.js';
16
+ import { compileAggregate } from '../shared/aggregate.js';
17
+ import { readExactly, summed } from '../shared/decimals.js';
16
18
  import { buildUpdateGuard } from './scoped/updateGuard.js';
17
19
  import { createScopedReads } from './scoped/scopedReads.js';
18
20
  import { scopedCommandInvoker } from './scoped/scopedCommandOperations.js';
19
- import { createScopedTransaction, wholeWithin } from './scoped/scopedTransaction.js';
20
- import { readSqliteScopeCatalog } from './scoped/sqliteScopeCatalog.js';
21
+ import { createScopedTransaction, foreignKeyRefusal, isForeignKeyViolation, referencesStatement, wholeWithin, } from './scoped/scopedTransaction.js';
22
+ import { readSqliteScopeCatalog, sqliteBlockedTables, sqliteKeyFacts } from './scoped/sqliteScopeCatalog.js';
23
+ import { readSqliteTrigger } from './scoped/sqliteTriggers.js';
21
24
  import { EpochCache } from './epochCache.js';
22
25
  import { captureRowScope } from '../shared/rowScope.js';
23
26
  import { isRuntimeTable } from '../shared/runtimeTables.js';
@@ -138,6 +141,28 @@ export class SqliteAdapter {
138
141
  current();
139
142
  return current;
140
143
  }
144
+ /** Each table's triggers, read on the reader as a scoped write reads them on its writer (sqliteTriggers.ts). A TEMP
145
+ * trigger is its own connection's, which this reader cannot see; the writer reads those too. */
146
+ async triggers() {
147
+ const fold = (name) => name.replace(/[A-Z]/g, (char) => char.toLowerCase());
148
+ const tables = new Map((await this.introspect()).tables.filter((table) => !table.isView).map((table) => [fold(table.name), table]));
149
+ const found = new Map();
150
+ const rows = this.db.prepare("SELECT tbl_name, name, sql FROM main.sqlite_schema WHERE type = 'trigger' ORDER BY name").all();
151
+ for (const row of rows) {
152
+ const table = tables.get(fold(String(row.tbl_name)));
153
+ if (!table)
154
+ continue;
155
+ const generated = this.db.prepare('SELECT name FROM pragma_table_xinfo(?) WHERE hidden IN (2, 3)').all(table.name);
156
+ const trigger = readSqliteTrigger(String(row.name), row.sql, new Set(generated.map((column) => fold(String(column.name)))));
157
+ found.set(table.id, [...(found.get(table.id) ?? []), trigger]);
158
+ }
159
+ return found;
160
+ }
161
+ /** What a write reads to judge a trigger's effects (sqliteScopeCatalog.ts): every table's keys, the conflict clauses
162
+ * its schema declares, and the tables a scoped write cannot target, on the reader. */
163
+ async triggerKeys() {
164
+ return { ...sqliteKeyFacts(this.db), blocked: sqliteBlockedTables(this.db) };
165
+ }
141
166
  async introspect() {
142
167
  const version = this.schemaVersion();
143
168
  const tableRows = this.db
@@ -477,7 +502,7 @@ export class SqliteAdapter {
477
502
  const schema = await this.schema();
478
503
  const compiled = compileBreakdown(schema, breakdown, ANSI_STYLE, offset);
479
504
  const started = performance.now();
480
- const rows = await this.readRows(compiled.text, compiled.params, true);
505
+ const { rows } = await readExactly(async () => ({ rows: await this.readRows(compiled.text, compiled.params, true) }), summed(breakdown.measures));
481
506
  await this.turn();
482
507
  return {
483
508
  columns: compiled.columns.map((c) => c.name),
@@ -642,6 +667,11 @@ export class SqliteAdapter {
642
667
  await this.turn();
643
668
  return counted;
644
669
  }
670
+ async aggregate(request) {
671
+ const answered = await this.aggregateOn(request, this.db);
672
+ await this.turn();
673
+ return answered;
674
+ }
645
675
  async countEach(items, limit) {
646
676
  const relations = items.map((item) => ({ relation: ANSI_STYLE.quote(this.readTable(item.table)), filter: item.filter }));
647
677
  const statement = countEachStatement(relations, ANSI_STYLE, { limit, together: countsTogether(this.cachedSchema, items, limit) });
@@ -656,6 +686,15 @@ export class SqliteAdapter {
656
686
  const [row] = await this.readRows(text, where.params, true, db);
657
687
  return Number(row?.n ?? 0);
658
688
  }
689
+ /* A command's aggregate on its own transaction's handle (shared/aggregate.ts): one statement, its table's own
690
+ columns, its integers past 2^53 kept as their digits. */
691
+ async aggregateOn(request, db) {
692
+ this.assertTable(request.table, db);
693
+ const schema = await this.schema();
694
+ const compiled = compileAggregate(schema, request, ANSI_STYLE);
695
+ const { rows } = await readExactly(async () => ({ rows: await this.readRows(compiled.text, compiled.params, true, db) }), summed(request.measures));
696
+ return { rows: rows.map(toJsonSafe) };
697
+ }
659
698
  /**
660
699
  * One compiled read's rows, BigInts and all. A read that could hold the
661
700
  * server's one thread — a count, a filter or a sort over a large table —
@@ -831,18 +870,21 @@ export class SqliteAdapter {
831
870
  and normalised on the way out like every other read. */
832
871
  inserted.setReadBigInts(true);
833
872
  const described = inserted.columns();
834
- const row = scaleRows([inserted.get(...sqliteParams(params))], described.map((c) => c.name), scalesOf(described))[0];
873
+ const stored = inserted.get(...sqliteParams(params));
874
+ // The key as stored (a scoped write reads its row back by it); the row as a person reads it, a decimal with its scale.
875
+ const raw = toJsonSafe(stored);
876
+ const row = scaleRows([{ ...stored }], described.map((c) => c.name), scalesOf(described))[0];
835
877
  const safe = toJsonSafe(row);
836
878
  const pk = meta.filter((c) => c.pk > 0).map((c) => c.name);
837
879
  if (scoped &&
838
880
  pk.some((column) => {
839
- const value = row[column];
881
+ const value = stored[column];
840
882
  return !(typeof value === 'string' ||
841
883
  (typeof value === 'number' && Number.isFinite(value)) ||
842
884
  (typeof value === 'bigint' && value >= -9007199254740991n && value <= 9007199254740991n));
843
885
  }))
844
886
  throw new Refusal('The stored key cannot be checked in this row scope.');
845
- const key = Object.fromEntries(pk.map((c) => [c, safe[c]]));
887
+ const key = Object.fromEntries(pk.map((c) => [c, (scoped ? raw : safe)[c]]));
846
888
  return { key, row: safe, applied, sql: { text, params } };
847
889
  }
848
890
  /**
@@ -1117,20 +1159,20 @@ export class SqliteAdapter {
1117
1159
  * scope or bearer authority.
1118
1160
  */
1119
1161
  async journalTransaction(journal, run, options = {}) {
1120
- const { scope, verify, ordinaryVerify, bound } = options;
1162
+ const { scope, verify, ordinaryVerify, bound, triggers } = options;
1121
1163
  if (ordinaryVerify !== undefined) {
1122
1164
  if (!isSqliteVerifiedCommandSource(this))
1123
1165
  throw new Refusal('This verified command transaction is unavailable.');
1124
1166
  if (typeof ordinaryVerify !== 'function' || isProxy(ordinaryVerify) || typeof run !== 'function' || isProxy(run) || this.closed)
1125
1167
  throw new Refusal('This verified command transaction is unavailable.');
1126
1168
  }
1127
- return this.transact(run, journal, scope, verify, undefined, ordinaryVerify, bound);
1169
+ return this.transact(run, journal, scope, verify, undefined, ordinaryVerify, bound, triggers);
1128
1170
  }
1129
- async scopeTransaction(input, run) {
1171
+ async scopeTransaction(input, run, triggers) {
1130
1172
  const scope = captureRowScope(input);
1131
1173
  if (this.closed)
1132
1174
  throw new Refusal('This adapter is closed.');
1133
- return this.transact((tx, _journal, catalog) => run(tx, catalog), undefined, scope);
1175
+ return this.transact((tx, _journal, catalog) => run(tx, catalog), undefined, scope, undefined, undefined, undefined, undefined, triggers);
1134
1176
  }
1135
1177
  /** Private source-owner boundary, deliberately absent from Adapter. The
1136
1178
  * verifier is trusted host code, never a business callback or wire value.
@@ -1152,12 +1194,21 @@ export class SqliteAdapter {
1152
1194
  throw new Refusal('Runtime maintenance is unavailable.');
1153
1195
  }, undefined, {}, verify, run);
1154
1196
  }
1155
- async transact(run, journal, scope, verify, maintenance, ordinaryVerify, bound) {
1197
+ async transact(run, journal, scope, verify, maintenance, ordinaryVerify, bound, triggers) {
1156
1198
  return this.queueWrite(async () => {
1199
+ /* Inside the queued step, not before it: an await ahead of queueWrite lets a write asked for later take its place in the
1200
+ queue, and a transaction asked for first must run first (row-scope-transaction.test.ts). */
1201
+ /* Loaded where a transaction uses them, not at startup: the keyed-effect judge for a scoped write (its database
1202
+ may hold keyed triggers), and the proof and its session witness where the App names triggers. */
1203
+ const effects = scope !== undefined ? await import('./scoped/triggerEffects.js') : undefined;
1204
+ const proving = scope !== undefined && triggers
1205
+ ? await Promise.all([import('./scoped/triggerProof.js'), import('./scoped/sqliteTriggerSession.js')])
1206
+ : undefined;
1157
1207
  const operations = new TransactionOperations();
1158
1208
  let write;
1159
1209
  let began = false;
1160
1210
  let commitAttempted = false;
1211
+ let witness;
1161
1212
  try {
1162
1213
  if (this.closed)
1163
1214
  throw new Refusal('This adapter is closed.');
@@ -1203,6 +1254,7 @@ export class SqliteAdapter {
1203
1254
  let records = {
1204
1255
  query: (r) => this.queryOn(r, handle),
1205
1256
  count: (table, filter) => this.countOn(table, filter, handle),
1257
+ aggregate: (r) => this.aggregateOn(r, handle),
1206
1258
  insert: history ? (r) => history.insert(r, insert) : insert,
1207
1259
  remove: history ? (r) => history.remove(r, remove) : remove,
1208
1260
  update: history ? (r) => history.update(r, update) : update,
@@ -1221,7 +1273,7 @@ export class SqliteAdapter {
1221
1273
  throw new Refusal('Transaction verification must be synchronous.');
1222
1274
  };
1223
1275
  if (scope !== undefined) {
1224
- const catalog = this.catalogCache.throughSync(epoch, 'scope', () => readSqliteScopeCatalog(handle));
1276
+ const catalog = this.catalogCache.throughSync(epoch, 'scope', () => readSqliteScopeCatalog(handle, effects?.resolveTriggerEffects));
1225
1277
  physicalCatalog = catalog;
1226
1278
  journal?.admit?.(handle, catalog, scope);
1227
1279
  // Keep the complete physical graph for row/FK enforcement. Runtime
@@ -1235,7 +1287,12 @@ export class SqliteAdapter {
1235
1287
  const statement = handle.prepare(sql.text);
1236
1288
  statement.setReadBigInts(true);
1237
1289
  const described = statement.columns();
1238
- const rows = scaleRows(statement.all(...sqliteParams(sql.params)), described.map((c) => c.name), scalesOf(described));
1290
+ const read = statement.all(...sqliteParams(sql.params));
1291
+ /* A guard's row is what a check names rows by: the value as stored, never said with a decimal's scale
1292
+ (19.987 said as "19.99" named another row). Only what a person reads is said so. */
1293
+ const rows = guard
1294
+ ? read
1295
+ : scaleRows(read, described.map((c) => c.name), scalesOf(described));
1239
1296
  return {
1240
1297
  columns: described.map((column) => column.name),
1241
1298
  rows: rows.map((row) => guard
@@ -1250,9 +1307,62 @@ export class SqliteAdapter {
1250
1307
  sql,
1251
1308
  };
1252
1309
  });
1253
- records = createScopedTransaction(catalog, records, scopedReads(false), scopedReads(true), undefined, bound && { reads: scopedReads(true, bound), whole: wholeWithin(scope, bound) },
1310
+ /* What the triggers it accepts wrote is seen again by a session before COMMIT (sqliteTriggerSession.ts). */
1311
+ const seen = proving && !journal?.fixedRecords ? proving[1].sqliteTriggerWitness(handle, catalog.schema.tables) : undefined;
1312
+ witness = seen;
1313
+ const accepted = seen && {
1314
+ acceptance: {
1315
+ ...triggers,
1316
+ changed: async (change) => {
1317
+ seen.proven(change);
1318
+ // Kept and announced as the transaction's own writes are (writeCapture.ts).
1319
+ await history?.triggered?.(change);
1320
+ await triggers.changed?.(change);
1321
+ },
1322
+ },
1323
+ scope,
1324
+ watch: seen.watch,
1325
+ prove: proving[0].createTriggerProof,
1326
+ };
1327
+ records = createScopedTransaction(catalog, records, scopedReads(false), scopedReads(true), undefined, bound && {
1328
+ reads: scopedReads(true, bound),
1329
+ whole: wholeWithin(scope, bound),
1330
+ references: async (fk, key) => {
1331
+ const statement = referencesStatement(catalog, fk, key, ANSI_STYLE);
1332
+ const [row] = await this.readRows(statement.text, statement.params, true, handle);
1333
+ return Number(row?.n ?? 0);
1334
+ },
1335
+ },
1254
1336
  // A public intake writes exactly its fixed rows; only a principal's own transaction counts its new parents in.
1255
- journal?.fixedRecords ? undefined : { scope, reads: (within, guard) => scopedReads(guard, within) });
1337
+ journal?.fixedRecords ? undefined : { scope, reads: (within, guard) => scopedReads(guard, within) },
1338
+ // Triggers the App names, on a principal's own transaction: their effects proven per write (triggerProof.ts).
1339
+ accepted);
1340
+ /* Each row the transaction's own statements write, told to the session after the statement and after the
1341
+ proof has said what its triggers reached: a trigger the proof did not reach that writes it again later is
1342
+ a change SQLite still calls direct. */
1343
+ if (seen) {
1344
+ const inner = records;
1345
+ records = {
1346
+ ...inner,
1347
+ insert: async (r) => {
1348
+ const result = await inner.insert(r);
1349
+ seen.wrote(r.table, result.key);
1350
+ return result;
1351
+ },
1352
+ update: async (r) => {
1353
+ const result = await inner.update(r);
1354
+ seen.wrote(r.table, r.key, r.values);
1355
+ return result;
1356
+ },
1357
+ remove: async (r) => {
1358
+ // Gone after the statement: its key as stored is read before it (sqliteTriggerSession.ts stored).
1359
+ const key = seen.stored(r.table, r.key);
1360
+ const result = await inner.remove(r);
1361
+ seen.wrote(r.table, key);
1362
+ return result;
1363
+ },
1364
+ };
1365
+ }
1256
1366
  if (journal?.fixedRecords)
1257
1367
  records = journal.fixedRecords(records);
1258
1368
  }
@@ -1268,6 +1378,7 @@ export class SqliteAdapter {
1268
1378
  : {}),
1269
1379
  query: (r) => invoke(r, (r) => records.query(r)),
1270
1380
  count: (table, filter) => invoke({ table, filter }, (r) => records.count(r.table, r.filter)),
1381
+ ...(records.aggregate ? { aggregate: (r) => invoke(r, (r) => records.aggregate(r)) } : {}),
1271
1382
  insert: (r) => invoke(r, (r) => records.insert(r)),
1272
1383
  remove: (r) => invoke(r, (r) => records.remove(r)),
1273
1384
  update: (r) => invoke(r, (r) => records.update(r)),
@@ -1275,16 +1386,28 @@ export class SqliteAdapter {
1275
1386
  ...(history ? { tell: (r) => invoke(r, (r) => history.tell(r)) } : {}),
1276
1387
  }), journal?.open(handle, operations, verify ? verified : undefined), evidence);
1277
1388
  await operations.finish();
1389
+ witness?.settle();
1278
1390
  await finishWriteCapture(history);
1279
1391
  verified();
1280
1392
  verifyOrdinary();
1281
1393
  commitAttempted = true;
1282
- write.exec('COMMIT');
1394
+ try {
1395
+ write.exec('COMMIT');
1396
+ }
1397
+ catch (error) {
1398
+ /* A deferred foreign key the COMMIT found broken, in a scoped write: SQLite leaves the transaction open, so
1399
+ nothing committed (the ROLLBACK below ends it), and the refusal is a hidden parent's (foreignKeyRefusal). */
1400
+ if (scope === undefined || !isForeignKeyViolation(error) || !handle.isTransaction)
1401
+ throw error;
1402
+ commitAttempted = false;
1403
+ throw foreignKeyRefusal(error);
1404
+ }
1283
1405
  committedWriteCapture(history);
1284
1406
  return out;
1285
1407
  }
1286
1408
  catch (err) {
1287
1409
  await operations.finish().catch(() => { });
1410
+ witness?.close();
1288
1411
  let rolledBack = false;
1289
1412
  if (began) {
1290
1413
  try {
@@ -10,8 +10,8 @@
10
10
  * declared scale, an integer column and an expression (no declared type) are
11
11
  * left exactly as they were.
12
12
  */
13
- /** The scale a declared type asks for, when it is a DECIMAL/NUMERIC with one above zero. */
14
- export declare function declaredScale(type: string | null | undefined): number;
13
+ import { declaredScale } from '../shared/decimals.js';
14
+ export { declaredScale };
15
15
  /** Per output position, the scale to apply, or undefined when no column needs one. */
16
16
  export declare function scalesOf(columns: ReadonlyArray<{
17
17
  type?: string | null;
@@ -10,14 +10,8 @@
10
10
  * declared scale, an integer column and an expression (no declared type) are
11
11
  * left exactly as they were.
12
12
  */
13
- const DECLARED_SCALE = /^\s*(?:decimal|numeric)\s*\(\s*\d+\s*,\s*(\d+)\s*\)\s*$/i;
14
- /** The scale a declared type asks for, when it is a DECIMAL/NUMERIC with one above zero. */
15
- export function declaredScale(type) {
16
- if (!type)
17
- return 0;
18
- const scale = Number(DECLARED_SCALE.exec(type)?.[1] ?? 0);
19
- return scale > 0 ? scale : 0;
20
- }
13
+ import { declaredScale } from '../shared/decimals.js';
14
+ export { declaredScale };
21
15
  /** Per output position, the scale to apply, or undefined when no column needs one. */
22
16
  export function scalesOf(columns) {
23
17
  let scales;
@@ -23,6 +23,8 @@ import type { PostgresScopeCatalog } from './scoped/postgresScopeCatalog.js';
23
23
  import type { SqliteScopeCatalog } from './scoped/sqliteScopeCatalog.js';
24
24
  import type { DatabaseSchema } from '../shared/schema.js';
25
25
  import type { RowScope } from '../shared/rowScope.js';
26
+ import type { TriggerAcceptance } from './scoped/triggerProof.js';
27
+ export type { TriggerAcceptance, TriggerChange } from './scoped/triggerProof.js';
26
28
  /** A reserved store's meta row: the one row whose version says the layout, and the cache key its attestation is kept under. */
27
29
  export interface StoreMeta {
28
30
  readonly name: string;
@@ -71,6 +73,8 @@ export interface JournalTransactionOptions<V> {
71
73
  readonly verify?: V;
72
74
  /** A verified ordinary transaction's trusted host check, after BEGIN and again before COMMIT. */
73
75
  readonly ordinaryVerify?: () => unknown;
76
+ /** A scoped transaction's triggers the App accepts, by physical table (scoped/triggerProof.ts). */
77
+ readonly triggers?: TriggerAcceptance;
74
78
  }
75
79
  /** The callback: the records, the journal, and a scoped transaction's catalog evidence (absent in an ordinary one). */
76
80
  export type JournalRun<J, T> = (records: WriteTransaction, journal: J, catalog?: DatabaseSchema) => Promise<T>;
@@ -127,4 +131,3 @@ interface WriteView {
127
131
  /** The wrapper's constructor only. */
128
132
  export declare function lendWriteView(wrapped: object, view: WriteView): void;
129
133
  export declare const writeViewOf: (wrapped: unknown) => WriteView | undefined;
130
- export {};
@@ -15,6 +15,7 @@
15
15
  import type { DatabaseSync } from 'node:sqlite';
16
16
  import type { DeleteRequest, DeleteResult, InsertRequest, InsertResult, TellRequest, UpdateRequest, UpdateResult } from './adapter.js';
17
17
  import type { PgQuery } from './postgresPool.js';
18
+ import type { TriggerChange } from './scoped/triggerProof.js';
18
19
  /** The transaction's writer, by dialect: a capture's own rows join it. */
19
20
  type CaptureWriter = {
20
21
  readonly dialect: 'postgres';
@@ -46,6 +47,12 @@ interface WriteCapture {
46
47
  remove(request: DeleteRequest, write: (request: DeleteRequest) => Promise<DeleteResult>): Promise<DeleteResult>;
47
48
  /** A flow's notify step (WriteTransaction.tell). */
48
49
  tell(request: TellRequest): Promise<void>;
50
+ /**
51
+ * A row a trigger the App accepts changed, once the proof reached it (scoped/triggerProof.ts): the database wrote
52
+ * it, not a statement of the transaction, so nothing above passes through the three writes. Kept and announced as
53
+ * one of them is, attributed to the trigger.
54
+ */
55
+ triggered?(change: TriggerChange): Promise<void>;
49
56
  /**
50
57
  * Once, after the transaction's last operation has settled and just before
51
58
  * COMMIT, on the same writer: what a capture keeps per transaction rather
@@ -11,6 +11,12 @@ import type { PoolerMode } from '../../adapters/poolMetrics.js';
11
11
  * validation, run as CI runs it, and not a spelling of the command line.
12
12
  */
13
13
  export declare const CHECK_VALIDATION = "MISTRI_CHECK_VALIDATION";
14
+ /**
15
+ * Set beside CHECK_VALIDATION when `mistri check` has already found stores the App uses missing from its database
16
+ * (`store-pending`, docs/bootstrap-plan.md): the child validates with the store attestations set aside, so a real
17
+ * fault in the App is said in the same run instead of hiding behind the first absent store.
18
+ */
19
+ export declare const CHECK_STORES_ASIDE = "MISTRI_CHECK_STORES_ASIDE";
14
20
  export interface Options {
15
21
  target: string;
16
22
  /** The target is DATABASE_URL's, not a flag's: a config's connections win over it (readStartupConfig). */
@@ -13,6 +13,12 @@ import { envName, productEnv } from '../product.js';
13
13
  * validation, run as CI runs it, and not a spelling of the command line.
14
14
  */
15
15
  export const CHECK_VALIDATION = 'MISTRI_CHECK_VALIDATION';
16
+ /**
17
+ * Set beside CHECK_VALIDATION when `mistri check` has already found stores the App uses missing from its database
18
+ * (`store-pending`, docs/bootstrap-plan.md): the child validates with the store attestations set aside, so a real
19
+ * fault in the App is said in the same run instead of hiding behind the first absent store.
20
+ */
21
+ export const CHECK_STORES_ASIDE = 'MISTRI_CHECK_STORES_ASIDE';
16
22
  export function parseArgs(argv) {
17
23
  /* Loopback by default. A database browser listening on every interface is
18
24
  one shared wifi away from being someone else's database browser, and the
@@ -11,6 +11,7 @@
11
11
  import { Refusal, type Filter, type QueryRequest, type WriteTransaction, type InsertRequest, type UpdateRequest, type DeleteRequest } from '../adapters/adapter.js';
12
12
  import { type ViewDef } from '../shared/view.js';
13
13
  import { type BreakdownDef } from '../shared/breakdown.js';
14
+ import { type AggregateRequest } from '../shared/aggregate.js';
14
15
  import { type DatabaseSchema } from '../shared/schema.js';
15
16
  import type { RowScope } from '../shared/rowScope.js';
16
17
  /**
@@ -69,10 +70,12 @@ export declare function resourceChecks(full: DatabaseSchema, access: ColumnGrant
69
70
  limit: number;
70
71
  offset: number;
71
72
  binary?: true;
73
+ storedText?: true;
72
74
  columns: string[];
73
75
  filter: Filter | undefined;
74
76
  };
75
77
  filterFor: (table: string, input?: Filter, server?: boolean) => Filter | undefined;
78
+ aggregateFor: (input: AggregateRequest) => AggregateRequest;
76
79
  viewFor: (input: ViewDef) => ViewDef;
77
80
  breakdownFor: (input: BreakdownDef) => BreakdownDef;
78
81
  assertRelatedWrites: (table: string, fields?: string[]) => void;
@@ -13,6 +13,7 @@ import { freezeDerivedCatalog, isCapturedScopeCatalog, isFixedScopeCatalog } fro
13
13
  import { compileView, resolveAggregate, tryResolvePath } from '../shared/view.js';
14
14
  import { rollupReads } from '../shared/rollups.js';
15
15
  import { compileBreakdown, measureConditions } from '../shared/breakdown.js';
16
+ import { aggregateColumns } from '../shared/aggregate.js';
16
17
  import { textToView } from '../shared/viewtext.js';
17
18
  import { textToBreakdown } from '../shared/breakdowntext.js';
18
19
  import { isJsonType } from '../shared/schema.js';
@@ -418,6 +419,14 @@ export function resourceChecks(full, access) {
418
419
  assertFields(request.table, [order.column], 'read', server);
419
420
  return { ...request, columns, filter: filterFor(request.table, request.filter, server) };
420
421
  };
422
+ /* An aggregate reads the columns it keys and measures by, and its where's, as a query reads its own. */
423
+ const aggregateFor = (input) => {
424
+ const server = isServerRequest(input);
425
+ const request = requestData(input);
426
+ assertRead(request.table);
427
+ assertFields(request.table, aggregateColumns(request), 'read', server);
428
+ return { ...request, filter: filterFor(request.table, request.filter, server) };
429
+ };
421
430
  /* A rollup reads its child rows: the child, the reference, the column it
422
431
  sums and the columns its filter narrows by must all be readable here —
423
432
  an inner rollup's too — or the number would be built from values this
@@ -606,6 +615,7 @@ export function resourceChecks(full, access) {
606
615
  keyFields,
607
616
  queryFor,
608
617
  filterFor,
618
+ aggregateFor,
609
619
  viewFor,
610
620
  breakdownFor,
611
621
  assertRelatedWrites,
@@ -650,7 +660,7 @@ export function authorizeColumnWrites(full, grant, writer, rules) {
650
660
  if (grant.rows !== undefined)
651
661
  throw new AppAccessDenied();
652
662
  const checks = resourceChecks(full, grant);
653
- const { assertRead, assertWrite, assertAllFields, recordFields, keyFields, queryFor, filterFor, assertRelatedWrites } = checks;
663
+ const { assertRead, assertWrite, assertAllFields, recordFields, keyFields, queryFor, filterFor, aggregateFor, assertRelatedWrites } = checks;
654
664
  const product = rules?.(checks);
655
665
  // Keep each call's rules stable across its awaits; the next call reads current grants.
656
666
  const capture = (current) => (product ? product.capture(current) : captureGrant(current));
@@ -681,6 +691,7 @@ export function authorizeColumnWrites(full, grant, writer, rules) {
681
691
  : {}),
682
692
  query: (request) => writer.query(queryFor(request)),
683
693
  count: (table, filter) => writer.count(table, filterFor(table, filter)),
694
+ ...(writer.aggregate ? { aggregate: (request) => writer.aggregate(aggregateFor(request)) } : {}),
684
695
  insert: (input) => {
685
696
  const server = isServerRequest(input);
686
697
  const request = jsonWritten(full, requestData(input));
@@ -0,0 +1,123 @@
1
+ /** An entry the block may name, and the flag it stands for. */
2
+ declare const PROFILE_ENTRIES: {
3
+ readonly commands: '--commands';
4
+ readonly jobs: '--jobs';
5
+ readonly policy: '--policy';
6
+ readonly entry: '--app-entry';
7
+ };
8
+ type ProfileEntry = keyof typeof PROFILE_ENTRIES;
9
+ /** Where an entry came from: a flag, the profile, the `commands.ts` beside the App, or the build that bundled it. */
10
+ type EntryFrom = 'flag' | 'profile' | 'convention' | 'build';
11
+ /** Why the config file is the one read. */
12
+ type ConfigFrom = 'flag' | 'app' | 'cwd' | 'user';
13
+ /** The `app` block, its paths resolved from the file's folder. */
14
+ interface ProjectProfile {
15
+ readonly file: string;
16
+ readonly dir?: string;
17
+ readonly commands?: string;
18
+ readonly jobs?: string;
19
+ readonly policy?: string;
20
+ readonly entry?: string;
21
+ }
22
+ /** What a run resolved, said before anything else and carried in `--json` as `context`. */
23
+ interface ProjectContext {
24
+ readonly config?: {
25
+ readonly path: string;
26
+ readonly from: ConfigFrom;
27
+ };
28
+ readonly app?: {
29
+ readonly path: string;
30
+ readonly from: 'flag' | 'profile';
31
+ };
32
+ readonly entries: Partial<Record<ProfileEntry, {
33
+ readonly path: string;
34
+ readonly from: 'flag' | 'profile';
35
+ }>>;
36
+ /** Where a flag overrode the profile: what the profile said. */
37
+ readonly over: Partial<Record<ProfileEntry | 'dir', string>>;
38
+ }
39
+ /** A profile that cannot be used: refused before anything is read. */
40
+ export declare class ProfileRefusal extends Error {
41
+ }
42
+ /** What an App command takes of the profile: the App, and which entries. */
43
+ export interface ProfileTakes {
44
+ readonly entries: readonly ProfileEntry[];
45
+ /** `dev` also takes the App as its first word. */
46
+ readonly positional?: boolean;
47
+ /** The command prints the context in its own output and `--json` (check, migrate); another says it on stderr. */
48
+ readonly says?: boolean;
49
+ /** False where the command reads no App (`migrate diff` reads schema.sql and the config's connection). */
50
+ readonly app?: boolean;
51
+ }
52
+ /**
53
+ * The config file a run reads, by the one order: `--config`, the App's own, the working directory's, the person's
54
+ * own. Undefined when there is none (a run on a database named by `--db`).
55
+ */
56
+ export declare function findConfig(given: {
57
+ explicit?: string;
58
+ app?: string;
59
+ }, env?: NodeJS.ProcessEnv): {
60
+ path: string;
61
+ from: ConfigFrom;
62
+ } | undefined;
63
+ /** The `app` block of `file`, checked; undefined when the file has none. Throws a ProfileRefusal naming what is wrong. */
64
+ export declare function readProfile(file: string): ProjectProfile | undefined;
65
+ /**
66
+ * Whether the context is still to be said: true once a run, so a check `migrate up --app` runs inside it does not
67
+ * say it again after the migrate run said it first.
68
+ */
69
+ export declare function claimContext(): boolean;
70
+ /** The context the run's profile step resolved (applyProfile), for the command to print. */
71
+ export declare const projectContext: () => ProjectContext | undefined;
72
+ /**
73
+ * The run's arguments with the profile applied under them: the App when no `--app` names one, each entry the
74
+ * command takes that no flag names, and `--config` pinned where the file found is not the one a plain read would
75
+ * find (the App's own `mistri.json`), so the run and any child it starts read the same file. Records the context.
76
+ * Throws a ProfileRefusal for a block that cannot be used.
77
+ */
78
+ export declare function applyProfile(argv: readonly string[], takes: ProfileTakes, env?: NodeJS.ProcessEnv): string[];
79
+ /** Whether the profile, or the App's own mistri.json, decided any of the run's context: then the run says it. */
80
+ export declare function fromProfile(context: ProjectContext | undefined): boolean;
81
+ /**
82
+ * The entries the App itself says, where neither a flag nor the profile named one: the `commands.ts` beside its
83
+ * manifest (convention), or a build's own bundles (build).
84
+ */
85
+ export declare function appEntries(app: {
86
+ readonly build?: {
87
+ readonly entries: Partial<Record<'commands' | 'jobs' | 'policy', string>>;
88
+ };
89
+ readonly commands?: {
90
+ readonly file: string;
91
+ };
92
+ }, given: Partial<Record<'commands' | 'jobs' | 'policy', string | undefined>>): Partial<Record<ProfileEntry, {
93
+ path: string;
94
+ from: EntryFrom;
95
+ }>>;
96
+ /**
97
+ * The context as the run prints it, one line an entry: where it came from, and a flag that overrode the profile.
98
+ * `convention` and `build` are the App's to say (the loaded App's commands.ts beside it, a build's own bundles).
99
+ */
100
+ export declare function contextLines(context: ProjectContext | undefined, more?: Partial<Record<ProfileEntry, {
101
+ path: string;
102
+ from: EntryFrom;
103
+ }>>): string[];
104
+ /** The context as `--json` carries it: `{ config, app, entries: { commands|jobs|policy|entry: { path, from } | null } }`. */
105
+ export declare function contextJson(context: ProjectContext | undefined, more?: Partial<Record<ProfileEntry, {
106
+ path: string;
107
+ from: EntryFrom;
108
+ }>>): {
109
+ config: {
110
+ path: string;
111
+ from: ConfigFrom;
112
+ } | null;
113
+ app: {
114
+ path: string;
115
+ from: string;
116
+ } | null;
117
+ entries: Record<ProfileEntry, {
118
+ path: string;
119
+ from: EntryFrom;
120
+ over?: string;
121
+ } | null>;
122
+ };
123
+ export {};