@mistriwork/engine 1.0.0-rc.77 → 1.0.0-rc.79
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/adapter.d.ts +6 -0
- package/dist/adapters/postgres.js +13 -7
- package/dist/adapters/scoped/scopedReads.js +5 -1
- package/dist/adapters/scoped/scopedTransaction.js +3 -1
- package/dist/adapters/sqlite.js +7 -5
- package/dist/server/columnGrants.d.ts +3 -0
- package/dist/server/columnGrants.js +29 -4
- package/dist/server/http/probes.d.ts +10 -1
- package/dist/server/http/probes.js +6 -2
- package/dist/server/http/recordWrites.js +7 -1
- package/dist/server/http/serverCore.d.ts +2 -1
- package/dist/server/migrate/cli.js +7 -0
- package/dist/server/resourceReadOperations.js +5 -1
- package/dist/shared/aggregate.js +7 -1
- package/dist/shared/computedParse.d.ts +2 -0
- package/dist/shared/computedParse.js +1 -1
- package/dist/shared/computedWhere.d.ts +47 -0
- package/dist/shared/computedWhere.js +159 -0
- package/dist/shared/filter.d.ts +5 -0
- package/dist/shared/filter.js +4 -0
- package/dist/shared/query.js +20 -22
- package/dist/shared/queryDates.d.ts +16 -0
- package/dist/shared/queryDates.js +29 -0
- package/dist/shared/queryOperands.d.ts +24 -0
- package/dist/shared/queryOperands.js +36 -0
- package/dist/shared/refusal.d.ts +8 -0
- package/dist/shared/refusal.js +15 -0
- package/dist/shared/resourceHttp.d.ts +1 -1
- package/dist/shared/rowCondition.d.ts +5 -0
- package/dist/shared/rowCondition.js +80 -0
- package/dist/shared/schema.d.ts +10 -0
- package/dist/shared/schema.js +8 -0
- package/dist/shared/scopeCatalog.d.ts +1 -1
- package/dist/shared/scopeCatalog.js +13 -1
- package/dist/shared/servedApp.d.ts +19 -0
- package/dist/shared/structuredFilter.d.ts +3 -0
- package/dist/shared/viewLabels.d.ts +6 -1
- package/dist/shared/viewLabels.js +1 -1
- package/package.json +2 -1
- package/schema/model.d.ts +12 -3
|
@@ -823,6 +823,12 @@ export interface UpdateResult {
|
|
|
823
823
|
affected: number;
|
|
824
824
|
/** Values after coercion to their column types, for the caller to confirm. */
|
|
825
825
|
applied: Record<string, string | number | boolean | null>;
|
|
826
|
+
/**
|
|
827
|
+
* The row as the UPDATE left it, where the engine gives it back with the
|
|
828
|
+
* write (a PostgreSQL transaction's `RETURNING *`); absent elsewhere, and
|
|
829
|
+
* dropped by any layer that writes again before handing the result on.
|
|
830
|
+
*/
|
|
831
|
+
row?: Record<string, unknown>;
|
|
826
832
|
sql: {
|
|
827
833
|
text: string;
|
|
828
834
|
params: unknown[];
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
*/
|
|
10
10
|
import { isProxy } from 'node:util/types';
|
|
11
11
|
import { inlineRows, inlineWithin } from '../shared/anyRows.js';
|
|
12
|
-
import {
|
|
12
|
+
import { asksComputed, computedTable, readsCatalog, withComputedWhere } from '../shared/computedWhere.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';
|
|
15
15
|
import { compileAggregate } from '../shared/aggregate.js';
|
|
@@ -525,18 +525,20 @@ export class PostgresAdapter {
|
|
|
525
525
|
(commandDb.ts). The catalog is read only when the filter asks, and a filter that asks of a catalog this handle
|
|
526
526
|
cannot read is refused, as one nothing resolves is. */
|
|
527
527
|
async whereOf(id, qualified, filter) {
|
|
528
|
-
if (!
|
|
528
|
+
if (!readsCatalog(filter))
|
|
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
532
|
/* Another table's keys (`in`) are read as this table is, from its own relation: the same bound. */
|
|
533
533
|
const within = schema && inlineWithin(schema, POSTGRES_STYLE, (other) => qualifiedTable(schema, other.id, POSTGRES_STYLE));
|
|
534
|
-
|
|
534
|
+
/* A computed column is written as its expression over this table's own columns (computedWhere.ts). */
|
|
535
|
+
const own = filter && asksComputed(filter) ? withComputedWhere(filter, computedTable(schema, id), POSTGRES_STYLE) : undefined;
|
|
536
|
+
return buildWhere(own?.filter ?? filter, POSTGRES_STYLE, 1, own?.column, undefined, undefined, undefined, false, undefined, false, inline, within);
|
|
535
537
|
}
|
|
536
538
|
async queryOn(request, db, read = false) {
|
|
537
539
|
const { schema, name } = read ? await this.readTable(request.table) : await this.assertTable(request.table, db);
|
|
538
540
|
const qualified = `${POSTGRES_STYLE.quote(schema)}.${POSTGRES_STYLE.quote(name)}`;
|
|
539
|
-
const where =
|
|
541
|
+
const where = readsCatalog(request.filter)
|
|
540
542
|
? await this.whereOf(request.table, qualified, request.filter)
|
|
541
543
|
: buildWhere(request.filter, POSTGRES_STYLE);
|
|
542
544
|
const cols = request.columns?.length ? request.columns.map((c) => POSTGRES_STYLE.quote(c)).join(', ') : '*';
|
|
@@ -665,7 +667,7 @@ export class PostgresAdapter {
|
|
|
665
667
|
async countOn(table, filter, db, limit, read = false) {
|
|
666
668
|
const { schema, name } = read ? await this.readTable(table) : await this.assertTable(table, db);
|
|
667
669
|
const qualified = `${POSTGRES_STYLE.quote(schema)}.${POSTGRES_STYLE.quote(name)}`;
|
|
668
|
-
const where =
|
|
670
|
+
const where = readsCatalog(filter) ? await this.whereOf(table, qualified, filter) : buildWhere(filter, POSTGRES_STYLE);
|
|
669
671
|
const text = countStatement(`${qualified}${where.text}`, limit);
|
|
670
672
|
const result = await db.query(text, where.params);
|
|
671
673
|
return Number(result.rows[0]?.n ?? 0);
|
|
@@ -765,7 +767,10 @@ export class PostgresAdapter {
|
|
|
765
767
|
const assignments = columns.map((c, i) => `${POSTGRES_STYLE.quote(c)} = ${POSTGRES_STYLE.placeholder(i + 1)}`).join(', ');
|
|
766
768
|
const where = buildWhere(filter, POSTGRES_STYLE, columns.length + 1);
|
|
767
769
|
const guard = buildUpdateGuard(request, Object.fromEntries(meta.map((column) => [column.name, column.type])), POSTGRES_STYLE, 'postgres', columns.length + where.params.length + 1);
|
|
768
|
-
|
|
770
|
+
/* Inside a transaction the row comes back with the write, as insert's does: an App that validates the stored
|
|
771
|
+
row after its save (appValidation.ts validateStoredPostImage) read it again, a round trip of its own between
|
|
772
|
+
the UPDATE and the COMMIT. Outside one nothing validates after, so nothing comes back. */
|
|
773
|
+
const text = `UPDATE ${qualified(found)} SET ${assignments}${where.text}${guard.text}${transactional ? ' RETURNING *' : ''}`;
|
|
769
774
|
const params = [...Object.values(applied), ...where.params, ...guard.params];
|
|
770
775
|
const result = await write.query(text, params);
|
|
771
776
|
if (request.expected !== undefined && Number(result.rowCount ?? 0) === 0)
|
|
@@ -775,7 +780,8 @@ export class PostgresAdapter {
|
|
|
775
780
|
/* Postgres counts rows matched, not rows whose bytes changed, so saving a
|
|
776
781
|
cell without changing its value still reports 1 — the editor would
|
|
777
782
|
otherwise tell the user a successful write failed. */
|
|
778
|
-
|
|
783
|
+
const row = transactional && result.rows?.length === 1 ? toJsonSafe(result.rows[0]) : undefined;
|
|
784
|
+
return { affected: Number(result.rowCount ?? 0), applied, ...(row ? { row } : {}), sql: { text, params } };
|
|
779
785
|
}
|
|
780
786
|
/**
|
|
781
787
|
* Add one row.
|
|
@@ -9,6 +9,7 @@ import { compileAggregate } from '../../shared/aggregate.js';
|
|
|
9
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
|
+
import { asksComputed, withComputedWhere } from '../../shared/computedWhere.js';
|
|
12
13
|
import { storedTextOf, storedTextSelect } from './storedText.js';
|
|
13
14
|
/** Structured read arguments must not retain mutable values across driver
|
|
14
15
|
* waits. This is deliberately separate from stricter nonempty policy filters:
|
|
@@ -151,7 +152,10 @@ references = false) {
|
|
|
151
152
|
throw new RequestFault("This read cannot compare a column with another table's rows.");
|
|
152
153
|
})()
|
|
153
154
|
: undefined;
|
|
154
|
-
|
|
155
|
+
/* A computed column is written as its expression over this table's own columns (computedWhere.ts), each read
|
|
156
|
+
through the same resolver, so a column this read cannot name is refused inside the expression as outside. */
|
|
157
|
+
const own = filter && asksComputed(filter) ? withComputedWhere(filter, table, style, column, typeIn(table)) : undefined;
|
|
158
|
+
const where = buildWhere(own?.filter ?? filter, style, prefix.params.length + 1, own?.column ?? column, undefined, undefined, exists, references, own?.types ?? typeIn(table), false, undefined, within);
|
|
155
159
|
return { table, column, relation, prefix, where, params: [...prefix.params, ...where.params] };
|
|
156
160
|
};
|
|
157
161
|
/* A SQLite sum that cannot be exact says so in its cell (decimals.ts); PostgreSQL's numeric always is. */
|
|
@@ -828,7 +828,9 @@ referencesOf) {
|
|
|
828
828
|
shiftedRefusal(table, protectedColumns, before, now);
|
|
829
829
|
await parents(table, now, set);
|
|
830
830
|
await afterward(afterwards, { new: now, old: before });
|
|
831
|
-
|
|
831
|
+
// The row the UPDATE returned was not read through the scope, and what ran after it may have written it again.
|
|
832
|
+
const { row: _unscoped, ...said } = result;
|
|
833
|
+
return said;
|
|
832
834
|
};
|
|
833
835
|
/* A batch keeps every rule a row at a time keeps, and pays its flights once: the references it gives checked before
|
|
834
836
|
its one statement, a foreign key a read (combined), then one locked read of the rows it wrote, key IN (…), and
|
package/dist/adapters/sqlite.js
CHANGED
|
@@ -13,7 +13,7 @@ import { waitForLocks, assertIndependentWrite, writeToFile, prepareGuarded, tran
|
|
|
13
13
|
export { cachedStatement, schemaFingerprint } from './sqliteHandles.js';
|
|
14
14
|
import { captureScopeCatalog, mapScopeCatalog } from '../shared/scopeCatalog.js';
|
|
15
15
|
import { inlineRows, inlineWithin } from '../shared/anyRows.js';
|
|
16
|
-
import {
|
|
16
|
+
import { asksComputed, computedTable, readsCatalog, withComputedWhere } from '../shared/computedWhere.js';
|
|
17
17
|
import { qualifiedTable } from '../shared/sqlRelation.js';
|
|
18
18
|
import { DatabaseSync } from 'node:sqlite';
|
|
19
19
|
import { isPromise, isProxy } from 'node:util/types';
|
|
@@ -495,13 +495,15 @@ export class SqliteAdapter {
|
|
|
495
495
|
(commandDb.ts). The catalog is read only when the filter asks, and a filter that asks of a catalog this handle
|
|
496
496
|
cannot read is refused, as one nothing resolves is. */
|
|
497
497
|
async whereOf(id, relation, filter) {
|
|
498
|
-
if (!
|
|
498
|
+
if (!readsCatalog(filter))
|
|
499
499
|
return buildWhere(filter, ANSI_STYLE);
|
|
500
500
|
const schema = await this.schema().catch(() => undefined);
|
|
501
501
|
const inline = schema && inlineRows(schema, id, ANSI_STYLE.quote(relation), ANSI_STYLE, (child) => qualifiedTable(schema, child.id, ANSI_STYLE));
|
|
502
502
|
/* Another table's keys (`in`) are read as this table is, from its own relation: the same bound. */
|
|
503
503
|
const within = schema && inlineWithin(schema, ANSI_STYLE, (other) => qualifiedTable(schema, other.id, ANSI_STYLE));
|
|
504
|
-
|
|
504
|
+
/* A computed column is written as its expression over this table's own columns (computedWhere.ts). */
|
|
505
|
+
const own = filter && asksComputed(filter) ? withComputedWhere(filter, computedTable(schema, id), ANSI_STYLE) : undefined;
|
|
506
|
+
return buildWhere(own?.filter ?? filter, ANSI_STYLE, 1, own?.column, undefined, undefined, undefined, false, undefined, false, inline, within);
|
|
505
507
|
}
|
|
506
508
|
async queryOn(request, db, read = false) {
|
|
507
509
|
const table = read ? this.readTable(request.table) : this.assertTable(request.table, db);
|
|
@@ -512,7 +514,7 @@ export class SqliteAdapter {
|
|
|
512
514
|
its operation input, and the schema wait below must not outlive it. */
|
|
513
515
|
const limit = clampLimit(request.limit);
|
|
514
516
|
const offset = clampOffset(request.offset);
|
|
515
|
-
const where =
|
|
517
|
+
const where = readsCatalog(request.filter)
|
|
516
518
|
? await this.whereOf(request.table, table, request.filter)
|
|
517
519
|
: buildWhere(request.filter, ANSI_STYLE);
|
|
518
520
|
/* A sort by declared vocabulary needs the catalog, but an ordinary read must
|
|
@@ -754,7 +756,7 @@ export class SqliteAdapter {
|
|
|
754
756
|
}
|
|
755
757
|
async countOn(table, filter, db, limit, read = false) {
|
|
756
758
|
const name = read ? this.readTable(table) : this.assertTable(table, db);
|
|
757
|
-
const where =
|
|
759
|
+
const where = readsCatalog(filter) ? await this.whereOf(table, name, filter) : buildWhere(filter, ANSI_STYLE);
|
|
758
760
|
const text = countStatement(`${ANSI_STYLE.quote(name)}${where.text}`, limit);
|
|
759
761
|
const [row] = await this.readRows(text, where.params, true, db);
|
|
760
762
|
return Number(row?.n ?? 0);
|
|
@@ -36,6 +36,9 @@ export interface ColumnGrant {
|
|
|
36
36
|
read: ReadonlySet<string>;
|
|
37
37
|
write: ReadonlySet<string>;
|
|
38
38
|
}>;
|
|
39
|
+
/** Of each table's rollups and computed columns, the ones a field grant names to be read, by table id: what the
|
|
40
|
+
App's catalog keeps of them (GRANTED_DERIVED). A table missing here keeps every one its reader can compute. */
|
|
41
|
+
derived?: ReadonlyMap<string, ReadonlySet<string>>;
|
|
39
42
|
/** Columns read and written by the server for its own work (a file's object
|
|
40
43
|
and hash): never listed in a served catalog, and refused to every request
|
|
41
44
|
but the server's own (`serverRequest`). */
|
|
@@ -14,9 +14,10 @@ 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
16
|
import { aggregateColumns } from '../shared/aggregate.js';
|
|
17
|
+
import { checkedComputed } from '../shared/computedWhere.js';
|
|
17
18
|
import { textToView } from '../shared/viewtext.js';
|
|
18
19
|
import { textToBreakdown } from '../shared/breakdowntext.js';
|
|
19
|
-
import { isJsonType } from '../shared/schema.js';
|
|
20
|
+
import { GRANTED_DERIVED, isJsonType } from '../shared/schema.js';
|
|
20
21
|
import { impliedEdges } from '../shared/declaredReferences.js';
|
|
21
22
|
import { unformedColumns } from '../shared/form.js';
|
|
22
23
|
import { isServerRequest } from './serverRequests.js';
|
|
@@ -45,6 +46,7 @@ export function captureGrant(grant) {
|
|
|
45
46
|
fields: new Map([...grant.fields].map(([table, fields]) => [table, { read: new Set(fields.read), write: new Set(fields.write) }])),
|
|
46
47
|
}
|
|
47
48
|
: {}),
|
|
49
|
+
...(grant.derived ? { derived: new Map([...grant.derived].map(([table, names]) => [table, new Set(names)])) } : {}),
|
|
48
50
|
};
|
|
49
51
|
}
|
|
50
52
|
const withoutCarriedReferences = (def) => {
|
|
@@ -68,7 +70,7 @@ const prunedCatalogs = new WeakMap();
|
|
|
68
70
|
/* A tenant request resolves its grant afresh each time, so its sets are never
|
|
69
71
|
the same objects twice, and every read in it pruned the catalog again
|
|
70
72
|
(G118). A captured catalog is frozen, and pruning reads nothing of the grant
|
|
71
|
-
but
|
|
73
|
+
but what this key holds, so the answer for a catalog and a key is
|
|
72
74
|
kept — bounded, oldest out first — and frozen, so no caller can change the
|
|
73
75
|
copy another one is handed. */
|
|
74
76
|
const prunedByContent = new WeakMap();
|
|
@@ -87,6 +89,7 @@ function grantKey(access) {
|
|
|
87
89
|
access.internal
|
|
88
90
|
? [...access.internal].map(([table, columns]) => [table, sorted(columns)]).sort((a, b) => (a[0] < b[0] ? -1 : 1))
|
|
89
91
|
: null,
|
|
92
|
+
access.derived ? [...access.derived].map(([table, names]) => [table, sorted(names)]).sort((a, b) => (a[0] < b[0] ? -1 : 1)) : null,
|
|
90
93
|
]);
|
|
91
94
|
}
|
|
92
95
|
/** Test-only: how many times a catalog was pruned rather than reused. */
|
|
@@ -120,13 +123,14 @@ export function accessibleSchema(schema, access) {
|
|
|
120
123
|
kept.appendOnly === access.appendOnly &&
|
|
121
124
|
kept.fields === access.fields &&
|
|
122
125
|
kept.internal === access.internal &&
|
|
126
|
+
kept.derived === access.derived &&
|
|
123
127
|
kept.scoped === scoped)
|
|
124
128
|
return kept.pruned;
|
|
125
129
|
// Frozen over a captured catalog, as the content path's is: what reads it later (resourceChecks) may then keep by its identity.
|
|
126
130
|
const made = pruneCatalog(schema, access);
|
|
127
131
|
const pruned = isCapturedScopeCatalog(schema) ? freezeDerivedCatalog(made) : made;
|
|
128
|
-
const { writable, creatable, appendOnly, fields, internal } = access;
|
|
129
|
-
byGrant.set(access.readable, { writable, creatable, appendOnly, fields, internal, scoped, pruned });
|
|
132
|
+
const { writable, creatable, appendOnly, fields, internal, derived } = access;
|
|
133
|
+
byGrant.set(access.readable, { writable, creatable, appendOnly, fields, internal, derived, scoped, pruned });
|
|
130
134
|
return pruned;
|
|
131
135
|
}
|
|
132
136
|
function pruneCatalog(schema, access) {
|
|
@@ -217,6 +221,7 @@ function pruneCatalog(schema, access) {
|
|
|
217
221
|
deletable above; the columns stay writable, for the New form. Capability only, never the authority
|
|
218
222
|
(authorizeColumnWrites is). */
|
|
219
223
|
...(access.appendOnly?.has(table.id) ? { appendOnly: true, deletable: false } : {}),
|
|
224
|
+
...(access.derived?.has(table.id) ? { [GRANTED_DERIVED]: Object.freeze([...access.derived.get(table.id)].sort()) } : {}),
|
|
220
225
|
...(table.indexed ? { indexed: table.indexed.filter((column) => listed(table.id, column)) } : {}),
|
|
221
226
|
// Like `indexed`: a hidden column is not named, even as unique.
|
|
222
227
|
...(table.unique ? { unique: table.unique.filter((column) => listed(table.id, column)) } : {}),
|
|
@@ -407,11 +412,31 @@ export function resourceChecks(full, access) {
|
|
|
407
412
|
const filter = requestData(input);
|
|
408
413
|
/* An `any`/`no` condition names the rows that point here, not a column of this table: its child, reference and
|
|
409
414
|
conditions are held to the reader as a view's are (assertAnyRows), and its own `column` is never read. */
|
|
415
|
+
/* A computed column a condition names is no column of the table: the condition is held to what its expression
|
|
416
|
+
reads (worked out from the expression, never from the request), each one a column this reader is served. */
|
|
417
|
+
let computed = new Map();
|
|
418
|
+
if (filter?.computed !== undefined) {
|
|
419
|
+
const declared = full.tables.find((item) => item.id === table);
|
|
420
|
+
if (!declared)
|
|
421
|
+
throw new AppAccessDenied();
|
|
422
|
+
try {
|
|
423
|
+
computed = checkedComputed(filter, declared);
|
|
424
|
+
}
|
|
425
|
+
catch {
|
|
426
|
+
throw new AppAccessDenied();
|
|
427
|
+
}
|
|
428
|
+
for (const def of computed.values())
|
|
429
|
+
assertFields(table, def.reads, 'read', server);
|
|
430
|
+
}
|
|
410
431
|
if (filter)
|
|
411
432
|
for (const group of filter.groups)
|
|
412
433
|
for (const condition of group)
|
|
413
434
|
if (condition.any)
|
|
414
435
|
assertAnyRows({ groups: [[{ ...condition, path: condition.column }]] });
|
|
436
|
+
else if (computed.has(condition.column)) {
|
|
437
|
+
if (condition.within !== undefined || condition.other !== undefined)
|
|
438
|
+
throw new AppAccessDenied();
|
|
439
|
+
}
|
|
415
440
|
else {
|
|
416
441
|
assertFields(table, [condition.column], 'read', server);
|
|
417
442
|
if (condition.within !== undefined)
|
|
@@ -13,10 +13,19 @@
|
|
|
13
13
|
* in the container's environment on each deploy, and `build` is the id of the
|
|
14
14
|
* build being served. Both are random or a hash, so safe to expose.
|
|
15
15
|
*
|
|
16
|
+
* While a tenant server is still opening its tenants, `/readyz` also says how far
|
|
17
|
+
* it has got (`ready`, `opened`, `of`), and afterward which tenant keys failed to
|
|
18
|
+
* open: counts and keys only, never an error.
|
|
19
|
+
*
|
|
16
20
|
* The operator reads with more in them (`/api/operations/readiness`) are
|
|
17
21
|
* signed-in and granted; these are not, and so say nothing else.
|
|
18
22
|
*/
|
|
19
23
|
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
20
24
|
/** Where `/readyz` reads the served build's id from; the App framework registers it when it serves a build. */
|
|
21
25
|
export declare function setBuildIdSource(source: () => string | null): void;
|
|
22
|
-
|
|
26
|
+
/** What a readiness check may say beside yes or no: words for the body (how far a start has got, what failed to open). */
|
|
27
|
+
export type ReadyAnswer = boolean | {
|
|
28
|
+
readonly ok: boolean;
|
|
29
|
+
readonly body?: Readonly<Record<string, unknown>>;
|
|
30
|
+
};
|
|
31
|
+
export declare function probe(req: IncomingMessage, res: ServerResponse, path: string, ready?: () => ReadyAnswer): boolean;
|
|
@@ -11,9 +11,13 @@ export function probe(req, res, path, ready) {
|
|
|
11
11
|
return true;
|
|
12
12
|
}
|
|
13
13
|
let ok = true;
|
|
14
|
+
let extra = {};
|
|
14
15
|
if (path === '/readyz') {
|
|
15
16
|
try {
|
|
16
|
-
|
|
17
|
+
const answer = ready?.() ?? true;
|
|
18
|
+
ok = typeof answer === 'boolean' ? answer : answer.ok;
|
|
19
|
+
if (typeof answer === 'object' && answer.body)
|
|
20
|
+
extra = answer.body;
|
|
17
21
|
}
|
|
18
22
|
catch {
|
|
19
23
|
ok = false;
|
|
@@ -22,6 +26,6 @@ export function probe(req, res, path, ready) {
|
|
|
22
26
|
res.writeHead(ok ? 200 : 503, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' });
|
|
23
27
|
res.end(req.method === 'HEAD'
|
|
24
28
|
? undefined
|
|
25
|
-
: JSON.stringify({ status: ok ? 'ok' : 'unavailable', deploy: process.env.MISTRI_DEPLOY_ID || null, build: buildSource() }));
|
|
29
|
+
: JSON.stringify({ status: ok ? 'ok' : 'unavailable', deploy: process.env.MISTRI_DEPLOY_ID || null, build: buildSource(), ...extra }));
|
|
26
30
|
return true;
|
|
27
31
|
}
|
|
@@ -7,6 +7,7 @@ import { AppAccessDenied } from '../columnGrants.js';
|
|
|
7
7
|
import { deleteImpact } from '../impact.js';
|
|
8
8
|
import { explainWriteFailure, writeFailureOutcome, writeRefusalStatus } from '../constraints.js';
|
|
9
9
|
import { findTable } from '../../shared/schema.js';
|
|
10
|
+
import { isFrozenRefusal } from '../../shared/refusal.js';
|
|
10
11
|
import { FILE_UPLOAD_PATH, bytesLabelled, send, sendBusy, sendResource, sendRowLocked } from './reply.js';
|
|
11
12
|
/* A failed insert or update's status. The database refusing it for good — a key taken, a reference to nothing, a
|
|
12
13
|
value its column cannot hold — is 409 or 422 (constraints.ts writeRefusalStatus); it was a 200 whose body alone
|
|
@@ -107,6 +108,11 @@ export async function recordWriteRoutes(post, hooks) {
|
|
|
107
108
|
}
|
|
108
109
|
if (!completed && rules?.refused(res, path, err))
|
|
109
110
|
return true;
|
|
111
|
+
/* A frozen record (`frozen`, appModel.ts) is the record's state, said as a conflict is: 409, by its own code. */
|
|
112
|
+
if (!completed && isFrozenRefusal(err)) {
|
|
113
|
+
sendResource(res, path, 409, { errors: [await failureOf(err, false, values)], outcome: 'rejected' });
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
110
116
|
if (!completed && (err instanceof Refusal || err instanceof WriteConflict))
|
|
111
117
|
throw err;
|
|
112
118
|
/* Pointing an existing row at a row that is not there is the same
|
|
@@ -290,7 +296,7 @@ export async function recordWriteRoutes(post, hooks) {
|
|
|
290
296
|
}
|
|
291
297
|
const outcome = completed ? 'unknown' : err instanceof Refusal ? 'rejected' : writeFailureOutcome(err);
|
|
292
298
|
const errors = [rules ? rules.failure(post, err, completed) : { message: err.message }];
|
|
293
|
-
sendResource(res, path, outcome === 'unknown' ? 200 : (writeRefusalStatus(err) ?? 400), outcome === 'unknown' ? { impact, deleted: null, outcome, errors } : { impact, deleted: false, outcome, errors });
|
|
299
|
+
sendResource(res, path, outcome === 'unknown' ? 200 : isFrozenRefusal(err) ? 409 : (writeRefusalStatus(err) ?? 400), outcome === 'unknown' ? { impact, deleted: null, outcome, errors } : { impact, deleted: false, outcome, errors });
|
|
294
300
|
return true;
|
|
295
301
|
}
|
|
296
302
|
}
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
import type { IncomingMessage, Server, ServerResponse } from 'node:http';
|
|
16
16
|
import type { RequestDiagnostics } from '../operations/diagnostics.js';
|
|
17
17
|
import { type AddressMount } from '../../shared/mount.js';
|
|
18
|
+
import { type ReadyAnswer } from './probes.js';
|
|
18
19
|
import type { Bound } from './trust.js';
|
|
19
20
|
/**
|
|
20
21
|
* The request read through the server's mounts (shared/mount.ts): the base
|
|
@@ -37,7 +38,7 @@ export interface Listening {
|
|
|
37
38
|
readonly bound: Bound;
|
|
38
39
|
readonly diagnostics?: RequestDiagnostics;
|
|
39
40
|
/** Whether the server should take traffic now: false while it starts, reloads its policy or shuts down (`/readyz`). */
|
|
40
|
-
readonly ready?: () =>
|
|
41
|
+
readonly ready?: () => ReadyAnswer;
|
|
41
42
|
/** A suite owns the source client and HMR at its shared listener. */
|
|
42
43
|
readonly sourceClient?: false;
|
|
43
44
|
/** A suite must drain even disconnected form submissions before closing its App registry. */
|
|
@@ -570,6 +570,13 @@ async function runDesired(command, target, options, name, write) {
|
|
|
570
570
|
const path = newMigration(target.dir, name, false, new Date(), `${diffHeader()}${sql}\n`);
|
|
571
571
|
write(`wrote ${shown(path)}:`);
|
|
572
572
|
write(sql);
|
|
573
|
+
// A trigger's body is the database's to run: `check` names the ones that read other tables once they are migrated in.
|
|
574
|
+
const triggers = [
|
|
575
|
+
...sql.matchAll(/\bCREATE\s+(?:OR\s+REPLACE\s+)?(?:CONSTRAINT\s+)?TRIGGER\s+(?:IF\s+NOT\s+EXISTS\s+)?("[^"]+"|[\w$.]+)/gi),
|
|
576
|
+
].map((found) => found[1]);
|
|
577
|
+
if (triggers.length)
|
|
578
|
+
write(`note: it creates ${plural(triggers.length, 'trigger')} (${triggers.join(', ')}). A trigger that reads another table can refuse a write ` +
|
|
579
|
+
'in ways an App cannot see; `mistri check --app` names those [trigger-cross-table].');
|
|
573
580
|
if (/\bDROP\b/i.test(sql))
|
|
574
581
|
write('note: it drops something. If that is a rename, rewrite it as RENAME: a diff cannot tell a rename from a drop and an add.');
|
|
575
582
|
return EXIT.ok;
|
|
@@ -91,7 +91,11 @@ export function assertStructuredReadFilter(filter, field) {
|
|
|
91
91
|
if (typeof filter !== 'object' || Array.isArray(filter))
|
|
92
92
|
fail();
|
|
93
93
|
const given = filter;
|
|
94
|
-
|
|
94
|
+
/* A column filter may carry the computed columns its conditions name (Filter.computed): its shape is checked here,
|
|
95
|
+
each expression where it is written (computedWhere.ts checkedComputed), never on the strength of this check. */
|
|
96
|
+
const computed = given.computed;
|
|
97
|
+
const carriesComputed = field === 'column' && computed !== undefined && computed !== null && typeof computed === 'object' && !Array.isArray(computed);
|
|
98
|
+
if (Object.keys(given).some((key) => key !== 'groups' && !(key === 'computed' && carriesComputed)) || !Array.isArray(given.groups))
|
|
95
99
|
fail();
|
|
96
100
|
const scalar = (value) => value === null || typeof value === 'string' || typeof value === 'boolean' || (typeof value === 'number' && Number.isFinite(value));
|
|
97
101
|
for (const group of given.groups) {
|
package/dist/shared/aggregate.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { compileBreakdown, DATE_UNITS } from './breakdown.js';
|
|
2
|
+
import { checkedComputed } from './computedWhere.js';
|
|
2
3
|
import { RequestFault } from './refusal.js';
|
|
3
4
|
import { findTable } from './schema.js';
|
|
4
5
|
import { PathError } from './viewPaths.js';
|
|
@@ -60,13 +61,17 @@ function aggregateBreakdown(schema, request) {
|
|
|
60
61
|
...(measure.column === undefined ? {} : { path: column(measure.column, 'measure') }),
|
|
61
62
|
};
|
|
62
63
|
});
|
|
64
|
+
/* A computed column its where names is written as its expression over the table's own columns, as a view writes
|
|
65
|
+
one (ViewDef.expressions); checked first, as a single-table read's is (computedWhere.ts). Keys and measures
|
|
66
|
+
stay the table's own columns. */
|
|
67
|
+
const computed = request.filter ? checkedComputed(request.filter, table) : new Map();
|
|
63
68
|
const groups = (request.filter?.groups ?? []).map((group) => group.map((condition) => {
|
|
64
69
|
/* A comparison with another column, rows that point here (`any`), or another table's rows (`within`), reads past this table's own values:
|
|
65
70
|
neither is a condition a command's where writes (compileWhere), and neither is taken here. */
|
|
66
71
|
if (condition.any !== undefined || condition.other !== undefined || condition.within !== undefined)
|
|
67
72
|
throw new RequestFault("An aggregate's where compares the table's own columns with values.");
|
|
68
73
|
return {
|
|
69
|
-
path: column(condition.column, 'where'),
|
|
74
|
+
path: computed.has(condition.column) ? condition.column : column(condition.column, 'where'),
|
|
70
75
|
op: condition.op,
|
|
71
76
|
...(condition.value === undefined ? {} : { value: condition.value }),
|
|
72
77
|
...(condition.collation ? { collation: condition.collation } : {}),
|
|
@@ -86,6 +91,7 @@ function aggregateBreakdown(schema, request) {
|
|
|
86
91
|
...(dates.length ? { dateColumns: dates } : {}),
|
|
87
92
|
measures,
|
|
88
93
|
...(groups.length ? { filter: { groups } } : {}),
|
|
94
|
+
...(computed.size ? { expressions: Object.fromEntries([...computed].map(([name, def]) => [name, { ...def, on: '' }])) } : {}),
|
|
89
95
|
...(orderBy.length ? { orderBy } : {}),
|
|
90
96
|
limit: request.limit,
|
|
91
97
|
};
|
|
@@ -5,6 +5,8 @@
|
|
|
5
5
|
* the first page reaches is drawn into the first page's chunk, so the parser lived there for a form's live lines.
|
|
6
6
|
*/
|
|
7
7
|
import { type ComputedNode } from './computed.js';
|
|
8
|
+
/** The functions an expression may call: each written alike on every engine and worked out alike in JavaScript. */
|
|
9
|
+
export declare const COMPUTED_FUNCTIONS: readonly ["coalesce", "round", "min", "max", "abs", "concat"];
|
|
8
10
|
/**
|
|
9
11
|
* An expression's tree, or why it is not one. `isColumn` says which bare
|
|
10
12
|
* words are names: a word on the right of `=` or `!=` that is not one is a
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import { ComputedError } from './computed.js';
|
|
8
8
|
/** The functions an expression may call: each written alike on every engine and worked out alike in JavaScript. */
|
|
9
|
-
const COMPUTED_FUNCTIONS = ['coalesce', 'round', 'min', 'max', 'abs', 'concat'];
|
|
9
|
+
export const COMPUTED_FUNCTIONS = ['coalesce', 'round', 'min', 'max', 'abs', 'concat'];
|
|
10
10
|
const isFunction = (word) => COMPUTED_FUNCTIONS.includes(word);
|
|
11
11
|
/* The greatest number of places `round` keeps: past it a figure is no longer one an engine says exactly. */
|
|
12
12
|
const ROUND_PLACES = 10;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Computed columns in a single-table read's filter: `db.tasks.where('is_overdue')` in a handler or a loader.
|
|
3
|
+
*
|
|
4
|
+
* A computed column is no column of the table (computed.ts), so a read that names one carries its expression on the
|
|
5
|
+
* filter (`Filter.computed`), and the adapter writes that expression where a condition names it, with the same
|
|
6
|
+
* compiler a view uses (`compileComputed`). The expression is what the reader's catalog served, its constants folded:
|
|
7
|
+
* the caller resolved it, never the person asking. It is still checked here, node by node, before a word of it reaches
|
|
8
|
+
* a statement, because `compileComputed` writes a function's name, an operator, a unit and a JSON key as text: anything
|
|
9
|
+
* the parser would never have produced is refused, and so is a read of anything but one of the table's own columns.
|
|
10
|
+
* What it reads is worked out from the expression (`computedReads`), never taken from the request, so a grant check
|
|
11
|
+
* of those reads (columnGrants.ts) is a check of what the statement reads.
|
|
12
|
+
*/
|
|
13
|
+
import { type ComputedNode, type ComputedType } from './computed.js';
|
|
14
|
+
import { type Condition, type Filter, type SqlStyle } from './filter.js';
|
|
15
|
+
import { type DatabaseSchema, type TableMeta } from './schema.js';
|
|
16
|
+
/** The computed columns a filter's conditions may name, by name: each one's type and its expression, constants folded. */
|
|
17
|
+
export type FilterComputed = Readonly<Record<string, {
|
|
18
|
+
readonly type: ComputedType;
|
|
19
|
+
readonly expr: ComputedNode;
|
|
20
|
+
}>>;
|
|
21
|
+
/** Whether a filter carries computed columns, so the adapter needs the table's columns to write them. */
|
|
22
|
+
export declare const asksComputed: (filter: Filter | undefined) => boolean;
|
|
23
|
+
/** Whether a single-table read's filter needs the catalog: it asks about other rows, or names a computed column. */
|
|
24
|
+
export declare const readsCatalog: (filter: Filter | undefined) => boolean;
|
|
25
|
+
/** The table a computed column is written over; a catalog that cannot say it refuses the read, never drops the condition. */
|
|
26
|
+
export declare function computedTable(schema: DatabaseSchema | undefined, id: string): TableMeta;
|
|
27
|
+
/**
|
|
28
|
+
* The filter's computed columns, each checked against the table: a name that is no column of it, a type, and an
|
|
29
|
+
* expression of the parser's own shapes reading only the table's stored columns. Its constants are folded already:
|
|
30
|
+
* `today`, `now` and `me` are the request's, said by whoever resolved the column, so an unfolded one is refused.
|
|
31
|
+
*/
|
|
32
|
+
export declare function checkedComputed(filter: Filter, table: TableMeta): Map<string, {
|
|
33
|
+
type: ComputedType;
|
|
34
|
+
expr: ComputedNode;
|
|
35
|
+
reads: string[];
|
|
36
|
+
}>;
|
|
37
|
+
/**
|
|
38
|
+
* The filter as buildWhere takes it, with its computed columns written where a condition names one: the filter
|
|
39
|
+
* without them, a column resolver that writes the expression in place of the name, and a types reader that says
|
|
40
|
+
* nothing of a computed name (it has no catalog type, so it compares by the operator as written). `column` is how
|
|
41
|
+
* the read writes its own columns; the expression's reads are written through it too.
|
|
42
|
+
*/
|
|
43
|
+
export declare function withComputedWhere(filter: Filter, table: TableMeta, style: SqlStyle, column?: (name: string, condition?: Condition) => string, types?: (name: string) => string | undefined): {
|
|
44
|
+
filter: Filter;
|
|
45
|
+
column: (name: string, condition?: Condition) => string;
|
|
46
|
+
types?: (name: string) => string | undefined;
|
|
47
|
+
};
|