@mistriwork/engine 1.0.0-rc.15 → 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 (43) hide show
  1. package/dist/server/cli/startup.js +7 -2
  2. package/dist/server/cli/usage.d.ts +2 -0
  3. package/dist/server/cli/usage.js +12 -1
  4. package/dist/server/clientHttp.js +20 -3
  5. package/dist/server/config/profile.d.ts +10 -3
  6. package/dist/server/config/profile.js +28 -4
  7. package/dist/server/http/baselineHeaders.d.ts +13 -3
  8. package/dist/server/http/baselineHeaders.js +6 -0
  9. package/dist/server/http/frontDoor.d.ts +41 -0
  10. package/dist/server/http/frontDoor.js +36 -0
  11. package/dist/server/http/recordCompose.d.ts +3 -0
  12. package/dist/server/http/recordCompose.js +1 -0
  13. package/dist/server/http/serverCore.d.ts +2 -2
  14. package/dist/server/http/serverCore.js +16 -32
  15. package/dist/server/migrate/cli.js +8 -1
  16. package/dist/server/migrate/target.d.ts +8 -0
  17. package/dist/server/migrate/target.js +15 -0
  18. package/dist/server/seed/seed.js +46 -11
  19. package/dist/shared/appCards.d.ts +4 -0
  20. package/dist/shared/appCards.js +5 -0
  21. package/dist/shared/breakdown.js +8 -2
  22. package/dist/shared/computed.d.ts +16 -2
  23. package/dist/shared/computed.js +23 -4
  24. package/dist/shared/computedValue.js +23 -0
  25. package/dist/shared/mentionLookup.js +3 -1
  26. package/dist/shared/page.js +23 -5
  27. package/dist/shared/pageAnatomy.js +2 -0
  28. package/dist/shared/pageConfig.d.ts +1 -1
  29. package/dist/shared/pageConfig.js +68 -18
  30. package/dist/shared/pageModel.d.ts +42 -5
  31. package/dist/shared/pagePaths.js +4 -1
  32. package/dist/shared/pagePlans.d.ts +2 -2
  33. package/dist/shared/pagePlans.js +3 -3
  34. package/dist/shared/recordLabel.d.ts +12 -3
  35. package/dist/shared/recordLabel.js +39 -18
  36. package/dist/shared/recordReads.d.ts +6 -30
  37. package/dist/shared/recordReads.js +13 -1
  38. package/dist/shared/rollups.d.ts +4 -4
  39. package/dist/shared/rollups.js +1 -1
  40. package/dist/shared/schema.d.ts +7 -0
  41. package/dist/shared/schema.js +17 -0
  42. package/dist/shared/viewLabels.js +22 -17
  43. package/package.json +3 -2
@@ -1,32 +1,3 @@
1
- /**
2
- * What a record page reads, as the requests its sections send.
3
- *
4
- * A record page used to reach its reader in three waves of requests — the
5
- * root and the plan, then a request or two per section, then the history —
6
- * and at 20 ms to the database each wave was a round of admissions and
7
- * statements: a lender's loan finished at 0.9 s. The server now answers the
8
- * whole page in one request (server/http/recordCompose.ts, and the tenant
9
- * host's own): it plans the page, then asks each section's read on the
10
- * section's behalf, through the same route and the same grants as the
11
- * section's own request, and hands every answer back beside the plan. The
12
- * client keeps them for the requests they answer (client/composedReads.ts),
13
- * and a section that asks finds its answer waiting.
14
- *
15
- * So the one rule is that server and client build each request alike, and
16
- * the builders live here, used by both: the sections' own modules call them
17
- * for the request they send, and the server calls them to know what the
18
- * sections will ask. A request built differently is only asked again — the
19
- * section sends it, as it always did — so a drift costs a request, never a
20
- * wrong answer.
21
- *
22
- * The record's own row is the one read that is not asked alike: every fields
23
- * section, the header, the root probe and a custom section's record fields
24
- * read the one row, each for its columns, and which of them ask together
25
- * depends on what is drawn (a tab not yet opened asks nothing). The server
26
- * reads every column any of them names, once, and each is answered with
27
- * exactly its part (`rowPart`), as the client's own shared read already was
28
- * (pageFieldsRead.ts).
29
- */
30
1
  import { type DatabaseSchema, type TableMeta } from './schema.js';
31
2
  import { type PageSection } from './pageModel.js';
32
3
  import type { HttpBreakdownDefinition, HttpFilter } from './resourceHttp.js';
@@ -47,18 +18,23 @@ export interface RecordReadNames {
47
18
  readonly defined?: (tableId: string) => Readonly<Record<string, {
48
19
  per?: string;
49
20
  }>> | undefined;
21
+ /** A column's display rule (`resources.<r>.display`), where a shown column's `subtitle` is read with the record. */
22
+ readonly display?: (tableId: string, column: string) => {
23
+ subtitle?: string;
24
+ } | null | undefined;
50
25
  }
51
26
  /** The parts of a section its reads are built from (client pageModel.ts PageSection, shared page.ts). */
52
27
  export interface ReadSection {
53
28
  readonly kind: PageSection['kind'];
54
29
  readonly hidden?: boolean;
55
- readonly emptyMode?: 'hide' | 'collapse';
30
+ readonly emptyMode?: 'hide' | 'collapse' | 'show';
56
31
  /** A grouped related list reads its groups, then each group's rows (the rows wait on the groups). */
57
32
  readonly group?: unknown;
58
33
  readonly presentation?: unknown;
59
34
  readonly paths?: readonly string[];
60
35
  readonly status?: string;
61
36
  readonly subfigure?: string;
37
+ readonly subtitle?: string;
62
38
  readonly was?: string;
63
39
  readonly actions?: ReadonlyArray<{
64
40
  readonly prefill?: Readonly<Record<string, string>>;
@@ -27,6 +27,7 @@
27
27
  * exactly its part (`rowPart`), as the client's own shared read already was
28
28
  * (pageFieldsRead.ts).
29
29
  */
30
+ import { subtitlePath } from './subtitlePath.js';
30
31
  import { findTable, labelPath, primaryKey } from './schema.js';
31
32
  import { DEFAULT_HISTORY_LIMIT, kindReading } from './pageModel.js';
32
33
  import { assertNever } from './model.js';
@@ -82,6 +83,7 @@ export function fieldsColumns(names, base, section) {
82
83
  const extra = section.presentation === 'headline'
83
84
  ? [
84
85
  ...(section.subfigure ? [section.subfigure] : []),
86
+ ...(section.subtitle ? [section.subtitle] : []),
85
87
  ...(section.was ? [section.was] : []),
86
88
  ...(section.actions ?? []).flatMap((action) => [
87
89
  ...Object.values(action.prefill ?? {}),
@@ -89,7 +91,17 @@ export function fieldsColumns(names, base, section) {
89
91
  ]),
90
92
  ].filter((path) => !paths.includes(path))
91
93
  : [];
92
- const projection = referenceProjection(names.schema, base, [...paths, ...new Set(extra)], section.names !== false && names.app, (table) => namePathOf(names, table));
94
+ /* A column whose rule has a `subtitle` is drawn with it wherever it is a field, so the record reads it too
95
+ (the same rule a list's cell reads, listQuestion.ts subtitleColumns). */
96
+ const seconds = names.display
97
+ ? [...new Set([...paths, ...(section.presentation === 'headline' ? extra : [])])].flatMap((path) => {
98
+ const hop = path.lastIndexOf('.');
99
+ const owner = hop < 0 ? base.id : tableAtPrefix(names.schema, base.id, path.slice(0, hop))?.id;
100
+ const second = owner ? subtitlePath(names.display(owner, path.slice(hop + 1)), path) : undefined;
101
+ return second ? [second] : [];
102
+ })
103
+ : [];
104
+ const projection = referenceProjection(names.schema, base, [...new Set([...paths, ...extra, ...seconds])], section.names !== false && names.app, (table) => namePathOf(names, table));
93
105
  const fetched = projection.fetched;
94
106
  if (namePath && !fetched.includes(namePath))
95
107
  fetched.push(namePath);
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Rollups: one number about the rows that point at a record — an order's
3
3
  * line total, an account's open deals — declared once on the resource
4
- * (`rollups: { line_total: sum('order_line', 'amount') }`) and read wherever
4
+ * (`rollups: { line_total: rollup.sum('order_line', 'amount') }`) and read wherever
5
5
  * a column of the record is: a list's `show`, a sort, a tab's condition, a
6
6
  * record page's fields, a card's figure and facts, a queue's facts, a
7
7
  * picker's query, a dashboard's measure and a breakdown's key — and through
@@ -28,7 +28,7 @@
28
28
  import type { Filter } from './filter.js';
29
29
  import type { ComputedColumn } from './computed.js';
30
30
  import type { ColumnMeta } from './schema.js';
31
- export declare const ROLLUP_FNS: readonly ['count', 'sum', 'min', 'max', 'latest'];
31
+ export declare const ROLLUP_FNS: readonly ['count', 'sum', 'avg', 'min', 'max', 'latest'];
32
32
  export type RollupFn = (typeof ROLLUP_FNS)[number];
33
33
  /** A rollup as the catalog serves it and a view carries it: no request ever supplies one. */
34
34
  export interface Rollup {
@@ -99,13 +99,13 @@ export interface Rollup {
99
99
  type: string;
100
100
  /**
101
101
  * Set when `column` names a rollup of the child rather than one of its
102
- * columns — a customer's `lines: sum('sales_order', 'lines')`, the count
102
+ * columns — a customer's `lines: rollup.sum('sales_order', 'lines')`, the count
103
103
  * of its orders' lines, which is how a rollup reaches two hops. The inner
104
104
  * rollup is compiled inside the outer one's aggregate, each through its own
105
105
  * child's relation, so a row policy narrows both levels. One level only:
106
106
  * the inner one reads a column (appRollupRules says so when it does not).
107
107
  *
108
- * A rollup whose `whose` walks two references (`sum('time_entry', 'hours',
108
+ * A rollup whose `whose` walks two references (`rollup.sum('time_entry', 'hours',
109
109
  * { whose: 'task_id.project_id' })`) is served in the same shape: the outer
110
110
  * reads the middle table (`task.project_id`) with no column of its own,
111
111
  * the inner the rows and column declared, with the declared query. A count
@@ -1,4 +1,4 @@
1
- export const ROLLUP_FNS = ['count', 'sum', 'min', 'max', 'latest'];
1
+ export const ROLLUP_FNS = ['count', 'sum', 'avg', 'min', 'max', 'latest'];
2
2
  /** A rollup read as a column: never written, null only where there are no rows to read (a count is 0). */
3
3
  export function rollupColumnMeta(name, rollup) {
4
4
  return {
@@ -527,3 +527,10 @@ export declare function comparableType(type: string | undefined): boolean;
527
527
  * as a plain value of a comparable type: the write is then as blind as ever.
528
528
  */
529
529
  export declare function expectedValues(table: Pick<TableMeta, 'columns'>, values: Record<string, unknown>, row: Record<string, unknown> | undefined): Record<string, string | number | boolean | null> | undefined;
530
+ /**
531
+ * The reference a card over `table`'s row opens, where that row is a link (join) table's: the far side. The author's
532
+ * `opens` where they name one; otherwise, a table that is two references and nothing else of note (its key and
533
+ * dates aside) opens the one that `whose` — the reference back to the page's record — does not name. Any other table
534
+ * opens its own row, and gives none.
535
+ */
536
+ export declare function linkTarget(table: TableMeta, whose: string, opens?: string): string | undefined;
@@ -625,3 +625,20 @@ export function expectedValues(table, values, row) {
625
625
  }
626
626
  return Object.keys(expected).length ? expected : undefined;
627
627
  }
628
+ /**
629
+ * The reference a card over `table`'s row opens, where that row is a link (join) table's: the far side. The author's
630
+ * `opens` where they name one; otherwise, a table that is two references and nothing else of note (its key and
631
+ * dates aside) opens the one that `whose` — the reference back to the page's record — does not name. Any other table
632
+ * opens its own row, and gives none.
633
+ */
634
+ export function linkTarget(table, whose, opens) {
635
+ if (opens)
636
+ return opens;
637
+ if (whose.includes('.'))
638
+ return undefined;
639
+ const references = table.columns.filter((column) => column.references && column.references.table !== table.id);
640
+ if (references.length !== 2 || !references.some((column) => column.name === whose))
641
+ return undefined;
642
+ const plain = table.columns.every((column) => column.primaryKey || references.includes(column) || (column.type !== undefined && columnKind(column.type) === 'date'));
643
+ return plain ? references.find((column) => column.name !== whose)?.name : undefined;
644
+ }
@@ -355,23 +355,28 @@ export function viewPaths(schema, view, plan, style, relations) {
355
355
  return named(style.concat(parts.map((part) => {
356
356
  if ('text' in part)
357
357
  return literal(part.text);
358
- const value = style.text(plain(at(prefix, part.path)));
359
- /* A masked column says its placeholder in the name too, computed here
360
- like the rest of it, so no list, picker or breadcrumb reads the value. */
361
- if (part.mask?.prefix) {
362
- return `CASE WHEN SUBSTR(${value}, 1, ${[...part.mask.prefix].length}) = ${literal(part.mask.prefix)} THEN ${literal(part.mask.prefix + MASK)} ELSE COALESCE(${value}, '') END`;
363
- }
364
- if (part.mask)
365
- return `CASE WHEN ${value} IS NULL THEN '' ELSE ${literal(MASK)} END`;
366
- /* A stored value said as its display rule's word, as a cell says it:
367
- the words are the catalog's, held to what every engine reads alike. */
368
- if (part.words) {
369
- const cases = Object.entries(part.words)
370
- .map(([stored, word]) => `WHEN ${literal(stored)} THEN ${literal(word)}`)
371
- .join(' ');
372
- return `CASE ${value} ${cases} ELSE COALESCE(${value}, '') END`;
373
- }
374
- return `COALESCE(${value}, '')`;
358
+ const one = (field) => {
359
+ const value = style.text(plain(at(prefix, field.path)));
360
+ /* A masked column says its placeholder in the name too, computed here
361
+ like the rest of it, so no list, picker or breadcrumb reads the value. */
362
+ if (field.mask?.prefix) {
363
+ return `CASE WHEN SUBSTR(${value}, 1, ${[...field.mask.prefix].length}) = ${literal(field.mask.prefix)} THEN ${literal(field.mask.prefix + MASK)} ELSE COALESCE(${value}, '') END`;
364
+ }
365
+ if (field.mask)
366
+ return `CASE WHEN ${value} IS NULL THEN '' ELSE ${literal(MASK)} END`;
367
+ /* A stored value said as its display rule's word, as a cell says it:
368
+ the words are the catalog's, held to what every engine reads alike. */
369
+ if (field.words) {
370
+ const cases = Object.entries(field.words)
371
+ .map(([stored, word]) => `WHEN ${literal(stored)} THEN ${literal(word)}`)
372
+ .join(' ');
373
+ return `CASE ${value} ${cases} ELSE COALESCE(${value}, '') END`;
374
+ }
375
+ return `COALESCE(${value}, '')`;
376
+ };
377
+ /* A fallback chain is its first alternative with a value: each said as it would alone, an empty text
378
+ counting as none, so a blank nickname gives way to the name behind it. */
379
+ return part.or ? `COALESCE(${[part, ...part.or].map((field) => `NULLIF(${one(field)}, '')`).join(', ')}, '')` : one(part);
375
380
  })));
376
381
  };
377
382
  /* The catalog's type of what a path reads where it reads one column as it is stored, as `qualify` writes it plain:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mistriwork/engine",
3
- "version": "1.0.0-rc.15",
3
+ "version": "1.0.0-rc.16",
4
4
  "description": "The engine both products stand on: the adapters (SQLite, Postgres), the catalog, the query language, keyset pages, the HTTP kit and the shared rules. No React.",
5
5
  "homepage": "https://mistriwork.dev",
6
6
  "bugs": {
@@ -103,6 +103,7 @@
103
103
  "./server/http/door.js": "./dist/server/http/door.js",
104
104
  "./server/http/exportRoute.js": "./dist/server/http/exportRoute.js",
105
105
  "./server/http/firstPaint.js": "./dist/server/http/firstPaint.js",
106
+ "./server/http/frontDoor.js": "./dist/server/http/frontDoor.js",
106
107
  "./server/http/httpOperationRoutes.js": "./dist/server/http/httpOperationRoutes.js",
107
108
  "./server/http/pageReads.js": "./dist/server/http/pageReads.js",
108
109
  "./server/http/pageRoutes.js": "./dist/server/http/pageRoutes.js",
@@ -264,6 +265,6 @@
264
265
  "pg": "^8.23.0"
265
266
  },
266
267
  "engines": {
267
- "node": ">=24.22.1"
268
+ "node": ">=24"
268
269
  }
269
270
  }