@mistriwork/engine 1.0.0-rc.17 → 1.0.0-rc.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/dist/adapters/adapter.d.ts +35 -2
  2. package/dist/adapters/adapter.js +32 -1
  3. package/dist/adapters/postgres.js +8 -4
  4. package/dist/adapters/scoped/scopedReads.js +23 -5
  5. package/dist/adapters/scoped/sqliteTriggers.js +65 -38
  6. package/dist/adapters/sqlite.js +8 -4
  7. package/dist/server/columnGrants.d.ts +5 -0
  8. package/dist/server/connections.d.ts +1 -0
  9. package/dist/server/connections.js +13 -3
  10. package/dist/server/http/firstPaint.d.ts +5 -1
  11. package/dist/server/http/firstPaint.js +4 -4
  12. package/dist/server/httpConnection.d.ts +4 -2
  13. package/dist/server/httpConnection.js +10 -3
  14. package/dist/server/journalStore.d.ts +2 -2
  15. package/dist/server/journalStore.js +33 -10
  16. package/dist/server/resourceReadHttp.d.ts +4 -2
  17. package/dist/server/resourceReadHttp.js +40 -4
  18. package/dist/server/resourceReadOperations.d.ts +4 -0
  19. package/dist/server/scaffoldMachine.js +12 -1
  20. package/dist/shared/boardLanes.d.ts +44 -0
  21. package/dist/shared/boardLanes.js +69 -0
  22. package/dist/shared/builtInThemes.js +26 -20
  23. package/dist/shared/dashboardAsks.d.ts +2 -0
  24. package/dist/shared/filter.d.ts +2 -0
  25. package/dist/shared/filter.js +3 -2
  26. package/dist/shared/form.d.ts +24 -1
  27. package/dist/shared/form.js +53 -0
  28. package/dist/shared/keysetPage.js +3 -0
  29. package/dist/shared/listInference.d.ts +91 -0
  30. package/dist/shared/listInference.js +323 -0
  31. package/dist/shared/listPresentations.d.ts +3 -0
  32. package/dist/shared/listQuestion.d.ts +2 -0
  33. package/dist/shared/listQuestion.js +1 -0
  34. package/dist/shared/pageAnatomy.d.ts +27 -3
  35. package/dist/shared/pageAnatomy.js +217 -36
  36. package/dist/shared/pageModel.d.ts +17 -1
  37. package/dist/shared/pageModel.js +8 -0
  38. package/dist/shared/query.d.ts +2 -0
  39. package/dist/shared/query.js +11 -3
  40. package/dist/shared/reads.d.ts +9 -0
  41. package/dist/shared/reads.js +9 -0
  42. package/dist/shared/recordLabel.d.ts +5 -0
  43. package/dist/shared/recordLabel.js +22 -2
  44. package/dist/shared/recordReads.d.ts +30 -0
  45. package/dist/shared/recordReads.js +72 -8
  46. package/dist/shared/referenceSubline.d.ts +18 -0
  47. package/dist/shared/referenceSubline.js +62 -0
  48. package/dist/shared/resourceHttp.d.ts +5 -0
  49. package/dist/shared/rowScope.js +52 -6
  50. package/dist/shared/schema.d.ts +2 -0
  51. package/dist/shared/servedApp.d.ts +18 -0
  52. package/dist/shared/servedApp.js +10 -0
  53. package/dist/shared/themeLooks.js +61 -35
  54. package/dist/shared/themeRecipeWords.d.ts +5 -0
  55. package/dist/shared/themeRecipeWords.js +6 -1
  56. package/dist/shared/themeRecipes.d.ts +5 -0
  57. package/dist/shared/themeRecipes.js +3 -0
  58. package/dist/shared/themeType.js +2 -1
  59. package/dist/shared/view.js +29 -2
  60. package/dist/shared/viewLabels.js +16 -0
  61. package/dist/shared/viewPaths.d.ts +9 -0
  62. package/package.json +4 -1
  63. package/schema/theme.d.ts +8 -4
@@ -14,14 +14,14 @@ import type { CompiledColumn, CompiledJoin, ViewDef } from '../shared/view.js';
14
14
  import type { BreakdownDef, CompiledMeasure } from '../shared/breakdown.js';
15
15
  import type { AggregateRequest, AggregateResult } from '../shared/aggregate.js';
16
16
  import type { RowScope } from '../shared/rowScope.js';
17
- import type { QueryResult } from '../shared/reads.js';
17
+ import { type QueryResult } from '../shared/reads.js';
18
18
  import { type Filter, type SqlFragment, type SqlStyle } from '../shared/filter.js';
19
19
  import type { PoolRoleStats } from './poolMetrics.js';
20
20
  import type { ScopeTrigger } from './scoped/scopeTriggers.js';
21
21
  import type { TriggerKeyFacts } from './scoped/triggerEffects.js';
22
22
  export { ANSI_STYLE, OPS, POSTGRES_STYLE, buildWhere, type Condition, type Filter, type Op, type SqlFragment, type SqlStyle, } from '../shared/filter.js';
23
23
  export type { AggregateMeasure, AggregateRequest, AggregateResult } from '../shared/aggregate.js';
24
- export { DEFAULT_LIMIT, ESTIMATE_ABOVE, MAX_LIMIT, clampLimit, clampOffset, countStatement, countEachStatement, countEachRows, type CountResult, type QueryResult, } from '../shared/reads.js';
24
+ export { DEFAULT_LIMIT, ESTIMATE_ABOVE, MAX_LIMIT, clampLimit, clampOffset, countStatement, countEachStatement, countEachRows, LANE_ROW, type CountResult, type QueryResult, } from '../shared/reads.js';
25
25
  /** One count of `Adapter.countEach`: a table, and the rows of it to count. */
26
26
  export interface CountItem {
27
27
  table: string;
@@ -43,6 +43,30 @@ export { Refusal } from '../shared/refusal.js';
43
43
  * three engines.
44
44
  */
45
45
  export declare function orderClause(order: readonly Order[] | undefined, table: TableMeta | undefined, style: SqlStyle, startIndex: number, quote?: (column: string) => string): SqlFragment;
46
+ /**
47
+ * A board's every lane in one statement: the filtered rows, numbered within
48
+ * each value of the lane column in the read's own order (ROW_NUMBER, which
49
+ * SQLite, PostgreSQL and MySQL 8 all speak), and the first `lanes.limit` of
50
+ * each kept. A board read each lane apiece, five or eight of one shape
51
+ * every time it opened; this is one. The rows are read whole inside so the
52
+ * order and the lane resolve whatever the read projects, and the
53
+ * projection is taken outside. Rows come lane-number first, so a page cut
54
+ * across lanes cuts every lane evenly. `n` placeholders come before the
55
+ * three this adds (the lane's limit, the read's limit, its offset), as the
56
+ * caller's WHERE and ORDER BY spent them, in that order of text.
57
+ */
58
+ export declare function lanesStatement(style: SqlStyle, parts: {
59
+ prefix?: string;
60
+ columns: string;
61
+ source: string;
62
+ lane: string;
63
+ order: string;
64
+ }, n: number): string;
65
+ /** A lanes read's rows and columns without the number it kept them by. */
66
+ export declare function withoutLaneRow<T extends {
67
+ columns: string[];
68
+ rows: Array<Record<string, unknown>>;
69
+ }>(result: T): T;
46
70
  declare const conflictBrand: unique symbol;
47
71
  /** A conditional update matched no row and did not apply its proposed values. */
48
72
  export declare class WriteConflict extends Error {
@@ -66,9 +90,18 @@ export interface QueryRequest {
66
90
  orderBy?: {
67
91
  column: string;
68
92
  direction: 'asc' | 'desc';
93
+ nulls?: 'last';
69
94
  }[];
70
95
  limit: number;
71
96
  offset: number;
97
+ /**
98
+ * A board's lanes in one statement (lanesStatement): the first `limit` rows of each value of `by`, in the read's
99
+ * order, `limit` and `offset` then counting rows across every lane.
100
+ */
101
+ lanes?: {
102
+ by: string;
103
+ limit: number;
104
+ };
72
105
  /** Binary columns come back as their bytes, not the `<n bytes>` label: a file download's read, never a grid's. */
73
106
  binary?: true;
74
107
  /**
@@ -11,12 +11,13 @@
11
11
  */
12
12
  import { declaredOrder } from '../shared/schema.js';
13
13
  import { Refusal } from '../shared/refusal.js';
14
+ import { LANE_ROW } from '../shared/reads.js';
14
15
  import { orderClauseSql } from '../shared/filter.js';
15
16
  /* The filter, the limit policy and Refusal live in shared/, where the query
16
17
  language and views reach them without depending on the adapters. Adapter
17
18
  and server code keep reaching them through this contract. */
18
19
  export { ANSI_STYLE, OPS, POSTGRES_STYLE, buildWhere, } from '../shared/filter.js';
19
- export { DEFAULT_LIMIT, ESTIMATE_ABOVE, MAX_LIMIT, clampLimit, clampOffset, countStatement, countEachStatement, countEachRows, } from '../shared/reads.js';
20
+ export { DEFAULT_LIMIT, ESTIMATE_ABOVE, MAX_LIMIT, clampLimit, clampOffset, countStatement, countEachStatement, countEachRows, LANE_ROW, } from '../shared/reads.js';
20
21
  /**
21
22
  * Whether one scan can answer every item (countEachStatement's `together`):
22
23
  * they all read one table, and the catalog says that table holds no more
@@ -44,8 +45,38 @@ export function orderClause(order, table, style, startIndex, quote = (column) =>
44
45
  expression: quote(o.column),
45
46
  direction: o.direction,
46
47
  declared: declaredOrder(table?.columns.find((column) => column.name === o.column)),
48
+ ...(o.nulls ? { nulls: o.nulls } : {}),
47
49
  })), style, startIndex);
48
50
  }
51
+ /**
52
+ * A board's every lane in one statement: the filtered rows, numbered within
53
+ * each value of the lane column in the read's own order (ROW_NUMBER, which
54
+ * SQLite, PostgreSQL and MySQL 8 all speak), and the first `lanes.limit` of
55
+ * each kept. A board read each lane apiece, five or eight of one shape
56
+ * every time it opened; this is one. The rows are read whole inside so the
57
+ * order and the lane resolve whatever the read projects, and the
58
+ * projection is taken outside. Rows come lane-number first, so a page cut
59
+ * across lanes cuts every lane evenly. `n` placeholders come before the
60
+ * three this adds (the lane's limit, the read's limit, its offset), as the
61
+ * caller's WHERE and ORDER BY spent them, in that order of text.
62
+ */
63
+ export function lanesStatement(style, parts, n) {
64
+ const [rows, lanes, number] = [style.quote('lane_rows'), style.quote('lanes'), style.quote(LANE_ROW)];
65
+ return (`${parts.prefix ?? ''}SELECT ${parts.columns} FROM (SELECT ${rows}.*, ROW_NUMBER() OVER (PARTITION BY ${rows}.${parts.lane}${parts.order}) AS ${number}` +
66
+ ` FROM (SELECT * FROM ${parts.source}) AS ${rows}) AS ${lanes}` +
67
+ ` WHERE ${lanes}.${number} <= ${style.placeholder(n + 1)} ORDER BY ${lanes}.${number}` +
68
+ ` LIMIT ${style.placeholder(n + 2)} OFFSET ${style.placeholder(n + 3)}`);
69
+ }
70
+ /** A lanes read's rows and columns without the number it kept them by. */
71
+ export function withoutLaneRow(result) {
72
+ if (!result.columns.includes(LANE_ROW) && !result.rows.some((row) => LANE_ROW in row))
73
+ return result;
74
+ return {
75
+ ...result,
76
+ columns: result.columns.filter((column) => column !== LANE_ROW),
77
+ rows: result.rows.map(({ [LANE_ROW]: _number, ...row }) => row),
78
+ };
79
+ }
49
80
  /* Registered, not a local symbol: a command handler catches the conflict its
50
81
  `tx.update` threw and rethrows through the copy of tablewalk it imported,
51
82
  which need not be this one (see businessCommandExecution.ts). */
@@ -8,7 +8,7 @@
8
8
  * connect, or the leased writer inside a scoped transaction.
9
9
  */
10
10
  import { isProxy } from 'node:util/types';
11
- import { POSTGRES_STYLE, buildWhere, clampLimit, boundStatement, dedupe, looksWrappable, refuseStacked, trimStatement, clampOffset, Refusal, WriteConflict, TransactionOperations, queueRecordLock, recordLockFilter, TransactionOutcomeUnknown, refuseAnalyze, countStatement, countEachStatement, countEachRows, countsTogether, orderClause, } from './adapter.js';
11
+ import { POSTGRES_STYLE, buildWhere, clampLimit, boundStatement, dedupe, looksWrappable, refuseStacked, trimStatement, clampOffset, Refusal, WriteConflict, TransactionOperations, queueRecordLock, recordLockFilter, TransactionOutcomeUnknown, refuseAnalyze, countStatement, countEachStatement, countEachRows, countsTogether, orderClause, lanesStatement, withoutLaneRow, } from './adapter.js';
12
12
  import { compileAggregate } from '../shared/aggregate.js';
13
13
  import { coerceForColumn } from '../shared/schema.js';
14
14
  import { buildUpdateGuard } from './scoped/updateGuard.js';
@@ -727,12 +727,15 @@ export class PostgresAdapter {
727
727
  /* LIMIT and OFFSET continue the placeholder numbering the WHERE clause
728
728
  started, which is the one thing Postgres needs that SQLite does not. */
729
729
  const n = where.params.length + order.params.length;
730
- const text = `SELECT ${cols} FROM ${qualified}${where.text}${order.text} LIMIT $${n + 1} OFFSET $${n + 2}`;
731
- const params = [...where.params, ...order.params, limit, clampOffset(request.offset)];
730
+ const lanes = request.lanes;
731
+ const text = lanes
732
+ ? lanesStatement(POSTGRES_STYLE, { columns: cols, source: `${qualified}${where.text}`, lane: POSTGRES_STYLE.quote(lanes.by), order: order.text }, n)
733
+ : `SELECT ${cols} FROM ${qualified}${where.text}${order.text} LIMIT $${n + 1} OFFSET $${n + 2}`;
734
+ const params = [...where.params, ...order.params, ...(lanes ? [clampLimit(lanes.limit)] : []), limit, clampOffset(request.offset)];
732
735
  const started = performance.now();
733
736
  const result = await db.query(text, params);
734
737
  const ms = Math.round(performance.now() - started);
735
- return {
738
+ const answer = {
736
739
  // Taking columns from `fields` rather than the first row means an empty
737
740
  // result still renders its headers instead of collapsing to nothing.
738
741
  columns: result.fields.map((f) => f.name),
@@ -740,6 +743,7 @@ export class PostgresAdapter {
740
743
  ms,
741
744
  sql: { text, params },
742
745
  };
746
+ return lanes ? withoutLaneRow(answer) : answer;
743
747
  }
744
748
  /**
745
749
  * Run a compiled view.
@@ -1,4 +1,4 @@
1
- import { buildWhere, clampLimit, orderClause, clampOffset, countStatement, Refusal, } from '../adapter.js';
1
+ import { buildWhere, clampLimit, orderClause, clampOffset, countStatement, lanesStatement, withoutLaneRow, Refusal, } from '../adapter.js';
2
2
  import { findTable } from '../../shared/schema.js';
3
3
  import { COUNT_EACH_MAX, countEachRows } from '../../shared/reads.js';
4
4
  import { isCapturedScopeCatalog } from '../../shared/scopeCatalog.js';
@@ -129,10 +129,28 @@ references = false) {
129
129
  columns.push(`${storedTextSelect(texts.columns, (name) => style.quote(name))} AS ${style.quote(texts.alias)}`);
130
130
  const order = orderClause(request.orderBy, plan.table, style, plan.params.length + 1, plan.column);
131
131
  const n = plan.params.length + order.params.length;
132
- const text = `${plan.prefix.text}SELECT ${columns.join(', ')} FROM ${plan.relation}${plan.where.text}${order.text}` +
133
- ` LIMIT ${style.placeholder(n + 1)} OFFSET ${style.placeholder(n + 2)}`;
134
- const result = await execute({ text, params: [...plan.params, ...order.params, clampLimit(request.limit), clampOffset(request.offset)] }, request.binary ? { binary: true } : undefined);
135
- return { ...result, columns: [...names] };
132
+ const lanes = request.lanes;
133
+ const text = lanes
134
+ ? lanesStatement(style, {
135
+ prefix: plan.prefix.text,
136
+ columns: columns.join(', '),
137
+ source: `${plan.relation}${plan.where.text}`,
138
+ lane: plan.column(lanes.by),
139
+ order: order.text,
140
+ }, n)
141
+ : `${plan.prefix.text}SELECT ${columns.join(', ')} FROM ${plan.relation}${plan.where.text}${order.text}` +
142
+ ` LIMIT ${style.placeholder(n + 1)} OFFSET ${style.placeholder(n + 2)}`;
143
+ const result = await execute({
144
+ text,
145
+ params: [
146
+ ...plan.params,
147
+ ...order.params,
148
+ ...(lanes ? [clampLimit(lanes.limit)] : []),
149
+ clampLimit(request.limit),
150
+ clampOffset(request.offset),
151
+ ],
152
+ }, request.binary ? { binary: true } : undefined);
153
+ return { ...(lanes ? withoutLaneRow(result) : result), columns: [...names] };
136
154
  },
137
155
  async count(table, filter, limit) {
138
156
  const plan = from(table, snapshotScopedData(filter));
@@ -101,10 +101,16 @@ const EXPRESSION_WORDS = new Set([
101
101
  'ELSE',
102
102
  ]);
103
103
  const WRITES = new Set(['INSERT', 'UPDATE', 'DELETE', 'REPLACE', 'UPSERT']);
104
+ /** Where reading stopped: an offset in the definition, or the token found there; neither when it ran out of text. */
104
105
  class Unreadable extends Error {
106
+ stop;
107
+ constructor(stop) {
108
+ super('unreadable');
109
+ this.stop = stop;
110
+ }
105
111
  }
106
- const fail = () => {
107
- throw new Unreadable();
112
+ const fail = (stop) => {
113
+ throw new Unreadable(stop);
108
114
  };
109
115
  const identifierStart = (char) => /[A-Za-z_]/.test(char) || char.charCodeAt(0) >= 0x80;
110
116
  const identifierPart = (char) => /[A-Za-z0-9_$]/.test(char) || char.charCodeAt(0) >= 0x80;
@@ -119,10 +125,11 @@ function tokenize(sql) {
119
125
  let i = 0;
120
126
  const quoted = (close, doubled) => {
121
127
  let text = '';
128
+ const open = i;
122
129
  i += 1;
123
130
  for (;;) {
124
131
  if (i >= sql.length)
125
- return fail();
132
+ return fail(open);
126
133
  const char = sql[i];
127
134
  if (char === close) {
128
135
  if (doubled && sql[i + 1] === close) {
@@ -139,6 +146,7 @@ function tokenize(sql) {
139
146
  };
140
147
  while (i < sql.length) {
141
148
  const char = sql[i];
149
+ const at = i;
142
150
  if (whitespace(char)) {
143
151
  i += 1;
144
152
  }
@@ -150,48 +158,49 @@ function tokenize(sql) {
150
158
  const end = sql.indexOf('*/', i + 2);
151
159
  // SQLite ends an unterminated comment at the end of input; a trigger's text never has one, so it refuses.
152
160
  if (end === -1)
153
- return fail();
161
+ return fail(at);
154
162
  i = end + 2;
155
163
  }
156
164
  else if (char === "'") {
157
- tokens.push({ kind: 'string', text: quoted("'", true) });
165
+ tokens.push({ kind: 'string', text: quoted("'", true), at });
158
166
  }
159
167
  else if ((char === 'x' || char === 'X') && sql[i + 1] === "'") {
160
168
  i += 1;
161
- tokens.push({ kind: 'literal', text: `X'${quoted("'", false)}'` });
169
+ tokens.push({ kind: 'literal', text: `X'${quoted("'", false)}'`, at });
162
170
  }
163
171
  else if (char === '"') {
164
- tokens.push({ kind: 'id', text: quoted('"', true) });
172
+ tokens.push({ kind: 'id', text: quoted('"', true), at });
165
173
  }
166
174
  else if (char === '`') {
167
- tokens.push({ kind: 'id', text: quoted('`', true) });
175
+ tokens.push({ kind: 'id', text: quoted('`', true), at });
168
176
  }
169
177
  else if (char === '[') {
170
- tokens.push({ kind: 'id', text: quoted(']', false) });
178
+ tokens.push({ kind: 'id', text: quoted(']', false), at });
171
179
  }
172
180
  else if (/[0-9]/.test(char) || (char === '.' && /[0-9]/.test(sql[i + 1] ?? ''))) {
173
181
  const match = /^(?:0[xX][0-9A-Fa-f_]+|(?:[0-9][0-9_]*(?:\.[0-9_]*)?|\.[0-9][0-9_]*)(?:[eE][+-]?[0-9_]+)?)/.exec(sql.slice(i));
174
182
  if (!match)
175
- return fail();
183
+ return fail(at);
176
184
  i += match[0].length;
177
185
  // `12abc` is one bad token to SQLite, not a number beside a word.
178
186
  if (i < sql.length && identifierPart(sql[i]))
179
- return fail();
180
- tokens.push({ kind: 'literal', text: match[0] });
187
+ return fail(at);
188
+ tokens.push({ kind: 'literal', text: match[0], at });
181
189
  }
182
190
  else if (identifierStart(char)) {
183
191
  let end = i + 1;
184
192
  while (end < sql.length && identifierPart(sql[end]))
185
193
  end += 1;
186
194
  const text = sql.slice(i, end);
187
- tokens.push({ kind: 'word', text, upper: text.toUpperCase() });
195
+ tokens.push({ kind: 'word', text, upper: text.toUpperCase(), at });
188
196
  i = end;
189
197
  }
190
198
  else {
191
199
  const op = ['->>', '||', '<=', '>=', '==', '!=', '<>', '<<', '>>', '->'].find((candidate) => sql.startsWith(candidate, i)) ?? char;
192
- if (!'()[],;.+-*/%<>=&|~'.includes(op[0]))
193
- return fail();
194
- tokens.push({ kind: 'op', text: op });
200
+ // `!` opens only `!=` to SQLite: a lone one, `?`, `$`, `@`, `:` or `#` (a parameter) refuses.
201
+ if (!'()[],;.+-*/%<>=&|~'.includes(op[0]) && op !== '!=')
202
+ return fail(at);
203
+ tokens.push({ kind: 'op', text: op, at });
195
204
  i += op.length;
196
205
  }
197
206
  }
@@ -219,12 +228,12 @@ class Cursor {
219
228
  }
220
229
  expectWord(word) {
221
230
  if (this.word() !== word)
222
- fail();
231
+ fail(this.peek());
223
232
  this.at += 1;
224
233
  }
225
234
  expectOp(op) {
226
235
  if (this.op() !== op)
227
- fail();
236
+ fail(this.peek());
228
237
  this.at += 1;
229
238
  }
230
239
  /** A name: a bare word or a quoted identifier. */
@@ -232,7 +241,7 @@ class Cursor {
232
241
  const token = this.take();
233
242
  if (token.kind === 'word' || token.kind === 'id')
234
243
  return token.text;
235
- return fail();
244
+ return fail(token);
236
245
  }
237
246
  /** `name` or `schema.name`. */
238
247
  qualified() {
@@ -297,7 +306,7 @@ function expression(cursor, stop, options = {}) {
297
306
  if (token.kind === 'string' || token.kind === 'literal')
298
307
  continue;
299
308
  if (token.kind === 'id')
300
- fail();
309
+ fail(token);
301
310
  if (token.kind === 'op') {
302
311
  if (token.text === '(') {
303
312
  // A keyed read: one column of one row another table holds, found by NEW's or OLD's columns.
@@ -308,16 +317,16 @@ function expression(cursor, stop, options = {}) {
308
317
  }
309
318
  else if (token.text === ')') {
310
319
  if (!parens.pop())
311
- fail();
320
+ fail(token);
312
321
  }
313
322
  else if (token.text === ',')
314
323
  continue;
315
324
  else if (!OPERATORS.has(token.text))
316
- fail();
325
+ fail(token);
317
326
  continue;
318
327
  }
319
328
  // A quoted name here is a column of no row, or a string SQLite falls back to: not proven.
320
- const word = token.kind === 'word' ? token.upper : fail();
329
+ const word = token.kind === 'word' ? token.upper : fail(token);
321
330
  if (word === 'NEW' || word === 'OLD') {
322
331
  cursor.expectOp('.');
323
332
  cursor.name();
@@ -325,14 +334,14 @@ function expression(cursor, stop, options = {}) {
325
334
  else if (word === 'IN') {
326
335
  // After IN a bare word is a table to SQLite (`NEW.note IN TRUE` reads one named "true"): only a list is proven.
327
336
  if (cursor.op() !== '(')
328
- fail();
337
+ fail(cursor.peek());
329
338
  }
330
339
  else if (word === 'CASE') {
331
340
  cases += 1;
332
341
  }
333
342
  else if (word === 'END') {
334
343
  if (!cases)
335
- fail();
344
+ fail(token);
336
345
  cases -= 1;
337
346
  }
338
347
  else if (word === 'DISTINCT') {
@@ -342,7 +351,7 @@ function expression(cursor, stop, options = {}) {
342
351
  const isBefore = (before?.kind === 'word' && before.upper === 'IS') ||
343
352
  (before?.kind === 'word' && before.upper === 'NOT' && twoBefore?.kind === 'word' && twoBefore.upper === 'IS');
344
353
  if (!isBefore)
345
- fail();
354
+ fail(token);
346
355
  cursor.expectWord('FROM');
347
356
  }
348
357
  else if (word === 'CAST') {
@@ -351,7 +360,7 @@ function expression(cursor, stop, options = {}) {
351
360
  }
352
361
  else if (word === 'AS') {
353
362
  if (parens.at(-1) !== 'cast')
354
- fail();
363
+ fail(token);
355
364
  // A type name: words, and at most one `(n)` or `(n, m)`.
356
365
  if (cursor.peek()?.kind !== 'word')
357
366
  fail();
@@ -403,7 +412,7 @@ function expression(cursor, stop, options = {}) {
403
412
  options.own.add(token.text);
404
413
  }
405
414
  else {
406
- fail();
415
+ fail(token);
407
416
  }
408
417
  }
409
418
  }
@@ -560,6 +569,7 @@ function keyedDelete(cursor) {
560
569
  cursor.expectWord('WHERE');
561
570
  return { kind: 'delete', table, key: keyEqualities(cursor, table, (at) => at.op() === ';') };
562
571
  }
572
+ const DOES_MORE = 'it does more than check the row it fires on';
563
573
  /**
564
574
  * Why a body that is not validation-only is not, from the tokens it holds outside its literals: said as exactly as
565
575
  * they allow, since the owner reads it to know what to change. A subquery is not said to read a table it may not
@@ -590,7 +600,7 @@ function why(tokens) {
590
600
  };
591
601
  if (words.some((word) => word.upper === 'RAISE' && !literal(word)))
592
602
  return "its RAISE is not RAISE(ABORT, '…') with a literal message";
593
- return 'it does more than check the row it fires on';
603
+ return DOES_MORE;
594
604
  }
595
605
  /** The definition as tokens, one space apart: comments, spacing and keyword case never move the digest. */
596
606
  function digestOf(tokens) {
@@ -614,12 +624,17 @@ export function readSqliteTrigger(name, sql, generated) {
614
624
  const unknown = { name, events: EVERY, validates: false, why: 'its definition is not one mistri can read' };
615
625
  if (typeof sql !== 'string')
616
626
  return unknown;
627
+ // Where reading stopped, so the person who wrote the trigger can find the construct and rewrite it.
628
+ const stoppedAt = (error, cursor) => {
629
+ const stop = error instanceof Unreadable && error.stop !== undefined ? error.stop : cursor?.peek();
630
+ return stop === undefined ? 'the end of the definition' : located(sql, stop);
631
+ };
617
632
  let tokens;
618
633
  try {
619
634
  tokens = tokenize(sql);
620
635
  }
621
- catch {
622
- return unknown;
636
+ catch (error) {
637
+ return { ...unknown, why: `its definition has ${stoppedAt(error)}, which mistri does not read` };
623
638
  }
624
639
  const digest = digestOf(tokens);
625
640
  const cursor = new Cursor(tokens);
@@ -643,6 +658,7 @@ export function readSqliteTrigger(name, sql, generated) {
643
658
  cursor.at += 1;
644
659
  cursor.expectWord('OF');
645
660
  }
661
+ const eventToken = cursor.peek();
646
662
  const event = cursor.word();
647
663
  cursor.at += 1;
648
664
  if (event === 'INSERT')
@@ -664,18 +680,19 @@ export function readSqliteTrigger(name, sql, generated) {
664
680
  }
665
681
  }
666
682
  else
667
- return { ...unknown, digest };
683
+ return fail(eventToken);
668
684
  cursor.expectWord('ON');
669
685
  cursor.qualified();
670
686
  bodyStart = cursor.at;
671
687
  }
672
- catch {
673
- return { ...unknown, digest };
688
+ catch (error) {
689
+ return { ...unknown, digest, why: `its definition has ${stoppedAt(error, cursor)}, which mistri does not read` };
674
690
  }
675
691
  const fired = { name, events, ...(columns ? { columns } : {}), digest };
676
692
  try {
677
- if (tokens.slice(bodyStart).some((token) => (token.kind === 'word' || token.kind === 'id') && wide(token.text)))
678
- fail();
693
+ const past = tokens.slice(bodyStart).find((token) => (token.kind === 'word' || token.kind === 'id') && wide(token.text));
694
+ if (past)
695
+ fail(past);
679
696
  if (cursor.word() === 'FOR') {
680
697
  cursor.at += 1;
681
698
  cursor.expectWord('EACH');
@@ -722,10 +739,20 @@ export function readSqliteTrigger(name, sql, generated) {
722
739
  ...(plainDefinition(sql, tokens, 'sqlite') ? {} : { unaccepted: PLAIN }),
723
740
  };
724
741
  }
725
- catch {
726
- return { ...fired, validates: false, why: why(tokens.slice(bodyStart)) };
742
+ catch (error) {
743
+ const said = why(tokens.slice(bodyStart));
744
+ return { ...fired, validates: false, why: said === DOES_MORE ? `${said}: mistri does not read ${stoppedAt(error, cursor)}` : said };
727
745
  }
728
746
  }
747
+ /** A token, or the character at an offset, and its line and column: `` `!` at line 3, column 34 ``. */
748
+ function located(sql, stop) {
749
+ const at = typeof stop === 'number' ? stop : stop.at;
750
+ const shown = typeof stop === 'number' ? sql[at] : stop.kind === 'string' ? `'${stop.text}'` : stop.kind === 'id' ? `"${stop.text}"` : stop.text;
751
+ const before = sql.slice(0, at);
752
+ const line = before.split('\n').length;
753
+ const column = at - before.lastIndexOf('\n');
754
+ return `\`${shown.length > 40 ? `${shown.slice(0, 40)}…` : shown}\` at line ${line}, column ${column}`;
755
+ }
729
756
  /** Words that open a table constraint, never a column definition (a column so named must be quoted). */
730
757
  const TABLE_CONSTRAINTS = new Set(['CONSTRAINT', 'PRIMARY', 'UNIQUE', 'CHECK', 'FOREIGN']);
731
758
  /**
@@ -12,7 +12,7 @@ import { waitForLocks, assertIndependentWrite, writeToFile, prepareGuarded, tran
12
12
  import { captureScopeCatalog, mapScopeCatalog } from '../shared/scopeCatalog.js';
13
13
  import { DatabaseSync } from 'node:sqlite';
14
14
  import { isPromise, isProxy } from 'node:util/types';
15
- import { ANSI_STYLE, buildWhere, clampLimit, clampOffset, boundStatement, looksWrappable, dedupe, refuseStacked, trimStatement, Refusal, WriteConflict, TransactionOutcomeUnknown, Serial, TransactionOperations, queueRecordLock, recordLockFilter, refuseAnalyze, countStatement, countEachStatement, countEachRows, countsTogether, orderClause, } from './adapter.js';
15
+ import { ANSI_STYLE, buildWhere, clampLimit, clampOffset, boundStatement, looksWrappable, dedupe, refuseStacked, trimStatement, Refusal, WriteConflict, TransactionOutcomeUnknown, Serial, TransactionOperations, queueRecordLock, recordLockFilter, refuseAnalyze, countStatement, countEachStatement, countEachRows, countsTogether, orderClause, lanesStatement, withoutLaneRow, } from './adapter.js';
16
16
  import { compileAggregate } from '../shared/aggregate.js';
17
17
  import { readExactly, summed } from '../shared/decimals.js';
18
18
  import { buildUpdateGuard } from './scoped/updateGuard.js';
@@ -453,19 +453,23 @@ export class SqliteAdapter {
453
453
  const orderBy = request.orderBy;
454
454
  const meta = orderBy?.length ? (await this.schema().catch(() => undefined))?.tables.find((t) => t.id === request.table) : undefined;
455
455
  const order = orderClause(orderBy, meta, ANSI_STYLE, 1);
456
- const text = `SELECT ${cols} FROM ${ANSI_STYLE.quote(table)}${where.text}${order.text} LIMIT ? OFFSET ?`;
457
- const params = [...where.params, ...order.params, limit, offset];
456
+ const lanes = request.lanes;
457
+ const text = lanes
458
+ ? lanesStatement(ANSI_STYLE, { columns: cols, source: `${ANSI_STYLE.quote(table)}${where.text}`, lane: ANSI_STYLE.quote(lanes.by), order: order.text }, 0)
459
+ : `SELECT ${cols} FROM ${ANSI_STYLE.quote(table)}${where.text}${order.text} LIMIT ? OFFSET ?`;
460
+ const params = [...where.params, ...order.params, ...(lanes ? [clampLimit(lanes.limit)] : []), limit, offset];
458
461
  const started = performance.now();
459
462
  /* Integers past 2^53 are read as BigInt rather than throwing; valueSafe
460
463
  turns the safe ones back into numbers and keeps the rest as strings. */
461
464
  const rows = await this.readRows(text, params, false, db);
462
465
  const ms = Math.round(performance.now() - started);
463
- return {
466
+ const result = {
464
467
  columns: rows.length ? Object.keys(rows[0]) : (request.columns ?? []),
465
468
  rows: rows.map(request.binary ? withBytes : toJsonSafe),
466
469
  ms,
467
470
  sql: { text, params },
468
471
  };
472
+ return lanes ? withoutLaneRow(result) : result;
469
473
  }
470
474
  /**
471
475
  * Run a compiled view.
@@ -66,9 +66,14 @@ export declare function resourceChecks(full: DatabaseSchema, access: ColumnGrant
66
66
  orderBy?: {
67
67
  column: string;
68
68
  direction: 'asc' | 'desc';
69
+ nulls?: 'last';
69
70
  }[];
70
71
  limit: number;
71
72
  offset: number;
73
+ lanes?: {
74
+ by: string;
75
+ limit: number;
76
+ };
72
77
  binary?: true;
73
78
  storedText?: true;
74
79
  columns: string[];
@@ -133,6 +133,7 @@ export declare class Registry {
133
133
  private cacheWarnedAt;
134
134
  private readonly moduleState;
135
135
  private readonly rolesSaid;
136
+ private readonly journalSaid;
136
137
  /** A service from the config. Names are unique among services; a kind that is a singleton (the cache, the email service) is configured once. */
137
138
  addService(service: ServiceConfig): void;
138
139
  /** The entries of kinds this binary does not read (`others: 'keep'`), as written, for a save to put back. */
@@ -107,6 +107,8 @@ export class Registry {
107
107
  it again: the same paragraph every time an agent re-read the schema. Keyed by what was said, so a
108
108
  role that gained a grant since is still news. */
109
109
  rolesSaid = new Set();
110
+ /* What keepWriteJournal said about a connection's journal, once per run, as above. */
111
+ journalSaid = new Set();
110
112
  /** A service from the config. Names are unique among services; a kind that is a singleton (the cache, the email service) is configured once. */
111
113
  addService(service) {
112
114
  if (this.services.some((s) => s.name === service.name))
@@ -488,9 +490,17 @@ export class Registry {
488
490
  hook(entry.config.id, adapter);
489
491
  /* A writable connection keeps its Undo history in its database where the
490
492
  store is provisioned (journalStore.ts), read back before the first
491
- write; elsewhere it stays in memory, as it always has. */
493
+ write; elsewhere it stays in memory, as it always has, and in the
494
+ browser a database that could hold the store and does not is told once
495
+ per run: a dial again (a refresh, a reconnect) would say the same
496
+ paragraph. */
492
497
  if (entry.config.writable)
493
- await keepWriteJournal(entry.config.id, adapter, this.onWarning);
498
+ await keepWriteJournal(entry.config.id, adapter, (message) => {
499
+ if (this.journalSaid.has(message))
500
+ return;
501
+ this.journalSaid.add(message);
502
+ this.onWarning(message);
503
+ });
494
504
  /* Wrapped before it is stored, so nothing can reach the unwrapped one:
495
505
  the boundary is a property of the connection, not a rule each caller
496
506
  remembers. Introspection happens first because the catalog is the
@@ -812,7 +822,7 @@ export async function loadConfig(explicit, sections) {
812
822
  !['undefined', 'string'].includes(typeof migrations.table))) {
813
823
  throw new Error(`${path}: connection "${c.name}" has "migrations": ${JSON.stringify(migrations)}. It is { "dir": "<directory>" }, and "table" when the history table is not ${product()}'s own.`);
814
824
  }
815
- const http = httpConnectionOf(c, path);
825
+ const http = httpConnectionOf(c, path, Object.keys(sections.kinds));
816
826
  const pool = poolOf(c, http !== undefined, path);
817
827
  const replicas = replicasOf(c, http !== undefined, path);
818
828
  const listen = listenOf(c, http !== undefined, path);
@@ -78,6 +78,8 @@ export interface PaintRooms {
78
78
  me: Record<string, unknown>;
79
79
  runtime: Record<string, unknown>;
80
80
  ask: PaintAsk;
81
+ /** The request's Cookie header: how this browser last chose to read a room (an App's board), where it says. */
82
+ cookie?: string;
81
83
  }): Promise<{
82
84
  app: string;
83
85
  plan: RoomPlan;
@@ -89,7 +91,7 @@ export interface PaintRooms {
89
91
  * room asks (`rooms`, an App's: firstPaintRooms.ts). Each round's requests
90
92
  * are asked together.
91
93
  */
92
- export declare function firstPaint({ path, search, loop: asked, most, budget, rooms, }: {
94
+ export declare function firstPaint({ path, search, loop: asked, most, budget, rooms, cookie, }: {
93
95
  path: string;
94
96
  search: string;
95
97
  loop: Loop;
@@ -97,6 +99,8 @@ export declare function firstPaint({ path, search, loop: asked, most, budget, ro
97
99
  budget?: number;
98
100
  /** What the product's page asks past who-am-I and the runtime (PaintRooms), or nothing more. */
99
101
  rooms?: PaintRooms;
102
+ /** The request's Cookie header, handed to `rooms`. */
103
+ cookie?: string;
100
104
  }): Promise<FirstPaintPage>;
101
105
  /**
102
106
  * The data block, as the page carries it: JSON with every `<`, `>` and `&`
@@ -173,7 +173,7 @@ const chunks = (items, size) => {
173
173
  * room asks (`rooms`, an App's: firstPaintRooms.ts). Each round's requests
174
174
  * are asked together.
175
175
  */
176
- export async function firstPaint({ path, search, loop: asked, most = MOST, budget = BUDGET, rooms, }) {
176
+ export async function firstPaint({ path, search, loop: asked, most = MOST, budget = BUDGET, rooms, cookie, }) {
177
177
  const carried = new Carried();
178
178
  /* Past the budget every answer still out is none: the client asks it. */
179
179
  const controller = new AbortController();
@@ -193,14 +193,14 @@ export async function firstPaint({ path, search, loop: asked, most = MOST, budge
193
193
  return Promise.race([answer, late]);
194
194
  };
195
195
  try {
196
- return await answered(carried, path, search, loop, most, rooms);
196
+ return await answered(carried, path, search, loop, most, rooms, cookie);
197
197
  }
198
198
  finally {
199
199
  clearTimeout(timer);
200
200
  controller.abort();
201
201
  }
202
202
  }
203
- async function answered(carried, path, search, loop, most, rooms) {
203
+ async function answered(carried, path, search, loop, most, rooms, cookie) {
204
204
  const ask = async (method, target, body) => carried.read(method, target, body, await loop(method, target, body));
205
205
  const done = () => ({ json: carried.json(most) });
206
206
  /* A page that is all its own reads is asked for before who-am-I (boot.js). */
@@ -224,7 +224,7 @@ async function answered(carried, path, search, loop, most, rooms) {
224
224
  ]));
225
225
  if (!me || !runtime || !rooms)
226
226
  return done();
227
- const room = await rooms.room({ path, search, requested, me, runtime, ask });
227
+ const room = await rooms.room({ path, search, requested, me, runtime, ask, ...(cookie ? { cookie } : {}) });
228
228
  if (!room)
229
229
  return done();
230
230
  const { app: selected, plan } = room;