@mistriwork/engine 1.0.0-rc.14 → 1.0.0-rc.16

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 (60) hide show
  1. package/dist/adapters/cache/cacheStore.d.ts +1 -1
  2. package/dist/adapters/cache/cacheStore.js +1 -1
  3. package/dist/server/cli/startup.js +7 -2
  4. package/dist/server/cli/usage.d.ts +2 -0
  5. package/dist/server/cli/usage.js +12 -1
  6. package/dist/server/clientHttp.js +20 -3
  7. package/dist/server/config/profile.d.ts +10 -3
  8. package/dist/server/config/profile.js +28 -4
  9. package/dist/server/connections.js +1 -1
  10. package/dist/server/demo.js +3 -3
  11. package/dist/server/http/baselineHeaders.d.ts +13 -3
  12. package/dist/server/http/baselineHeaders.js +6 -0
  13. package/dist/server/http/frontDoor.d.ts +41 -0
  14. package/dist/server/http/frontDoor.js +36 -0
  15. package/dist/server/http/rateLimit.d.ts +2 -2
  16. package/dist/server/http/rateLimit.js +16 -2
  17. package/dist/server/http/recordCompose.d.ts +3 -0
  18. package/dist/server/http/recordCompose.js +1 -0
  19. package/dist/server/http/serverCore.d.ts +2 -2
  20. package/dist/server/http/serverCore.js +16 -32
  21. package/dist/server/migrate/cli.js +8 -1
  22. package/dist/server/migrate/desired.js +2 -2
  23. package/dist/server/migrate/target.d.ts +8 -0
  24. package/dist/server/migrate/target.js +15 -0
  25. package/dist/server/seed/seed.js +46 -11
  26. package/dist/server/startupWarnings.js +1 -1
  27. package/dist/shared/appCards.d.ts +4 -0
  28. package/dist/shared/appCards.js +5 -0
  29. package/dist/shared/breakdown.js +8 -2
  30. package/dist/shared/computed.d.ts +16 -2
  31. package/dist/shared/computed.js +23 -4
  32. package/dist/shared/computedValue.js +23 -0
  33. package/dist/shared/iconWeights.js +1 -0
  34. package/dist/shared/listPresentations.d.ts +9 -2
  35. package/dist/shared/listPresentations.js +2 -0
  36. package/dist/shared/listQuestion.d.ts +9 -0
  37. package/dist/shared/listQuestion.js +18 -0
  38. package/dist/shared/mentionLookup.js +3 -1
  39. package/dist/shared/page.d.ts +8 -1
  40. package/dist/shared/page.js +151 -7
  41. package/dist/shared/pageAnatomy.js +2 -0
  42. package/dist/shared/pageConfig.d.ts +1 -1
  43. package/dist/shared/pageConfig.js +106 -19
  44. package/dist/shared/pageModel.d.ts +62 -5
  45. package/dist/shared/pagePaths.js +4 -1
  46. package/dist/shared/pagePlans.d.ts +4 -2
  47. package/dist/shared/pagePlans.js +6 -4
  48. package/dist/shared/recordLabel.d.ts +12 -3
  49. package/dist/shared/recordLabel.js +39 -18
  50. package/dist/shared/recordReads.d.ts +6 -30
  51. package/dist/shared/recordReads.js +13 -1
  52. package/dist/shared/rollups.d.ts +4 -4
  53. package/dist/shared/rollups.js +1 -1
  54. package/dist/shared/schema.d.ts +7 -0
  55. package/dist/shared/schema.js +17 -0
  56. package/dist/shared/servedApp.d.ts +5 -1
  57. package/dist/shared/viewLabels.js +22 -17
  58. package/package.json +3 -2
  59. package/schema/model.d.ts +14 -5
  60. package/schema/theme.d.ts +1 -1
@@ -29,7 +29,7 @@ import { compareVersions, newMigration, readMigrations } from './files.js';
29
29
  import { FLYWAY_COMMANDS, runFlyway } from './flyway.js';
30
30
  import { migrateBaseline, migrateRepair, migrateUp, migrateValidate, migrationStatus, recordAround } from './runner.js';
31
31
  import { sayChange } from './snapshot.js';
32
- import { resolveTarget, targetDir } from './target.js';
32
+ import { migrateWriteRefusal, resolveTarget, targetDir } from './target.js';
33
33
  import { product } from '../product.js';
34
34
  import { claimContext, contextJson, contextLines, fromProfile, projectContext } from '../config/profile.js';
35
35
  const migrateUsage = () => product() +
@@ -89,6 +89,8 @@ export async function runMigrate(argv, write = (line) => process.stdout.write(`$
89
89
  'With --app, status prints the bootstrap plan (migrations, stores, sign-in store, seed, check) and up carries it out:\n' +
90
90
  ' the stores the App uses (--commands, --jobs, --policy name its modules), a PostgreSQL auth.store, then the check.\n' +
91
91
  ' validate --app exits 2 while any phase is pending. --json prints the plan as data.\n' +
92
+ 'up, baseline and repair write to the database: without --app, a connection a config names must say "writable": true\n' +
93
+ '(a database named with --db, and --dry-run, are not asked).\n' +
92
94
  'Exit codes: 1 could not run, 2 files and history disagree, 3 a migration failed, 4 applied but the App no longer checks,\n' +
93
95
  '5 a store step failed.\n' +
94
96
  'mistri docs migrate is the guide.');
@@ -278,6 +280,11 @@ export async function runMigrate(argv, write = (line) => process.stdout.write(`$
278
280
  return EXIT.ok;
279
281
  }
280
282
  const target = await resolveTarget(registry, config, flags, appDir, loaded?.binding);
283
+ /* An App's own database is the App's to migrate (`--app`): the plan it carries out is the deployment, and the
284
+ connection is the one it binds. The database browser, and a run with no App, answer to the flag. */
285
+ const refused = loaded ? undefined : migrateWriteRefusal(target.connection, command, dryRun);
286
+ if (refused)
287
+ return fail(refused);
281
288
  if (DESIRED.includes(command)) {
282
289
  const file = schemaFile !== undefined ? resolve(schemaFile) : defaultSchemaFile(target.dir);
283
290
  /* The first migration is schema.sql itself: written before any diff tool
@@ -136,7 +136,7 @@ async function scratch(url, devUrl) {
136
136
  if (devUrl !== undefined && dialectOf(devUrl) !== dialect)
137
137
  throw new Error(`--dev-url must be the same kind of database as the connection (${dialect}).`);
138
138
  if (dialect === 'sqlite') {
139
- const dir = mkdtempSync(join(tmpdir(), 'tablewalk-schema-'));
139
+ const dir = mkdtempSync(join(tmpdir(), `${product()}-schema-`));
140
140
  return {
141
141
  built: join(dir, 'built.db'),
142
142
  dev: 'sqlite://dev?mode=memory',
@@ -144,7 +144,7 @@ async function scratch(url, devUrl) {
144
144
  };
145
145
  }
146
146
  const server = devUrl ?? url;
147
- const stem = `tablewalk_schema_${process.pid}_${randomBytes(3).toString('hex')}`;
147
+ const stem = `${product()}_schema_${process.pid}_${randomBytes(3).toString('hex')}`;
148
148
  const names = [`${stem}_built`, `${stem}_dev`];
149
149
  const admin = await openMigrationDb(server, [], false);
150
150
  const at = (name) => {
@@ -27,6 +27,14 @@ interface ResolvedTarget extends MigrateTarget {
27
27
  }
28
28
  /** The connection the run is about: named, the App's home source's, or the only one. `tablewalk seed` reads it the same way. */
29
29
  export declare function targetConnection(registry: Registry, flags: Pick<TargetFlags, 'connection' | 'argument'>, binding?: TargetBinding, purpose?: string): ConnectionConfig;
30
+ /**
31
+ * Why a run may not write to this connection, or undefined. `"writable": true` is the config's statement about one
32
+ * named database (connections.ts), enforced wherever the server writes; migrate wrote DDL and its history table to a
33
+ * connection that said nothing of the kind. Only a connection a config names carries the flag: a database handed
34
+ * over on the command line (`<db>`, --db, DATABASE_URL) is the person's own act, as it always was, and a run that
35
+ * only prints a plan writes nothing, so neither is refused.
36
+ */
37
+ export declare function migrateWriteRefusal(connection: ConnectionConfig, command: string, dryRun?: boolean): string | undefined;
30
38
  /** The directory alone, for `migrate new`, which writes a file and opens no database. */
31
39
  export declare function targetDir(registry: Registry, config: StartupConfig, flags: TargetFlags, appDir?: string, binding?: TargetBinding): string;
32
40
  export declare function resolveTarget(registry: Registry, config: StartupConfig, flags: TargetFlags, appDir?: string, binding?: TargetBinding): Promise<ResolvedTarget>;
@@ -38,6 +38,21 @@ export function targetConnection(registry, flags, binding, purpose = 'migrate')
38
38
  throw new Error(`several connections are configured (${configs.map((config) => config.name).join(', ')}): name one with --connection.`);
39
39
  throw new Error(`no database to ${purpose}. Name it — a SQLite file, --db <url>, or --config <file> — or keep a ${configFileName()} beside where you run it.`);
40
40
  }
41
+ /** The commands that write DDL and history to the database; the rest only read it. */
42
+ const WRITING = new Set(['up', 'baseline', 'repair']);
43
+ /**
44
+ * Why a run may not write to this connection, or undefined. `"writable": true` is the config's statement about one
45
+ * named database (connections.ts), enforced wherever the server writes; migrate wrote DDL and its history table to a
46
+ * connection that said nothing of the kind. Only a connection a config names carries the flag: a database handed
47
+ * over on the command line (`<db>`, --db, DATABASE_URL) is the person's own act, as it always was, and a run that
48
+ * only prints a plan writes nothing, so neither is refused.
49
+ */
50
+ export function migrateWriteRefusal(connection, command, dryRun = false) {
51
+ if (!WRITING.has(command) || dryRun || connection.source !== 'config' || connection.writable)
52
+ return undefined;
53
+ return (`"${connection.name}" is not writable, and ${command} changes its schema and its history table. ` +
54
+ `Set "writable": true on the connection in ${configFileName()} to allow it, or name the database with --db <url> if it is one you mean to change.`);
55
+ }
41
56
  function directoryOf(connection, config, flags, appDir) {
42
57
  const base = config.configPath ? dirname(config.configPath) : process.cwd();
43
58
  return flags.migrations !== undefined
@@ -230,6 +230,9 @@ class Taken {
230
230
  function textFor(dice, plan, column, person, n, personLike, companyLike) {
231
231
  const name = column.name.toLowerCase();
232
232
  const table = plan.table;
233
+ /* A uuid column holds a uuid whatever it is called: `code` and `key` are words for text. */
234
+ if (/uuid/i.test(column.type))
235
+ return uuid(dice);
233
236
  if (/(^|_)e?mail(_address)?$/.test(name))
234
237
  return companyLike && !personLike
235
238
  ? `hello@${slug(person.company)}.example.com`
@@ -534,7 +537,7 @@ export async function seedDatabase(adapter, schema, opts) {
534
537
  if (column.nullable)
535
538
  values[column.name] = null;
536
539
  else
537
- ok = false;
540
+ throw new SeedRefusal(`${table.name}.${column.name} references ${ref.table}.${ref.column}, which has no rows to point at, so no ${table.name} row can be made.`);
538
541
  continue;
539
542
  }
540
543
  /* The first rows go one to each parent, so none is childless; the rest favor a few, as a busy doctor or a
@@ -548,8 +551,21 @@ export async function seedDatabase(adapter, schema, opts) {
548
551
  let order = ordersOf.get(column.name);
549
552
  if (!order)
550
553
  ordersOf.set(column.name, (order = crowd(dice, options)));
551
- if (!covering && (table.unique ?? []).includes(column.name)) {
552
- ok = false;
554
+ /* A reference that is unique (a primary key that is also a foreign key: a profile is its customer's) draws
555
+ each parent once, whatever the reference is called or typed; it is never made distinct by editing the key. */
556
+ if ((table.unique ?? []).includes(column.name) || (column.primaryKey && primary.length === 1)) {
557
+ const free = order.rows.find((row) => !taken.has(`${table.id}.${column.name}`, row[ref.column]));
558
+ if (!free) {
559
+ if (made > 0) {
560
+ ok = false;
561
+ continue;
562
+ }
563
+ throw new SeedRefusal(`${table.name}.${column.name} is unique and references ${ref.table}.${ref.column}, which has ${options.length} ${options.length === 1 ? 'row' : 'rows'}` +
564
+ ` and every one already has a ${table.name} row, so there is no key left to give it.` +
565
+ ' Add rows to the parent, or leave this table out.');
566
+ }
567
+ parents.set(column.name, free);
568
+ values[column.name] = free[ref.column];
553
569
  continue;
554
570
  }
555
571
  const parent = covering ? order.rows[made] : order.pick(dice);
@@ -690,30 +706,49 @@ export async function seedDatabase(adapter, schema, opts) {
690
706
  }
691
707
  }
692
708
  }
693
- /* Never repeat what must be unique: a second "Ada Lam" gets a number, a second pair is skipped. */
709
+ /* Never repeat what must be unique, in the column's own terms: a second "Ada Lam" gets a number, a uuid a fresh
710
+ uuid, a date the next day, an integer the next integer; a second pair is skipped. A reference is drawn above. */
694
711
  for (const column of table.unique ?? []) {
695
712
  const key = `${table.id}.${column}`;
696
713
  const value = values[column];
697
- if (value === undefined || value === null)
714
+ if (value === undefined || value === null || plan.refs.has(column))
698
715
  continue;
699
716
  if (!taken.has(key, value))
700
717
  continue;
718
+ const meta = table.columns.find((c) => c.name === column);
719
+ const kind = moment(meta, types);
701
720
  let changed;
702
721
  for (let k = 2; k < 1000 && changed === undefined; k++) {
703
- const next = typeof value === 'number'
704
- ? value + k * 1000
705
- : /@/.test(String(value))
706
- ? String(value).replace('@', `${k}@`)
707
- : `${value} ${k}`;
722
+ const next = /uuid/i.test(meta.type)
723
+ ? uuid(dice)
724
+ : kind
725
+ ? (() => {
726
+ const at = new Date(String(value));
727
+ return Number.isNaN(at.getTime()) ? undefined : written(new Date(at.getTime() + (k - 1) * DAY), kind);
728
+ })()
729
+ : typeof value === 'number'
730
+ ? value + (k - 1)
731
+ : /@/.test(String(value))
732
+ ? String(value).replace('@', `${k}@`)
733
+ : `${value} ${k}`;
734
+ if (next === undefined)
735
+ break;
708
736
  if (!taken.has(key, next))
709
737
  changed = next;
710
738
  }
739
+ if (changed === undefined && !meta.nullable)
740
+ throw new SeedRefusal(`${table.name}.${column} must be unique, and no new ${meta.type || 'value'} could be made that is not already taken.`);
711
741
  values[column] = changed ?? null;
712
742
  }
713
743
  const pairKeys = pairs.map(([a, b]) => `${table.id}.${a}+${b}`);
714
744
  if (pairs.some(([a, b], i) => values[a] != null && values[b] != null && taken.has(pairKeys[i], `${values[a]}\u0000${values[b]}`)))
715
745
  continue;
716
- const inserted = await tx.insert({ table: table.id, values });
746
+ const inserted = await tx.insert({ table: table.id, values }).catch((error) => {
747
+ if (error instanceof SeedRefusal)
748
+ throw error;
749
+ const column = error.column;
750
+ throw new SeedRefusal(`${table.name}${typeof column === 'string' && column ? `.${column}` : ''}: ${error instanceof Error ? error.message : String(error)}`);
751
+ });
717
752
  const row = { ...values, ...(inserted.row ?? {}), ...inserted.key };
718
753
  for (const column of table.unique ?? [])
719
754
  if (row[column] != null)
@@ -89,4 +89,4 @@ export const RESET_WITHOUT_MAIL_WARNING = 'password reset is offered (MISTRI_AUT
89
89
  'the link is never written to the log outside `mistri dev`. Name an email service in the config file, or unset MISTRI_AUTH_RESET.';
90
90
  /** A fetch-style host that names no client: every visitor is one address to the sign-in and form rate limits. */
91
91
  export const SHARED_RATE_LIMIT_WARNING = 'the fetch handler was called without a clientAddress, so every visitor shares one sign-in rate limit and lockout. ' +
92
- "Pass the visitor's address as the second argument: tw.fetch(request, { clientAddress }).";
92
+ "Pass the visitor's address as the second argument: mistri.fetch(request, { clientAddress }).";
@@ -69,6 +69,10 @@ export interface AppCardsList {
69
69
  }
70
70
  /** How many facts a card carries under its title. */
71
71
  export declare const CARDS_MAX_LINES = 4;
72
+ /** How many facts a record header says before the rest wait in its More: the first of them stay in the line. */
73
+ export declare const HEADER_FACTS_SHOWN = 6;
74
+ /** How many a header may name at all; past the line they fold, so an author never moves one into a side block to fit. */
75
+ export declare const HEADER_MAX_FACTS = 12;
72
76
  /** One sort key and its direction: "position", "added_at desc". */
73
77
  export declare function cardsImageSort(sort: string): {
74
78
  column: string;
@@ -27,6 +27,10 @@ import { singularOf } from './unitWord.js';
27
27
  export const CARDS_MAX_LINES = 4;
28
28
  /** How many values a card's facts line says. */
29
29
  const CARDS_MAX_FACTS = 6;
30
+ /** How many facts a record header says before the rest wait in its More: the first of them stay in the line. */
31
+ export const HEADER_FACTS_SHOWN = 6;
32
+ /** How many a header may name at all; past the line they fold, so an author never moves one into a side block to fit. */
33
+ export const HEADER_MAX_FACTS = 12;
30
34
  /** How many values stand along a card's foot. */
31
35
  const CARDS_MAX_FOOTER = 4;
32
36
  const CARDS_IMAGE_KEYS = ['about', 'from', 'whose', 'sort'];
@@ -88,6 +92,7 @@ export function parseFacts(raw, at, key = 'facts', max = CARDS_MAX_FACTS) {
88
92
  allowKeys(entry, ['column', 'before', 'after'], `${at}, ${key}`, {
89
93
  label: 'is now "after" (a word after the value) or "before" (a word before it)',
90
94
  position: 'is gone: "before" is a word before the value, "after" a word after it',
95
+ subtitle: 'is the second line of a headline figure (headline(figure, { subtitle })); a fact is one line, so its words go in "before" or "after"',
91
96
  });
92
97
  const path = entry.column.trim();
93
98
  const word = (value, which) => {
@@ -434,8 +434,14 @@ function compileBreakdownFresh(schema, def, style, offset, scope) {
434
434
  if (!names.has(o.name)) {
435
435
  throw new PathError(`Nothing called "${o.name}" is in this breakdown. It shows: ${[...names].join(', ')}.`, o.name, o.name, 0);
436
436
  }
437
- if (o.emptyLast)
438
- return `(${style.quote(o.name)} IS NULL) ASC`;
437
+ /* An output name is only a name when it stands alone in ORDER BY: inside an expression PostgreSQL reads it as an
438
+ input column, and a key (`hold_id`) that the joined record also has (`holds.hold_id`) is ambiguous there. The
439
+ expression the SELECT list gives that name is what is tested. */
440
+ if (o.emptyLast) {
441
+ const shown = select[columns.findIndex((c) => c.name === o.name)] ?? style.quote(o.name);
442
+ const quoted = ` AS ${style.quote(o.name)}`;
443
+ return `(${shown.endsWith(quoted) ? shown.slice(0, -quoted.length) : shown} IS NULL) ASC`;
444
+ }
439
445
  /* An exact sum is text; it sorts by the integer it says, never as text ("9.00" after "10.00"). */
440
446
  return `${orderedBy.get(o.name) ?? style.quote(o.name)} ${o.direction === 'desc' ? 'DESC' : 'ASC'}`;
441
447
  });
@@ -34,7 +34,7 @@
34
34
  */
35
35
  import type { SqlStyle } from './filter.js';
36
36
  import type { ColumnMeta, DatabaseSchema } from './schema.js';
37
- export declare const COMPUTED_TYPES: readonly ['boolean', 'number', 'date'];
37
+ export declare const COMPUTED_TYPES: readonly ['boolean', 'number', 'date', 'text'];
38
38
  export type ComputedType = (typeof COMPUTED_TYPES)[number];
39
39
  /** A span a date moves by: `3 days`, `term months`. */
40
40
  export type ComputedUnit = ComputedSpan['unit'];
@@ -78,12 +78,26 @@ export type ComputedNode = {
78
78
  k: 'call';
79
79
  fn: ComputedFunction;
80
80
  args: ComputedNode[];
81
- } | {
81
+ } | ComputedJson | {
82
82
  k: 'op';
83
83
  op: ComputedOp;
84
84
  left: ComputedNode;
85
85
  right: ComputedNode;
86
86
  };
87
+ /**
88
+ * A member of a JSON column, read as text: `filters_json.make`, `payload.customer.city`. Authored as a path; the
89
+ * catalog that knows `filters_json` is a JSON column, and no reference, makes it this (appComputed.ts). Each key is
90
+ * an identifier (the tokenizer's word), so it is written into the statement's path literal as it stands. The
91
+ * database is never asked whether the member is there: one that is not has no value.
92
+ * @preview
93
+ */
94
+ export interface ComputedJson {
95
+ k: 'json';
96
+ /** The table's own JSON column. */
97
+ column: string;
98
+ /** The members walked, outermost first. */
99
+ keys: string[];
100
+ }
87
101
  /** The functions an expression may call (computedParse.ts COMPUTED_FUNCTIONS): each written alike on every engine and worked out alike in JavaScript. */
88
102
  export type ComputedFunction = 'coalesce' | 'round' | 'min' | 'max' | 'abs';
89
103
  interface ComputedSpan {
@@ -1,5 +1,5 @@
1
1
  import { noteRead } from './query.js';
2
- export const COMPUTED_TYPES = ['boolean', 'number', 'date'];
2
+ export const COMPUTED_TYPES = ['boolean', 'number', 'date', 'text'];
3
3
  /** What a computed column is to anything that reads columns: typed, never written, never a key. */
4
4
  export function computedColumnMeta(name, computed) {
5
5
  return {
@@ -43,9 +43,10 @@ export class ComputedError extends Error {
43
43
  export function computedReads(node) {
44
44
  const out = [];
45
45
  const walk = (n) => {
46
- if (n.k === 'path') {
47
- if (!out.includes(n.path))
48
- out.push(n.path);
46
+ if (n.k === 'path' || n.k === 'json') {
47
+ const path = n.k === 'path' ? n.path : n.column;
48
+ if (!out.includes(path))
49
+ out.push(path);
49
50
  return;
50
51
  }
51
52
  if (n.k === 'op') {
@@ -78,6 +79,10 @@ export function computedKind(node, kindOf) {
78
79
  }
79
80
  case 'number':
80
81
  return 'number';
82
+ case 'json':
83
+ if (!kindOf(n.column))
84
+ throw new Error(`"${n.column}" is not a column here.`);
85
+ return 'text';
81
86
  case 'text':
82
87
  case 'me':
83
88
  case 'nobody':
@@ -257,6 +262,8 @@ export function compileComputed(computed, style, column, kindOf) {
257
262
  switch (n.k) {
258
263
  case 'path':
259
264
  return column(n.path);
265
+ case 'json':
266
+ return jsonMember(column(n.column), n.keys, dialect);
260
267
  case 'number':
261
268
  return String(n.value);
262
269
  case 'text':
@@ -350,6 +357,18 @@ export function compileComputed(computed, style, column, kindOf) {
350
357
  }
351
358
  return sql(computed.expr);
352
359
  }
360
+ /**
361
+ * A JSON member as text, alike on both engines: PostgreSQL's `#>>` gives text, and a yes-or-no as `true`/`false`;
362
+ * SQLite's `json_extract` gives a number as a number and a yes-or-no as 1 or 0, so it is said as PostgreSQL says it.
363
+ * An object or an array is its JSON text on both. The keys are identifiers (ComputedJson), never quoted text.
364
+ */
365
+ function jsonMember(column, keys, dialect) {
366
+ if (dialect === 'postgres')
367
+ return `(${column} #>> '{${keys.join(',')}}')`;
368
+ const at = `'$${keys.map((key) => `."${key}"`).join('')}'`;
369
+ return (`(CASE json_type(${column}, ${at}) WHEN 'true' THEN 'true' WHEN 'false' THEN 'false' ` +
370
+ `ELSE CAST(json_extract(${column}, ${at}) AS TEXT) END)`);
371
+ }
353
372
  /** Whether an expression says the instant (`now`), so it comes to a moment rather than a day. */
354
373
  export function readsNow(node) {
355
374
  if (node.k === 'now' || node.k === 'instant')
@@ -120,6 +120,8 @@ export function evaluateComputed(node, values, read = (path) => {
120
120
  const v = read(n.path);
121
121
  return v === undefined || v === '' ? null : v;
122
122
  }
123
+ case 'json':
124
+ return jsonMember(read(n.column), n.keys);
123
125
  case 'op': {
124
126
  if (n.op === 'and' || n.op === 'or') {
125
127
  const l = truth(value(n.left)), r = truth(value(n.right));
@@ -168,6 +170,27 @@ export function evaluateComputed(node, values, read = (path) => {
168
170
  const result = value(node);
169
171
  return result === null && isCondition(node) ? false : result;
170
172
  }
173
+ /* A JSON member as the statement reads it (computed.ts jsonMember): text, a yes-or-no as `true`/`false`, an object
174
+ or an array as its JSON. The column arrives as its text (SQLite) or already read (PostgreSQL's driver). */
175
+ function jsonMember(stored, keys) {
176
+ let at = stored;
177
+ if (typeof at === 'string') {
178
+ try {
179
+ at = JSON.parse(at);
180
+ }
181
+ catch {
182
+ return null;
183
+ }
184
+ }
185
+ for (const key of keys) {
186
+ if (at === null || typeof at !== 'object' || Array.isArray(at) || !Object.hasOwn(at, key))
187
+ return null;
188
+ at = at[key];
189
+ }
190
+ if (at === null || at === undefined)
191
+ return null;
192
+ return typeof at === 'object' ? JSON.stringify(at) : String(at);
193
+ }
171
194
  /**
172
195
  * A constant expression's value, worked out where a fixed action's `set`
173
196
  * writes it (appStamps on the server): `today + 14 days`, `now + 1 week`,
@@ -12,6 +12,7 @@ export const ICON_WEIGHT_GLYPHS = {
12
12
  'calendar-blank': [["M208,34H182V24a6,6,0,0,0-12,0V34H86V24a6,6,0,0,0-12,0V34H48A14,14,0,0,0,34,48V208a14,14,0,0,0,14,14H208a14,14,0,0,0,14-14V48A14,14,0,0,0,208,34ZM48,46H74V56a6,6,0,0,0,12,0V46h84V56a6,6,0,0,0,12,0V46h26a2,2,0,0,1,2,2V82H46V48A2,2,0,0,1,48,46ZM208,210H48a2,2,0,0,1-2-2V94H210V208A2,2,0,0,1,208,210Z"], ["M208,32H184V24a8,8,0,0,0-16,0v8H88V24a8,8,0,0,0-16,0v8H48A16,16,0,0,0,32,48V208a16,16,0,0,0,16,16H208a16,16,0,0,0,16-16V48A16,16,0,0,0,208,32Zm0,48H48V48H72v8a8,8,0,0,0,16,0V48h80v8a8,8,0,0,0,16,0V48h24Z"]],
13
13
  'bell': [["M220.07,176.94C214.41,167.2,206,139.73,206,104a78,78,0,1,0-156,0c0,35.74-8.42,63.2-14.08,72.94A14,14,0,0,0,48,198H90.48a38,38,0,0,0,75,0H208a14,14,0,0,0,12.06-21.06ZM128,218a26,26,0,0,1-25.29-20h50.58A26,26,0,0,1,128,218Zm81.71-33a1.9,1.9,0,0,1-1.7,1H48a1.9,1.9,0,0,1-1.7-1,2,2,0,0,1,0-2C53.87,170,62,139.69,62,104a66,66,0,1,1,132,0c0,35.68,8.14,65.95,15.71,79A2,2,0,0,1,209.71,185Z"], ["M221.8,175.94C216.25,166.38,208,139.33,208,104a80,80,0,1,0-160,0c0,35.34-8.26,62.38-13.81,71.94A16,16,0,0,0,48,200H88.81a40,40,0,0,0,78.38,0H208a16,16,0,0,0,13.8-24.06ZM128,216a24,24,0,0,1-22.62-16h45.24A24,24,0,0,1,128,216Z"]],
14
14
  'check-circle': [["M172.24,99.76a6,6,0,0,1,0,8.48l-56,56a6,6,0,0,1-8.48,0l-24-24a6,6,0,0,1,8.48-8.48L112,151.51l51.76-51.75A6,6,0,0,1,172.24,99.76ZM230,128A102,102,0,1,1,128,26,102.12,102.12,0,0,1,230,128Zm-12,0a90,90,0,1,0-90,90A90.1,90.1,0,0,0,218,128Z"], ["M128,24A104,104,0,1,0,232,128,104.11,104.11,0,0,0,128,24Zm45.66,85.66-56,56a8,8,0,0,1-11.32,0l-24-24a8,8,0,0,1,11.32-11.32L112,148.69l50.34-50.35a8,8,0,0,1,11.32,11.32Z"]],
15
+ 'copy': [["M216,34H88a6,6,0,0,0-6,6V82H40a6,6,0,0,0-6,6V216a6,6,0,0,0,6,6H168a6,6,0,0,0,6-6V174h42a6,6,0,0,0,6-6V40A6,6,0,0,0,216,34ZM162,210H46V94H162Zm48-48H174V88a6,6,0,0,0-6-6H94V46H210Z"], ["M216,32H88a8,8,0,0,0-8,8V80H40a8,8,0,0,0-8,8V216a8,8,0,0,0,8,8H168a8,8,0,0,0,8-8V176h40a8,8,0,0,0,8-8V40A8,8,0,0,0,216,32Zm-8,128H176V88a8,8,0,0,0-8-8H96V48H208Z"]],
15
16
  'gear': [["M128,82a46,46,0,1,0,46,46A46.06,46.06,0,0,0,128,82Zm0,80a34,34,0,1,1,34-34A34,34,0,0,1,128,162ZM214,130.84c.06-1.89.06-3.79,0-5.68L229.33,106a6,6,0,0,0,1.11-5.29A105.34,105.34,0,0,0,219.76,74.9a6,6,0,0,0-4.53-3l-24.45-2.71q-1.93-2.07-4-4l-2.72-24.46a6,6,0,0,0-3-4.53,105.65,105.65,0,0,0-25.77-10.66A6,6,0,0,0,150,26.68l-19.2,15.37c-1.89-.06-3.79-.06-5.68,0L106,26.67a6,6,0,0,0-5.29-1.11A105.34,105.34,0,0,0,74.9,36.24a6,6,0,0,0-3,4.53L69.23,65.22q-2.07,1.94-4,4L40.76,72a6,6,0,0,0-4.53,3,105.65,105.65,0,0,0-10.66,25.77A6,6,0,0,0,26.68,106l15.37,19.2c-.06,1.89-.06,3.79,0,5.68L26.67,150.05a6,6,0,0,0-1.11,5.29A105.34,105.34,0,0,0,36.24,181.1a6,6,0,0,0,4.53,3l24.45,2.71q1.94,2.07,4,4L72,215.24a6,6,0,0,0,3,4.53,105.65,105.65,0,0,0,25.77,10.66,6,6,0,0,0,5.29-1.11L125.16,214c1.89.06,3.79.06,5.68,0l19.21,15.38a6,6,0,0,0,3.75,1.31,6.2,6.2,0,0,0,1.54-.2,105.34,105.34,0,0,0,25.76-10.68,6,6,0,0,0,3-4.53l2.71-24.45q2.07-1.93,4-4l24.46-2.72a6,6,0,0,0,4.53-3,105.49,105.49,0,0,0,10.66-25.77,6,6,0,0,0-1.11-5.29Zm-3.1,41.63-23.64,2.63a6,6,0,0,0-3.82,2,75.14,75.14,0,0,1-6.31,6.31,6,6,0,0,0-2,3.82l-2.63,23.63A94.28,94.28,0,0,1,155.14,218l-18.57-14.86a6,6,0,0,0-3.75-1.31h-.36a78.07,78.07,0,0,1-8.92,0,6,6,0,0,0-4.11,1.3L100.87,218a94.13,94.13,0,0,1-17.34-7.17L80.9,187.21a6,6,0,0,0-2-3.82,75.14,75.14,0,0,1-6.31-6.31,6,6,0,0,0-3.82-2l-23.63-2.63A94.28,94.28,0,0,1,38,155.14l14.86-18.57a6,6,0,0,0,1.3-4.11,78.07,78.07,0,0,1,0-8.92,6,6,0,0,0-1.3-4.11L38,100.87a94.13,94.13,0,0,1,7.17-17.34L68.79,80.9a6,6,0,0,0,3.82-2,75.14,75.14,0,0,1,6.31-6.31,6,6,0,0,0,2-3.82l2.63-23.63A94.28,94.28,0,0,1,100.86,38l18.57,14.86a6,6,0,0,0,4.11,1.3,78.07,78.07,0,0,1,8.92,0,6,6,0,0,0,4.11-1.3L155.13,38a94.13,94.13,0,0,1,17.34,7.17l2.63,23.64a6,6,0,0,0,2,3.82,75.14,75.14,0,0,1,6.31,6.31,6,6,0,0,0,3.82,2l23.63,2.63A94.28,94.28,0,0,1,218,100.86l-14.86,18.57a6,6,0,0,0-1.3,4.11,78.07,78.07,0,0,1,0,8.92,6,6,0,0,0,1.3,4.11L218,155.13A94.13,94.13,0,0,1,210.85,172.47Z"], ["M216,130.16q.06-2.16,0-4.32l14.92-18.64a8,8,0,0,0,1.48-7.06,107.6,107.6,0,0,0-10.88-26.25,8,8,0,0,0-6-3.93l-23.72-2.64q-1.48-1.56-3-3L186,40.54a8,8,0,0,0-3.94-6,107.29,107.29,0,0,0-26.25-10.86,8,8,0,0,0-7.06,1.48L130.16,40Q128,40,125.84,40L107.2,25.11a8,8,0,0,0-7.06-1.48A107.6,107.6,0,0,0,73.89,34.51a8,8,0,0,0-3.93,6L67.32,64.27q-1.56,1.49-3,3L40.54,70a8,8,0,0,0-6,3.94,107.71,107.71,0,0,0-10.87,26.25,8,8,0,0,0,1.49,7.06L40,125.84Q40,128,40,130.16L25.11,148.8a8,8,0,0,0-1.48,7.06,107.6,107.6,0,0,0,10.88,26.25,8,8,0,0,0,6,3.93l23.72,2.64q1.49,1.56,3,3L70,215.46a8,8,0,0,0,3.94,6,107.71,107.71,0,0,0,26.25,10.87,8,8,0,0,0,7.06-1.49L125.84,216q2.16.06,4.32,0l18.64,14.92a8,8,0,0,0,7.06,1.48,107.21,107.21,0,0,0,26.25-10.88,8,8,0,0,0,3.93-6l2.64-23.72q1.56-1.48,3-3L215.46,186a8,8,0,0,0,6-3.94,107.71,107.71,0,0,0,10.87-26.25,8,8,0,0,0-1.49-7.06ZM128,168a40,40,0,1,1,40-40A40,40,0,0,1,128,168Z"]],
16
17
  'list': [["M222,128a6,6,0,0,1-6,6H40a6,6,0,0,1,0-12H216A6,6,0,0,1,222,128ZM40,70H216a6,6,0,0,0,0-12H40a6,6,0,0,0,0,12ZM216,186H40a6,6,0,0,0,0,12H216a6,6,0,0,0,0-12Z"], ["M208,32H48A16,16,0,0,0,32,48V208a16,16,0,0,0,16,16H208a16,16,0,0,0,16-16V48A16,16,0,0,0,208,32ZM192,184H64a8,8,0,0,1,0-16H192a8,8,0,0,1,0,16Zm0-48H64a8,8,0,0,1,0-16H192a8,8,0,0,1,0,16Zm0-48H64a8,8,0,0,1,0-16H192a8,8,0,0,1,0,16Z"]],
17
18
  'lock': [["M208,82H174V56a46,46,0,0,0-92,0V82H48A14,14,0,0,0,34,96V208a14,14,0,0,0,14,14H208a14,14,0,0,0,14-14V96A14,14,0,0,0,208,82ZM94,56a34,34,0,0,1,68,0V82H94ZM210,208a2,2,0,0,1-2,2H48a2,2,0,0,1-2-2V96a2,2,0,0,1,2-2H208a2,2,0,0,1,2,2Zm-72-56a10,10,0,1,1-10-10A10,10,0,0,1,138,152Z"], ["M208,80H176V56a48,48,0,0,0-96,0V80H48A16,16,0,0,0,32,96V208a16,16,0,0,0,16,16H208a16,16,0,0,0,16-16V96A16,16,0,0,0,208,80Zm-80,84a12,12,0,1,1,12-12A12,12,0,0,1,128,164Zm32-84H96V56a32,32,0,0,1,64,0Z"]],
@@ -124,13 +124,17 @@ export interface AppBoardFact {
124
124
  icon?: AppIconName;
125
125
  }
126
126
  /** A month of records by one date column, each on its day — or, with `end`, across the days from one date
127
- column to another; a phone reads it as an agenda. */
127
+ column to another; a phone reads it as an agenda. With a `span` of a week or a day, a time grid: the hours down
128
+ the side, a record placed by its start and end. */
128
129
  export interface AppCalendarList {
129
130
  kind: 'calendar';
130
131
  /** The date (or timestamp) column a record sits on, or starts on when it has an `end`. */
131
132
  date: string;
132
- /** A local date column holding a record's last day: the record is drawn across every day between. */
133
+ /** A local date column holding a record's last day: the record is drawn across every day between. In a week or a
134
+ day, a timestamp column holding the record's end, so it is drawn from its start to it. */
133
135
  end?: string;
136
+ /** What one page of the calendar shows: a week or a day as a time grid, read by timestamp columns. Absent, a month. */
137
+ span?: Exclude<CalendarSpan, 'month'>;
134
138
  /** A local text column or one-hop reference label path. */
135
139
  primary: string;
136
140
  /** At most one local column or one-hop label path beneath the primary. */
@@ -153,6 +157,9 @@ export interface AppCalendarBackdrop {
153
157
  /** The span's shade, in a display tone; absent, the quiet neutral. */
154
158
  tone?: (typeof DISPLAY_TONES)[number];
155
159
  }
160
+ /** What one page of a calendar shows: a month of days, or a week or a day of hours. */
161
+ export declare const CALENDAR_SPANS: readonly ['month', 'week', 'day'];
162
+ export type CalendarSpan = (typeof CALENDAR_SPANS)[number];
156
163
  /** How many kinds of span one calendar shades. */
157
164
  export declare const CALENDAR_BACKDROPS_MAX = 4;
158
165
  /** Records as the tree their parent column makes: a root per record with no parent, its children folded under it. */
@@ -1,4 +1,6 @@
1
1
  import { DISPLAY_TONES } from './model.js';
2
+ /** What one page of a calendar shows: a month of days, or a week or a day of hours. */
3
+ export const CALENDAR_SPANS = ['month', 'week', 'day'];
2
4
  /** How many kinds of span one calendar shades. */
3
5
  export const CALENDAR_BACKDROPS_MAX = 4;
4
6
  /** 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. */
@@ -28,6 +28,8 @@ export interface ListQuestionSpec {
28
28
  editable?: true | string[];
29
29
  /** The reference column whose record a row opens (a view's `opens`): fetched, as the row's key is, to open it. */
30
30
  opens?: string;
31
+ /** The column a grid's rows are grouped by (a view's `group`): fetched, shown or not, as each row's head is read from it. */
32
+ group?: string;
31
33
  }
32
34
  /**
33
35
  * The references among a list's columns, each with the path its record's
@@ -48,6 +50,13 @@ export declare function listReferenceNames(rules: ListRules, spec: Pick<ListQues
48
50
  export declare function showClause(rules: ListRules, spec: ListQuestionSpec): string;
49
51
  export declare const limitClause: (spec: Pick<ListQuestionSpec, 'limit'>) => string;
50
52
  export declare const orderClause: (spec: Pick<ListQuestionSpec, 'order' | 'sort'>) => string;
53
+ /**
54
+ * A grouped list's question as its rows are read: ordered by the group's column before anything else, so each
55
+ * group's rows stand together under its head, and then as the list or its reader sorts them. A sort the reader gave
56
+ * the group's column keeps its direction, at the front. Said only to the read (TableRoom.tsx, firstPaintRooms.ts):
57
+ * the bar, the address and the tab a list lights stay the reader's own question.
58
+ */
59
+ export declare function groupedQuestion(question: string, column: string): string;
51
60
  export declare function withOrder(spec: Pick<ListQuestionSpec, 'table' | 'order' | 'sort'>, question: string): string;
52
61
  /** The full query text one of the app's tabs stands for. A module's
53
62
  standing conditions come first; a tab refines on top of them. */
@@ -112,6 +112,7 @@ export function showClause(rules, spec) {
112
112
  ...subtitleColumns(rules, spec.table, names),
113
113
  ...editedReferences(rules, spec),
114
114
  ...(spec.opens ? [spec.opens] : []),
115
+ ...(spec.group ? [spec.group] : []),
115
116
  ...primaryKey(t),
116
117
  ]),
117
118
  ].join(', ')}`;
@@ -124,6 +125,23 @@ export const limitClause = (spec) => (spec.limit ? `limit ${spec.limit}` : '');
124
125
  folded out of what the bar shows. A view's default `sort` is the same
125
126
  reading, said in the query bar's words. */
126
127
  export const orderClause = (spec) => spec.order ? `sort ${spec.order}` : spec.sort ? `sort ${spec.sort}` : '';
128
+ /**
129
+ * A grouped list's question as its rows are read: ordered by the group's column before anything else, so each
130
+ * group's rows stand together under its head, and then as the list or its reader sorts them. A sort the reader gave
131
+ * the group's column keeps its direction, at the front. Said only to the read (TableRoom.tsx, firstPaintRooms.ts):
132
+ * the bar, the address and the tab a list lights stay the reader's own question.
133
+ */
134
+ export function groupedQuestion(question, column) {
135
+ const parts = splitQuery(question);
136
+ const fields = (parts.sort ?? '')
137
+ .replace(/^(?:sort|order\s+by)\s+/i, '')
138
+ .split(',')
139
+ .map((field) => field.trim())
140
+ .filter(Boolean);
141
+ const own = fields.find((field) => field.split(/\s+/)[0] === column);
142
+ const sort = `sort ${[own ?? column, ...fields.filter((field) => field !== own)].join(', ')}`;
143
+ return [parts.table, parts.filter, parts.show, sort, parts.limit].filter((part) => part && part.trim()).join(' ');
144
+ }
127
145
  export function withOrder(spec, question) {
128
146
  const order = orderClause(spec);
129
147
  return order && !splitQuery(`${spec.table} ${question}`).sort ? squeeze(`${question} ${order}`) : question;
@@ -24,7 +24,9 @@ export function mentionLabelText(source, row) {
24
24
  if ('problem' in parsed)
25
25
  return '';
26
26
  return parsed.parts
27
- .map((part) => ('text' in part ? part.text : said(row[part.path])))
27
+ .map((part) => 'text' in part
28
+ ? part.text
29
+ : ([part.path, ...(part.or ?? []).map((other) => other.path)].map((path) => said(row[path])).find(Boolean) ?? ''))
28
30
  .join('')
29
31
  .replace(/\s+/g, ' ')
30
32
  .trim();
@@ -2,7 +2,7 @@ import { type DatabaseSchema } from './schema.js';
2
2
  import { type BreakdownDef, type BreakdownKey } from './breakdown.js';
3
3
  import type { AuthoredPage, AuthoredSection } from '../../schema/model.js';
4
4
  import { type PageCustomTotal } from './appComponents.js';
5
- import { type PageBreakdown, type PageChart, type PageSection, type PageTimeline, type PageDef } from './pageModel.js';
5
+ import { type PageBreakdown, type PageChart, type PageSection, type PageTimeline, type TimelineStream, type TimelineReactions, type PageDef } from './pageModel.js';
6
6
  import { type OfKeys } from './pagePaths.js';
7
7
  export { type SectionWidth, WIDTHS, type SectionOffset, OFFSETS, type SectionRegion, SECTION_REGIONS, FIELD_COLUMNS, type ResolvedWorkflowStatus, type PageFields, type PageMetric, type PageList, type PageBreakdown, type PageChart, type TimelineStream, type PageTimeline, type PageSection, type PageDef, timelineMarkColumns, DEFAULT_PAGE_LIST_LIMIT, MAX_PAGE_SECTIONS, } from './pageModel.js';
8
8
  export { resolvePath, rootFilter, resolveOf, type OfKeys, listView, sectionQuery, metricView, defaultColumns, customReadView, } from './pagePaths.js';
@@ -16,6 +16,13 @@ export { parsePagesConfig, PAGE_KEYS, PAGE_SECTION_KEYS, STREAM_KEYS, parsePageB
16
16
  * always describe some connection someone is not on.
17
17
  */
18
18
  export declare function validatePage(schema: DatabaseSchema, page: PageDef): PageDef;
19
+ /**
20
+ * The column of a stream's table a timeline's reactions name its entries by — the column `whose` refers to — or
21
+ * undefined where the stream's rows are not what the reactions are to. Read on a validated page, whose `whose` is named.
22
+ */
23
+ export declare function reactedColumn(schema: DatabaseSchema, section: Pick<PageTimeline, 'reactions'>, stream: Pick<TimelineStream, 'from'>): string | undefined;
24
+ /** The table a timeline's reactions are to: the one `whose` refers to, on a validated page. */
25
+ export declare function reactionEntryTable(schema: DatabaseSchema, reactions: Pick<TimelineReactions, 'from' | 'whose'>): string | undefined;
19
26
  /**
20
27
  * Which rows of each timeline stream are the other party's: the condition each stream is asked, with `record.` reached
21
28
  * by the stream's way back to the record, or undefined for a stream that has no far side. A stream's own `side` is its