@mistriwork/engine 1.0.0-rc.38 → 1.0.0-rc.40

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.
@@ -395,6 +395,8 @@ export interface InsertRequest {
395
395
  * what defaults are for — and refuses what it must, in its own words.
396
396
  */
397
397
  values: Record<string, string | number | boolean | null>;
398
+ /** What the write gives its notices to say (`{words.code}` in a notify title): told, never stored on the row. */
399
+ words?: Readonly<Record<string, string>>;
398
400
  }
399
401
  export interface InsertResult {
400
402
  /** The new row's primary key, read back from the database. */
@@ -804,6 +806,8 @@ export interface UpdateRequest {
804
806
  restore?: true;
805
807
  /** Original values for exactly the touched fields; checked atomically by UPDATE. */
806
808
  expected?: Record<string, string | number | boolean | null>;
809
+ /** What the write gives its notices to say (`{words.code}` in a notify title): told, never stored on the row. */
810
+ words?: Readonly<Record<string, string>>;
807
811
  }
808
812
  export interface UpdateResult {
809
813
  affected: number;
@@ -108,6 +108,15 @@ interface ShellTitle {
108
108
  manifest: string;
109
109
  register: string;
110
110
  };
111
+ /**
112
+ * The App's control kit (Preview; the App framework's appComponents.ts compileUiKit): its module, preloaded and named
113
+ * in a meta the shell reads to install it before its first render, and its stylesheet, linked so it is in force from
114
+ * the first paint. Never the Workbench's.
115
+ */
116
+ uiKit?: {
117
+ js: string;
118
+ css?: string;
119
+ };
111
120
  /** The App declared `ui.microphone`: its pages may ask for the microphone, from this origin alone (`pagePermissions`). */
112
121
  microphone?: true;
113
122
  /** The scheme that look says (clientSurface.ts lookSchemeFor), said in the page so it is dark from its first paint. */
@@ -139,6 +139,7 @@ const writesHead = (head) => Boolean(head?.title ||
139
139
  head?.looks?.length ||
140
140
  head?.social ||
141
141
  head?.pane ||
142
+ head?.uiKit ||
142
143
  head?.pwa);
143
144
  /** What an App's own theme carries that a page's head never writes: its served files and where its menu sits. */
144
145
  const HEAD_UNWRITTEN = new Set(['faces', 'stylesheet', 'shell']);
@@ -255,6 +256,14 @@ export function withHead(html, head) {
255
256
  script is needed and the page's policy admits them as they stand. */
256
257
  if (head.pwa)
257
258
  out = out.replace(/<\/head>/i, () => ` <link rel="manifest" href="${escapeHtml(head.pwa.manifest)}">\n <script src="${escapeHtml(head.pwa.register)}" defer></script>\n </head>`);
259
+ /* The kit's module is fetched beside the entry rather than after it, and its sheet is a link in the head, so the
260
+ first render draws the kit's controls in the kit's rules: nothing is swapped, and nothing moves, after paint. */
261
+ if (head.uiKit && !head.workbench) {
262
+ const kit = head.uiKit;
263
+ const css = kit.css ? ` <link rel="stylesheet" href="${escapeHtml(kit.css)}" data-component-css="ui-kit">\n` : '';
264
+ out = out.replace(/<\/head>/i, () => ` <link rel="modulepreload" crossorigin href="${escapeHtml(kit.js)}">\n${css}` +
265
+ ` <meta name="mistri-ui-kit" content="${escapeHtml(kit.js)}"${kit.css ? ` data-css="${escapeHtml(kit.css)}"` : ''}>\n </head>`);
266
+ }
258
267
  if (!head?.title && !head?.canonical)
259
268
  return out;
260
269
  /* Replaced by a function, never a replacement string: a string's `$&`,
@@ -4,7 +4,7 @@ import { distance, nearest } from './suggest.js';
4
4
  import { columnKind, findTable } from './schema.js';
5
5
  import { dateWindow } from './queryDates.js';
6
6
  import { maskQuoted } from './maskQuoted.js';
7
- import { ME_SET, columnOperand, meOperand, meSetOperand } from './queryOperands.js';
7
+ import { ME_SET, columnOperand, meOperand, meSetOperand, tenantOperand } from './queryOperands.js';
8
8
  import { TAGS_TYPE } from './tagLists.js';
9
9
  import { DEFINED_PATH_TYPE } from './definedFields.js';
10
10
  const RANGE_OPS = new Set(['<', '<=', '>', '>=']);
@@ -758,6 +758,17 @@ function compileClause(raw, ctx) {
758
758
  const groups = item.quoted ? undefined : (column?.states ?? ctx.states?.[name])?.groups;
759
759
  if (groups && Object.hasOwn(groups, item.value))
760
760
  return { error: `"${item.value}" is a group of ${name}'s states; ask it with is: ${name} is ${item.value}.` };
761
+ const fromTenant = tenantOperand(ctx, column, item.value, item.quoted);
762
+ if (fromTenant) {
763
+ if (fromTenant.error)
764
+ return { error: fromTenant.error };
765
+ if (typeof fromTenant.value === 'boolean')
766
+ return { error: `${item.value} is a flag: ask it with =, as in ${name} = ${item.value}.` };
767
+ /* A null profile value is in no list: left out, as a row key no one has. */
768
+ if (fromTenant.value !== undefined)
769
+ values.push(fromTenant.value);
770
+ continue;
771
+ }
761
772
  if (!item.quoted && item.value === 'me') {
762
773
  if (ctx.me === undefined)
763
774
  return { error: NO_ONE_HERE };
@@ -812,6 +823,15 @@ function compileClause(raw, ctx) {
812
823
  nobody is signed in says so (`me: null`); a check before any request reads it as anyone's (ANYONE). */
813
824
  if (!quoted && value === 'me' && ctx.me === undefined)
814
825
  return { error: NO_ONE_HERE };
826
+ /* The reader's tenant profile (`country = tenant.country`), read once for this request; a null value matches no row. */
827
+ const fromTenant = tenantOperand(ctx, column, value, quoted);
828
+ if (fromTenant) {
829
+ if (fromTenant.error)
830
+ return { error: fromTenant.error };
831
+ if (fromTenant.none)
832
+ return { conditions: [{ column: name, op: 'in', value: [] }] };
833
+ return { conditions: [{ column: name, op, value: fromTenant.value }] };
834
+ }
815
835
  if (!quoted && value === 'me') {
816
836
  const me = meOperand(ctx, column, op);
817
837
  if (me.error)
@@ -41,7 +41,16 @@ export interface QueryPerson {
41
41
  * or why they cannot be read.
42
42
  */
43
43
  sets?: Readonly<Record<string, MeSet>>;
44
+ /**
45
+ * `tenant.<column>`: the reader's tenant profile row, by column, for the columns
46
+ * the App shares with its members (`auth.tenancy.shared`), read once for this
47
+ * request. A missing row reads as every column null. Absent where no tenant is
48
+ * meant, and `tenant.<column>` is then refused rather than read as text.
49
+ */
50
+ tenant?: Readonly<Record<string, TenantValue>>;
44
51
  }
52
+ /** One column of a tenant's profile row, as a member may read it. */
53
+ export type TenantValue = string | number | boolean | null;
45
54
  /** Nobody in particular (`anyone` above): what a check that runs before any request reads `me` as. */
46
55
  export declare const ANYONE: QueryPerson;
47
56
  /** One `me.<table>.<column>`: its values and their kind, or the sentence that refuses it. */
@@ -62,6 +71,18 @@ export interface CompileContext {
62
71
  /** A machine's states by column, where the catalog does not carry them (a parse with no schema). */
63
72
  states?: Readonly<Record<string, MachineStates>>;
64
73
  }
74
+ /**
75
+ * What `tenant.<column>` stands for beside one column: the profile's value, read for
76
+ * this request. Undefined where the word is not one (quoted, or the table has a
77
+ * `tenant` column of its own, whose path it then is). A null value matches no row, as
78
+ * SQL's `= NULL` would, but said as conditions every engine speaks; in a list it is
79
+ * left out. A check before any request (ANYONE) reads it as the word.
80
+ */
81
+ export declare function tenantOperand(ctx: CompileContext, column: ColumnMeta | undefined, word: string, quoted: boolean): {
82
+ value?: string | number | boolean;
83
+ none?: true;
84
+ error?: string;
85
+ } | undefined;
65
86
  /**
66
87
  * The column an unquoted right-hand side names, when it names one.
67
88
  *
@@ -1,8 +1,35 @@
1
1
  import { columnKind } from './schema.js';
2
+ /** `tenant.<column>`, unquoted, on the right of a comparison or in a value list. */
3
+ const TENANT_VALUE = /^tenant\.([A-Za-z_]\w*)$/;
2
4
  /** Nobody in particular (`anyone` above): what a check that runs before any request reads `me` as. */
3
5
  export const ANYONE = Object.freeze({ email: null, anyone: true });
4
6
  /** `me.<table>.<column>`, written as one item of a value list. */
5
7
  export const ME_SET = /^me\.([A-Za-z_]\w*)\.([A-Za-z_]\w*)$/;
8
+ /**
9
+ * What `tenant.<column>` stands for beside one column: the profile's value, read for
10
+ * this request. Undefined where the word is not one (quoted, or the table has a
11
+ * `tenant` column of its own, whose path it then is). A null value matches no row, as
12
+ * SQL's `= NULL` would, but said as conditions every engine speaks; in a list it is
13
+ * left out. A check before any request (ANYONE) reads it as the word.
14
+ */
15
+ export function tenantOperand(ctx, column, word, quoted) {
16
+ const match = quoted ? null : TENANT_VALUE.exec(word);
17
+ if (!match || ctx.table?.columns.some((c) => c.name === 'tenant'))
18
+ return undefined;
19
+ const me = ctx.me;
20
+ if (me?.anyone && me.tenant === undefined)
21
+ return { value: word };
22
+ if (!me?.tenant)
23
+ return { error: `${word} is the reader's tenant profile, and no tenant is meant here. Quote it ("${word}") to mean the text.` };
24
+ if (!Object.hasOwn(me.tenant, match[1]))
25
+ return { error: `${word} is not a column the App shares with its members: list "${match[1]}" in auth.tenancy.shared.` };
26
+ const value = me.tenant[match[1]];
27
+ if (value === null)
28
+ return { none: true };
29
+ if (typeof value === 'boolean' && column && columnKind(column.type) !== 'boolean')
30
+ return { error: `${word} is a flag, and ${column.name} is not.` };
31
+ return { value };
32
+ }
6
33
  const COMPARISON_OPS = new Set(['=', '!=', '<', '<=', '>', '>=']);
7
34
  const BARE_WORD = /^[A-Za-z_$][\w$]*$/;
8
35
  const PATH_WORD = /^[A-Za-z_$][\w$+]*(?:\.[A-Za-z_$][\w$+]*)+$/;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mistriwork/engine",
3
- "version": "1.0.0-rc.38",
3
+ "version": "1.0.0-rc.40",
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": {
package/schema/model.d.ts CHANGED
@@ -50,13 +50,23 @@ export type StaticAppTarget = AppTarget<string | number>;
50
50
  /**
51
51
  * A field's starting value: a literal (`'phone'`, `3`, `true`); `today` on a
52
52
  * date field or `now` on a datetime field, as the form opens in the App's time
53
- * zone; `me` on a text field, the reader's email; or `{ record: 'on_hand' }`,
54
- * the target record's column as the reader sees it. The constants are
55
- * `@mistriwork/mistri/app`'s. `me` and `{ record }` are read when one record's form is
56
- * prepared, so they fill a field of the input itself on a command that targets
57
- * one record.
53
+ * zone; `me` on a text field, the reader's email; `{ record: 'on_hand' }`,
54
+ * the target record's column as the reader sees it, or one its references
55
+ * reach (`{ record: 'customer_id.default_rate' }`, at most two hops, each read
56
+ * under the reader's grants: a hop they cannot read starts nothing); or
57
+ * `{ tenant: 'country' }`, a column of the tenant's profile its members read
58
+ * (`auth.tenancy.shared`). The constants are `@mistriwork/mistri/app`'s. `me`,
59
+ * `{ record }` and `{ tenant }` are read when the form is prepared, so they
60
+ * fill a field of the input itself; `{ record }` on a command that targets one
61
+ * record.
58
62
  */
59
- export type CommandFieldDefault = string | number | boolean | { readonly auto: 'today' | 'now' | 'me' } | { readonly record: string };
63
+ export type CommandFieldDefault =
64
+ | string
65
+ | number
66
+ | boolean
67
+ | { readonly auto: 'today' | 'now' | 'me' }
68
+ | { readonly record: string }
69
+ | { readonly tenant: string };
60
70
 
61
71
  /** One option of a `choice` input. */
62
72
  export interface AppCommandChoice {
@@ -276,6 +286,10 @@ export interface CommandActionBase<Label extends string | AppMessageRef> {
276
286
  the platform's control. An input in a group is named by its path (`trade.vin`), and in a repeating group stands in
277
287
  every row; what the value must be is still the command's to judge. */
278
288
  inputs?: Readonly<Record<string, { readonly id: string; readonly module: string; readonly kind: string }>>;
289
+ /** The command's own inputs this action's page supplies (`useCommand(name).open(record, { step })`): its form does not
290
+ draw them, and sends what the page gave. A required one with no default is typed by no one, so the action says
291
+ `header: false` and is not `bulk`: only a page opens it. The server judges the value as any other. */
292
+ given?: readonly string[];
279
293
  }
280
294
 
281
295
  /** A command action as the App serves it. */