@mistriwork/engine 1.0.0-rc.11 → 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.
- package/dist/adapters/postgres.js +28 -3
- package/dist/adapters/scoped/postgresScopeCatalog.d.ts +22 -1
- package/dist/adapters/scoped/postgresScopeCatalog.js +66 -2
- package/dist/adapters/scoped/scopedReads.js +6 -4
- package/dist/adapters/scoped/scopedTransaction.d.ts +21 -2
- package/dist/adapters/scoped/scopedTransaction.js +37 -7
- package/dist/adapters/sqlite.js +1 -1
- package/dist/server/http/router.d.ts +11 -22
- package/dist/server/http/router.js +0 -15
- package/dist/shared/addressParams.d.ts +2 -0
- package/dist/shared/addressParams.js +2 -0
- package/dist/shared/breakdown.js +2 -2
- package/dist/shared/filter.d.ts +18 -1
- package/dist/shared/filter.js +62 -8
- package/dist/shared/listPresentations.d.ts +2 -0
- package/dist/shared/listQuestion.d.ts +3 -1
- package/dist/shared/moneyEntry.d.ts +6 -3
- package/dist/shared/moneyEntry.js +14 -7
- package/dist/shared/recordview.d.ts +1 -1
- package/dist/shared/rowScope.js +12 -4
- package/dist/shared/schematext.d.ts +22 -6
- package/dist/shared/schematext.js +18 -2
- package/dist/shared/servedApp.d.ts +8 -2
- package/dist/shared/view.js +4 -4
- package/dist/shared/viewLabels.d.ts +1 -0
- package/dist/shared/viewLabels.js +8 -2
- package/dist/shared/viewPaths.d.ts +3 -0
- package/dist/shared/viewPaths.js +8 -0
- package/package.json +1 -2
- package/schema/model.d.ts +1 -1
- package/dist/shared/order.d.ts +0 -59
- package/dist/shared/order.js +0 -133
|
@@ -13,7 +13,7 @@ import { compileAggregate } from '../shared/aggregate.js';
|
|
|
13
13
|
import { coerceForColumn } from '../shared/schema.js';
|
|
14
14
|
import { buildUpdateGuard } from './scoped/updateGuard.js';
|
|
15
15
|
import { createScopedTransaction, foreignKeyRefusal, isForeignKeyViolation, referencesStatement, wholeWithin, } from './scoped/scopedTransaction.js';
|
|
16
|
-
import { attestPostgresTriggers, effectCatalogFacts, readPostgresScopeCatalog, } from './scoped/postgresScopeCatalog.js';
|
|
16
|
+
import { attestPostgresTriggers, effectCatalogFacts, hierarchyRecheck, joinedHierarchy, readPostgresScopeCatalog, } from './scoped/postgresScopeCatalog.js';
|
|
17
17
|
import { postgresTriggerAnalyzer, postgresTriggersByTable, postgresTriggerStatement, readPostgresTableCode, } from './scoped/postgresTriggers.js';
|
|
18
18
|
import { captureScopeCatalog, mapScopeCatalog } from '../shared/scopeCatalog.js';
|
|
19
19
|
import { createPinnedPostgresReadOwner } from './postgresReadCatalog.js';
|
|
@@ -23,7 +23,7 @@ import { lendStoreHost, } from './storeHost.js';
|
|
|
23
23
|
import { compileView, compileViewCount } from '../shared/view.js';
|
|
24
24
|
import { compileBreakdown, compileBreakdownCount } from '../shared/breakdown.js';
|
|
25
25
|
import { PgConnectionManager, sharedPgConnections } from './postgresPool.js';
|
|
26
|
-
import { aheadReport, behindWrite, generationOf, redoWithoutAhead, sendsAhead, waitsAhead, RolledBackAtCommit, transactionHandle, attestedOn, keepAttestedOn, forgetAttestations, noteCatalogVersion, readsCatalogVersion, suspectsStores, } from './postgresTransactionHandle.js';
|
|
26
|
+
import { aheadReport, beforeCommit, behindWrite, generationOf, redoWithoutAhead, sendsAhead, waitsAhead, RolledBackAtCommit, transactionHandle, attestedOn, keepAttestedOn, forgetAttestations, noteCatalogVersion, readsCatalogVersion, suspectsStores, } from './postgresTransactionHandle.js';
|
|
27
27
|
import { snapshotRead, withoutSnapshotRead } from './snapshotRead.js';
|
|
28
28
|
import { withoutWriteAhead, writeAhead } from './writeAhead.js';
|
|
29
29
|
import { KeyRows } from './scoped/keyRows.js';
|
|
@@ -1605,6 +1605,8 @@ export class PostgresAdapter {
|
|
|
1605
1605
|
});
|
|
1606
1606
|
// The tables whose validation-only triggers a write of this transaction fired: their proof is read again before COMMIT.
|
|
1607
1607
|
const relied = new Set();
|
|
1608
|
+
// The tables this transaction's writes targeted: whether one joined a hierarchy is read again with the COMMIT.
|
|
1609
|
+
const written = new Set();
|
|
1608
1610
|
attest = () => attestPostgresTriggers(write, catalog, relied);
|
|
1609
1611
|
let records = createScopedTransaction(catalog, {
|
|
1610
1612
|
query: (r) => this.queryOn(r, write),
|
|
@@ -1621,11 +1623,12 @@ export class PostgresAdapter {
|
|
|
1621
1623
|
},
|
|
1622
1624
|
together: sendsAhead(write),
|
|
1623
1625
|
relied: (table) => void relied.add(table.id),
|
|
1626
|
+
written: (table) => void written.add(table.id),
|
|
1624
1627
|
}, bound && {
|
|
1625
1628
|
reads: scopedReads(true, bound),
|
|
1626
1629
|
whole: wholeWithin(scope, bound),
|
|
1627
1630
|
references: async (fk, key) => {
|
|
1628
|
-
const statement = referencesStatement(catalog
|
|
1631
|
+
const statement = referencesStatement(catalog, fk, key, POSTGRES_STYLE);
|
|
1629
1632
|
return Number((await write.query(statement.text, statement.params)).rows[0]?.n ?? 0);
|
|
1630
1633
|
},
|
|
1631
1634
|
},
|
|
@@ -1666,11 +1669,33 @@ export class PostgresAdapter {
|
|
|
1666
1669
|
if (verify)
|
|
1667
1670
|
await verify(write, catalog, described);
|
|
1668
1671
|
await attestPostgresTriggers(write, catalog, relied);
|
|
1672
|
+
/* A target's triggers may write any table the scope can write (triggerEffects.ts): where one ran, each of
|
|
1673
|
+
those is read again too. */
|
|
1674
|
+
const reached = [...written].some((id) => catalog.triggered?.has(id))
|
|
1675
|
+
? [...written, ...catalog.writable, ...(catalog.triggered?.keys() ?? [])]
|
|
1676
|
+
: written;
|
|
1677
|
+
/* And what they reference, for a parent that gained an inheritance child (ScopeCatalog.inheritanceParents):
|
|
1678
|
+
one that was a parent when the catalog was read was judged by the parent checks, which refused a
|
|
1679
|
+
reference to it. A table that became one is refused whether or not a reference to it was checked. */
|
|
1680
|
+
const wrote = new Set(reached);
|
|
1681
|
+
const recheck = hierarchyRecheck(reached, catalog.schema.foreignKeys
|
|
1682
|
+
.filter((fk) => wrote.has(fk.from.table) && !catalog.inheritanceParents?.has(fk.to.table))
|
|
1683
|
+
.map((fk) => fk.to.table));
|
|
1684
|
+
if (recheck && !beforeCommit(write, [recheck]))
|
|
1685
|
+
await write.query(recheck.text, recheck.values).catch((error) => {
|
|
1686
|
+
throw joinedHierarchy(error) ?? error;
|
|
1687
|
+
});
|
|
1669
1688
|
commitAttempted = true;
|
|
1670
1689
|
try {
|
|
1671
1690
|
await write.query('COMMIT');
|
|
1672
1691
|
}
|
|
1673
1692
|
catch (error) {
|
|
1693
|
+
// The recheck sent with the COMMIT found a hierarchy: the server answered the COMMIT as a ROLLBACK.
|
|
1694
|
+
const joined = error instanceof RolledBackAtCommit ? joinedHierarchy(error.cause) : undefined;
|
|
1695
|
+
if (joined) {
|
|
1696
|
+
commitAttempted = false;
|
|
1697
|
+
throw joined;
|
|
1698
|
+
}
|
|
1674
1699
|
/* A deferred foreign key the COMMIT found broken: the server answered, so nothing committed, and the
|
|
1675
1700
|
refusal is a hidden parent's (foreignKeyRefusal). */
|
|
1676
1701
|
if (!isForeignKeyViolation(error))
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { DatabaseSchema } from '../../shared/schema.js';
|
|
2
2
|
import type { RowScope } from '../../shared/rowScope.js';
|
|
3
3
|
import type { PgQuery } from '../postgresPool.js';
|
|
4
|
-
import { type ScopeCatalog } from './scopedTransaction.js';
|
|
4
|
+
import { ScopeRefusal, type ScopeCatalog } from './scopedTransaction.js';
|
|
5
5
|
import type { TriggerKeyFacts } from './triggerEffects.js';
|
|
6
6
|
export interface PostgresScopeCatalog extends ScopeCatalog {
|
|
7
7
|
/** Ordinary tables the writer role may UPDATE, and so lock rows in. A
|
|
@@ -22,6 +22,27 @@ export interface PostgresScopeCatalog extends ScopeCatalog {
|
|
|
22
22
|
* could have used: a changed one refuses the transaction.
|
|
23
23
|
*/
|
|
24
24
|
export declare function attestPostgresTriggers(client: PgQuery, catalog: PostgresScopeCatalog, tables: ReadonlySet<string>): Promise<void>;
|
|
25
|
+
/**
|
|
26
|
+
* The tables a scoped write wrote, read again for a hierarchy just before COMMIT, as a statement that fails where one
|
|
27
|
+
* is in one: sent with the COMMIT (beforeCommit), its failure answers the COMMIT as a ROLLBACK, so it costs no round
|
|
28
|
+
* trip. The pin's ROW EXCLUSIVE holds a target against becoming a partition or a child (ATTACH PARTITION and ALTER
|
|
29
|
+
* TABLE … INHERIT take ACCESS EXCLUSIVE on it), but not against becoming a parent: ALTER TABLE c INHERIT t and CREATE
|
|
30
|
+
* TABLE c () INHERITS (t) take SHARE UPDATE EXCLUSIVE on t, and every mode that conflicts with that conflicts with an
|
|
31
|
+
* ordinary write's ROW EXCLUSIVE or with itself, which would line up every scoped write of the table behind one
|
|
32
|
+
* another. Under READ COMMITTED a statement after the transaction's last write sees every inheritance committed before
|
|
33
|
+
* that write was planned, and one committed after it no write of the transaction expanded into: so a target that
|
|
34
|
+
* passed the catalog's check (ScopeCatalog.hierarchy) and gained a child while the write ran refuses the transaction.
|
|
35
|
+
* The tables those reference are read in the same statement for the same reason: one that gained an inheritance child
|
|
36
|
+
* while the write ran (CREATE TABLE … INHERITS takes the same SHARE UPDATE EXCLUSIVE) may have let a parent check
|
|
37
|
+
* find the child's visible row where the foreign key, which reads the parent alone, found a hidden one
|
|
38
|
+
* (ScopeCatalog.inheritanceParents). Undefined where nothing was written.
|
|
39
|
+
*/
|
|
40
|
+
export declare function hierarchyRecheck(ids: Iterable<string>, referenced?: Iterable<string>): {
|
|
41
|
+
text: string;
|
|
42
|
+
values: unknown[];
|
|
43
|
+
} | undefined;
|
|
44
|
+
/** The refusal a hierarchyRecheck failure says, or undefined for any other failure. */
|
|
45
|
+
export declare function joinedHierarchy(error: unknown): ScopeRefusal | undefined;
|
|
25
46
|
/**
|
|
26
47
|
* For `mistri check`: what a write's own catalog reads about `ids` to judge a trigger's effects
|
|
27
48
|
* (readPostgresScopeCatalog), read on `client` outside a write: the keys, the tables in a partition or inheritance
|
|
@@ -27,7 +27,8 @@
|
|
|
27
27
|
import { POSTGRES_STYLE, Refusal } from '../adapter.js';
|
|
28
28
|
import { captureScopeCatalog } from '../../shared/scopeCatalog.js';
|
|
29
29
|
import { writtenType } from '../postgresCatalog.js';
|
|
30
|
-
import { referenceKey, triggerNames } from './scopedTransaction.js';
|
|
30
|
+
import { referenceKey, ScopeRefusal, triggerNames } from './scopedTransaction.js';
|
|
31
|
+
import { recordRefusalCause } from '../../shared/refusal.js';
|
|
31
32
|
import { baseType, CATALOG_OPERATORS, defaultBtreeClass, postgresTreeTriggerNames, postgresTriggerAnalyzer, postgresTriggersByTable, postgresTriggerStatement, postgresTriggerVersions, readPostgresTableCode, } from './postgresTriggers.js';
|
|
32
33
|
import { keyTypeRefusal, postgresKeyType } from './keyTypes.js';
|
|
33
34
|
const { EQ, GT, LT, NE, PLUS } = CATALOG_OPERATORS;
|
|
@@ -60,6 +61,7 @@ async function facts(client, targets) {
|
|
|
60
61
|
pg_catalog.has_table_privilege(c.oid, 'UPDATE') AS updatable,
|
|
61
62
|
c.relkind ${EQ} 'p' OR EXISTS (SELECT 1 FROM pg_catalog.pg_inherits i WHERE i.inhrelid ${EQ} c.oid OR i.inhparent ${EQ} c.oid)
|
|
62
63
|
AS hierarchy,
|
|
64
|
+
c.relkind ${EQ} 'r' AND EXISTS (SELECT 1 FROM pg_catalog.pg_inherits i WHERE i.inhparent ${EQ} c.oid) AS inherited,
|
|
63
65
|
unkeyed.name AS unkeyed_column, unkeyed.said AS unkeyed_type
|
|
64
66
|
FROM ${WANTED}
|
|
65
67
|
-- A key column whose type is not on the list (keyTypes.ts), its domains unwrapped: no scoped write targets the table.
|
|
@@ -74,7 +76,7 @@ async function facts(client, targets) {
|
|
|
74
76
|
if (typeof row.schema !== 'string' ||
|
|
75
77
|
typeof row.name !== 'string' ||
|
|
76
78
|
typeof row.kind !== 'string' ||
|
|
77
|
-
[row.rls, row.triggers, row.rules, row.policies, row.updatable, row.hierarchy].some((value) => typeof value !== 'boolean') ||
|
|
79
|
+
[row.rls, row.triggers, row.rules, row.policies, row.updatable, row.hierarchy, row.inherited].some((value) => typeof value !== 'boolean') ||
|
|
78
80
|
!Array.isArray(row.trigger_names) ||
|
|
79
81
|
row.trigger_names.some((name) => typeof name !== 'string') ||
|
|
80
82
|
(row.unkeyed_column !== null && typeof row.unkeyed_column !== 'string') ||
|
|
@@ -140,6 +142,64 @@ export async function attestPostgresTriggers(client, catalog, tables) {
|
|
|
140
142
|
return refuse();
|
|
141
143
|
}
|
|
142
144
|
}
|
|
145
|
+
/* The value a hierarchy found again is said in (hierarchyRecheck), between these, so its failure names the table. */
|
|
146
|
+
const JOINED = 'mistri: a scoped write target is in a hierarchy: ';
|
|
147
|
+
const INHERITED = 'mistri: a scoped write references an inheritance parent: ';
|
|
148
|
+
const JOINED_END = ' (end)';
|
|
149
|
+
/**
|
|
150
|
+
* The tables a scoped write wrote, read again for a hierarchy just before COMMIT, as a statement that fails where one
|
|
151
|
+
* is in one: sent with the COMMIT (beforeCommit), its failure answers the COMMIT as a ROLLBACK, so it costs no round
|
|
152
|
+
* trip. The pin's ROW EXCLUSIVE holds a target against becoming a partition or a child (ATTACH PARTITION and ALTER
|
|
153
|
+
* TABLE … INHERIT take ACCESS EXCLUSIVE on it), but not against becoming a parent: ALTER TABLE c INHERIT t and CREATE
|
|
154
|
+
* TABLE c () INHERITS (t) take SHARE UPDATE EXCLUSIVE on t, and every mode that conflicts with that conflicts with an
|
|
155
|
+
* ordinary write's ROW EXCLUSIVE or with itself, which would line up every scoped write of the table behind one
|
|
156
|
+
* another. Under READ COMMITTED a statement after the transaction's last write sees every inheritance committed before
|
|
157
|
+
* that write was planned, and one committed after it no write of the transaction expanded into: so a target that
|
|
158
|
+
* passed the catalog's check (ScopeCatalog.hierarchy) and gained a child while the write ran refuses the transaction.
|
|
159
|
+
* The tables those reference are read in the same statement for the same reason: one that gained an inheritance child
|
|
160
|
+
* while the write ran (CREATE TABLE … INHERITS takes the same SHARE UPDATE EXCLUSIVE) may have let a parent check
|
|
161
|
+
* find the child's visible row where the foreign key, which reads the parent alone, found a hidden one
|
|
162
|
+
* (ScopeCatalog.inheritanceParents). Undefined where nothing was written.
|
|
163
|
+
*/
|
|
164
|
+
export function hierarchyRecheck(ids, referenced = []) {
|
|
165
|
+
const at = [...new Set(ids)].map(located);
|
|
166
|
+
if (!at.length)
|
|
167
|
+
return undefined;
|
|
168
|
+
const parents = [...new Set(referenced)].map(located);
|
|
169
|
+
const said = (prefix) => `('${prefix}' ${CONCAT} n.nspname::pg_catalog.text ${CONCAT} '.' ${CONCAT} c.relname::pg_catalog.text ${CONCAT} '${JOINED_END}')::pg_catalog.int4`;
|
|
170
|
+
return {
|
|
171
|
+
text: `SELECT ${said(JOINED)}
|
|
172
|
+
FROM ${WANTED}
|
|
173
|
+
WHERE c.relkind ${EQ} 'p' OR EXISTS (SELECT FROM pg_catalog.pg_inherits i WHERE i.inhrelid ${EQ} c.oid OR i.inhparent ${EQ} c.oid)${parents.length
|
|
174
|
+
? `
|
|
175
|
+
UNION ALL
|
|
176
|
+
SELECT ${said(INHERITED)}
|
|
177
|
+
FROM ${WANTED.replace('$1::', '$3::').replace('$2::', '$4::')}
|
|
178
|
+
WHERE c.relkind ${EQ} 'r' AND EXISTS (SELECT FROM pg_catalog.pg_inherits i WHERE i.inhparent ${EQ} c.oid)`
|
|
179
|
+
: ''}`,
|
|
180
|
+
values: [
|
|
181
|
+
at.map((table) => table.schema),
|
|
182
|
+
at.map((table) => table.name),
|
|
183
|
+
...(parents.length ? [parents.map((table) => table.schema), parents.map((table) => table.name)] : []),
|
|
184
|
+
],
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
/** The refusal a hierarchyRecheck failure says, or undefined for any other failure. */
|
|
188
|
+
export function joinedHierarchy(error) {
|
|
189
|
+
const { code, message } = (error ?? {});
|
|
190
|
+
if (code !== '22P02' || typeof message !== 'string')
|
|
191
|
+
return undefined;
|
|
192
|
+
const reference = message.includes(INHERITED);
|
|
193
|
+
const prefix = reference ? INHERITED : JOINED;
|
|
194
|
+
const start = message.indexOf(prefix);
|
|
195
|
+
const end = message.indexOf(JOINED_END, start);
|
|
196
|
+
if (start === -1 || end === -1)
|
|
197
|
+
return undefined;
|
|
198
|
+
const table = message.slice(start + prefix.length, end);
|
|
199
|
+
return recordRefusalCause(new ScopeRefusal('This operation is not available in the current row scope.'), reference
|
|
200
|
+
? `"${table}" became an inheritance parent while the write ran, and a reference to it is not checked under a row policy: the foreign key reads its own rows, a parent check its children's too.`
|
|
201
|
+
: `"${table}" became part of a partition or inheritance hierarchy while the write ran, which writes under a row policy do not support yet.`);
|
|
202
|
+
}
|
|
143
203
|
/** The relations of `$1` (schemas) and `$2` (names), as `c` and `n`: the proof's own catalog read, its operators named in pg_catalog (postgresTriggers.ts). */
|
|
144
204
|
const WANTED = `pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid ${EQ} c.relnamespace
|
|
145
205
|
JOIN ROWS FROM (pg_catalog.unnest($1::pg_catalog.text[]), pg_catalog.unnest($2::pg_catalog.text[])) AS wanted(schema, name)
|
|
@@ -379,6 +439,7 @@ export async function readPostgresScopeCatalog(client, scope, describe) {
|
|
|
379
439
|
const blockedBy = new Map();
|
|
380
440
|
const unkeyed = new Map();
|
|
381
441
|
const hierarchy = new Set();
|
|
442
|
+
const inheritanceParents = new Set();
|
|
382
443
|
const locks = new Set();
|
|
383
444
|
const withTriggers = [];
|
|
384
445
|
for (const table of schema.tables) {
|
|
@@ -392,6 +453,8 @@ export async function readPostgresScopeCatalog(client, scope, describe) {
|
|
|
392
453
|
// Never a target, but still lockable: a reference to a partitioned table holds its parent row through it.
|
|
393
454
|
if (fact.hierarchy)
|
|
394
455
|
hierarchy.add(table.id);
|
|
456
|
+
if (fact.inherited)
|
|
457
|
+
inheritanceParents.add(table.id);
|
|
395
458
|
if (!lockable(fact))
|
|
396
459
|
continue;
|
|
397
460
|
locks.add(table.id);
|
|
@@ -442,6 +505,7 @@ export async function readPostgresScopeCatalog(client, scope, describe) {
|
|
|
442
505
|
...(blockedBy.size ? { blockedBy } : {}),
|
|
443
506
|
...(unkeyed.size ? { unkeyed } : {}),
|
|
444
507
|
...(hierarchy.size ? { hierarchy } : {}),
|
|
508
|
+
...(inheritanceParents.size ? { inheritanceParents } : {}),
|
|
445
509
|
...(triggered.size ? { triggered, triggerVersions } : {}),
|
|
446
510
|
lockable: locks,
|
|
447
511
|
pinned: relations,
|
|
@@ -96,6 +96,8 @@ references = false) {
|
|
|
96
96
|
throw new Refusal('Unknown resource in scoped read.');
|
|
97
97
|
return table;
|
|
98
98
|
};
|
|
99
|
+
/* Each column's type, so a comparison names the database's own operator for it (SqlStyle.builtin). */
|
|
100
|
+
const typeIn = (table) => (name) => table.columns.find((column) => column.name === name)?.type;
|
|
99
101
|
const from = (id, filter) => {
|
|
100
102
|
const table = tableFor(id);
|
|
101
103
|
const column = (name) => {
|
|
@@ -106,7 +108,7 @@ references = false) {
|
|
|
106
108
|
const relations = new ScopedRelations(schema, style, scope);
|
|
107
109
|
const relation = relations.table(id);
|
|
108
110
|
const prefix = relations.finish();
|
|
109
|
-
const where = buildWhere(filter, style, prefix.params.length + 1, column, undefined, undefined, undefined, references);
|
|
111
|
+
const where = buildWhere(filter, style, prefix.params.length + 1, column, undefined, undefined, undefined, references, typeIn(table));
|
|
110
112
|
return { table, column, relation, prefix, where, params: [...prefix.params, ...where.params] };
|
|
111
113
|
};
|
|
112
114
|
/* A SQLite sum that cannot be exact says so in its cell (decimals.ts); PostgreSQL's numeric always is. */
|
|
@@ -153,12 +155,12 @@ references = false) {
|
|
|
153
155
|
throw new RequestFault('Unknown column in scoped read.');
|
|
154
156
|
return style.quote(name);
|
|
155
157
|
};
|
|
156
|
-
return { relation: relations.table(item.table), column, filter: item.filter };
|
|
158
|
+
return { relation: relations.table(item.table), column, filter: item.filter, types: typeIn(table) };
|
|
157
159
|
});
|
|
158
160
|
const prefix = relations.finish();
|
|
159
161
|
const params = [...prefix.params];
|
|
160
|
-
const branches = planned.map(({ relation, column, filter }, i) => {
|
|
161
|
-
const where = buildWhere(filter, style, params.length + 1, column);
|
|
162
|
+
const branches = planned.map(({ relation, column, filter, types }, i) => {
|
|
163
|
+
const where = buildWhere(filter, style, params.length + 1, column, undefined, undefined, undefined, false, types);
|
|
162
164
|
params.push(...where.params);
|
|
163
165
|
const counted = countStatement(`${relation}${where.text}`, limit);
|
|
164
166
|
return counted.replace(/^SELECT COUNT\(\*\) AS n/, `SELECT ${i} AS i, COUNT(*) AS n`);
|
|
@@ -33,6 +33,13 @@ export interface ScopeCatalog {
|
|
|
33
33
|
* reference to a partitioned table from an ordinary one is still checked, through the partitioned table.
|
|
34
34
|
*/
|
|
35
35
|
hierarchy?: ReadonlySet<string>;
|
|
36
|
+
/**
|
|
37
|
+
* PostgreSQL: ordinary tables with inheritance children. A reference to one is refused: its foreign key checks the
|
|
38
|
+
* parent's own rows (`ONLY`), while a parent check reads the parent with its children, so a child's visible row
|
|
39
|
+
* could pass for a hidden one of the parent's under the same key. A partitioned table is not here: its key and its
|
|
40
|
+
* check both read every partition.
|
|
41
|
+
*/
|
|
42
|
+
inheritanceParents?: ReadonlySet<string>;
|
|
36
43
|
}
|
|
37
44
|
/** One foreign key's comparison, column by column in its order (ScopeCatalog.references); or the reason it has none. */
|
|
38
45
|
export type ReferenceComparison = {
|
|
@@ -68,6 +75,12 @@ export declare const loadTriggerEffects: () => Promise<typeof import("./triggerE
|
|
|
68
75
|
interface ScopeRowLocks {
|
|
69
76
|
target(table: TableMeta, filter: Filter): Promise<void>;
|
|
70
77
|
parent(table: TableMeta, filter: Filter): Promise<void>;
|
|
78
|
+
/**
|
|
79
|
+
* Told of each table a write of this transaction targets, once it is judged a target, before the write is sent:
|
|
80
|
+
* where a table can join a hierarchy under the lock the write's pin holds (PostgreSQL, ALTER TABLE … INHERIT), what
|
|
81
|
+
* it wrote is read again before COMMIT (postgresScopeCatalog.ts hierarchyRecheck).
|
|
82
|
+
*/
|
|
83
|
+
written?(table: TableMeta): void;
|
|
71
84
|
/**
|
|
72
85
|
* Run `ride` the moment the update of `table` is sent, so the read that
|
|
73
86
|
* proves the row still in scope leaves behind it in the same round trip
|
|
@@ -127,9 +140,15 @@ export interface ScopeBound {
|
|
|
127
140
|
* does, and missed such a reference, which the delete's cascade then reached.
|
|
128
141
|
* Here each parent column is the left side of a join, where both engines take
|
|
129
142
|
* its affinity and collation as the foreign key does; where two collations
|
|
130
|
-
* conflict, PostgreSQL refuses the count, and the delete with it.
|
|
143
|
+
* conflict, PostgreSQL refuses the count, and the delete with it. On
|
|
144
|
+
* PostgreSQL the join compares by the constraint's own operator
|
|
145
|
+
* (`conpfeqop`), each side cast as ri_triggers.c casts it: the comparison the
|
|
146
|
+
* catalog read for the parent check (ScopeCatalog.references,
|
|
147
|
+
* Condition.equality), so a `char(3)` key reaches the `text` reference 'ab '
|
|
148
|
+
* the cascade reaches, and no `=` on the search_path decides what is counted.
|
|
149
|
+
* A key whose comparison the catalog refused refuses the count.
|
|
131
150
|
*/
|
|
132
|
-
export declare function referencesStatement(
|
|
151
|
+
export declare function referencesStatement(catalog: Pick<ScopeCatalog, 'schema' | 'references'>, fk: ForeignKey, key: Filter, style: SqlStyle): SqlFragment;
|
|
133
152
|
/** The tables where `scope` is exactly `bound`, and the bound admits rows at all: nothing in it is hidden there. */
|
|
134
153
|
export declare function wholeWithin(scope: Readonly<Record<string, unknown>>, bound: Readonly<Record<string, unknown>>): ReadonlySet<string>;
|
|
135
154
|
/** A bound names exactly the scope's tables: it bounds that scope, never another. */
|
|
@@ -40,21 +40,44 @@ export const loadTriggerEffects = () => import('./triggerEffects.js');
|
|
|
40
40
|
* does, and missed such a reference, which the delete's cascade then reached.
|
|
41
41
|
* Here each parent column is the left side of a join, where both engines take
|
|
42
42
|
* its affinity and collation as the foreign key does; where two collations
|
|
43
|
-
* conflict, PostgreSQL refuses the count, and the delete with it.
|
|
43
|
+
* conflict, PostgreSQL refuses the count, and the delete with it. On
|
|
44
|
+
* PostgreSQL the join compares by the constraint's own operator
|
|
45
|
+
* (`conpfeqop`), each side cast as ri_triggers.c casts it: the comparison the
|
|
46
|
+
* catalog read for the parent check (ScopeCatalog.references,
|
|
47
|
+
* Condition.equality), so a `char(3)` key reaches the `text` reference 'ab '
|
|
48
|
+
* the cascade reaches, and no `=` on the search_path decides what is counted.
|
|
49
|
+
* A key whose comparison the catalog refused refuses the count.
|
|
44
50
|
*/
|
|
45
|
-
export function referencesStatement(
|
|
46
|
-
const
|
|
51
|
+
export function referencesStatement(catalog, fk, key, style) {
|
|
52
|
+
const { schema } = catalog;
|
|
53
|
+
const tableOf = (id) => {
|
|
47
54
|
const table = schema.tables.find((table) => table.id === id);
|
|
48
55
|
if (!table)
|
|
49
56
|
throw new Refusal('This operation is not available in the current row scope.');
|
|
50
|
-
return
|
|
57
|
+
return table;
|
|
51
58
|
};
|
|
52
|
-
const
|
|
53
|
-
const
|
|
59
|
+
const relation = (table) => `${style.quote(table.schema)}.${style.quote(table.name)}`;
|
|
60
|
+
const compared = catalog.references?.get(referenceKey(fk));
|
|
61
|
+
if (catalog.references && !compared)
|
|
62
|
+
refuse(`"${fk.from.table}" has a foreign key whose comparison was not read with the catalog.`);
|
|
63
|
+
if (compared && 'refused' in compared)
|
|
64
|
+
refuse(compared.refused);
|
|
65
|
+
const columns = compared && 'columns' in compared ? compared.columns : undefined;
|
|
66
|
+
const parent = tableOf(fk.to.table);
|
|
67
|
+
const on = buildWhere({
|
|
68
|
+
groups: [
|
|
69
|
+
fk.to.columns.map((column, index) => {
|
|
70
|
+
const equality = columns?.[index]?.equality;
|
|
71
|
+
return { column, op: '=', other: fk.from.columns[index], ...(equality ? { equality } : {}) };
|
|
72
|
+
}),
|
|
73
|
+
],
|
|
74
|
+
}, style, 1, (name) => `p.${style.quote(name)}`, (name) => `c.${style.quote(name)}`, undefined, undefined, true);
|
|
75
|
+
const typeOf = (name) => parent.columns.find((column) => column.name === name)?.type;
|
|
76
|
+
const where = buildWhere(key, style, 1, (name) => `p.${style.quote(name)}`, undefined, undefined, undefined, false, typeOf);
|
|
54
77
|
if (!where.text)
|
|
55
78
|
throw new Refusal('This operation is not available in the current row scope.');
|
|
56
79
|
return {
|
|
57
|
-
text: `SELECT COUNT(*) AS n FROM ${relation(fk.from.table)} AS c JOIN ${relation(
|
|
80
|
+
text: `SELECT COUNT(*) AS n FROM ${relation(tableOf(fk.from.table))} AS c JOIN ${relation(parent)} AS p ON ${on.text.replace(/^ WHERE /, '')}${where.text}`,
|
|
58
81
|
params: where.params,
|
|
59
82
|
};
|
|
60
83
|
}
|
|
@@ -182,6 +205,7 @@ export function createScopedTransaction(catalog, records, reads, guards, locks,
|
|
|
182
205
|
const unkeyed = catalog.unkeyed?.get(id);
|
|
183
206
|
if (unkeyed)
|
|
184
207
|
return refuse(`${unkeyed}.`);
|
|
208
|
+
locks?.written?.(table);
|
|
185
209
|
return table;
|
|
186
210
|
};
|
|
187
211
|
const keyFilter = (table, key) => {
|
|
@@ -290,6 +314,12 @@ export function createScopedTransaction(catalog, records, reads, guards, locks,
|
|
|
290
314
|
});
|
|
291
315
|
if (values.some((value) => value === null))
|
|
292
316
|
continue;
|
|
317
|
+
if (catalog.inheritanceParents?.has(fk.to.table)) {
|
|
318
|
+
checks.push({
|
|
319
|
+
refused: `"${table.id}" references "${fk.to.table}", an inheritance parent: its foreign key reads the parent's own rows and a parent check its children's too, which writes under a row policy do not support yet.`,
|
|
320
|
+
});
|
|
321
|
+
break;
|
|
322
|
+
}
|
|
293
323
|
if ('refused' in compared) {
|
|
294
324
|
checks.push({ refused: compared.refused });
|
|
295
325
|
break;
|
package/dist/adapters/sqlite.js
CHANGED
|
@@ -1328,7 +1328,7 @@ export class SqliteAdapter {
|
|
|
1328
1328
|
reads: scopedReads(true, bound),
|
|
1329
1329
|
whole: wholeWithin(scope, bound),
|
|
1330
1330
|
references: async (fk, key) => {
|
|
1331
|
-
const statement = referencesStatement(catalog
|
|
1331
|
+
const statement = referencesStatement(catalog, fk, key, ANSI_STYLE);
|
|
1332
1332
|
const [row] = await this.readRows(statement.text, statement.params, true, handle);
|
|
1333
1333
|
return Number(row?.n ?? 0);
|
|
1334
1334
|
},
|
|
@@ -18,28 +18,17 @@ export type Route<C extends RequestContext> = (ctx: C) => boolean | Promise<bool
|
|
|
18
18
|
/** Try `routes` in order; true when one answered. */
|
|
19
19
|
export declare function route<C extends RequestContext>(ctx: C, routes: readonly Route<C>[]): Promise<boolean>;
|
|
20
20
|
/**
|
|
21
|
-
*
|
|
21
|
+
* A dispatcher: a module that takes a request from a socket (or a host's
|
|
22
22
|
* server) and holds it to the door before any route reads it, in the door's
|
|
23
23
|
* order — the Host, the proxy's identity, the product's session, then a
|
|
24
|
-
* write's Origin and body.
|
|
25
|
-
*
|
|
24
|
+
* write's Origin and body. Each product names its own in a table of these
|
|
25
|
+
* (its server/http/dispatchers.ts), so the engine names no product's path;
|
|
26
|
+
* test/door.test.ts stands each up and sends it the same refusals, and a new
|
|
27
|
+
* one is named in its product's table and given a row there.
|
|
26
28
|
*/
|
|
27
|
-
export
|
|
28
|
-
|
|
29
|
-
readonly
|
|
30
|
-
|
|
31
|
-
readonly
|
|
32
|
-
|
|
33
|
-
}, {
|
|
34
|
-
readonly module: 'packages/mistri/src/server/embed/handler.ts';
|
|
35
|
-
readonly serves: 'an App mounted inside somebody else’s server';
|
|
36
|
-
}, {
|
|
37
|
-
readonly module: 'packages/mistri/src/server/http/suite.ts';
|
|
38
|
-
readonly serves: 'several deployments under one origin, each behind its own dispatcher';
|
|
39
|
-
}, {
|
|
40
|
-
readonly module: 'packages/mistri/src/server/tenant/tenantAuthenticatedHttp.ts';
|
|
41
|
-
readonly serves: 'a tenant App’s browser host and its native sign-in';
|
|
42
|
-
}, {
|
|
43
|
-
readonly module: 'packages/mistri/src/server/tenant/tenantHttp.ts';
|
|
44
|
-
readonly serves: 'a tenant App’s data, inside its browser host';
|
|
45
|
-
}];
|
|
29
|
+
export interface Dispatcher {
|
|
30
|
+
/** The module, by its path in the repository. */
|
|
31
|
+
readonly module: string;
|
|
32
|
+
/** What it serves. */
|
|
33
|
+
readonly serves: string;
|
|
34
|
+
}
|
|
@@ -5,18 +5,3 @@ export async function route(ctx, routes) {
|
|
|
5
5
|
return true;
|
|
6
6
|
return false;
|
|
7
7
|
}
|
|
8
|
-
/**
|
|
9
|
-
* Every dispatcher: a module that takes a request from a socket (or a host's
|
|
10
|
-
* server) and holds it to the door before any route reads it, in the door's
|
|
11
|
-
* order — the Host, the proxy's identity, the product's session, then a
|
|
12
|
-
* write's Origin and body. test/door.test.ts stands each up and sends it the
|
|
13
|
-
* same refusals; a new one is named here, and given a row there.
|
|
14
|
-
*/
|
|
15
|
-
export const DISPATCHERS = [
|
|
16
|
-
{ module: 'packages/tablewalk/src/server/http/browserHandler.ts', serves: 'the database browser' },
|
|
17
|
-
{ module: 'packages/mistri/src/server/http/handler.ts', serves: 'the Apps of a deployment that serves them' },
|
|
18
|
-
{ module: 'packages/mistri/src/server/embed/handler.ts', serves: 'an App mounted inside somebody else’s server' },
|
|
19
|
-
{ module: 'packages/mistri/src/server/http/suite.ts', serves: 'several deployments under one origin, each behind its own dispatcher' },
|
|
20
|
-
{ module: 'packages/mistri/src/server/tenant/tenantAuthenticatedHttp.ts', serves: 'a tenant App’s browser host and its native sign-in' },
|
|
21
|
-
{ module: 'packages/mistri/src/server/tenant/tenantHttp.ts', serves: 'a tenant App’s data, inside its browser host' },
|
|
22
|
-
];
|
|
@@ -26,3 +26,5 @@ export declare function addressSortValue(raw: string): {
|
|
|
26
26
|
}[] | undefined;
|
|
27
27
|
/** All time, said where a dashboard opens on a window of its own (`window: 'last 30 days'`): `?period=all-time`. */
|
|
28
28
|
export declare const ALL_TIME = "all-time";
|
|
29
|
+
/** An address's `period=` as it is read: `all` is the word a person guesses, and means all time as `all-time` does. */
|
|
30
|
+
export declare const readPeriod: (word: string) => string;
|
|
@@ -45,3 +45,5 @@ export function addressSortValue(raw) {
|
|
|
45
45
|
}
|
|
46
46
|
/** All time, said where a dashboard opens on a window of its own (`window: 'last 30 days'`): `?period=all-time`. */
|
|
47
47
|
export const ALL_TIME = 'all-time';
|
|
48
|
+
/** An address's `period=` as it is read: `all` is the word a person guesses, and means all time as `all-time` does. */
|
|
49
|
+
export const readPeriod = (word) => (word === 'all' ? ALL_TIME : word);
|
package/dist/shared/breakdown.js
CHANGED
|
@@ -416,12 +416,12 @@ function compileBreakdownFresh(schema, def, style, offset, scope) {
|
|
|
416
416
|
const prefix = relations.finish();
|
|
417
417
|
const whenParams = [];
|
|
418
418
|
for (const { at, name, where: held } of whens) {
|
|
419
|
-
const built = buildWhere(asFilter(held), style, prefix.params.length + whenParams.length + 1, qualify, qualify, membership, exists);
|
|
419
|
+
const built = buildWhere(asFilter(held), style, prefix.params.length + whenParams.length + 1, qualify, qualify, membership, exists, false, paths.typeOf);
|
|
420
420
|
whenParams.push(...built.params);
|
|
421
421
|
/* COUNT, not SUM: a count of no rows is 0, where a sum of none is null. */
|
|
422
422
|
select[at] = `COUNT(CASE WHEN ${built.text.replace(/^ WHERE /, '')} THEN 1 END) AS ${style.quote(name)}`;
|
|
423
423
|
}
|
|
424
|
-
const where = buildWhere(conditions, style, prefix.params.length + whenParams.length + 1, qualify, qualify, membership, exists);
|
|
424
|
+
const where = buildWhere(conditions, style, prefix.params.length + whenParams.length + 1, qualify, qualify, membership, exists, false, paths.typeOf);
|
|
425
425
|
/* Sorted by output name, and only by output name.
|
|
426
426
|
|
|
427
427
|
Every column of a breakdown is either a key or a measure, and both are in
|
package/dist/shared/filter.d.ts
CHANGED
|
@@ -174,6 +174,20 @@ export interface SqlStyle {
|
|
|
174
174
|
jsonKey?(expression: string, key: string, as: 'value' | 'text' | 'number'): string;
|
|
175
175
|
/** Whether a JSON key's list (or its one value) holds any of the bound values: `has` on a many-of-a-list field. */
|
|
176
176
|
jsonHas?(expression: string, key: string, holes: readonly string[]): string;
|
|
177
|
+
/**
|
|
178
|
+
* The built-in type a column of `type` (ColumnMeta.type, the catalog's own word) compares as, where a comparison
|
|
179
|
+
* names its operator by schema: PostgreSQL's `OPERATOR(pg_catalog.=)`, as `name` (what an IN list is read as) and
|
|
180
|
+
* whether a pattern reads it as text. A bare `=` resolves through the search_path, where an exact signature
|
|
181
|
+
* pg_catalog lacks (`varchar = varchar`) wins over pg_catalog's own. Undefined for every other type (an enum, an
|
|
182
|
+
* extension's citext), whose operators are its own and stay as written. Only where `buildWhere` is told the types
|
|
183
|
+
* (`types`): an App's scoped reads.
|
|
184
|
+
*/
|
|
185
|
+
builtin?(type: string): {
|
|
186
|
+
readonly name: string;
|
|
187
|
+
readonly text: boolean;
|
|
188
|
+
} | undefined;
|
|
189
|
+
/** A condition that holds for no row, said with no operator a search_path can shadow where the dialect has one. */
|
|
190
|
+
never?: string;
|
|
177
191
|
}
|
|
178
192
|
/** The periods a breakdown can group dates by: a calendar's (day to year), and the two that recur — the ISO
|
|
179
193
|
weekday, "1" (Monday) to "7", and the hour of the day, "00" to "23" — which a heat map crosses. */
|
|
@@ -234,5 +248,8 @@ membership?: (name: string, holes: readonly string[]) => string,
|
|
|
234
248
|
/** How an `any` condition is written: an EXISTS over the child's rows, binding nothing of its own; absent, one is refused. */
|
|
235
249
|
exists?: (rows: AnyRows) => string,
|
|
236
250
|
/** Whether a condition may compare as a foreign key does (`Condition.equality`): a scoped write's own reads only. */
|
|
237
|
-
references?: boolean
|
|
251
|
+
references?: boolean,
|
|
252
|
+
/** The catalog's type of a column the filter names, where the style names its operators by schema
|
|
253
|
+
* (SqlStyle.builtin): an App's scoped reads. Absent, or undefined for a name, the operator as written. */
|
|
254
|
+
types?: (name: string) => string | undefined): SqlFragment;
|
|
238
255
|
export {};
|
package/dist/shared/filter.js
CHANGED
|
@@ -105,7 +105,38 @@ export const POSTGRES_STYLE = {
|
|
|
105
105
|
? `CASE WHEN jsonb_typeof((${e})::jsonb -> '${key}') = 'number' THEN ((${e})::jsonb ->> '${key}')::numeric END`
|
|
106
106
|
: `((${e})::jsonb ->> '${key}')`,
|
|
107
107
|
jsonHas: (e, key, holes) => `(((${e})::jsonb -> '${key}') ?| ARRAY[${holes.join(', ')}]::text[])`,
|
|
108
|
+
/* information_schema's data_type, which says a domain by its base type: a varchar is read as text, as pg_catalog
|
|
109
|
+
compares it, and a char as bpchar, which keeps its trailing blanks insignificant without char(1)'s truncation. */
|
|
110
|
+
builtin: (type) => {
|
|
111
|
+
const name = POSTGRES_BUILTIN[type];
|
|
112
|
+
return name === undefined ? undefined : { name, text: name === 'text' || name === 'bpchar' };
|
|
113
|
+
},
|
|
114
|
+
never: 'FALSE',
|
|
108
115
|
};
|
|
116
|
+
/* The types a comparison names pg_catalog's operator for, by information_schema's word for them, and the name an IN
|
|
117
|
+
list is cast to. Absent: USER-DEFINED (an enum, citext, an extension's type) and ARRAY. */
|
|
118
|
+
const POSTGRES_BUILTIN = Object.assign(Object.create(null), {
|
|
119
|
+
text: 'text',
|
|
120
|
+
'character varying': 'text',
|
|
121
|
+
character: 'bpchar',
|
|
122
|
+
smallint: 'int2',
|
|
123
|
+
integer: 'int4',
|
|
124
|
+
bigint: 'int8',
|
|
125
|
+
numeric: 'numeric',
|
|
126
|
+
real: 'float4',
|
|
127
|
+
'double precision': 'float8',
|
|
128
|
+
boolean: 'bool',
|
|
129
|
+
uuid: 'uuid',
|
|
130
|
+
date: 'date',
|
|
131
|
+
'timestamp without time zone': 'timestamp',
|
|
132
|
+
'timestamp with time zone': 'timestamptz',
|
|
133
|
+
'time without time zone': 'time',
|
|
134
|
+
'time with time zone': 'timetz',
|
|
135
|
+
interval: 'interval',
|
|
136
|
+
bytea: 'bytea',
|
|
137
|
+
jsonb: 'jsonb',
|
|
138
|
+
inet: 'inet',
|
|
139
|
+
});
|
|
109
140
|
/**
|
|
110
141
|
* Postgres 12 and later: row-scope CTEs stay inlined however often a read
|
|
111
142
|
* joins their table. The adapter picks it once it knows the server's version
|
|
@@ -181,12 +212,27 @@ membership,
|
|
|
181
212
|
/** How an `any` condition is written: an EXISTS over the child's rows, binding nothing of its own; absent, one is refused. */
|
|
182
213
|
exists,
|
|
183
214
|
/** Whether a condition may compare as a foreign key does (`Condition.equality`): a scoped write's own reads only. */
|
|
184
|
-
references = false
|
|
215
|
+
references = false,
|
|
216
|
+
/** The catalog's type of a column the filter names, where the style names its operators by schema
|
|
217
|
+
* (SqlStyle.builtin): an App's scoped reads. Absent, or undefined for a name, the operator as written. */
|
|
218
|
+
types) {
|
|
185
219
|
if (!filter?.groups.length)
|
|
186
220
|
return { text: '', params: [] };
|
|
187
221
|
const params = [];
|
|
188
222
|
let index = startIndex;
|
|
189
223
|
const next = () => style.placeholder(index++);
|
|
224
|
+
const never = style.never ?? '1 = 0';
|
|
225
|
+
/* PostgreSQL's own operator for a built-in type, named so: a schema author's `=` on the search_path cannot decide
|
|
226
|
+
which rows a policy or a filter admits (SqlStyle.builtin). */
|
|
227
|
+
const builtin = (name) => {
|
|
228
|
+
const type = types?.(name);
|
|
229
|
+
return type === undefined ? undefined : style.builtin?.(type);
|
|
230
|
+
};
|
|
231
|
+
const operator = (op) => `OPERATOR(pg_catalog.${op === '!=' ? '<>' : op})`;
|
|
232
|
+
/* ILIKE … ESCAPE is `~~*` of like_escape, as the parser rewrites it; spelled so, the operator can be named. */
|
|
233
|
+
const pattern = (col, name) => style.likeOp === 'ILIKE' && builtin(name)?.text
|
|
234
|
+
? `${col} ${operator('~~*')} pg_catalog.like_escape(${next()}, '${LIKE_ESCAPE}')`
|
|
235
|
+
: `${col} ${style.likeOp} ${next()} ESCAPE '${LIKE_ESCAPE}'`;
|
|
190
236
|
/* A condition every group holds is said once, before the OR: the same
|
|
191
237
|
rows (AND distributes over OR in SQL's three-valued logic too), and the
|
|
192
238
|
shape an engine can read a range from. A keyset page's bound is in every
|
|
@@ -219,7 +265,7 @@ references = false) {
|
|
|
219
265
|
throw new RequestFault("This read cannot ask a record's tags.");
|
|
220
266
|
const labels = (Array.isArray(c.value) ? c.value : [c.value]).filter((v) => typeof v === 'string' || typeof v === 'number');
|
|
221
267
|
if (!labels.length)
|
|
222
|
-
return
|
|
268
|
+
return never;
|
|
223
269
|
const holes = labels.map((v) => {
|
|
224
270
|
params.push(String(v));
|
|
225
271
|
return next();
|
|
@@ -231,11 +277,14 @@ references = false) {
|
|
|
231
277
|
if (!references ||
|
|
232
278
|
c.op !== '=' ||
|
|
233
279
|
c.collation !== undefined ||
|
|
234
|
-
c.other !== undefined ||
|
|
280
|
+
(c.other !== undefined && (!other || typeof c.other !== 'string')) ||
|
|
235
281
|
typeof operator !== 'string' ||
|
|
236
282
|
!SQL_OPERATOR.test(operator) ||
|
|
237
283
|
![type, left, right].every((part, at) => (at > 0 && part === undefined) || (typeof part === 'string' && SQL_TYPE.test(part))))
|
|
238
284
|
throw new RequestFault('This read cannot compare as a foreign key does.');
|
|
285
|
+
/* A reference column on the right, as ri_triggers.c joins a key to its references: of its own type already. */
|
|
286
|
+
if (c.other !== undefined)
|
|
287
|
+
return `${column(c.column, c)}${left ? `::${left}` : ''} OPERATOR(${operator}) ${other(c.other)}${right ? `::${right}` : ''}`;
|
|
239
288
|
params.push(c.value ?? null);
|
|
240
289
|
return `${column(c.column, c)}${left ? `::${left}` : ''} OPERATOR(${operator}) ${next()}::${type}${right ? `::${right}` : ''}`;
|
|
241
290
|
}
|
|
@@ -244,7 +293,8 @@ references = false) {
|
|
|
244
293
|
if (!other || typeof c.other !== 'string' || !COMPARISONS.has(c.op)) {
|
|
245
294
|
throw new RequestFault('This read cannot compare two columns.');
|
|
246
295
|
}
|
|
247
|
-
|
|
296
|
+
const op = builtin(c.column) && builtin(c.other) ? operator(c.op) : c.op;
|
|
297
|
+
return `${col} ${op} ${collate(other(c.other), c.collation, style)}`;
|
|
248
298
|
}
|
|
249
299
|
switch (c.op) {
|
|
250
300
|
case 'isNull':
|
|
@@ -254,11 +304,15 @@ references = false) {
|
|
|
254
304
|
case 'in': {
|
|
255
305
|
const list = Array.isArray(c.value) ? c.value : [c.value];
|
|
256
306
|
if (!list.length)
|
|
257
|
-
return
|
|
307
|
+
return never; // an empty IN matches nothing
|
|
258
308
|
const holes = list.map((v) => {
|
|
259
309
|
params.push(v ?? null);
|
|
260
310
|
return next();
|
|
261
311
|
});
|
|
312
|
+
/* IN has no operator to name: the same rows as `= ANY` of the list read as the column's type. */
|
|
313
|
+
const typed = builtin(c.column);
|
|
314
|
+
if (typed)
|
|
315
|
+
return `${col} ${operator('=')} ANY (ARRAY[${holes.join(', ')}]::pg_catalog.${typed.name}[])`;
|
|
262
316
|
return `${col} IN (${holes.join(', ')})`;
|
|
263
317
|
}
|
|
264
318
|
/* All three go through one pattern builder, which escapes the
|
|
@@ -268,7 +322,7 @@ references = false) {
|
|
|
268
322
|
case 'startsWith':
|
|
269
323
|
case 'endsWith':
|
|
270
324
|
params.push(likePattern(c.value, c.op));
|
|
271
|
-
return
|
|
325
|
+
return pattern(col, c.column);
|
|
272
326
|
/* Bound, not escaped: the wildcards belong to whoever typed them.
|
|
273
327
|
Still a parameter — the value never becomes SQL text — so the
|
|
274
328
|
safety argument is unchanged, and still `ESCAPE '#'`, so the
|
|
@@ -276,7 +330,7 @@ references = false) {
|
|
|
276
330
|
internally. */
|
|
277
331
|
case 'like':
|
|
278
332
|
params.push(String(c.value ?? ''));
|
|
279
|
-
return
|
|
333
|
+
return pattern(col, c.column);
|
|
280
334
|
default: {
|
|
281
335
|
/* The one field in a structured request that becomes SQL text
|
|
282
336
|
rather than a bound parameter — and until now it was checked
|
|
@@ -292,7 +346,7 @@ references = false) {
|
|
|
292
346
|
throw new RequestFault(`"${String(c.op)}" is not an operator this tool knows.`);
|
|
293
347
|
}
|
|
294
348
|
params.push(c.value ?? null);
|
|
295
|
-
return `${col} ${c.op} ${next()}`;
|
|
349
|
+
return `${col} ${builtin(c.column) ? operator(c.op) : c.op} ${next()}`;
|
|
296
350
|
}
|
|
297
351
|
}
|
|
298
352
|
})
|
|
@@ -23,6 +23,8 @@ export interface AppListTab {
|
|
|
23
23
|
slug?: string;
|
|
24
24
|
/** The resource's segment the tab names (appSegments.ts): its `query` is that word, written out where it is read. */
|
|
25
25
|
segment?: string;
|
|
26
|
+
/** What the tab says while nothing is under it and nothing narrows it past its own condition. */
|
|
27
|
+
empty?: AppListEmpty;
|
|
26
28
|
}
|
|
27
29
|
/**
|
|
28
30
|
* Data only: the renderer supplies the list and detail nodes. One of the
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type DatabaseSchema } from './schema.js';
|
|
2
2
|
import type { ServedResource } from './servedApp.js';
|
|
3
|
-
import type { AppListPresentation } from './listPresentations.js';
|
|
3
|
+
import type { AppListEmpty, AppListPresentation } from './listPresentations.js';
|
|
4
4
|
/** What a list's question is read over: the schema, and the App's resources — their record labels and display rules. */
|
|
5
5
|
export interface ListRules {
|
|
6
6
|
readonly schema: DatabaseSchema;
|
|
@@ -12,6 +12,8 @@ export interface ListTab {
|
|
|
12
12
|
query?: string;
|
|
13
13
|
/** Its word in an address where the label is said in another language (addressParams.ts `tabKey`). */
|
|
14
14
|
slug?: string;
|
|
15
|
+
/** What it says while it holds no row and nothing narrows it past its own condition. */
|
|
16
|
+
empty?: AppListEmpty;
|
|
15
17
|
}
|
|
16
18
|
/** What of a list view its question is made from. */
|
|
17
19
|
export interface ListQuestionSpec {
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* A major amount — a number, or decimal text with a point ("19.99", "-5",
|
|
3
|
-
* ".5") — as the whole number of minor units it is,
|
|
4
|
-
* no amount or says more decimals than the
|
|
5
|
-
* typo, not a value to round.
|
|
3
|
+
* ".5") — as the whole number of minor units it is, written out in digits
|
|
4
|
+
* ("1999"), or undefined when it is no amount or says more decimals than the
|
|
5
|
+
* currency has: 19.999 dollars is a typo, not a value to round. Text to text,
|
|
6
|
+
* never through a float, so an amount past 2^53 stays exact.
|
|
6
7
|
*/
|
|
8
|
+
export declare function majorToMinorText(major: number | string, currency: string): string | undefined;
|
|
9
|
+
/** The same amount as a number of minor units, or undefined past 2^53 too (use majorToMinorText there). */
|
|
7
10
|
export declare function majorToMinor(major: number | string, currency: string): number | undefined;
|
|
@@ -7,11 +7,12 @@
|
|
|
7
7
|
import { currencyDigits } from './money.js';
|
|
8
8
|
/**
|
|
9
9
|
* A major amount — a number, or decimal text with a point ("19.99", "-5",
|
|
10
|
-
* ".5") — as the whole number of minor units it is,
|
|
11
|
-
* no amount or says more decimals than the
|
|
12
|
-
* typo, not a value to round.
|
|
10
|
+
* ".5") — as the whole number of minor units it is, written out in digits
|
|
11
|
+
* ("1999"), or undefined when it is no amount or says more decimals than the
|
|
12
|
+
* currency has: 19.999 dollars is a typo, not a value to round. Text to text,
|
|
13
|
+
* never through a float, so an amount past 2^53 stays exact.
|
|
13
14
|
*/
|
|
14
|
-
export function
|
|
15
|
+
export function majorToMinorText(major, currency) {
|
|
15
16
|
const text = typeof major === 'number' ? (Number.isFinite(major) ? String(major) : '') : major.trim();
|
|
16
17
|
const m = /^([-+]?)(\d*)(?:\.(\d*))?$/.exec(text);
|
|
17
18
|
if (!m || (!m[2] && !m[3]))
|
|
@@ -20,8 +21,14 @@ export function majorToMinor(major, currency) {
|
|
|
20
21
|
const fraction = (m[3] ?? '').replace(/0+$/, '');
|
|
21
22
|
if (fraction.length > digits)
|
|
22
23
|
return undefined;
|
|
23
|
-
const
|
|
24
|
-
|
|
24
|
+
const whole = `${m[2]}${fraction.padEnd(digits, '0')}`.replace(/^0+(?=\d)/, '') || '0';
|
|
25
|
+
return m[1] === '-' && whole !== '0' ? `-${whole}` : whole;
|
|
26
|
+
}
|
|
27
|
+
/** The same amount as a number of minor units, or undefined past 2^53 too (use majorToMinorText there). */
|
|
28
|
+
export function majorToMinor(major, currency) {
|
|
29
|
+
const text = majorToMinorText(major, currency);
|
|
30
|
+
if (text === undefined)
|
|
25
31
|
return undefined;
|
|
26
|
-
|
|
32
|
+
const n = Number(text);
|
|
33
|
+
return Number.isSafeInteger(n) ? n : undefined;
|
|
27
34
|
}
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
*/
|
|
30
30
|
import { type ColumnMeta, type DatabaseSchema, type ForeignKey, type TableMeta } from './schema.js';
|
|
31
31
|
/** A block of the base record's own fields, or of a to-one reference's. */
|
|
32
|
-
interface FieldSection {
|
|
32
|
+
export interface FieldSection {
|
|
33
33
|
kind: 'fields';
|
|
34
34
|
/** Shown above the block. Absent means no heading. */
|
|
35
35
|
title?: string;
|
package/dist/shared/rowScope.js
CHANGED
|
@@ -9,7 +9,7 @@ import { Refusal } from './refusal.js';
|
|
|
9
9
|
import { findTable } from './schema.js';
|
|
10
10
|
import { isFixedScopeCatalog } from './scopeCatalog.js';
|
|
11
11
|
import { qualifiedTable } from './sqlRelation.js';
|
|
12
|
-
import { tryResolvePath } from './viewPaths.js';
|
|
12
|
+
import { plainType, tryResolvePath } from './viewPaths.js';
|
|
13
13
|
/** Composition must budget expanded data against the same capture limits. */
|
|
14
14
|
export const ROW_SCOPE_LIMITS = Object.freeze({
|
|
15
15
|
resources: 256,
|
|
@@ -291,8 +291,8 @@ export class ScopedRelations {
|
|
|
291
291
|
this.names.set(id, name);
|
|
292
292
|
const policy = this.scope[id] ?? false;
|
|
293
293
|
const where = typeof policy === 'boolean'
|
|
294
|
-
? { text: policy ? '' :
|
|
295
|
-
: buildWhere(withoutRepeats(policy), this.style, this.params.length + 1, (column) => this.style.quote(column) + (policy.binary?.includes(column) ? ' COLLATE BINARY' : ''));
|
|
294
|
+
? { text: policy ? '' : ` WHERE ${this.style.never ?? '1 = 0'}`, params: [] }
|
|
295
|
+
: buildWhere(withoutRepeats(policy), this.style, this.params.length + 1, (column) => this.style.quote(column) + (policy.binary?.includes(column) ? ' COLLATE BINARY' : ''), undefined, undefined, undefined, false, (column) => table.columns.find((c) => c.name === column)?.type);
|
|
296
296
|
const columns = table.columns.map((column) => this.style.quote(column.name)).join(', ');
|
|
297
297
|
// The style's hint (cteHint) goes between AS and the body: Postgres inlines the CTE, so the
|
|
298
298
|
// scope's predicate meets the read's own filter, sort and limit instead of a copied slice.
|
|
@@ -363,7 +363,15 @@ export class ScopedRelations {
|
|
|
363
363
|
` WHERE ${first.on})`);
|
|
364
364
|
}
|
|
365
365
|
const local = (column) => paths.get(column) ?? own(column);
|
|
366
|
-
|
|
366
|
+
// A column's type, or a path's where it lands on a column: a computed one is an expression, compared as written.
|
|
367
|
+
const typeOf = (column) => {
|
|
368
|
+
if (written && Object.hasOwn(written, column))
|
|
369
|
+
return undefined;
|
|
370
|
+
if (!column.includes('.'))
|
|
371
|
+
return table.columns.find((c) => c.name === column)?.type;
|
|
372
|
+
return plainType(this.schema, id, column);
|
|
373
|
+
};
|
|
374
|
+
const where = buildWhere(filter, this.style, this.params.length + 1, local, local, undefined, undefined, false, typeOf);
|
|
367
375
|
const columns = table.columns.map((column) => q(column.name)).join(', ');
|
|
368
376
|
this.definitions.push(`${q(name)} AS (SELECT ${columns} FROM ${source}${row ? ` ${q(row)}` : ''}${where.text})`);
|
|
369
377
|
this.params.push(...where.params);
|
|
@@ -6,8 +6,13 @@
|
|
|
6
6
|
* committed CLAUDE.md schema section costs one command. Pure on purpose, the
|
|
7
7
|
* way shape.js is pure — a schema in, text out, importable from anywhere.
|
|
8
8
|
*/
|
|
9
|
-
/**
|
|
10
|
-
|
|
9
|
+
/**
|
|
10
|
+
* The tables to export, and the keys wholly between them.
|
|
11
|
+
*
|
|
12
|
+
* @param {any} schema
|
|
13
|
+
* @param {ReadonlySet<string> | null} [only] the tables to export, by id; every table when absent or empty
|
|
14
|
+
*/
|
|
15
|
+
export declare function selection(schema: any, only?: ReadonlySet<string> | null): {
|
|
11
16
|
tables: any;
|
|
12
17
|
keys: any;
|
|
13
18
|
};
|
|
@@ -24,15 +29,26 @@ export declare function selection(schema: any, only: any): {
|
|
|
24
29
|
* `app_invoice`. Said in a comment at the top of the output rather than
|
|
25
30
|
* silently, because someone comparing the diagram to their database will
|
|
26
31
|
* notice.
|
|
32
|
+
*
|
|
33
|
+
* @param {any} schema
|
|
34
|
+
* @param {ReadonlySet<string> | null} [only] the tables to export, by id; every table when absent or empty
|
|
27
35
|
*/
|
|
28
|
-
export declare function toMermaid(schema: any, only
|
|
29
|
-
/**
|
|
30
|
-
|
|
36
|
+
export declare function toMermaid(schema: any, only?: ReadonlySet<string> | null): string;
|
|
37
|
+
/**
|
|
38
|
+
* A readable reference document, of the kind that belongs in a repository.
|
|
39
|
+
*
|
|
40
|
+
* @param {any} schema
|
|
41
|
+
* @param {ReadonlySet<string> | null} [only] the tables to export, by id; every table when absent or empty
|
|
42
|
+
*/
|
|
43
|
+
export declare function toMarkdown(schema: any, only?: ReadonlySet<string> | null): string;
|
|
31
44
|
/**
|
|
32
45
|
* The structured form.
|
|
33
46
|
*
|
|
34
47
|
* Deliberately the shape tablewalk itself uses rather than a format invented
|
|
35
48
|
* for export — anything that reads it is reading the same model the tool
|
|
36
49
|
* reasons about, and there is no second definition to keep in step.
|
|
50
|
+
*
|
|
51
|
+
* @param {any} schema
|
|
52
|
+
* @param {ReadonlySet<string> | null} [only] the tables to export, by id; every table when absent or empty
|
|
37
53
|
*/
|
|
38
|
-
export declare function toSchemaJson(schema: any, only
|
|
54
|
+
export declare function toSchemaJson(schema: any, only?: ReadonlySet<string> | null): string;
|
|
@@ -6,7 +6,12 @@
|
|
|
6
6
|
* committed CLAUDE.md schema section costs one command. Pure on purpose, the
|
|
7
7
|
* way shape.js is pure — a schema in, text out, importable from anywhere.
|
|
8
8
|
*/
|
|
9
|
-
/**
|
|
9
|
+
/**
|
|
10
|
+
* The tables to export, and the keys wholly between them.
|
|
11
|
+
*
|
|
12
|
+
* @param {any} schema
|
|
13
|
+
* @param {ReadonlySet<string> | null} [only] the tables to export, by id; every table when absent or empty
|
|
14
|
+
*/
|
|
10
15
|
export function selection(schema, only) {
|
|
11
16
|
const wanted = only?.size ? schema.tables.filter((t) => only.has(t.id)) : schema.tables;
|
|
12
17
|
const ids = new Set(wanted.map((t) => t.id));
|
|
@@ -30,6 +35,9 @@ export function selection(schema, only) {
|
|
|
30
35
|
* `app_invoice`. Said in a comment at the top of the output rather than
|
|
31
36
|
* silently, because someone comparing the diagram to their database will
|
|
32
37
|
* notice.
|
|
38
|
+
*
|
|
39
|
+
* @param {any} schema
|
|
40
|
+
* @param {ReadonlySet<string> | null} [only] the tables to export, by id; every table when absent or empty
|
|
33
41
|
*/
|
|
34
42
|
export function toMermaid(schema, only) {
|
|
35
43
|
const { tables, keys } = selection(schema, only);
|
|
@@ -51,7 +59,12 @@ export function toMermaid(schema, only) {
|
|
|
51
59
|
const renamed = tables.some((t) => safe(t.id) !== t.id);
|
|
52
60
|
return ((renamed ? '%% Names containing a dot are written with an underscore: Mermaid ids cannot hold one.\n' : '') + lines.join('\n') + '\n');
|
|
53
61
|
}
|
|
54
|
-
/**
|
|
62
|
+
/**
|
|
63
|
+
* A readable reference document, of the kind that belongs in a repository.
|
|
64
|
+
*
|
|
65
|
+
* @param {any} schema
|
|
66
|
+
* @param {ReadonlySet<string> | null} [only] the tables to export, by id; every table when absent or empty
|
|
67
|
+
*/
|
|
55
68
|
export function toMarkdown(schema, only) {
|
|
56
69
|
const { tables, keys } = selection(schema, only);
|
|
57
70
|
const out = [`# ${schema.label}`, ''];
|
|
@@ -86,6 +99,9 @@ export function toMarkdown(schema, only) {
|
|
|
86
99
|
* Deliberately the shape tablewalk itself uses rather than a format invented
|
|
87
100
|
* for export — anything that reads it is reading the same model the tool
|
|
88
101
|
* reasons about, and there is no second definition to keep in step.
|
|
102
|
+
*
|
|
103
|
+
* @param {any} schema
|
|
104
|
+
* @param {ReadonlySet<string> | null} [only] the tables to export, by id; every table when absent or empty
|
|
89
105
|
*/
|
|
90
106
|
export function toSchemaJson(schema, only) {
|
|
91
107
|
const { tables, keys } = selection(schema, only);
|
|
@@ -486,8 +486,12 @@ export interface AppShell {
|
|
|
486
486
|
signed-out visitor does not, having nothing of their own to search; `{ signedOut: true }` offers it to a
|
|
487
487
|
visitor too, for an App whose public pages are worth searching. */
|
|
488
488
|
search?: 'members' | 'everyone' | false;
|
|
489
|
-
/**
|
|
490
|
-
|
|
489
|
+
/** The crumb bar: `true` everywhere with the home mark, `false` never; by default (`{ top: false, home: false }`)
|
|
490
|
+
not above a room reached straight from the menu, and without the home mark (Crumbs.tsx). */
|
|
491
|
+
breadcrumbs?: boolean | {
|
|
492
|
+
top?: boolean;
|
|
493
|
+
home?: boolean;
|
|
494
|
+
};
|
|
491
495
|
/** false keeps a room's heading for a screen reader only. Default true. */
|
|
492
496
|
heading?: boolean;
|
|
493
497
|
width?: 'full' | 'wide' | 'narrow';
|
|
@@ -644,6 +648,8 @@ export interface ServedApp<View extends object = ServedDashboardView | ServedFor
|
|
|
644
648
|
} & object)> {
|
|
645
649
|
id: string;
|
|
646
650
|
name: string;
|
|
651
|
+
/** What a phone's bar says where `name` does not fit (the App's `shortName`); `name` stays its accessible name. */
|
|
652
|
+
shortName?: string;
|
|
647
653
|
views: Record<string, View>;
|
|
648
654
|
nav: AppPayloadNavNode[];
|
|
649
655
|
home?: string;
|
package/dist/shared/view.js
CHANGED
|
@@ -199,7 +199,7 @@ export function compileView(schema, view, style, offset = 0, scope) {
|
|
|
199
199
|
const relations = new ScopedRelations(schema, style, scope);
|
|
200
200
|
const baseRelation = relations.table(base.id);
|
|
201
201
|
const plan = new JoinPlan(schema, base, style, relations.table);
|
|
202
|
-
const { resolve, qualify, membership, exists } = viewPaths(schema, view, plan, style, relations);
|
|
202
|
+
const { resolve, qualify, membership, exists, typeOf } = viewPaths(schema, view, plan, style, relations);
|
|
203
203
|
/* An empty column list means the base table's own columns. Expanding it
|
|
204
204
|
here rather than emitting `SELECT *` keeps the result's column list
|
|
205
205
|
knowable without running the statement, which is what the composer's
|
|
@@ -350,7 +350,7 @@ export function compileView(schema, view, style, offset = 0, scope) {
|
|
|
350
350
|
/* The other side of a column comparison is a path like any other: resolved
|
|
351
351
|
against this catalog, so a reader's pruned graph refuses one it may not
|
|
352
352
|
read exactly as it refuses the left. */
|
|
353
|
-
const where = buildWhere(conditions, style, prefix.params.length + 1, qualify, qualify, membership, exists);
|
|
353
|
+
const where = buildWhere(conditions, style, prefix.params.length + 1, qualify, qualify, membership, exists, false, typeOf);
|
|
354
354
|
/* Placeholder numbering continues across the whole statement, which is the
|
|
355
355
|
one thing Postgres needs and SQLite does not — and the exact place a join
|
|
356
356
|
builder gets it wrong, because the joins sit between the SELECT list and
|
|
@@ -737,7 +737,7 @@ export function compileViewCount(schema, view, style, scope, limit) {
|
|
|
737
737
|
const relations = new ScopedRelations(schema, style, scope);
|
|
738
738
|
const baseRelation = relations.table(base.id);
|
|
739
739
|
const plan = new JoinPlan(schema, base, style, relations.table);
|
|
740
|
-
const { qualify, membership, exists } = viewPaths(schema, view, plan, style, relations);
|
|
740
|
+
const { qualify, membership, exists, typeOf } = viewPaths(schema, view, plan, style, relations);
|
|
741
741
|
const conditions = viewConditions(view);
|
|
742
742
|
for (const group of conditions?.groups ?? []) {
|
|
743
743
|
for (const condition of group) {
|
|
@@ -752,7 +752,7 @@ export function compileViewCount(schema, view, style, scope, limit) {
|
|
|
752
752
|
}
|
|
753
753
|
const joins = plan.clauses();
|
|
754
754
|
const prefix = relations.finish();
|
|
755
|
-
const where = buildWhere(conditions, style, prefix.params.length + 1, qualify, qualify, membership, exists);
|
|
755
|
+
const where = buildWhere(conditions, style, prefix.params.length + 1, qualify, qualify, membership, exists, false, typeOf);
|
|
756
756
|
/* Only filter joins affect row count. Projection-only joins and scalar
|
|
757
757
|
aggregates deliberately do not register relations in this statement. */
|
|
758
758
|
const relation = `${baseRelation} ${style.quote(plan.baseAlias)}` + (joins.length ? `\n${joins.join('\n')}` : '') + where.text;
|
|
@@ -28,6 +28,7 @@ export declare function viewPaths(schema: DatabaseSchema, view: ViewDef, plan: J
|
|
|
28
28
|
qualify: (path: string, condition?: Condition) => string;
|
|
29
29
|
membership: (path: string, holes: readonly string[]) => string;
|
|
30
30
|
exists: (rows: AnyRows) => string;
|
|
31
|
+
typeOf: (path: string) => string | undefined;
|
|
31
32
|
};
|
|
32
33
|
/**
|
|
33
34
|
* The view with the record-label templates its `$label` paths need, read from
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { findTable } from './schema.js';
|
|
2
|
-
import { PathError, backrefsTo, labelColumnMeta, resolvePath, tableAtPrefix, tryResolvePath, } from './viewPaths.js';
|
|
2
|
+
import { PathError, backrefsTo, labelColumnMeta, plainType, resolvePath, tableAtPrefix, tryResolvePath, } from './viewPaths.js';
|
|
3
3
|
import { LABEL_SEGMENT, labelPrefix, parseLabelTemplate } from './recordLabel.js';
|
|
4
4
|
import { MASK } from './model.js';
|
|
5
5
|
import { rollupColumnMeta } from './rollups.js';
|
|
@@ -374,7 +374,13 @@ export function viewPaths(schema, view, plan, style, relations) {
|
|
|
374
374
|
return `COALESCE(${value}, '')`;
|
|
375
375
|
})));
|
|
376
376
|
};
|
|
377
|
-
|
|
377
|
+
/* The catalog's type of what a path reads where it reads one column as it is stored, as `qualify` writes it plain:
|
|
378
|
+
what a comparison names the database's own operator by (SqlStyle.builtin). A label, a JSON key, an expression,
|
|
379
|
+
a rollup or a record's tags is an expression and is compared as written. */
|
|
380
|
+
const typeOf = (path) => labelPrefix(path) !== undefined || jsonKeyAt(path) || expressionAt(path) || computedAt(path) || tagsAt(path)
|
|
381
|
+
? undefined
|
|
382
|
+
: plainType(schema, view.base, path);
|
|
383
|
+
return { resolve, qualify, membership, exists, typeOf };
|
|
378
384
|
}
|
|
379
385
|
/* What a stored column is to an expression that reads it: a yes-or-no column native or a 0/1 integer. */
|
|
380
386
|
function columnRead(column) {
|
|
@@ -206,6 +206,9 @@ export interface ResolvedPath {
|
|
|
206
206
|
key: string;
|
|
207
207
|
};
|
|
208
208
|
}
|
|
209
|
+
/** The type of the column a path lands on, where it is that column itself: undefined for a rollup, a computed column,
|
|
210
|
+
* a record's tags or a JSON key, which are expressions, and for a path that does not resolve. */
|
|
211
|
+
export declare function plainType(schema: DatabaseSchema, base: string, path: string): string | undefined;
|
|
209
212
|
/**
|
|
210
213
|
* Resolve a path against the schema, or explain precisely why it does not.
|
|
211
214
|
*
|
package/dist/shared/viewPaths.js
CHANGED
|
@@ -72,6 +72,14 @@ function latestReference(table, name) {
|
|
|
72
72
|
const rollup = table.rollups && Object.hasOwn(table.rollups, name) ? table.rollups[name] : undefined;
|
|
73
73
|
return rollup?.fn === 'latest' && rollup.references && !rollup.of ? rollup : undefined;
|
|
74
74
|
}
|
|
75
|
+
/** The type of the column a path lands on, where it is that column itself: undefined for a rollup, a computed column,
|
|
76
|
+
* a record's tags or a JSON key, which are expressions, and for a path that does not resolve. */
|
|
77
|
+
export function plainType(schema, base, path) {
|
|
78
|
+
const { resolved } = tryResolvePath(schema, base, path);
|
|
79
|
+
if (!resolved || resolved.rollup || resolved.computed || resolved.tags || resolved.jsonKey)
|
|
80
|
+
return undefined;
|
|
81
|
+
return resolved.column.type;
|
|
82
|
+
}
|
|
75
83
|
/**
|
|
76
84
|
* Resolve a path against the schema, or explain precisely why it does not.
|
|
77
85
|
*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mistriwork/engine",
|
|
3
|
-
"version": "1.0.0-rc.
|
|
3
|
+
"version": "1.0.0-rc.12",
|
|
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": {
|
|
@@ -199,7 +199,6 @@
|
|
|
199
199
|
"./shared/moneyEntry.js": "./dist/shared/moneyEntry.js",
|
|
200
200
|
"./shared/mount.js": "./dist/shared/mount.js",
|
|
201
201
|
"./shared/niceTicks.js": "./dist/shared/niceTicks.js",
|
|
202
|
-
"./shared/order.js": "./dist/shared/order.js",
|
|
203
202
|
"./shared/page.js": "./dist/shared/page.js",
|
|
204
203
|
"./shared/pageAnatomy.js": "./dist/shared/pageAnatomy.js",
|
|
205
204
|
"./shared/pageConfig.js": "./dist/shared/pageConfig.js",
|
package/schema/model.d.ts
CHANGED
|
@@ -157,7 +157,7 @@ export type AppCommandField = (CommandValueBase & (
|
|
|
157
157
|
| { type: 'richText'; minLength?: number; maxLength?: number; mentions?: CommandRichTextMentions; images?: string }
|
|
158
158
|
| { type: 'number'; min?: number; max?: number; integer?: boolean; step?: number; below?: string; above?: string; live?: CommandLiveLines }
|
|
159
159
|
/** An amount of money in the App's currency (`ui.currency`), from 0 unless `min` says less, on cents unless `step` says otherwise. */
|
|
160
|
-
| { type: 'money'; min?: number; max?: number; step?: number; below?: string; above?: string; live?: CommandLiveLines }
|
|
160
|
+
| { type: 'money'; min?: number; max?: number; step?: number; minor?: boolean; currency?: string; below?: string; above?: string; live?: CommandLiveLines }
|
|
161
161
|
/** `statement`: the long words a consent box stands beside (at most 2,000 characters), drawn as one paragraph. */
|
|
162
162
|
| { type: 'boolean'; accept?: true; statement?: string }
|
|
163
163
|
| { type: 'date'; min?: string; max?: string; below?: string; above?: string }
|
package/dist/shared/order.d.ts
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* What has to exist first, and what comes apart in which order.
|
|
3
|
-
*
|
|
4
|
-
* Every foreign key is a statement about time: a row cannot point at a row
|
|
5
|
-
* that is not there yet, and a row that is pointed at cannot leave before the
|
|
6
|
-
* rows pointing at it. Building test data means knowing that order, and
|
|
7
|
-
* deriving it by hand from a catalog is exactly the work this tool already
|
|
8
|
-
* did to draw the graph.
|
|
9
|
-
*
|
|
10
|
-
* A topological sort, with the two things a topological sort usually leaves
|
|
11
|
-
* out said out loud:
|
|
12
|
-
*
|
|
13
|
-
* - **Cycles.** Two tables that point at each other have no order. Rather
|
|
14
|
-
* than picking one and pretending, the cycle is named — and where one of
|
|
15
|
-
* its edges is nullable, the way through is named too: insert with the
|
|
16
|
-
* column null, then update it once the other row exists.
|
|
17
|
-
* - **Self-references.** `work_order.parent_id → work_order` is not a
|
|
18
|
-
* cycle between tables and does not affect the order at all, but it does
|
|
19
|
-
* mean the rows inside that table have an order of their own. Reported
|
|
20
|
-
* separately, because it is a different problem with a different answer.
|
|
21
|
-
*
|
|
22
|
-
* Ties break alphabetically. A stable answer is worth more than a marginally
|
|
23
|
-
* shorter one: an order that changes between runs makes a fixture that
|
|
24
|
-
* "sometimes works", which is the failure this exists to prevent.
|
|
25
|
-
*/
|
|
26
|
-
import { type DatabaseSchema } from './schema.js';
|
|
27
|
-
interface Requirement {
|
|
28
|
-
/** The table that must have the row already. */
|
|
29
|
-
table: string;
|
|
30
|
-
/** The columns pointing at it. */
|
|
31
|
-
via: string[];
|
|
32
|
-
/**
|
|
33
|
-
* Whether every one of those columns may be null.
|
|
34
|
-
*
|
|
35
|
-
* A nullable reference is a requirement you can postpone: insert the row
|
|
36
|
-
* without it, then update. That is the only way out of a cycle, so it is
|
|
37
|
-
* worth saying even where the order came out fine.
|
|
38
|
-
*/
|
|
39
|
-
optional: boolean;
|
|
40
|
-
}
|
|
41
|
-
interface OrderPlan {
|
|
42
|
-
/** Parents first: insert in this order and no reference dangles. */
|
|
43
|
-
insert: string[];
|
|
44
|
-
/** Children first: the reverse, which is what teardown needs. */
|
|
45
|
-
remove: string[];
|
|
46
|
-
/** Per table, what must exist before it. */
|
|
47
|
-
requires: Record<string, Requirement[]>;
|
|
48
|
-
/** Tables whose order could not be decided, grouped by the cycle they are in. */
|
|
49
|
-
cycles: string[][];
|
|
50
|
-
/** Tables that point at themselves: an order *within* the table. */
|
|
51
|
-
selfReferencing: string[];
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* @param tables The tables to order. Omitted means every table in the schema.
|
|
55
|
-
* Names are resolved through `findTable`, so a bare name works where the
|
|
56
|
-
* schema qualifies it.
|
|
57
|
-
*/
|
|
58
|
-
export declare function insertOrder(schema: DatabaseSchema, tables?: string[]): OrderPlan;
|
|
59
|
-
export {};
|
package/dist/shared/order.js
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* What has to exist first, and what comes apart in which order.
|
|
3
|
-
*
|
|
4
|
-
* Every foreign key is a statement about time: a row cannot point at a row
|
|
5
|
-
* that is not there yet, and a row that is pointed at cannot leave before the
|
|
6
|
-
* rows pointing at it. Building test data means knowing that order, and
|
|
7
|
-
* deriving it by hand from a catalog is exactly the work this tool already
|
|
8
|
-
* did to draw the graph.
|
|
9
|
-
*
|
|
10
|
-
* A topological sort, with the two things a topological sort usually leaves
|
|
11
|
-
* out said out loud:
|
|
12
|
-
*
|
|
13
|
-
* - **Cycles.** Two tables that point at each other have no order. Rather
|
|
14
|
-
* than picking one and pretending, the cycle is named — and where one of
|
|
15
|
-
* its edges is nullable, the way through is named too: insert with the
|
|
16
|
-
* column null, then update it once the other row exists.
|
|
17
|
-
* - **Self-references.** `work_order.parent_id → work_order` is not a
|
|
18
|
-
* cycle between tables and does not affect the order at all, but it does
|
|
19
|
-
* mean the rows inside that table have an order of their own. Reported
|
|
20
|
-
* separately, because it is a different problem with a different answer.
|
|
21
|
-
*
|
|
22
|
-
* Ties break alphabetically. A stable answer is worth more than a marginally
|
|
23
|
-
* shorter one: an order that changes between runs makes a fixture that
|
|
24
|
-
* "sometimes works", which is the failure this exists to prevent.
|
|
25
|
-
*/
|
|
26
|
-
import { findTable } from './schema.js';
|
|
27
|
-
/**
|
|
28
|
-
* @param tables The tables to order. Omitted means every table in the schema.
|
|
29
|
-
* Names are resolved through `findTable`, so a bare name works where the
|
|
30
|
-
* schema qualifies it.
|
|
31
|
-
*/
|
|
32
|
-
export function insertOrder(schema, tables) {
|
|
33
|
-
const wanted = tables?.length
|
|
34
|
-
? tables.map((name) => findTable(schema, name)?.id ?? name)
|
|
35
|
-
: schema.tables.filter((t) => !t.isView).map((t) => t.id);
|
|
36
|
-
const inSet = new Set(wanted);
|
|
37
|
-
const requires = {};
|
|
38
|
-
const selfReferencing = [];
|
|
39
|
-
/* `after.get(a)` holds the tables that must come *after* a — the edges of
|
|
40
|
-
the sort, pointing the way an insert travels. */
|
|
41
|
-
const after = new Map(wanted.map((id) => [id, new Set()]));
|
|
42
|
-
const waitingOn = new Map(wanted.map((id) => [id, 0]));
|
|
43
|
-
for (const fk of schema.foreignKeys) {
|
|
44
|
-
const from = fk.from.table;
|
|
45
|
-
const to = fk.to.table;
|
|
46
|
-
if (!inSet.has(from))
|
|
47
|
-
continue;
|
|
48
|
-
if (from === to) {
|
|
49
|
-
if (!selfReferencing.includes(from))
|
|
50
|
-
selfReferencing.push(from);
|
|
51
|
-
continue;
|
|
52
|
-
}
|
|
53
|
-
/* A reference out of the set is still a requirement — the row it points
|
|
54
|
-
at has to exist — but it cannot be ordered, because it is not part of
|
|
55
|
-
what was asked about. It belongs in `requires` and not in the sort. */
|
|
56
|
-
const columns = findTable(schema, from)?.columns ?? [];
|
|
57
|
-
const optional = fk.from.columns.every((name) => columns.find((c) => c.name === name)?.nullable !== false);
|
|
58
|
-
(requires[from] ??= []).push({ table: to, via: fk.from.columns, optional });
|
|
59
|
-
if (!inSet.has(to))
|
|
60
|
-
continue;
|
|
61
|
-
if (after.get(to).has(from))
|
|
62
|
-
continue;
|
|
63
|
-
after.get(to).add(from);
|
|
64
|
-
waitingOn.set(from, (waitingOn.get(from) ?? 0) + 1);
|
|
65
|
-
}
|
|
66
|
-
const insert = [];
|
|
67
|
-
const ready = wanted.filter((id) => !waitingOn.get(id)).sort();
|
|
68
|
-
while (ready.length) {
|
|
69
|
-
const id = ready.shift();
|
|
70
|
-
insert.push(id);
|
|
71
|
-
/* Sorted on the way in, so the answer does not depend on the order the
|
|
72
|
-
catalog happened to list the keys in. */
|
|
73
|
-
for (const next of [...after.get(id)].sort()) {
|
|
74
|
-
const left = (waitingOn.get(next) ?? 0) - 1;
|
|
75
|
-
waitingOn.set(next, left);
|
|
76
|
-
if (left === 0) {
|
|
77
|
-
ready.push(next);
|
|
78
|
-
ready.sort();
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
/* Whatever is left is in a cycle, or behind one. Grouped so the answer
|
|
83
|
-
names the knot rather than listing its members. */
|
|
84
|
-
const stuck = wanted.filter((id) => !insert.includes(id));
|
|
85
|
-
const cycles = groupCycles(stuck, after);
|
|
86
|
-
for (const list of Object.values(requires)) {
|
|
87
|
-
list.sort((a, b) => a.table.localeCompare(b.table));
|
|
88
|
-
}
|
|
89
|
-
return {
|
|
90
|
-
insert,
|
|
91
|
-
remove: [...insert].reverse(),
|
|
92
|
-
requires,
|
|
93
|
-
cycles,
|
|
94
|
-
selfReferencing: selfReferencing.sort(),
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* The knots, as knots.
|
|
99
|
-
*
|
|
100
|
-
* Two tables pointing at each other and a third waiting behind them are one
|
|
101
|
-
* problem with one fix, and three lines saying "could not be ordered" is
|
|
102
|
-
* three problems to work out are the same one.
|
|
103
|
-
*/
|
|
104
|
-
function groupCycles(stuck, after) {
|
|
105
|
-
const left = new Set(stuck);
|
|
106
|
-
const groups = [];
|
|
107
|
-
while (left.size) {
|
|
108
|
-
const start = [...left].sort()[0];
|
|
109
|
-
const group = new Set([start]);
|
|
110
|
-
const queue = [start];
|
|
111
|
-
while (queue.length) {
|
|
112
|
-
const id = queue.shift();
|
|
113
|
-
for (const next of after.get(id) ?? []) {
|
|
114
|
-
if (!left.has(next) || group.has(next))
|
|
115
|
-
continue;
|
|
116
|
-
group.add(next);
|
|
117
|
-
queue.push(next);
|
|
118
|
-
}
|
|
119
|
-
/* Both directions: a cycle is only visible if the walk can go back the
|
|
120
|
-
way the arrows came. */
|
|
121
|
-
for (const [other, targets] of after) {
|
|
122
|
-
if (!left.has(other) || group.has(other) || !targets.has(id))
|
|
123
|
-
continue;
|
|
124
|
-
group.add(other);
|
|
125
|
-
queue.push(other);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
for (const id of group)
|
|
129
|
-
left.delete(id);
|
|
130
|
-
groups.push([...group].sort());
|
|
131
|
-
}
|
|
132
|
-
return groups;
|
|
133
|
-
}
|