@mistriwork/engine 1.0.0-rc.40 → 1.0.0-rc.41
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.d.ts +3 -50
- package/dist/adapters/postgres.js +36 -232
- package/dist/adapters/postgresSql.d.ts +63 -0
- package/dist/adapters/postgresSql.js +238 -0
- package/dist/adapters/postgresTransactionHandle.d.ts +13 -1
- package/dist/adapters/postgresTransactionHandle.js +36 -0
- package/dist/adapters/postgresWriteAhead.d.ts +4 -1
- package/dist/adapters/postgresWriteAhead.js +14 -6
- package/dist/server/devclient.d.ts +18 -0
- package/dist/server/devclient.js +44 -0
- package/dist/shared/addressParams.d.ts +6 -0
- package/dist/shared/addressParams.js +17 -0
- package/dist/shared/appLinks.d.ts +15 -3
- package/dist/shared/appLinks.js +24 -7
- package/dist/shared/computedParse.js +4 -0
- package/dist/shared/listEmpty.d.ts +4 -1
- package/dist/shared/listEmpty.js +13 -4
- package/dist/shared/listPresentations.d.ts +2 -1
- package/dist/shared/model.d.ts +9 -0
- package/dist/shared/model.js +35 -3
- package/dist/shared/page.js +12 -2
- package/dist/shared/pageAnatomy.d.ts +2 -0
- package/dist/shared/pageAnatomy.js +20 -9
- package/dist/shared/pageConfig.js +14 -6
- package/dist/shared/pageModel.d.ts +2 -0
- package/package.json +1 -1
- package/schema/model.d.ts +3 -1
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The three statements a person writes or asks about rather than the ones this
|
|
3
|
+
* adapter builds: raw SQL (read-only by transaction, or on the write handle),
|
|
4
|
+
* EXPLAIN, and the planner's statistics. They share nothing with the adapter's
|
|
5
|
+
* catalog or write caches, only its connection pools, so they live apart from
|
|
6
|
+
* postgres.ts, which had outgrown a file a person can hold.
|
|
7
|
+
*/
|
|
8
|
+
import { boundStatement, clampLimit, dedupe, looksWrappable, refuseAnalyze, refuseStacked, trimStatement, } from './adapter.js';
|
|
9
|
+
import { postgresRowEstimate } from './postgresCatalogRows.js';
|
|
10
|
+
import { refusePostgresReserved, refuseReservedPlan, refuseReservedText } from './reservedSql.js';
|
|
11
|
+
import { statementCap } from './statementControl.js';
|
|
12
|
+
import { valueSafe } from './postgresValues.js';
|
|
13
|
+
/**
|
|
14
|
+
* Run a statement the user wrote.
|
|
15
|
+
*
|
|
16
|
+
* The session already carries `default_transaction_read_only = on`, and for
|
|
17
|
+
* everything else in this adapter that is enough, because every statement is
|
|
18
|
+
* one this file built. It is **not** enough here: `SET
|
|
19
|
+
* default_transaction_read_only = off` is itself a statement, and a user who
|
|
20
|
+
* typed it followed by a `DELETE` would get both.
|
|
21
|
+
*
|
|
22
|
+
* `BEGIN READ ONLY` is the version that holds, for one statement. Postgres
|
|
23
|
+
* refuses `SET TRANSACTION READ WRITE` once the transaction has taken its
|
|
24
|
+
* first snapshot, and one statement takes it before it runs. It does not hold
|
|
25
|
+
* against several: `COMMIT; BEGIN READ WRITE; DELETE …` ends the read-only
|
|
26
|
+
* transaction and opens a writable one. So the text goes on the extended
|
|
27
|
+
* protocol (`queryMode: 'extended'`), where Postgres itself refuses a second
|
|
28
|
+
* statement; refuseStacked is the earlier, clearer refusal, not the guard.
|
|
29
|
+
* (node-postgres sends a query with an empty `values` on the simple protocol,
|
|
30
|
+
* which runs every statement in the string.) The `ROLLBACK` afterwards also
|
|
31
|
+
* discards any session state the statement set, so one run cannot change what
|
|
32
|
+
* the next one means.
|
|
33
|
+
*
|
|
34
|
+
* The rollback runs in a `finally`: a statement that errors has still opened
|
|
35
|
+
* a transaction, and leaving it open would leave the connection in an aborted
|
|
36
|
+
* state where every later query fails with "current transaction is aborted".
|
|
37
|
+
*
|
|
38
|
+
* `write: true` is the other half, and it is a different request rather than
|
|
39
|
+
* a relaxation of this one: a separate handle, no READ ONLY transaction, and
|
|
40
|
+
* a decision made further up about whether this connection may write at all.
|
|
41
|
+
* The two paths share the statement text and nothing else.
|
|
42
|
+
*/
|
|
43
|
+
export async function runPostgresSql(connections, writes, request) {
|
|
44
|
+
const stacked = refuseStacked(request.text, 'postgres');
|
|
45
|
+
if (stacked)
|
|
46
|
+
throw new Error(stacked);
|
|
47
|
+
if (request.refuseReserved)
|
|
48
|
+
refusePostgresReserved(request.text, request.write);
|
|
49
|
+
const limit = clampLimit(request.limit);
|
|
50
|
+
/* A write is never wrapped in a bounding subquery: `UPDATE … RETURNING`
|
|
51
|
+
inside `SELECT * FROM (…)` is a different statement, and one Postgres
|
|
52
|
+
refuses outright. The caller asked for this text; it runs as typed. */
|
|
53
|
+
const bounded = !request.write && looksWrappable(request.text);
|
|
54
|
+
const text = bounded ? boundStatement(request.text, limit) : trimStatement(request.text);
|
|
55
|
+
if (request.write) {
|
|
56
|
+
/* On the write handle, and outside the READ ONLY transaction every other
|
|
57
|
+
statement here gets — which is the whole difference. Whether this is
|
|
58
|
+
allowed at all was decided before the request arrived: the connection
|
|
59
|
+
has to be marked writable in the config, and that check does not live
|
|
60
|
+
in the adapter.
|
|
61
|
+
|
|
62
|
+
No wrapper transaction of our own. One statement is already atomic in
|
|
63
|
+
Postgres, and a BEGIN/COMMIT around it would only add a way to leave a
|
|
64
|
+
transaction open on a connection that outlives the request. */
|
|
65
|
+
return writes.run('all', () => connections.withWriteClient(async (write) => {
|
|
66
|
+
const started = performance.now();
|
|
67
|
+
// Extended protocol: one statement, as on the read path below.
|
|
68
|
+
const result = await write.query({ text, values: [], rowMode: 'array', queryMode: 'extended' });
|
|
69
|
+
return {
|
|
70
|
+
/* A write that returns rows — `RETURNING` — is worth showing, and one
|
|
71
|
+
that does not returns an empty grid rather than a fabricated one.
|
|
72
|
+
Array rows, so two columns of one name keep both values. */
|
|
73
|
+
columns: dedupe(result.fields?.map((f) => f.name) ?? []),
|
|
74
|
+
rows: (result.rows ?? []).map((row) => row.map(valueSafe)),
|
|
75
|
+
ms: Math.round(performance.now() - started),
|
|
76
|
+
truncated: false,
|
|
77
|
+
bounded: false,
|
|
78
|
+
affected: Number(result.rowCount ?? 0),
|
|
79
|
+
sql: { text, params: [] },
|
|
80
|
+
};
|
|
81
|
+
}, true));
|
|
82
|
+
}
|
|
83
|
+
return connections.withReadClient(async (db) => {
|
|
84
|
+
const started = performance.now();
|
|
85
|
+
/* The statement's cap, set where it dies with the transaction, so the
|
|
86
|
+
session's own is untouched — and statementCap's floor stops a caller
|
|
87
|
+
asking for a timeout so low that even the fast path loses races to it. */
|
|
88
|
+
const cap = statementCap(request.timeoutMs);
|
|
89
|
+
const begin = () => db.query('BEGIN READ ONLY').then(() => db.query(`SET LOCAL statement_timeout = ${cap}`));
|
|
90
|
+
await begin();
|
|
91
|
+
try {
|
|
92
|
+
if (request.refuseReserved)
|
|
93
|
+
await refuseReservedPlan(db, request.text, []);
|
|
94
|
+
/* `rowMode: 'array'` so two output columns of the same name keep both
|
|
95
|
+
values; the default object rows would let the second overwrite the
|
|
96
|
+
first. `queryMode: 'extended'` is what carries exactly one statement:
|
|
97
|
+
an empty `values` alone leaves node-postgres on the simple protocol,
|
|
98
|
+
which would run a stacked COMMIT and a write after it. */
|
|
99
|
+
let wrapped = bounded;
|
|
100
|
+
const result = await db.query({ text, values: [], rowMode: 'array', queryMode: 'extended' }).catch(async (err) => {
|
|
101
|
+
/* The wrap is an optimisation that is allowed to fail — not every
|
|
102
|
+
legal statement survives being made a derived table. Run as
|
|
103
|
+
written and reported as unbounded. The transaction has to be
|
|
104
|
+
restarted: a failed statement aborts it, and every later query in
|
|
105
|
+
it would fail with "current transaction is aborted". A statement
|
|
106
|
+
that was stopped — cancelled, or past its cap (57014) — is not a
|
|
107
|
+
wrap that failed, and running it again would spend the cap twice. */
|
|
108
|
+
if (!bounded || err.code === '57014')
|
|
109
|
+
throw err;
|
|
110
|
+
wrapped = false;
|
|
111
|
+
await db.query('ROLLBACK');
|
|
112
|
+
await begin();
|
|
113
|
+
return db.query({ text: trimStatement(request.text), values: [], rowMode: 'array', queryMode: 'extended' });
|
|
114
|
+
});
|
|
115
|
+
const truncated = result.rows.length > limit;
|
|
116
|
+
return {
|
|
117
|
+
columns: dedupe(result.fields.map((f) => f.name)),
|
|
118
|
+
rows: (truncated ? result.rows.slice(0, limit) : result.rows).map((row) => row.map(valueSafe)),
|
|
119
|
+
ms: Math.round(performance.now() - started),
|
|
120
|
+
truncated,
|
|
121
|
+
bounded: wrapped,
|
|
122
|
+
sql: { text: wrapped ? text : trimStatement(request.text), params: [] },
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
finally {
|
|
126
|
+
await db.query('ROLLBACK').catch(() => {
|
|
127
|
+
/* The connection is already unusable if this fails; the original error
|
|
128
|
+
is the one worth reporting. */
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
}, true);
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* EXPLAIN, plain — never the analysing variant that executes to time.
|
|
135
|
+
*
|
|
136
|
+
* `runSql`'s bounding wrap is precisely wrong here: a statement made a
|
|
137
|
+
* derived table is a different statement, with a different plan. The text
|
|
138
|
+
* goes to the planner as written, inside the same READ ONLY transaction
|
|
139
|
+
* every user statement gets — so even a smuggled write is only ever
|
|
140
|
+
* planned, and an execution attempt is the transaction's to refuse, not a
|
|
141
|
+
* string check's.
|
|
142
|
+
*/
|
|
143
|
+
export async function explainPostgresSql(connections, request) {
|
|
144
|
+
const stacked = refuseStacked(request.text, 'postgres');
|
|
145
|
+
if (stacked)
|
|
146
|
+
throw new Error(stacked);
|
|
147
|
+
const analyze = refuseAnalyze(request.text);
|
|
148
|
+
if (analyze)
|
|
149
|
+
throw new Error(analyze);
|
|
150
|
+
if (request.refuseReserved)
|
|
151
|
+
refuseReservedText(request.text, 'postgres');
|
|
152
|
+
return connections.withReadClient(async (db) => {
|
|
153
|
+
const started = performance.now();
|
|
154
|
+
await db.query('BEGIN READ ONLY');
|
|
155
|
+
try {
|
|
156
|
+
/* Planning runs nothing, but planning a wide join still takes time. */
|
|
157
|
+
await db.query(`SET LOCAL statement_timeout = ${statementCap()}`);
|
|
158
|
+
if (request.refuseReserved)
|
|
159
|
+
await refuseReservedPlan(db, request.text, (request.params ?? []));
|
|
160
|
+
const result = await db.query({
|
|
161
|
+
text: `EXPLAIN ${trimStatement(request.text)}`,
|
|
162
|
+
values: (request.params ?? []),
|
|
163
|
+
rowMode: 'array',
|
|
164
|
+
// One statement, whether or not there are parameters: see runSql.
|
|
165
|
+
queryMode: 'extended',
|
|
166
|
+
});
|
|
167
|
+
return {
|
|
168
|
+
plan: result.rows.map((r) => String(r[0])),
|
|
169
|
+
ms: Math.round(performance.now() - started),
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
finally {
|
|
173
|
+
await db.query('ROLLBACK').catch(() => { });
|
|
174
|
+
}
|
|
175
|
+
}, true);
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* The planner's own statistics, read rather than computed.
|
|
179
|
+
*
|
|
180
|
+
* `pg_stats` already holds the null share, the distinct estimate, the most
|
|
181
|
+
* common values with their frequencies, and the histogram — ANALYZE paid
|
|
182
|
+
* for them, reading them touches no user table. The numbers are
|
|
183
|
+
* sample-based and stale until the next ANALYZE, which is what
|
|
184
|
+
* `source: 'statistics'` tells the caller; a column ANALYZE has never
|
|
185
|
+
* visited simply has no numbers, which beats invented ones.
|
|
186
|
+
*/
|
|
187
|
+
export async function profilePostgresTable(table, schema, name, db) {
|
|
188
|
+
const started = performance.now();
|
|
189
|
+
const rel = await db.query(`SELECT CASE WHEN c.reltuples < 0 THEN NULL ELSE c.reltuples END AS approx_rows
|
|
190
|
+
FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
191
|
+
WHERE n.nspname = $1 AND c.relname = $2`, [schema, name]);
|
|
192
|
+
const approx = rel.rows[0]?.approx_rows;
|
|
193
|
+
const rows = postgresRowEstimate(approx);
|
|
194
|
+
/* Every live column, with its stats row where ANALYZE has made one.
|
|
195
|
+
`most_common_vals` and `histogram_bounds` are `anyarray`, which the
|
|
196
|
+
driver has no parser for; the ::text::text[] two-step turns them into
|
|
197
|
+
the text array it can parse. */
|
|
198
|
+
const stats = await db.query(`SELECT a.attname::text AS column,
|
|
199
|
+
format_type(a.atttypid, a.atttypmod) AS type,
|
|
200
|
+
s.null_frac,
|
|
201
|
+
s.n_distinct,
|
|
202
|
+
s.most_common_vals::text::text[] AS common_values,
|
|
203
|
+
s.most_common_freqs AS common_freqs,
|
|
204
|
+
s.histogram_bounds::text::text[] AS bounds
|
|
205
|
+
FROM pg_attribute a
|
|
206
|
+
JOIN pg_class c ON c.oid = a.attrelid
|
|
207
|
+
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
208
|
+
LEFT JOIN pg_stats s ON s.schemaname = n.nspname AND s.tablename = c.relname AND s.attname = a.attname
|
|
209
|
+
WHERE n.nspname = $1 AND c.relname = $2 AND a.attnum > 0 AND NOT a.attisdropped
|
|
210
|
+
ORDER BY a.attnum`, [schema, name]);
|
|
211
|
+
const columns = stats.rows.map((r) => {
|
|
212
|
+
/* Negative n_distinct is a ratio — "-0.3 of the rows are distinct" —
|
|
213
|
+
which only converts when a row estimate exists to multiply by. */
|
|
214
|
+
const distinct = r.n_distinct == null
|
|
215
|
+
? undefined
|
|
216
|
+
: Number(r.n_distinct) >= 0
|
|
217
|
+
? Math.round(Number(r.n_distinct))
|
|
218
|
+
: rows === undefined
|
|
219
|
+
? undefined
|
|
220
|
+
: Math.round(-Number(r.n_distinct) * rows);
|
|
221
|
+
const common = r.common_values?.length && r.common_freqs?.length
|
|
222
|
+
? r.common_values.map((v, i) => ({ value: v, frac: Number(r.common_freqs?.[i] ?? 0) }))
|
|
223
|
+
: undefined;
|
|
224
|
+
return {
|
|
225
|
+
column: r.column,
|
|
226
|
+
type: r.type,
|
|
227
|
+
nullFrac: r.null_frac == null ? undefined : Number(r.null_frac),
|
|
228
|
+
distinct,
|
|
229
|
+
/* The histogram's outer edges, as text from the sample. A column too
|
|
230
|
+
uniform to earn a histogram gets no min/max — its common values
|
|
231
|
+
sorted lexically would call 9 larger than 10. */
|
|
232
|
+
min: r.bounds?.length ? r.bounds[0] : undefined,
|
|
233
|
+
max: r.bounds?.length ? r.bounds[r.bounds.length - 1] : undefined,
|
|
234
|
+
common,
|
|
235
|
+
};
|
|
236
|
+
});
|
|
237
|
+
return { table, rows, columns, source: 'statistics', ms: Math.round(performance.now() - started) };
|
|
238
|
+
}
|
|
@@ -23,7 +23,7 @@ export interface AheadReport {
|
|
|
23
23
|
taken: number;
|
|
24
24
|
/** Statements asked in their turn while answers sent ahead were still waiting: each cost a round trip one sent ahead might have saved. */
|
|
25
25
|
missed: number;
|
|
26
|
-
/** Answers sent ahead that nothing took
|
|
26
|
+
/** Answers sent ahead that nothing took and that the transaction never asked again in its turn, by what each said. */
|
|
27
27
|
left: string[];
|
|
28
28
|
/** Times the transaction began again without them, because one failed. */
|
|
29
29
|
redone: number;
|
|
@@ -58,6 +58,18 @@ export declare function waitsAhead(client: PgQuery, statement: Readonly<{
|
|
|
58
58
|
text: string;
|
|
59
59
|
values?: readonly unknown[];
|
|
60
60
|
}>): boolean;
|
|
61
|
+
/**
|
|
62
|
+
* A lock sent ahead that the transaction stands on without asking it again:
|
|
63
|
+
* the row was held from the moment its answer came back, whatever dropped
|
|
64
|
+
* that answer since (postgres.ts `lock` and `holds`). It counts as taken, so
|
|
65
|
+
* the report does not warn of a lock the transaction relied on as never used
|
|
66
|
+
* (a field report: each tenant command that saved its journal before writing
|
|
67
|
+
* a row it had locked ahead warned of that row's lock).
|
|
68
|
+
*/
|
|
69
|
+
export declare function heldAhead(client: PgQuery, statement: Readonly<{
|
|
70
|
+
text: string;
|
|
71
|
+
values?: readonly unknown[];
|
|
72
|
+
}>): void;
|
|
61
73
|
/**
|
|
62
74
|
* Send a read now that a caller will answer later reads from itself (a row
|
|
63
75
|
* read in full, answered by projection: scoped/keyRows.ts), rather than the
|
|
@@ -75,6 +75,16 @@ export function transactionHandle(client, origin) {
|
|
|
75
75
|
return answer;
|
|
76
76
|
}
|
|
77
77
|
}
|
|
78
|
+
/* One dropped and asked again in its turn was asked: what it cost is in missed and asked, and only one never
|
|
79
|
+
asked is a hint that no longer says what the transaction does. A tenant command's first save after a start
|
|
80
|
+
describes the catalog before its journal, and every statement sent for the journal was warned of (a field
|
|
81
|
+
report). */
|
|
82
|
+
if (state.report.left.length) {
|
|
83
|
+
const key = said(args);
|
|
84
|
+
const at = key === undefined ? -1 : state.report.left.indexOf(key);
|
|
85
|
+
if (at !== -1)
|
|
86
|
+
state.report.left.splice(at, 1);
|
|
87
|
+
}
|
|
78
88
|
// A rollback of the whole transaction: what waited for its COMMIT is never sent.
|
|
79
89
|
if (typeof text === 'string' && /^\s*ROLLBACK\s*;?\s*$/i.test(text)) {
|
|
80
90
|
state.tail = [];
|
|
@@ -173,6 +183,32 @@ export function waitsAhead(client, statement) {
|
|
|
173
183
|
const key = said(statement.values?.length ? [statement.text, [...statement.values]] : [statement.text]);
|
|
174
184
|
return key !== undefined && (state.ahead.get(key)?.length ?? 0) > 0;
|
|
175
185
|
}
|
|
186
|
+
/**
|
|
187
|
+
* A lock sent ahead that the transaction stands on without asking it again:
|
|
188
|
+
* the row was held from the moment its answer came back, whatever dropped
|
|
189
|
+
* that answer since (postgres.ts `lock` and `holds`). It counts as taken, so
|
|
190
|
+
* the report does not warn of a lock the transaction relied on as never used
|
|
191
|
+
* (a field report: each tenant command that saved its journal before writing
|
|
192
|
+
* a row it had locked ahead warned of that row's lock).
|
|
193
|
+
*/
|
|
194
|
+
export function heldAhead(client, statement) {
|
|
195
|
+
const state = transactions.get(client);
|
|
196
|
+
const key = state && said(statement.values?.length ? [statement.text, [...statement.values]] : [statement.text]);
|
|
197
|
+
if (!state || key === undefined)
|
|
198
|
+
return;
|
|
199
|
+
const waiting = state.ahead.get(key);
|
|
200
|
+
const left = state.report.left.indexOf(key);
|
|
201
|
+
if (waiting?.length) {
|
|
202
|
+
waiting.shift();
|
|
203
|
+
if (!waiting.length)
|
|
204
|
+
state.ahead.delete(key);
|
|
205
|
+
}
|
|
206
|
+
else if (left !== -1)
|
|
207
|
+
state.report.left.splice(left, 1);
|
|
208
|
+
else
|
|
209
|
+
return;
|
|
210
|
+
state.report.taken += 1;
|
|
211
|
+
}
|
|
176
212
|
/**
|
|
177
213
|
* Send a read now that a caller will answer later reads from itself (a row
|
|
178
214
|
* read in full, answered by projection: scoped/keyRows.ts), rather than the
|
|
@@ -44,4 +44,7 @@ export declare function sendPlainAhead(owner: object, write: PgQuery, schema: Da
|
|
|
44
44
|
* its turn, built by the same rules; one it never sends is left, counted,
|
|
45
45
|
* and changes nothing. None where the client does not pipeline.
|
|
46
46
|
*/
|
|
47
|
-
export declare function sendWriteAhead(owner: object, write: PgQuery, scope: RowScope, peeked: PostgresScopeCatalog | undefined, keyRows: KeyRows, journal: PostgresTransactionJournal<unknown> | undefined, verify: PostgresTransactionVerifier | undefined, hint: WriteAhead | undefined, locked:
|
|
47
|
+
export declare function sendWriteAhead(owner: object, write: PgQuery, scope: RowScope, peeked: PostgresScopeCatalog | undefined, keyRows: KeyRows, journal: PostgresTransactionJournal<unknown> | undefined, verify: PostgresTransactionVerifier | undefined, hint: WriteAhead | undefined, locked: Map<string, {
|
|
48
|
+
text: string;
|
|
49
|
+
values: unknown[];
|
|
50
|
+
}>): Promise<unknown>[];
|
|
@@ -182,8 +182,9 @@ export function sendWriteAhead(owner, write, scope, peeked, keyRows, journal, ve
|
|
|
182
182
|
const filter = { groups: [pk.map((column) => ({ column, op: '=', value: key[column] }))] };
|
|
183
183
|
const where = buildWhere(filter, POSTGRES_STYLE);
|
|
184
184
|
if (!hint.readOnly) {
|
|
185
|
-
|
|
186
|
-
|
|
185
|
+
const lock = { text: `SELECT 1 FROM ${qualified(table)}${where.text} FOR UPDATE`, values: where.params };
|
|
186
|
+
answers.push(...sendAhead(write, [lock]));
|
|
187
|
+
locked.set(rowLockKey(table, filter), lock);
|
|
187
188
|
}
|
|
188
189
|
// With its reference columns' stored text, as the scoped transaction reads its rows (storedText.ts).
|
|
189
190
|
const read = scopedStatement(peeked.schema, scope, { table: table.id, filter, limit: 2, offset: 0, storedText: true });
|
|
@@ -193,16 +194,23 @@ export function sendWriteAhead(owner, write, scope, peeked, keyRows, journal, ve
|
|
|
193
194
|
keyRows.keep(scope, table, filter, row.answer.then((result) => result.rows), row.take);
|
|
194
195
|
}
|
|
195
196
|
if (hint.writes && !hint.readOnly && capturesWrites(owner, table.id)) {
|
|
196
|
-
// historyWrites'
|
|
197
|
-
const given = buildWhere({ groups: [Object.entries(key).map(([column, value]) => ({ column, op: '=', value }))] }, POSTGRES_STYLE);
|
|
198
|
-
const n = given.params.length;
|
|
197
|
+
// historyWrites' lock: lockRecordNow's statement.
|
|
199
198
|
answers.push(...sendAhead(write, [
|
|
200
199
|
{
|
|
201
200
|
text: `SELECT ${pk.map(POSTGRES_STYLE.quote).join(', ')} FROM ${qualified(table)}${where.text} FOR UPDATE`,
|
|
202
201
|
values: where.params,
|
|
203
202
|
},
|
|
204
|
-
{ text: `SELECT * FROM ${qualified(table)}${given.text} LIMIT $${n + 1} OFFSET $${n + 2}`, values: [...given.params, 2, 0] },
|
|
205
203
|
]));
|
|
204
|
+
/* Its read of the row: the whole row by the key as given, kept to answer the before image, which names only the
|
|
205
|
+
columns the write sets and the rules read (recordHistory.ts). Sent as the statement that read asks, it was
|
|
206
|
+
never asked, and every command run on a record warned of it (a field report). */
|
|
207
|
+
const given = { groups: [Object.entries(key).map(([column, value]) => ({ column, op: '=', value }))] };
|
|
208
|
+
const whole = plainStatement({ table: table.id, filter: given, limit: 2, offset: 0 });
|
|
209
|
+
const captured = whole && holdAhead(write, { text: whole.text, values: whole.params });
|
|
210
|
+
if (captured) {
|
|
211
|
+
answers.push(captured.answer);
|
|
212
|
+
keyRows.keep(PLAIN_SCOPE, table, given, captured.answer.then((result) => result.rows), captured.take);
|
|
213
|
+
}
|
|
206
214
|
}
|
|
207
215
|
}
|
|
208
216
|
return answers;
|
|
@@ -83,6 +83,24 @@ export declare function closeDevClientHmr(server: Server): Promise<void>;
|
|
|
83
83
|
/** Release the source renderer after accepted HTTP responses have drained. */
|
|
84
84
|
export declare function retireDevClient(server: Server): Promise<void>;
|
|
85
85
|
export declare function sweepDevCaches(root: string): Promise<void>;
|
|
86
|
+
/**
|
|
87
|
+
* The dependency optimizer's bundles in flight. Vite's close only flags a
|
|
88
|
+
* running bundle as cancelled and deletes its folder while the bundle may
|
|
89
|
+
* still be writing there, and the cache's removal after it raced the same
|
|
90
|
+
* write: the failure ("Failed to write file … deps_temp_…") landed after the
|
|
91
|
+
* test that started the server had ended. Rolldown starts a bundle's build at
|
|
92
|
+
* its write and calls closeBundle once it is written or has failed, so a
|
|
93
|
+
* plugin in the optimizer's own bundle sees each run end. One environment's
|
|
94
|
+
* optimizer runs one bundle at a time.
|
|
95
|
+
*/
|
|
96
|
+
export declare function optimizerRuns(): {
|
|
97
|
+
plugin: {
|
|
98
|
+
name: string;
|
|
99
|
+
buildStart(): void;
|
|
100
|
+
closeBundle(): void;
|
|
101
|
+
};
|
|
102
|
+
settled: (limit?: number) => Promise<void>;
|
|
103
|
+
};
|
|
86
104
|
/**
|
|
87
105
|
* What Vite's file watcher may look at: the package whose client it serves,
|
|
88
106
|
* and nothing outside it. Vite watches its root and adds every file it loads
|
package/dist/server/devclient.js
CHANGED
|
@@ -274,6 +274,44 @@ async function devCache(workspace) {
|
|
|
274
274
|
}, () => { })),
|
|
275
275
|
};
|
|
276
276
|
}
|
|
277
|
+
/**
|
|
278
|
+
* The dependency optimizer's bundles in flight. Vite's close only flags a
|
|
279
|
+
* running bundle as cancelled and deletes its folder while the bundle may
|
|
280
|
+
* still be writing there, and the cache's removal after it raced the same
|
|
281
|
+
* write: the failure ("Failed to write file … deps_temp_…") landed after the
|
|
282
|
+
* test that started the server had ended. Rolldown starts a bundle's build at
|
|
283
|
+
* its write and calls closeBundle once it is written or has failed, so a
|
|
284
|
+
* plugin in the optimizer's own bundle sees each run end. One environment's
|
|
285
|
+
* optimizer runs one bundle at a time.
|
|
286
|
+
*/
|
|
287
|
+
export function optimizerRuns() {
|
|
288
|
+
let running = 0;
|
|
289
|
+
let idle, wake = () => { };
|
|
290
|
+
return {
|
|
291
|
+
plugin: {
|
|
292
|
+
name: 'mistri:optimizer-runs',
|
|
293
|
+
buildStart() {
|
|
294
|
+
running++;
|
|
295
|
+
},
|
|
296
|
+
closeBundle() {
|
|
297
|
+
// A bundle that fails closes itself, and Vite's finally closes it again.
|
|
298
|
+
running = Math.max(0, running - 1);
|
|
299
|
+
if (running > 0)
|
|
300
|
+
return;
|
|
301
|
+
idle = undefined;
|
|
302
|
+
wake();
|
|
303
|
+
},
|
|
304
|
+
},
|
|
305
|
+
/* Bounded: a build that starts and never says it ended must not hold a server's close forever; the folder's
|
|
306
|
+
removal then races one late write, which is what happened before this wait existed, not a hang. */
|
|
307
|
+
settled: (limit = 10_000) => running > 0
|
|
308
|
+
? Promise.race([
|
|
309
|
+
(idle ??= new Promise((resolve) => (wake = resolve))),
|
|
310
|
+
new Promise((resolve) => setTimeout(resolve, limit).unref()),
|
|
311
|
+
])
|
|
312
|
+
: Promise.resolve(),
|
|
313
|
+
};
|
|
314
|
+
}
|
|
277
315
|
/**
|
|
278
316
|
* What Vite's file watcher may look at: the package whose client it serves,
|
|
279
317
|
* and nothing outside it. Vite watches its root and adds every file it loads
|
|
@@ -321,6 +359,7 @@ export function quietWatcherErrors(watcher, warn = (message) => console.error(`$
|
|
|
321
359
|
export function devClient(files, base = '') {
|
|
322
360
|
return hosts.get(async (httpServer) => {
|
|
323
361
|
const cache = await devCache(files.workspace);
|
|
362
|
+
const runs = optimizerRuns();
|
|
324
363
|
try {
|
|
325
364
|
const { createServer } = await import('vite');
|
|
326
365
|
const created = await createServer({
|
|
@@ -330,6 +369,7 @@ export function devClient(files, base = '') {
|
|
|
330
369
|
// imports, CSS URLs, React refresh and the HMR socket all use it.
|
|
331
370
|
base: `${base}/`,
|
|
332
371
|
cacheDir: cache.path,
|
|
372
|
+
optimizeDeps: { rolldownOptions: { plugins: [runs.plugin] } },
|
|
333
373
|
appType: 'custom',
|
|
334
374
|
logLevel: 'warn',
|
|
335
375
|
server: {
|
|
@@ -349,7 +389,11 @@ export function devClient(files, base = '') {
|
|
|
349
389
|
// Closing the optimizer then strands import-analysis hooks waiting for
|
|
350
390
|
// its dependency batch, and pluginContainer.close never resolves.
|
|
351
391
|
await created.environments.client.waitForRequestsIdle();
|
|
392
|
+
// Before close, so Vite's cancel does not delete a folder mid-write;
|
|
393
|
+
// after it, for a run the scan's end started while closing.
|
|
394
|
+
await runs.settled();
|
|
352
395
|
await close();
|
|
396
|
+
await runs.settled();
|
|
353
397
|
};
|
|
354
398
|
return ownDevClient(created, cache.remove);
|
|
355
399
|
}
|
|
@@ -5,6 +5,12 @@ export declare const tabKey: (tab: {
|
|
|
5
5
|
label: string;
|
|
6
6
|
slug?: string;
|
|
7
7
|
}) => string;
|
|
8
|
+
/** Whether a word names a tab, in any case: `?tab=Open`, or a section's `tab: 'Open'`, is the tab whose word is
|
|
9
|
+
`open`. Matched exactly, a capital opened the view's first tab instead, with nothing said (a field report). */
|
|
10
|
+
export declare const namesTab: (tab: {
|
|
11
|
+
label: string;
|
|
12
|
+
slug?: string;
|
|
13
|
+
}, word: string | null | undefined) => boolean;
|
|
8
14
|
/** A record tab's word in an address: a section group's name, a related list's title. */
|
|
9
15
|
export declare function tabWord(token: string): string;
|
|
10
16
|
/** Whether a tab token is the one an address's word names. */
|
|
@@ -13,6 +13,23 @@ export function tabSlug(label) {
|
|
|
13
13
|
}
|
|
14
14
|
/** A tab's word in an address: the App's own language's, whatever language its label is said in. */
|
|
15
15
|
export const tabKey = (tab) => tab.slug ?? tabSlug(tab.label);
|
|
16
|
+
/** Whether a word names a tab, in any case: `?tab=Open`, or a section's `tab: 'Open'`, is the tab whose word is
|
|
17
|
+
`open`. Matched exactly, a capital opened the view's first tab instead, with nothing said (a field report). */
|
|
18
|
+
export const namesTab = (tab, word) => {
|
|
19
|
+
if (typeof word !== 'string')
|
|
20
|
+
return false;
|
|
21
|
+
const key = tabKey(tab);
|
|
22
|
+
if (key.toLowerCase() === word.toLowerCase())
|
|
23
|
+
return true;
|
|
24
|
+
/* A segment's tab keeps the segment's own name (`high_value`), and a link spells a tab the way an address does
|
|
25
|
+
(`high-value`): both said in that spelling, so a link never opens the first tab instead. Only for one word: a
|
|
26
|
+
label with spaces (`All tasks`) is refused with the word to say instead. Never on an empty spelling, which a
|
|
27
|
+
word in another script would give. */
|
|
28
|
+
if (/\s/.test(word))
|
|
29
|
+
return false;
|
|
30
|
+
const spelled = tabSlug(key);
|
|
31
|
+
return spelled !== '' && spelled === tabSlug(word);
|
|
32
|
+
};
|
|
16
33
|
/** A record tab's word in an address: a section group's name, a related list's title. */
|
|
17
34
|
export function tabWord(token) {
|
|
18
35
|
if (token.startsWith('tab:'))
|
|
@@ -2,15 +2,27 @@
|
|
|
2
2
|
import type { AppTarget } from '../../schema/model.js';
|
|
3
3
|
export type { AppTarget, AppLinkValue, AppTenantIntent } from '../../schema/model.js';
|
|
4
4
|
export type BoundAppTarget = AppTarget<string | number>;
|
|
5
|
+
/** A view of this App by key, and for a list the tab it opens on (`?tab=`): `to: 'leads'` or `to: { view: 'leads', tab: 'working' }`. */
|
|
6
|
+
export type ViewTo = {
|
|
7
|
+
view: string;
|
|
8
|
+
tab?: string;
|
|
9
|
+
};
|
|
5
10
|
export type AppLinkAction = {
|
|
6
11
|
label: string;
|
|
7
12
|
to: AppTarget;
|
|
8
|
-
} | {
|
|
13
|
+
} | ({
|
|
9
14
|
label: string;
|
|
10
|
-
|
|
11
|
-
};
|
|
15
|
+
} & ViewTo);
|
|
12
16
|
/** Parse closed authoring data. Record bindings are opt-in at the source surface. */
|
|
13
17
|
export declare function parseAppTarget(raw: unknown, at?: string, record?: boolean): AppTarget;
|
|
18
|
+
/**
|
|
19
|
+
* `to` is a view's key, `{ view, tab }` for a list opened on one of its tabs, or an App link (`appLink({ … })`): one
|
|
20
|
+
* key says where a link goes. An App link says its App and kind; `{ view, tab }` says neither. Whether the view is a
|
|
21
|
+
* room and the tab one of its tabs is judged where the App is whole (appDestinations.ts).
|
|
22
|
+
*/
|
|
23
|
+
export declare function parseLinkTo(to: unknown, at: string, record?: boolean): ViewTo | {
|
|
24
|
+
to: AppTarget;
|
|
25
|
+
};
|
|
14
26
|
export declare function parseAppLinkAction(raw: unknown, at: string, record?: boolean): AppLinkAction;
|
|
15
27
|
export declare function appTargetFields(target: AppTarget): string[];
|
|
16
28
|
/** Only bind values already served to this reader. Missing/hidden values refuse. */
|
package/dist/shared/appLinks.js
CHANGED
|
@@ -83,16 +83,33 @@ export function parseAppTarget(raw, at = 'App link', record = false) {
|
|
|
83
83
|
key: Object.fromEntries(keys.map((column) => [column, value(key[column], `${at}.key.${column}`, record)])),
|
|
84
84
|
};
|
|
85
85
|
}
|
|
86
|
-
/**
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
86
|
+
/**
|
|
87
|
+
* `to` is a view's key, `{ view, tab }` for a list opened on one of its tabs, or an App link (`appLink({ … })`): one
|
|
88
|
+
* key says where a link goes. An App link says its App and kind; `{ view, tab }` says neither. Whether the view is a
|
|
89
|
+
* room and the tab one of its tabs is judged where the App is whole (appDestinations.ts).
|
|
90
|
+
*/
|
|
91
|
+
export function parseLinkTo(to, at, record = false) {
|
|
92
|
+
if (typeof to === 'string')
|
|
93
|
+
return { view: viewKey(to, at) };
|
|
94
|
+
if (to && typeof to === 'object' && !Array.isArray(to) && !Object.hasOwn(to, 'app') && !Object.hasOwn(to, 'kind')) {
|
|
95
|
+
const o = object(to, ['view', 'tab'], at);
|
|
96
|
+
if (typeof o.view !== 'string')
|
|
97
|
+
throw new Error(`${at} needs a "view": the key of one of the App's views.`);
|
|
98
|
+
const tab = o.tab;
|
|
99
|
+
if (tab === undefined)
|
|
100
|
+
return { view: viewKey(o.view, at) };
|
|
101
|
+
if (typeof tab !== 'string' || !tab.trim())
|
|
102
|
+
throw new Error(`${at} has a "tab" that is not a tab's address word, like 'working'.`);
|
|
103
|
+
return { view: viewKey(o.view, at), tab: tab.trim() };
|
|
93
104
|
}
|
|
94
105
|
return { to: parseAppTarget(to, at, record) };
|
|
95
106
|
}
|
|
107
|
+
function viewKey(raw, at) {
|
|
108
|
+
const view = raw.trim();
|
|
109
|
+
if (!view || view.length > 1024 || /[\p{Cc}\p{Cs}]/u.test(view))
|
|
110
|
+
throw new Error(`${at} needs the key of a view, or an appLink().`);
|
|
111
|
+
return view;
|
|
112
|
+
}
|
|
96
113
|
export function parseAppLinkAction(raw, at, record = false) {
|
|
97
114
|
const o = object(raw, ['label', 'to'], at);
|
|
98
115
|
if (typeof o.label !== 'string' || !o.label.trim())
|
|
@@ -265,6 +265,10 @@ export function parseComputed(text, isColumn) {
|
|
|
265
265
|
return { k: 'bool', value: lower === 'true' };
|
|
266
266
|
if (lower === 'today' || lower === 'now' || lower === 'me')
|
|
267
267
|
return { k: lower };
|
|
268
|
+
/* A column named for a unit of time (`minutes * 60`) is the column: a unit is read only after a value
|
|
269
|
+
(`3 days`, `term months`), where no column stands. */
|
|
270
|
+
if (Object.hasOwn(UNITS, lower) && isColumn(token.v))
|
|
271
|
+
return { k: 'path', path: token.v };
|
|
268
272
|
if (['and', 'or', 'not', 'is', 'empty', 'in'].includes(lower) || Object.hasOwn(UNITS, lower))
|
|
269
273
|
throw new ComputedError(`"${token.v}" stands where a value goes.`, token.at);
|
|
270
274
|
return { k: 'path', path: token.v };
|
package/dist/shared/listEmpty.js
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import { allowKeys, isRecord } from './model.js';
|
|
8
8
|
import { parseCaption } from './appMessages.js';
|
|
9
|
+
import { parseLinkTo } from './appLinks.js';
|
|
9
10
|
/** Read the shared empty-list copy used by list views and related sections. */
|
|
10
11
|
export function parseListEmpty(raw, at) {
|
|
11
12
|
const here = `${at}, "empty"`;
|
|
@@ -22,15 +23,23 @@ export function parseListEmpty(raw, at) {
|
|
|
22
23
|
const actionAt = `${here}, "action"`;
|
|
23
24
|
allowKeys(raw.action, ['label', 'to'], actionAt);
|
|
24
25
|
const label = parseCaption(raw.action.label, `${actionAt} needs a non-empty "label"`);
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
const to = typeof raw.action.to === 'string' || isRecord(raw.action.to) ? parseLinkTo(raw.action.to, `${actionAt}, "to"`) : undefined;
|
|
27
|
+
if (!to || !('view' in to))
|
|
28
|
+
throw new Error(`${actionAt} needs a "to": the key of a list, a dashboard or a signed-in form view, or { view, tab }.`);
|
|
29
|
+
return { ...result, action: { label, ...to } };
|
|
28
30
|
}
|
|
29
31
|
/** The authored shape again, for a definition written back out. */
|
|
30
32
|
export function listEmptyConfig(empty) {
|
|
31
33
|
return {
|
|
32
34
|
title: empty.title,
|
|
33
35
|
...(empty.description === undefined ? {} : { description: empty.description }),
|
|
34
|
-
...(empty.action
|
|
36
|
+
...(empty.action
|
|
37
|
+
? {
|
|
38
|
+
action: {
|
|
39
|
+
label: empty.action.label,
|
|
40
|
+
to: empty.action.tab ? { view: empty.action.view, tab: empty.action.tab } : empty.action.view,
|
|
41
|
+
},
|
|
42
|
+
}
|
|
43
|
+
: {}),
|
|
35
44
|
};
|
|
36
45
|
}
|
|
@@ -196,7 +196,8 @@ export interface AppListEmpty {
|
|
|
196
196
|
description?: string;
|
|
197
197
|
action?: {
|
|
198
198
|
label: string;
|
|
199
|
-
view: string;
|
|
199
|
+
view: string; /** The tab of a list it opens on, by its address word. */
|
|
200
|
+
tab?: string;
|
|
200
201
|
};
|
|
201
202
|
}
|
|
202
203
|
/** Whether the Export menu is offered for a table, here and not beside the payload that sets `exportableTables`, since a list's first paint asks it and appPayload.ts imports the manifest parser: `ui.export` is the App's switch, and `exportableTables` (present in an App that grants by role) the person's. */
|