@atscript/ui 0.1.152 → 0.1.154
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +521 -272
- package/dist/index.d.cts +108 -16
- package/dist/index.d.mts +108 -16
- package/dist/index.mjs +518 -274
- package/package.json +10 -10
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Validator, createDataFromAnnotatedType, deserializeAnnotatedType, detectDiscriminator, flattenAnnotatedType } from "@atscript/typescript/utils";
|
|
2
|
-
import { Client, actionIdentifier, encodeNavigateId } from "@atscript/db-client";
|
|
2
|
+
import { Client, actionIdentifier, clearMetaStore, defaultMetaStore, encodeNavigateId } from "@atscript/db-client";
|
|
3
3
|
import { createDbValidatorPlugin } from "@atscript/db/validator";
|
|
4
4
|
//#region src/shared/annotation-keys.ts
|
|
5
5
|
const UI_TYPE = "ui.type";
|
|
@@ -434,6 +434,7 @@ const UI_TAGS = new Set([
|
|
|
434
434
|
"radio",
|
|
435
435
|
"checkbox"
|
|
436
436
|
]);
|
|
437
|
+
const EDIT = { mode: "edit" };
|
|
437
438
|
/**
|
|
438
439
|
* Converts an ATScript annotated type into a FormDef.
|
|
439
440
|
*
|
|
@@ -442,11 +443,13 @@ const UI_TAGS = new Set([
|
|
|
442
443
|
* with `path: ''`.
|
|
443
444
|
*
|
|
444
445
|
* A `@db.column.derived` field is marked `derived` straight from its metadata;
|
|
445
|
-
* a computed view column, from `opts.metaFields
|
|
446
|
+
* a computed view column, from `opts.metaFields`; an `@db.onUpdate.now` field,
|
|
447
|
+
* when `opts.mode` is `"edit"`.
|
|
446
448
|
*/
|
|
447
449
|
function createFormDef(type, opts) {
|
|
450
|
+
const edit = opts?.mode === "edit";
|
|
448
451
|
if (type.type.kind !== "object") {
|
|
449
|
-
const rootField = createFieldDef("", type);
|
|
452
|
+
const rootField = createFieldDef("", type, edit);
|
|
450
453
|
const fields = [rootField];
|
|
451
454
|
return {
|
|
452
455
|
type,
|
|
@@ -472,7 +475,7 @@ function createFormDef(type, opts) {
|
|
|
472
475
|
if (originalProp.type.of.type.kind === "array" && !getFieldMeta(originalProp, "ui.form.component")) continue;
|
|
473
476
|
}
|
|
474
477
|
if (kind === "array" || kind === "object" || kind === "union" || kind === "tuple") structuredPrefixes.add(path + ".");
|
|
475
|
-
const field = createFieldDef(path, originalProp);
|
|
478
|
+
const field = createFieldDef(path, originalProp, edit);
|
|
476
479
|
if (opts?.metaFields?.[path]?.computed) field.derived = true;
|
|
477
480
|
fields.push(field);
|
|
478
481
|
}
|
|
@@ -504,14 +507,18 @@ function createFormDef(type, opts) {
|
|
|
504
507
|
rootField.objectDef = def;
|
|
505
508
|
return def;
|
|
506
509
|
}
|
|
507
|
-
/**
|
|
508
|
-
|
|
510
|
+
/**
|
|
511
|
+
* Creates a FormFieldDef from any ATScript annotated type. `edit`: the form
|
|
512
|
+
* edits a stored row (`CreateFormDefOptions.mode`).
|
|
513
|
+
*/
|
|
514
|
+
function createFieldDef(path, prop, edit = false) {
|
|
509
515
|
const kind = prop.type.kind;
|
|
510
516
|
const name = path.slice(path.lastIndexOf(".") + 1);
|
|
511
517
|
const allStatic = !hasComputedAnnotations(prop);
|
|
512
518
|
const uiType = getFieldMeta(prop, "ui.form.type") ?? getFieldMeta(prop, "ui.type");
|
|
513
519
|
const pushDown = getFieldMeta(prop, UI_FORM_PUSH_DOWN) !== void 0;
|
|
514
520
|
const valueHelpInfo = extractValueHelp(prop);
|
|
521
|
+
const derived = getFieldMeta(prop, "db.column.derived") !== void 0 || edit && path !== "" && isUpdateStamped(prop);
|
|
515
522
|
const base = {
|
|
516
523
|
path,
|
|
517
524
|
prop,
|
|
@@ -520,7 +527,7 @@ function createFieldDef(path, prop) {
|
|
|
520
527
|
allStatic,
|
|
521
528
|
pushDown,
|
|
522
529
|
valueHelpInfo,
|
|
523
|
-
...
|
|
530
|
+
...derived && { derived: true }
|
|
524
531
|
};
|
|
525
532
|
const customType = uiType;
|
|
526
533
|
if (kind === "array") {
|
|
@@ -531,21 +538,21 @@ function createFieldDef(path, prop) {
|
|
|
531
538
|
type: "array",
|
|
532
539
|
customType: isMultiselect ? "multiselect" : customType,
|
|
533
540
|
itemType: arrayType.of,
|
|
534
|
-
itemField: createFieldDef("", arrayType.of)
|
|
541
|
+
itemField: createFieldDef("", arrayType.of, edit)
|
|
535
542
|
};
|
|
536
543
|
}
|
|
537
544
|
if (kind === "object") return {
|
|
538
545
|
...base,
|
|
539
546
|
type: "object",
|
|
540
547
|
customType,
|
|
541
|
-
objectDef: createFormDef(prop)
|
|
548
|
+
objectDef: createFormDef(prop, edit ? EDIT : void 0)
|
|
542
549
|
};
|
|
543
550
|
if (kind === "union") {
|
|
544
551
|
if (isPureLiteralUnion(prop)) return {
|
|
545
552
|
...base,
|
|
546
553
|
type: uiType ?? "select"
|
|
547
554
|
};
|
|
548
|
-
const unionVariants =
|
|
555
|
+
const unionVariants = createUnionVariants(prop, edit && soleValueMember(prop) !== void 0);
|
|
549
556
|
if (unionVariants.length > 1) return {
|
|
550
557
|
...base,
|
|
551
558
|
type: "union",
|
|
@@ -558,7 +565,8 @@ function createFieldDef(path, prop) {
|
|
|
558
565
|
path,
|
|
559
566
|
name,
|
|
560
567
|
allStatic,
|
|
561
|
-
pushDown
|
|
568
|
+
pushDown,
|
|
569
|
+
...derived && { derived: true }
|
|
562
570
|
};
|
|
563
571
|
if (v?.def) return {
|
|
564
572
|
...base,
|
|
@@ -676,17 +684,20 @@ function isChildOfStructured(path, prefixes) {
|
|
|
676
684
|
* Iterates top-level items directly — one variant per item.
|
|
677
685
|
*/
|
|
678
686
|
function buildUnionVariants(typeDef) {
|
|
687
|
+
return createUnionVariants(typeDef, false);
|
|
688
|
+
}
|
|
689
|
+
function createUnionVariants(typeDef, edit) {
|
|
679
690
|
const items = typeDef.type.items ?? [typeDef];
|
|
680
691
|
const variants = [];
|
|
681
692
|
for (const item of items) {
|
|
682
|
-
const v = createVariant(item);
|
|
693
|
+
const v = createVariant(item, edit);
|
|
683
694
|
if (items.length > 1) v.label = `${String(variants.length + 1)}. ${v.label}`;
|
|
684
695
|
variants.push(v);
|
|
685
696
|
}
|
|
686
697
|
return variants;
|
|
687
698
|
}
|
|
688
699
|
/** Creates a single union variant from an annotated type item. */
|
|
689
|
-
function createVariant(def) {
|
|
700
|
+
function createVariant(def, edit) {
|
|
690
701
|
const kind = def.type.kind;
|
|
691
702
|
if (kind === "object") {
|
|
692
703
|
const label = getFieldMeta(def, "meta.label") ?? "Object";
|
|
@@ -694,8 +705,8 @@ function createVariant(def) {
|
|
|
694
705
|
return {
|
|
695
706
|
label,
|
|
696
707
|
type: def,
|
|
697
|
-
def: createFormDef(def),
|
|
698
|
-
itemField: hasComponent ? createFieldDef("", def) : void 0
|
|
708
|
+
def: createFormDef(def, edit ? EDIT : void 0),
|
|
709
|
+
itemField: hasComponent ? createFieldDef("", def, edit) : void 0
|
|
699
710
|
};
|
|
700
711
|
}
|
|
701
712
|
if (kind === "") {
|
|
@@ -703,15 +714,36 @@ function createVariant(def) {
|
|
|
703
714
|
return {
|
|
704
715
|
label: capitalize(dt === "phantom" ? "item" : dt),
|
|
705
716
|
type: def,
|
|
706
|
-
itemField: createFieldDef("", def),
|
|
717
|
+
itemField: createFieldDef("", def, edit),
|
|
707
718
|
designType: dt
|
|
708
719
|
};
|
|
709
720
|
}
|
|
710
721
|
return {
|
|
711
722
|
label: capitalize(kind),
|
|
712
723
|
type: def,
|
|
713
|
-
itemField: createFieldDef("", def)
|
|
724
|
+
itemField: createFieldDef("", def, edit)
|
|
725
|
+
};
|
|
726
|
+
}
|
|
727
|
+
/**
|
|
728
|
+
* `@db.onUpdate.now` (`number.timestamp.updated`): the server sets the field
|
|
729
|
+
* on every update. Read off the prop, or — as `@atscript/db` does — off the one
|
|
730
|
+
* value member of `T | null`, unless the prop references another field.
|
|
731
|
+
*/
|
|
732
|
+
function isUpdateStamped(prop) {
|
|
733
|
+
if (hasFieldMeta(prop, "db.onUpdate.now")) return true;
|
|
734
|
+
const member = prop.ref?.field ? void 0 : soleValueMember(prop);
|
|
735
|
+
return member !== void 0 && hasFieldMeta(member, "db.onUpdate.now");
|
|
736
|
+
}
|
|
737
|
+
/** The one non-`null` / `undefined` member of a (nested) union, if there is exactly one. */
|
|
738
|
+
function soleValueMember(prop) {
|
|
739
|
+
if (prop.type.kind !== "union") return void 0;
|
|
740
|
+
const members = /* @__PURE__ */ new Set();
|
|
741
|
+
const visit = (t) => {
|
|
742
|
+
if (t.type.kind === "union") for (const item of t.type.items) visit(item);
|
|
743
|
+
else if (t.type.kind !== "" || t.type.designType !== "null" && t.type.designType !== "undefined") members.add(t);
|
|
714
744
|
};
|
|
745
|
+
visit(prop);
|
|
746
|
+
return members.size === 1 ? members.values().next().value : void 0;
|
|
715
747
|
}
|
|
716
748
|
function capitalize(s) {
|
|
717
749
|
return s.charAt(0).toUpperCase() + s.slice(1);
|
|
@@ -1013,7 +1045,7 @@ async function fetchDistinctValues(client, field, opts = {}) {
|
|
|
1013
1045
|
}
|
|
1014
1046
|
//#endregion
|
|
1015
1047
|
//#region src/client-factory.ts
|
|
1016
|
-
const builtin = (url) => new Client(url);
|
|
1048
|
+
const builtin = (url, options) => new Client(url, options);
|
|
1017
1049
|
let _default = builtin;
|
|
1018
1050
|
/**
|
|
1019
1051
|
* Override the app-wide default factory. Call once at startup (e.g. in
|
|
@@ -1023,7 +1055,7 @@ let _default = builtin;
|
|
|
1023
1055
|
function setDefaultClientFactory(factory) {
|
|
1024
1056
|
_default = factory;
|
|
1025
1057
|
}
|
|
1026
|
-
/** Current app-wide default factory. Falls back to `new Client(url)`. */
|
|
1058
|
+
/** Current app-wide default factory. Falls back to `new Client(url, options)`. */
|
|
1027
1059
|
function getDefaultClientFactory() {
|
|
1028
1060
|
return _default;
|
|
1029
1061
|
}
|
|
@@ -1032,43 +1064,443 @@ function resetDefaultClientFactory() {
|
|
|
1032
1064
|
_default = builtin;
|
|
1033
1065
|
}
|
|
1034
1066
|
//#endregion
|
|
1067
|
+
//#region src/form/measurement.ts
|
|
1068
|
+
/**
|
|
1069
|
+
* Read measurement annotations off a single field prop. Returns `undefined`
|
|
1070
|
+
* for any annotation that's absent — callers can spread the result into a
|
|
1071
|
+
* larger record.
|
|
1072
|
+
*/
|
|
1073
|
+
function extractMeasurement(prop) {
|
|
1074
|
+
const precisionMeta = getFieldMeta(prop, DB_COLUMN_PRECISION);
|
|
1075
|
+
return {
|
|
1076
|
+
currencyCode: getFieldMeta(prop, DB_AMOUNT_CURRENCY),
|
|
1077
|
+
currencyRefField: getFieldMeta(prop, DB_AMOUNT_CURRENCY_REF),
|
|
1078
|
+
unitCode: getFieldMeta(prop, DB_UNIT),
|
|
1079
|
+
unitRefField: getFieldMeta(prop, DB_UNIT_REF),
|
|
1080
|
+
precisionScale: precisionMeta?.scale
|
|
1081
|
+
};
|
|
1082
|
+
}
|
|
1083
|
+
//#endregion
|
|
1084
|
+
//#region src/table/create-table-def.ts
|
|
1085
|
+
/**
|
|
1086
|
+
* Builds a TableDef from a moost-db MetaResponse.
|
|
1087
|
+
*
|
|
1088
|
+
* 1. Deserializes `meta.type` into a live TAtscriptAnnotatedType
|
|
1089
|
+
* 2. Flattens to discover all field paths
|
|
1090
|
+
* 3. Builds ColumnDef per field using annotations + meta.fields capabilities
|
|
1091
|
+
* 4. Sorts by @ui.table.order
|
|
1092
|
+
*/
|
|
1093
|
+
function createTableDef(meta, preDeserializedType) {
|
|
1094
|
+
const type = preDeserializedType ?? deserializeAnnotatedType(meta.type);
|
|
1095
|
+
const flatMap = type.type.kind === "object" ? flattenAnnotatedType(type, { excludePhantomTypes: true }) : /* @__PURE__ */ new Map();
|
|
1096
|
+
const columns = [];
|
|
1097
|
+
for (const [path, prop] of flatMap.entries()) {
|
|
1098
|
+
if (path === "") continue;
|
|
1099
|
+
if (path === meta.versionColumn) continue;
|
|
1100
|
+
const fieldMeta = meta.fields[path];
|
|
1101
|
+
if (!isReadable(fieldMeta)) continue;
|
|
1102
|
+
if (getFieldMeta(prop, "ui.table.exclude") !== void 0) continue;
|
|
1103
|
+
const options = extractFieldLiteralOptions(prop);
|
|
1104
|
+
const valueHelpInfo = extractValueHelp(prop);
|
|
1105
|
+
let distinct;
|
|
1106
|
+
if (!valueHelpInfo && !options && fieldMeta.groupable && fieldMeta.filterable) {
|
|
1107
|
+
const url = type.metadata.get(DB_HTTP_PATH);
|
|
1108
|
+
if (url && getFieldMeta(prop, "ui.valueHelp.distinct") !== void 0) distinct = {
|
|
1109
|
+
url,
|
|
1110
|
+
field: path
|
|
1111
|
+
};
|
|
1112
|
+
}
|
|
1113
|
+
columns.push(buildColumn(path, prop, fieldMeta, {
|
|
1114
|
+
options,
|
|
1115
|
+
valueHelpInfo,
|
|
1116
|
+
distinct
|
|
1117
|
+
}));
|
|
1118
|
+
}
|
|
1119
|
+
if (meta.decorations) {
|
|
1120
|
+
const decoType = deserializeAnnotatedType(meta.decorations);
|
|
1121
|
+
if (decoType.type.kind === "object") for (const [path, prop] of decoType.type.props) {
|
|
1122
|
+
const fieldMeta = meta.fields[path];
|
|
1123
|
+
if (!fieldMeta?.decoration || flatMap.has(path)) continue;
|
|
1124
|
+
if (getFieldMeta(prop, "ui.table.exclude") !== void 0) continue;
|
|
1125
|
+
flatMap.set(path, prop);
|
|
1126
|
+
columns.push(buildColumn(path, prop, fieldMeta, { options: extractFieldLiteralOptions(prop) }));
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
1129
|
+
columns.sort((a, b) => a.order - b.order);
|
|
1130
|
+
const actions = groupActions(meta.actions ?? []);
|
|
1131
|
+
const crud = meta.crud ?? {};
|
|
1132
|
+
const preferredId = meta.preferredId ?? meta.primaryKeys;
|
|
1133
|
+
return {
|
|
1134
|
+
type,
|
|
1135
|
+
columns,
|
|
1136
|
+
flatMap,
|
|
1137
|
+
fetchableFields: new Set(Object.keys(meta.fields).filter((p) => isReadable(meta.fields[p]))),
|
|
1138
|
+
primaryKeys: meta.primaryKeys,
|
|
1139
|
+
preferredId,
|
|
1140
|
+
identifierFields: identifierFieldsOf(preferredId, meta.actions ?? []),
|
|
1141
|
+
versionColumn: meta.versionColumn,
|
|
1142
|
+
crud,
|
|
1143
|
+
canRemove: "remove" in crud,
|
|
1144
|
+
actions,
|
|
1145
|
+
searchable: meta.searchable,
|
|
1146
|
+
vectorSearchable: meta.vectorSearchable,
|
|
1147
|
+
searchIndexes: meta.searchIndexes,
|
|
1148
|
+
relations: meta.relations
|
|
1149
|
+
};
|
|
1150
|
+
}
|
|
1151
|
+
/**
|
|
1152
|
+
* One column. A server-declared display-only column (`fieldMeta.decoration`)
|
|
1153
|
+
* is never sortable, filterable or groupable and always nullable (a decoration
|
|
1154
|
+
* can be absent); an ordinary one takes its capabilities from `/meta`.
|
|
1155
|
+
*/
|
|
1156
|
+
function buildColumn(path, prop, fieldMeta, resolved) {
|
|
1157
|
+
const { options, valueHelpInfo, distinct } = resolved;
|
|
1158
|
+
const maxLengthMeta = getFieldMeta(prop, EXPECT_MAX_LENGTH);
|
|
1159
|
+
const tableType = getFieldMeta(prop, UI_TABLE_TYPE);
|
|
1160
|
+
const sharedType = getFieldMeta(prop, UI_TYPE);
|
|
1161
|
+
const tableComponent = getFieldMeta(prop, UI_TABLE_COMPONENT);
|
|
1162
|
+
const decoration = fieldMeta.decoration === true;
|
|
1163
|
+
const filterOps = decoration ? void 0 : fieldMeta.filterOps;
|
|
1164
|
+
return {
|
|
1165
|
+
path,
|
|
1166
|
+
label: getFieldMeta(prop, "meta.label") ?? humanizePath(path),
|
|
1167
|
+
type: tableType ?? sharedType ?? (valueHelpInfo ? "ref" : inferDisplayType(prop, options)),
|
|
1168
|
+
valueKind: inferValueKind(prop),
|
|
1169
|
+
component: tableComponent,
|
|
1170
|
+
selectWith: getFieldMeta(prop, UI_TABLE_SELECT_WITH),
|
|
1171
|
+
sortable: !decoration && (fieldMeta.sortable ?? false),
|
|
1172
|
+
filterable: !decoration && (fieldMeta.filterable ?? false),
|
|
1173
|
+
...filterOps && { filterOps: [...filterOps] },
|
|
1174
|
+
nullable: decoration || prop.optional === true,
|
|
1175
|
+
width: getFieldMeta(prop, UI_TABLE_WIDTH),
|
|
1176
|
+
maxLen: maxLengthMeta?.length,
|
|
1177
|
+
order: getFieldMeta(prop, "ui.table.order") ?? Infinity,
|
|
1178
|
+
options,
|
|
1179
|
+
valueHelpInfo,
|
|
1180
|
+
...distinct && { distinct },
|
|
1181
|
+
...extractMeasurement(prop)
|
|
1182
|
+
};
|
|
1183
|
+
}
|
|
1184
|
+
/**
|
|
1185
|
+
* `preferredId` ∪ every delegated action's `idMap` paths, in that order —
|
|
1186
|
+
* the same array when no action carries an `idMap`.
|
|
1187
|
+
*/
|
|
1188
|
+
function identifierFieldsOf(preferredId, actions) {
|
|
1189
|
+
let out;
|
|
1190
|
+
for (const a of actions) {
|
|
1191
|
+
if (!a.idMap) continue;
|
|
1192
|
+
for (const path of Object.values(a.idMap)) {
|
|
1193
|
+
if (preferredId.includes(path) || out?.includes(path)) continue;
|
|
1194
|
+
(out ??= [...preferredId]).push(path);
|
|
1195
|
+
}
|
|
1196
|
+
}
|
|
1197
|
+
return out ?? preferredId;
|
|
1198
|
+
}
|
|
1199
|
+
/** A field the caller may read: listed in `meta.fields` and not write-only. */
|
|
1200
|
+
function isReadable(field) {
|
|
1201
|
+
return field !== void 0 && field.writeOnly !== true;
|
|
1202
|
+
}
|
|
1203
|
+
/** Sort by (order ?? 0). `toSorted` is stable per spec, so ties preserve declaration order. */
|
|
1204
|
+
function byOrder(xs) {
|
|
1205
|
+
return xs.toSorted((x, y) => (x.order ?? 0) - (y.order ?? 0));
|
|
1206
|
+
}
|
|
1207
|
+
/**
|
|
1208
|
+
* Partition actions by `level`, sort each group by `(order ?? 0)` then
|
|
1209
|
+
* declaration order, and pick the first `default: true` entry per level.
|
|
1210
|
+
* The synthesised `__remove` UI action lives outside this set and is never
|
|
1211
|
+
* a candidate for `default.row`.
|
|
1212
|
+
*/
|
|
1213
|
+
function groupActions(actions) {
|
|
1214
|
+
const table = [];
|
|
1215
|
+
const row = [];
|
|
1216
|
+
const rows = [];
|
|
1217
|
+
for (const a of actions) if (a.level === "table") table.push(a);
|
|
1218
|
+
else if (a.level === "row") row.push(a);
|
|
1219
|
+
else if (a.level === "rows") rows.push(a);
|
|
1220
|
+
const tableSorted = byOrder(table);
|
|
1221
|
+
const rowSorted = byOrder(row);
|
|
1222
|
+
const rowsSorted = byOrder(rows);
|
|
1223
|
+
return {
|
|
1224
|
+
table: tableSorted,
|
|
1225
|
+
row: rowSorted,
|
|
1226
|
+
rows: rowsSorted,
|
|
1227
|
+
default: {
|
|
1228
|
+
table: tableSorted.find((a) => a.default === true),
|
|
1229
|
+
row: rowSorted.find((a) => a.default === true),
|
|
1230
|
+
rows: rowsSorted.find((a) => a.default === true)
|
|
1231
|
+
}
|
|
1232
|
+
};
|
|
1233
|
+
}
|
|
1234
|
+
/** Infers a display type string from the annotated type's kind and designType. */
|
|
1235
|
+
function inferDisplayType(prop, literalOpts) {
|
|
1236
|
+
const kind = prop.type.kind;
|
|
1237
|
+
if (kind === "array") return "array";
|
|
1238
|
+
if (kind === "object") return "object";
|
|
1239
|
+
if (kind === "union") return literalOpts !== void 0 ? "enum" : "union";
|
|
1240
|
+
if (kind === "") {
|
|
1241
|
+
const final = prop.type;
|
|
1242
|
+
const dt = final.designType;
|
|
1243
|
+
if (dt === "number") return final.tags?.has("timestamp") ? "datetime" : "number";
|
|
1244
|
+
if (dt === "decimal") return "number";
|
|
1245
|
+
if (dt === "boolean") return "boolean";
|
|
1246
|
+
return "text";
|
|
1247
|
+
}
|
|
1248
|
+
return "text";
|
|
1249
|
+
}
|
|
1250
|
+
/**
|
|
1251
|
+
* Number metadata that makes the server treat a plain `number` column as an
|
|
1252
|
+
* integer (mirrors the db layer's `valueTypeOf`), beside the `int` / `expect.int` tags.
|
|
1253
|
+
*/
|
|
1254
|
+
const INTEGER_NUMBER_META = [
|
|
1255
|
+
"expect.int",
|
|
1256
|
+
"db.default.increment",
|
|
1257
|
+
"db.default.now",
|
|
1258
|
+
"db.agg.count",
|
|
1259
|
+
"db.agg.countDistinct"
|
|
1260
|
+
];
|
|
1261
|
+
/** The kind of one scalar leaf, or `undefined` for a non-scalar / unknown design type. */
|
|
1262
|
+
function scalarValueKind(prop) {
|
|
1263
|
+
const final = prop.type;
|
|
1264
|
+
const tags = final.tags;
|
|
1265
|
+
switch (final.designType) {
|
|
1266
|
+
case "string": return tags?.has("isoDate") ? "isoDate" : tags?.has("date") ? "date" : "string";
|
|
1267
|
+
case "number":
|
|
1268
|
+
if (tags?.has("timestamp")) return "timestamp";
|
|
1269
|
+
if (tags?.has("int") || INTEGER_NUMBER_META.some((k) => prop.metadata.has(k))) return "integer";
|
|
1270
|
+
return "number";
|
|
1271
|
+
case "decimal": return "decimal";
|
|
1272
|
+
case "boolean": return "boolean";
|
|
1273
|
+
default: return;
|
|
1274
|
+
}
|
|
1275
|
+
}
|
|
1276
|
+
/** Kinds of every non-null leaf of a (possibly nested) union; `null` when one is not a scalar. */
|
|
1277
|
+
function collectUnionKinds(items, out) {
|
|
1278
|
+
for (const item of items) {
|
|
1279
|
+
if (item.type.kind === "union") {
|
|
1280
|
+
collectUnionKinds(item.type.items, out);
|
|
1281
|
+
continue;
|
|
1282
|
+
}
|
|
1283
|
+
if (item.type.kind === "") {
|
|
1284
|
+
const dt = item.type.designType;
|
|
1285
|
+
if (dt === "null" || dt === "undefined") continue;
|
|
1286
|
+
out.add(scalarValueKind(item));
|
|
1287
|
+
continue;
|
|
1288
|
+
}
|
|
1289
|
+
out.add(void 0);
|
|
1290
|
+
}
|
|
1291
|
+
}
|
|
1292
|
+
/**
|
|
1293
|
+
* Scalar storage kind of the column's value — what the server's filter guard
|
|
1294
|
+
* checks. A scalar maps to its kind; a union that agrees on one kind across
|
|
1295
|
+
* its non-null members (`'a' | 'b'`, `1 | 2`, `number.timestamp | null`)
|
|
1296
|
+
* takes it; arrays, objects, JSON and mixed unions have none (`undefined`),
|
|
1297
|
+
* which means no value coercion.
|
|
1298
|
+
*/
|
|
1299
|
+
function inferValueKind(prop) {
|
|
1300
|
+
const kind = prop.type.kind;
|
|
1301
|
+
if (kind === "") return scalarValueKind(prop);
|
|
1302
|
+
if (kind !== "union") return void 0;
|
|
1303
|
+
const kinds = /* @__PURE__ */ new Set();
|
|
1304
|
+
collectUnionKinds(prop.type.items, kinds);
|
|
1305
|
+
if (kinds.size !== 1) return void 0;
|
|
1306
|
+
return [...kinds][0];
|
|
1307
|
+
}
|
|
1308
|
+
//#endregion
|
|
1035
1309
|
//#region src/shared/meta-cache.ts
|
|
1310
|
+
/**
|
|
1311
|
+
* Enough for every URL a page touches — a table, the dictionaries its
|
|
1312
|
+
* filters and forms pick from — plus the recent navigation history, so going
|
|
1313
|
+
* back is instant; small enough to cap a long session at a few MB of parsed
|
|
1314
|
+
* `/meta`. Twice the `@atscript/db-client` `MetaStore` body limit, since
|
|
1315
|
+
* parametric URLs share bodies.
|
|
1316
|
+
*/
|
|
1317
|
+
const DEFAULT_MAX_ENTRIES = 100;
|
|
1318
|
+
let maxEntries = DEFAULT_MAX_ENTRIES;
|
|
1319
|
+
/** Per-URL entries, least recently used first. Browser only. */
|
|
1036
1320
|
const cache = /* @__PURE__ */ new Map();
|
|
1321
|
+
const slots = /* @__PURE__ */ new WeakMap();
|
|
1322
|
+
/** Derived shapes per `ETag`, counted by the cached entries that use them. Browser only. */
|
|
1323
|
+
const derivedByEtag = /* @__PURE__ */ new Map();
|
|
1324
|
+
/** Custom `/meta` stores of cached clients, counted by the entries that use them. */
|
|
1325
|
+
const customStores = /* @__PURE__ */ new Map();
|
|
1037
1326
|
/** `undefined` until the first `setMetaCacheIdentity` call; `null` = anonymous. */
|
|
1038
1327
|
let identity;
|
|
1328
|
+
const resetListeners = /* @__PURE__ */ new Set();
|
|
1039
1329
|
/**
|
|
1040
|
-
* Get or create the cache entry for `url`. First caller's `factory`
|
|
1041
|
-
* `Client`; subsequent callers reuse it. On
|
|
1042
|
-
* evicted so the next call retries.
|
|
1043
|
-
*
|
|
1330
|
+
* Get or create the cache entry for `url`. First caller's `factory` (and
|
|
1331
|
+
* `options.metaKey`) seeds the `Client`; subsequent callers reuse it. On
|
|
1332
|
+
* `meta` rejection, the entry is evicted so the next call retries. A hit
|
|
1333
|
+
* marks the URL most recently used; hold an entry you keep using with
|
|
1334
|
+
* {@link retainMetaEntry}. Without a browser `window` nothing is cached and
|
|
1335
|
+
* the client is asked for `metaStore: false`.
|
|
1044
1336
|
*/
|
|
1045
|
-
function getMetaEntry(url, factory) {
|
|
1337
|
+
function getMetaEntry(url, factory, options) {
|
|
1046
1338
|
const browser = typeof window !== "undefined";
|
|
1047
1339
|
const existing = browser ? cache.get(url) : void 0;
|
|
1048
|
-
if (existing)
|
|
1049
|
-
|
|
1340
|
+
if (existing) {
|
|
1341
|
+
touch(url, existing);
|
|
1342
|
+
return existing;
|
|
1343
|
+
}
|
|
1344
|
+
const f = factory ?? getDefaultClientFactory();
|
|
1345
|
+
let client;
|
|
1346
|
+
if (!browser) client = f(url, { metaStore: false });
|
|
1347
|
+
else if (options?.metaKey) client = f(url, { metaKey: options.metaKey });
|
|
1348
|
+
else client = f(url);
|
|
1050
1349
|
const meta = client.meta().catch((err) => {
|
|
1051
|
-
if (cache.get(url) === entry)
|
|
1350
|
+
if (cache.get(url) === entry) evict(url, entry);
|
|
1052
1351
|
throw err;
|
|
1053
1352
|
});
|
|
1353
|
+
const derived = meta.then((m) => derive(entry, client, m));
|
|
1354
|
+
const type = derived.then((d) => d.type);
|
|
1355
|
+
type.catch(() => {});
|
|
1054
1356
|
const entry = {
|
|
1055
1357
|
client,
|
|
1056
1358
|
meta,
|
|
1057
|
-
type
|
|
1359
|
+
type
|
|
1360
|
+
};
|
|
1361
|
+
const slot = {
|
|
1362
|
+
url,
|
|
1363
|
+
derived,
|
|
1364
|
+
holds: 0
|
|
1058
1365
|
};
|
|
1059
|
-
|
|
1366
|
+
slots.set(entry, slot);
|
|
1367
|
+
if (browser) {
|
|
1368
|
+
slot.store = customStoreOf(client);
|
|
1369
|
+
if (slot.store) customStores.set(slot.store, (customStores.get(slot.store) ?? 0) + 1);
|
|
1370
|
+
cache.set(url, entry);
|
|
1371
|
+
trim(entry);
|
|
1372
|
+
}
|
|
1060
1373
|
return entry;
|
|
1061
1374
|
}
|
|
1062
1375
|
/**
|
|
1376
|
+
* Keep `entry` in the cache while it is in use: a held entry is never
|
|
1377
|
+
* evicted by the size limit ({@link setMetaCacheMaxEntries}), so later
|
|
1378
|
+
* `getMetaEntry` / `resolveValueHelp` calls for its URL keep returning it.
|
|
1379
|
+
* Call the returned function (idempotent) once done — `useTable` and the
|
|
1380
|
+
* form value-help do this for their component's lifetime. A reset still
|
|
1381
|
+
* drops held entries. Since 0.1.154.
|
|
1382
|
+
*/
|
|
1383
|
+
function retainMetaEntry(entry) {
|
|
1384
|
+
const slot = slots.get(entry);
|
|
1385
|
+
if (!slot) return () => {};
|
|
1386
|
+
slot.holds++;
|
|
1387
|
+
let released = false;
|
|
1388
|
+
return () => {
|
|
1389
|
+
if (released) return;
|
|
1390
|
+
released = true;
|
|
1391
|
+
slot.holds--;
|
|
1392
|
+
if (slot.holds === 0 && cache.get(slot.url) === entry) {
|
|
1393
|
+
touch(slot.url, entry);
|
|
1394
|
+
trim();
|
|
1395
|
+
}
|
|
1396
|
+
};
|
|
1397
|
+
}
|
|
1398
|
+
/**
|
|
1399
|
+
* How many URLs the meta cache keeps (default `100`). Least recently used
|
|
1400
|
+
* entries beyond it are dropped — entries held with {@link retainMetaEntry}
|
|
1401
|
+
* (every mounted table and form value-help) never are, and may exceed it.
|
|
1402
|
+
* At least `1`; `Infinity` turns the limit off; no argument restores the
|
|
1403
|
+
* default. Since 0.1.154.
|
|
1404
|
+
*/
|
|
1405
|
+
function setMetaCacheMaxEntries(max) {
|
|
1406
|
+
maxEntries = max === void 0 ? DEFAULT_MAX_ENTRIES : max >= 1 ? max : 1;
|
|
1407
|
+
trim();
|
|
1408
|
+
}
|
|
1409
|
+
function touch(url, entry) {
|
|
1410
|
+
cache.delete(url);
|
|
1411
|
+
cache.set(url, entry);
|
|
1412
|
+
}
|
|
1413
|
+
/**
|
|
1414
|
+
* Drop least recently used entries nothing holds until the cache fits.
|
|
1415
|
+
* `spare` — the entry just handed out, about to be retained by its caller.
|
|
1416
|
+
*/
|
|
1417
|
+
function trim(spare) {
|
|
1418
|
+
if (cache.size <= maxEntries) return;
|
|
1419
|
+
for (const [url, entry] of cache) {
|
|
1420
|
+
if (entry !== spare && slots.get(entry).holds === 0) evict(url, entry);
|
|
1421
|
+
if (cache.size <= maxEntries) return;
|
|
1422
|
+
}
|
|
1423
|
+
}
|
|
1424
|
+
/**
|
|
1425
|
+
* Remove `entry` and release what only it kept: its shared `Derived` and
|
|
1426
|
+
* custom store. A reset no longer reaches its client, so the client's memoized
|
|
1427
|
+
* `/meta` goes now — a factory that reuses clients per URL would otherwise
|
|
1428
|
+
* hand it to the next viewer.
|
|
1429
|
+
*/
|
|
1430
|
+
function evict(url, entry) {
|
|
1431
|
+
cache.delete(url);
|
|
1432
|
+
entry.client.invalidateMeta?.();
|
|
1433
|
+
const slot = slots.get(entry);
|
|
1434
|
+
if (slot.etag !== void 0 && --derivedByEtag.get(slot.etag).refs === 0) derivedByEtag.delete(slot.etag);
|
|
1435
|
+
if (slot.store) {
|
|
1436
|
+
const refs = customStores.get(slot.store);
|
|
1437
|
+
if (refs > 1) customStores.set(slot.store, refs - 1);
|
|
1438
|
+
else {
|
|
1439
|
+
slot.store.clear();
|
|
1440
|
+
customStores.delete(slot.store);
|
|
1441
|
+
}
|
|
1442
|
+
}
|
|
1443
|
+
}
|
|
1444
|
+
/**
|
|
1445
|
+
* The `TableDef` for a cache entry, built once per entry — and once per
|
|
1446
|
+
* `/meta` `ETag` across entries. Since 0.1.153.
|
|
1447
|
+
*/
|
|
1448
|
+
function getMetaTableDef(entry) {
|
|
1449
|
+
if (!entry.tableDef) {
|
|
1450
|
+
const derived = slots.get(entry)?.derived;
|
|
1451
|
+
entry.tableDef = derived ? Promise.all([entry.meta, derived]).then(([meta, d]) => d.tableDef ??= createTableDef(meta, d.type)) : Promise.all([entry.meta, entry.type]).then(([meta, type]) => createTableDef(meta, type));
|
|
1452
|
+
}
|
|
1453
|
+
return entry.tableDef;
|
|
1454
|
+
}
|
|
1455
|
+
/**
|
|
1456
|
+
* Shares the `Derived` of one `ETag` among the cached entries that loaded it;
|
|
1457
|
+
* an entry no longer cached (evicted, reset, server render) keeps its own.
|
|
1458
|
+
*/
|
|
1459
|
+
function derive(entry, client, meta) {
|
|
1460
|
+
const slot = slots.get(entry);
|
|
1461
|
+
const etag = cache.get(slot.url) === entry ? client.metaEtag?.() : void 0;
|
|
1462
|
+
if (etag === void 0) return { type: deserializeAnnotatedType(meta.type) };
|
|
1463
|
+
let shared = derivedByEtag.get(etag);
|
|
1464
|
+
if (shared) shared.refs++;
|
|
1465
|
+
else {
|
|
1466
|
+
shared = {
|
|
1467
|
+
derived: { type: deserializeAnnotatedType(meta.type) },
|
|
1468
|
+
refs: 1
|
|
1469
|
+
};
|
|
1470
|
+
derivedByEtag.set(etag, shared);
|
|
1471
|
+
}
|
|
1472
|
+
slot.etag = etag;
|
|
1473
|
+
return shared.derived;
|
|
1474
|
+
}
|
|
1475
|
+
/**
|
|
1476
|
+
* The custom `/meta` store a factory gave `client`, so a reset (and the
|
|
1477
|
+
* eviction of its last cached client) can clear it. The shared
|
|
1478
|
+
* `defaultMetaStore` is skipped: a reset clears it through
|
|
1479
|
+
* `clearMetaStore()`, and an eviction must not drop the ETags of every
|
|
1480
|
+
* other URL. A store-less client (`metaStore: false`) is skipped too.
|
|
1481
|
+
*/
|
|
1482
|
+
function customStoreOf(client) {
|
|
1483
|
+
const store = client.metaStore;
|
|
1484
|
+
return store && store !== defaultMetaStore ? store : void 0;
|
|
1485
|
+
}
|
|
1486
|
+
/**
|
|
1063
1487
|
* Drop every cached `/meta` — including the copy each cached `Client`
|
|
1064
|
-
* memoizes
|
|
1488
|
+
* memoizes and the `@atscript/db-client` meta stores used to revalidate it
|
|
1489
|
+
* (the shared default one and any custom one a factory supplied) — the
|
|
1490
|
+
* value-help searches shared under them, and everything registered through
|
|
1491
|
+
* {@link onMetaCacheReset} (e.g. the table-presets cache). Entries already
|
|
1065
1492
|
* handed out keep working; the next `getMetaEntry` refetches.
|
|
1066
1493
|
*/
|
|
1067
1494
|
function resetMetaCache() {
|
|
1068
1495
|
const entries = [...cache.values()];
|
|
1069
1496
|
cache.clear();
|
|
1497
|
+
derivedByEtag.clear();
|
|
1070
1498
|
for (const entry of entries) entry.client.invalidateMeta?.();
|
|
1499
|
+
clearMetaStore();
|
|
1500
|
+
for (const store of customStores.keys()) store.clear();
|
|
1501
|
+
customStores.clear();
|
|
1071
1502
|
invalidateValueHelpCache();
|
|
1503
|
+
for (const listener of resetListeners) listener();
|
|
1072
1504
|
}
|
|
1073
1505
|
/**
|
|
1074
1506
|
* Bind the meta cache to the current viewer. Call it whenever the signed-in
|
|
@@ -1076,6 +1508,9 @@ function resetMetaCache() {
|
|
|
1076
1508
|
* every reload of the current user. A key that differs from the previous
|
|
1077
1509
|
* one resets the cache (`resetMetaCache`); the same key keeps it. `null` /
|
|
1078
1510
|
* `undefined` = anonymous.
|
|
1511
|
+
*
|
|
1512
|
+
* Binding an identity also turns on caches that are only safe when the app
|
|
1513
|
+
* reports sign-in changes, such as the table-presets cache.
|
|
1079
1514
|
*/
|
|
1080
1515
|
function setMetaCacheIdentity(key) {
|
|
1081
1516
|
const next = key ?? null;
|
|
@@ -1083,6 +1518,24 @@ function setMetaCacheIdentity(key) {
|
|
|
1083
1518
|
identity = next;
|
|
1084
1519
|
resetMetaCache();
|
|
1085
1520
|
}
|
|
1521
|
+
/**
|
|
1522
|
+
* The identity last passed to {@link setMetaCacheIdentity}: `undefined` when
|
|
1523
|
+
* the app never bound one, `null` for anonymous. Since 0.1.153.
|
|
1524
|
+
*/
|
|
1525
|
+
function getMetaCacheIdentity() {
|
|
1526
|
+
return identity;
|
|
1527
|
+
}
|
|
1528
|
+
/**
|
|
1529
|
+
* Run `listener` on every {@link resetMetaCache} (and so on every identity
|
|
1530
|
+
* change). For caches that belong to the signed-in viewer. Returns an
|
|
1531
|
+
* unsubscribe function. Since 0.1.153.
|
|
1532
|
+
*/
|
|
1533
|
+
function onMetaCacheReset(listener) {
|
|
1534
|
+
resetListeners.add(listener);
|
|
1535
|
+
return () => {
|
|
1536
|
+
resetListeners.delete(listener);
|
|
1537
|
+
};
|
|
1538
|
+
}
|
|
1086
1539
|
//#endregion
|
|
1087
1540
|
//#region src/value-help/resolve.ts
|
|
1088
1541
|
/**
|
|
@@ -1354,6 +1807,36 @@ const serverManagedPlugin = (ctx, def, value) => {
|
|
|
1354
1807
|
if (value !== void 0 || def.metadata.has("db.rel.FK")) return void 0;
|
|
1355
1808
|
return dbPlugin(Object.create(ctx, { context: { value: INSERT_CONTEXT } }), def, value);
|
|
1356
1809
|
};
|
|
1810
|
+
/**
|
|
1811
|
+
* The props of the fields `createFormDef` marked `derived`, at any depth — a
|
|
1812
|
+
* computed view column (`metaFields`) or an edit form's `@db.onUpdate.now`
|
|
1813
|
+
* field carries no annotation the plugin above could read. The validator
|
|
1814
|
+
* matches by prop, so a prop that is also used where it is not derived (an
|
|
1815
|
+
* interface's stamp below a union of several types, too) stays validated.
|
|
1816
|
+
*/
|
|
1817
|
+
function collectDerivedProps(def) {
|
|
1818
|
+
const derived = /* @__PURE__ */ new Set();
|
|
1819
|
+
const kept = /* @__PURE__ */ new Set();
|
|
1820
|
+
const walk = (fields) => {
|
|
1821
|
+
for (const field of fields) {
|
|
1822
|
+
if (field.derived) {
|
|
1823
|
+
derived.add(field.prop);
|
|
1824
|
+
continue;
|
|
1825
|
+
}
|
|
1826
|
+
kept.add(field.prop);
|
|
1827
|
+
if (isObjectField(field)) walk(field.objectDef.fields);
|
|
1828
|
+
else if (isArrayField(field)) walk([field.itemField]);
|
|
1829
|
+
else if (isTupleField(field)) walk(field.itemFields);
|
|
1830
|
+
else if (isUnionField(field)) for (const v of field.unionVariants) {
|
|
1831
|
+
if (v.def) walk(v.def.fields);
|
|
1832
|
+
if (v.itemField) walk([v.itemField]);
|
|
1833
|
+
}
|
|
1834
|
+
}
|
|
1835
|
+
};
|
|
1836
|
+
walk(def.fields);
|
|
1837
|
+
for (const prop of kept) derived.delete(prop);
|
|
1838
|
+
return derived;
|
|
1839
|
+
}
|
|
1357
1840
|
let defaultValidatorPlugins = [];
|
|
1358
1841
|
/** Replace the default validator plugins applied to every form/field validator. */
|
|
1359
1842
|
function setDefaultValidatorPlugins(plugins) {
|
|
@@ -1368,14 +1851,17 @@ function getDefaultValidatorPlugins() {
|
|
|
1368
1851
|
*
|
|
1369
1852
|
* Validator is created once and reused on every call.
|
|
1370
1853
|
* ATScript's @expect.* validation runs automatically; server-managed db
|
|
1371
|
-
* fields (
|
|
1854
|
+
* fields (every `derived` field, absent `@db.default*` / version) pass.
|
|
1372
1855
|
* For custom `ui.fn.*` validators, install ui-fns and pass its plugin via `opts.plugins`.
|
|
1373
1856
|
*/
|
|
1374
1857
|
function getFormValidator(def, opts) {
|
|
1858
|
+
const derived = collectDerivedProps(def);
|
|
1859
|
+
const derivedPlugin = (_ctx, prop) => derived.has(prop) ? true : void 0;
|
|
1375
1860
|
const validator = new Validator(def.type, {
|
|
1376
1861
|
unknownProps: "ignore",
|
|
1377
1862
|
...opts,
|
|
1378
1863
|
plugins: [
|
|
1864
|
+
...derived.size > 0 ? [derivedPlugin] : [],
|
|
1379
1865
|
serverManagedPlugin,
|
|
1380
1866
|
...defaultValidatorPlugins,
|
|
1381
1867
|
...opts?.plugins ?? []
|
|
@@ -2226,23 +2712,6 @@ function resolveSingularLabel(meta) {
|
|
|
2226
2712
|
return getFieldMeta(meta, "ui.form.label.singular") || "item";
|
|
2227
2713
|
}
|
|
2228
2714
|
//#endregion
|
|
2229
|
-
//#region src/form/measurement.ts
|
|
2230
|
-
/**
|
|
2231
|
-
* Read measurement annotations off a single field prop. Returns `undefined`
|
|
2232
|
-
* for any annotation that's absent — callers can spread the result into a
|
|
2233
|
-
* larger record.
|
|
2234
|
-
*/
|
|
2235
|
-
function extractMeasurement(prop) {
|
|
2236
|
-
const precisionMeta = getFieldMeta(prop, DB_COLUMN_PRECISION);
|
|
2237
|
-
return {
|
|
2238
|
-
currencyCode: getFieldMeta(prop, DB_AMOUNT_CURRENCY),
|
|
2239
|
-
currencyRefField: getFieldMeta(prop, DB_AMOUNT_CURRENCY_REF),
|
|
2240
|
-
unitCode: getFieldMeta(prop, DB_UNIT),
|
|
2241
|
-
unitRefField: getFieldMeta(prop, DB_UNIT_REF),
|
|
2242
|
-
precisionScale: precisionMeta?.scale
|
|
2243
|
-
};
|
|
2244
|
-
}
|
|
2245
|
-
//#endregion
|
|
2246
2715
|
//#region src/form/decimal-format.ts
|
|
2247
2716
|
const WHITESPACE_GROUP_RE = /[ ]/g;
|
|
2248
2717
|
const numberFormatCache = /* @__PURE__ */ new Map();
|
|
@@ -2557,231 +3026,6 @@ function groupInteger(integer, locale) {
|
|
|
2557
3026
|
return out.join(sep);
|
|
2558
3027
|
}
|
|
2559
3028
|
//#endregion
|
|
2560
|
-
//#region src/table/create-table-def.ts
|
|
2561
|
-
/**
|
|
2562
|
-
* Builds a TableDef from a moost-db MetaResponse.
|
|
2563
|
-
*
|
|
2564
|
-
* 1. Deserializes `meta.type` into a live TAtscriptAnnotatedType
|
|
2565
|
-
* 2. Flattens to discover all field paths
|
|
2566
|
-
* 3. Builds ColumnDef per field using annotations + meta.fields capabilities
|
|
2567
|
-
* 4. Sorts by @ui.table.order
|
|
2568
|
-
*/
|
|
2569
|
-
function createTableDef(meta, preDeserializedType) {
|
|
2570
|
-
const type = preDeserializedType ?? deserializeAnnotatedType(meta.type);
|
|
2571
|
-
const flatMap = type.type.kind === "object" ? flattenAnnotatedType(type, { excludePhantomTypes: true }) : /* @__PURE__ */ new Map();
|
|
2572
|
-
const columns = [];
|
|
2573
|
-
for (const [path, prop] of flatMap.entries()) {
|
|
2574
|
-
if (path === "") continue;
|
|
2575
|
-
if (path === meta.versionColumn) continue;
|
|
2576
|
-
const fieldMeta = meta.fields[path];
|
|
2577
|
-
if (!isReadable(fieldMeta)) continue;
|
|
2578
|
-
if (getFieldMeta(prop, "ui.table.exclude") !== void 0) continue;
|
|
2579
|
-
const options = extractFieldLiteralOptions(prop);
|
|
2580
|
-
const valueHelpInfo = extractValueHelp(prop);
|
|
2581
|
-
let distinct;
|
|
2582
|
-
if (!valueHelpInfo && !options && fieldMeta.groupable && fieldMeta.filterable) {
|
|
2583
|
-
const url = type.metadata.get(DB_HTTP_PATH);
|
|
2584
|
-
if (url && getFieldMeta(prop, "ui.valueHelp.distinct") !== void 0) distinct = {
|
|
2585
|
-
url,
|
|
2586
|
-
field: path
|
|
2587
|
-
};
|
|
2588
|
-
}
|
|
2589
|
-
columns.push(buildColumn(path, prop, fieldMeta, {
|
|
2590
|
-
options,
|
|
2591
|
-
valueHelpInfo,
|
|
2592
|
-
distinct
|
|
2593
|
-
}));
|
|
2594
|
-
}
|
|
2595
|
-
if (meta.decorations) {
|
|
2596
|
-
const decoType = deserializeAnnotatedType(meta.decorations);
|
|
2597
|
-
if (decoType.type.kind === "object") for (const [path, prop] of decoType.type.props) {
|
|
2598
|
-
const fieldMeta = meta.fields[path];
|
|
2599
|
-
if (!fieldMeta?.decoration || flatMap.has(path)) continue;
|
|
2600
|
-
if (getFieldMeta(prop, "ui.table.exclude") !== void 0) continue;
|
|
2601
|
-
flatMap.set(path, prop);
|
|
2602
|
-
columns.push(buildColumn(path, prop, fieldMeta, { options: extractFieldLiteralOptions(prop) }));
|
|
2603
|
-
}
|
|
2604
|
-
}
|
|
2605
|
-
columns.sort((a, b) => a.order - b.order);
|
|
2606
|
-
const actions = groupActions(meta.actions ?? []);
|
|
2607
|
-
const crud = meta.crud ?? {};
|
|
2608
|
-
const preferredId = meta.preferredId ?? meta.primaryKeys;
|
|
2609
|
-
return {
|
|
2610
|
-
type,
|
|
2611
|
-
columns,
|
|
2612
|
-
flatMap,
|
|
2613
|
-
fetchableFields: new Set(Object.keys(meta.fields).filter((p) => isReadable(meta.fields[p]))),
|
|
2614
|
-
primaryKeys: meta.primaryKeys,
|
|
2615
|
-
preferredId,
|
|
2616
|
-
identifierFields: identifierFieldsOf(preferredId, meta.actions ?? []),
|
|
2617
|
-
versionColumn: meta.versionColumn,
|
|
2618
|
-
crud,
|
|
2619
|
-
canRemove: "remove" in crud,
|
|
2620
|
-
actions,
|
|
2621
|
-
searchable: meta.searchable,
|
|
2622
|
-
vectorSearchable: meta.vectorSearchable,
|
|
2623
|
-
searchIndexes: meta.searchIndexes,
|
|
2624
|
-
relations: meta.relations
|
|
2625
|
-
};
|
|
2626
|
-
}
|
|
2627
|
-
/**
|
|
2628
|
-
* One column. A server-declared display-only column (`fieldMeta.decoration`)
|
|
2629
|
-
* is never sortable, filterable or groupable and always nullable (a decoration
|
|
2630
|
-
* can be absent); an ordinary one takes its capabilities from `/meta`.
|
|
2631
|
-
*/
|
|
2632
|
-
function buildColumn(path, prop, fieldMeta, resolved) {
|
|
2633
|
-
const { options, valueHelpInfo, distinct } = resolved;
|
|
2634
|
-
const maxLengthMeta = getFieldMeta(prop, EXPECT_MAX_LENGTH);
|
|
2635
|
-
const tableType = getFieldMeta(prop, UI_TABLE_TYPE);
|
|
2636
|
-
const sharedType = getFieldMeta(prop, UI_TYPE);
|
|
2637
|
-
const tableComponent = getFieldMeta(prop, UI_TABLE_COMPONENT);
|
|
2638
|
-
const decoration = fieldMeta.decoration === true;
|
|
2639
|
-
const filterOps = decoration ? void 0 : fieldMeta.filterOps;
|
|
2640
|
-
return {
|
|
2641
|
-
path,
|
|
2642
|
-
label: getFieldMeta(prop, "meta.label") ?? humanizePath(path),
|
|
2643
|
-
type: tableType ?? sharedType ?? (valueHelpInfo ? "ref" : inferDisplayType(prop, options)),
|
|
2644
|
-
valueKind: inferValueKind(prop),
|
|
2645
|
-
component: tableComponent,
|
|
2646
|
-
selectWith: getFieldMeta(prop, UI_TABLE_SELECT_WITH),
|
|
2647
|
-
sortable: !decoration && (fieldMeta.sortable ?? false),
|
|
2648
|
-
filterable: !decoration && (fieldMeta.filterable ?? false),
|
|
2649
|
-
...filterOps && { filterOps: [...filterOps] },
|
|
2650
|
-
nullable: decoration || prop.optional === true,
|
|
2651
|
-
width: getFieldMeta(prop, UI_TABLE_WIDTH),
|
|
2652
|
-
maxLen: maxLengthMeta?.length,
|
|
2653
|
-
order: getFieldMeta(prop, "ui.table.order") ?? Infinity,
|
|
2654
|
-
options,
|
|
2655
|
-
valueHelpInfo,
|
|
2656
|
-
...distinct && { distinct },
|
|
2657
|
-
...extractMeasurement(prop)
|
|
2658
|
-
};
|
|
2659
|
-
}
|
|
2660
|
-
/**
|
|
2661
|
-
* `preferredId` ∪ every delegated action's `idMap` paths, in that order —
|
|
2662
|
-
* the same array when no action carries an `idMap`.
|
|
2663
|
-
*/
|
|
2664
|
-
function identifierFieldsOf(preferredId, actions) {
|
|
2665
|
-
let out;
|
|
2666
|
-
for (const a of actions) {
|
|
2667
|
-
if (!a.idMap) continue;
|
|
2668
|
-
for (const path of Object.values(a.idMap)) {
|
|
2669
|
-
if (preferredId.includes(path) || out?.includes(path)) continue;
|
|
2670
|
-
(out ??= [...preferredId]).push(path);
|
|
2671
|
-
}
|
|
2672
|
-
}
|
|
2673
|
-
return out ?? preferredId;
|
|
2674
|
-
}
|
|
2675
|
-
/** A field the caller may read: listed in `meta.fields` and not write-only. */
|
|
2676
|
-
function isReadable(field) {
|
|
2677
|
-
return field !== void 0 && field.writeOnly !== true;
|
|
2678
|
-
}
|
|
2679
|
-
/** Sort by (order ?? 0). `toSorted` is stable per spec, so ties preserve declaration order. */
|
|
2680
|
-
function byOrder(xs) {
|
|
2681
|
-
return xs.toSorted((x, y) => (x.order ?? 0) - (y.order ?? 0));
|
|
2682
|
-
}
|
|
2683
|
-
/**
|
|
2684
|
-
* Partition actions by `level`, sort each group by `(order ?? 0)` then
|
|
2685
|
-
* declaration order, and pick the first `default: true` entry per level.
|
|
2686
|
-
* The synthesised `__remove` UI action lives outside this set and is never
|
|
2687
|
-
* a candidate for `default.row`.
|
|
2688
|
-
*/
|
|
2689
|
-
function groupActions(actions) {
|
|
2690
|
-
const table = [];
|
|
2691
|
-
const row = [];
|
|
2692
|
-
const rows = [];
|
|
2693
|
-
for (const a of actions) if (a.level === "table") table.push(a);
|
|
2694
|
-
else if (a.level === "row") row.push(a);
|
|
2695
|
-
else if (a.level === "rows") rows.push(a);
|
|
2696
|
-
const tableSorted = byOrder(table);
|
|
2697
|
-
const rowSorted = byOrder(row);
|
|
2698
|
-
const rowsSorted = byOrder(rows);
|
|
2699
|
-
return {
|
|
2700
|
-
table: tableSorted,
|
|
2701
|
-
row: rowSorted,
|
|
2702
|
-
rows: rowsSorted,
|
|
2703
|
-
default: {
|
|
2704
|
-
table: tableSorted.find((a) => a.default === true),
|
|
2705
|
-
row: rowSorted.find((a) => a.default === true),
|
|
2706
|
-
rows: rowsSorted.find((a) => a.default === true)
|
|
2707
|
-
}
|
|
2708
|
-
};
|
|
2709
|
-
}
|
|
2710
|
-
/** Infers a display type string from the annotated type's kind and designType. */
|
|
2711
|
-
function inferDisplayType(prop, literalOpts) {
|
|
2712
|
-
const kind = prop.type.kind;
|
|
2713
|
-
if (kind === "array") return "array";
|
|
2714
|
-
if (kind === "object") return "object";
|
|
2715
|
-
if (kind === "union") return literalOpts !== void 0 ? "enum" : "union";
|
|
2716
|
-
if (kind === "") {
|
|
2717
|
-
const final = prop.type;
|
|
2718
|
-
const dt = final.designType;
|
|
2719
|
-
if (dt === "number") return final.tags?.has("timestamp") ? "datetime" : "number";
|
|
2720
|
-
if (dt === "decimal") return "number";
|
|
2721
|
-
if (dt === "boolean") return "boolean";
|
|
2722
|
-
return "text";
|
|
2723
|
-
}
|
|
2724
|
-
return "text";
|
|
2725
|
-
}
|
|
2726
|
-
/**
|
|
2727
|
-
* Number metadata that makes the server treat a plain `number` column as an
|
|
2728
|
-
* integer (mirrors the db layer's `valueTypeOf`), beside the `int` / `expect.int` tags.
|
|
2729
|
-
*/
|
|
2730
|
-
const INTEGER_NUMBER_META = [
|
|
2731
|
-
"expect.int",
|
|
2732
|
-
"db.default.increment",
|
|
2733
|
-
"db.default.now",
|
|
2734
|
-
"db.agg.count",
|
|
2735
|
-
"db.agg.countDistinct"
|
|
2736
|
-
];
|
|
2737
|
-
/** The kind of one scalar leaf, or `undefined` for a non-scalar / unknown design type. */
|
|
2738
|
-
function scalarValueKind(prop) {
|
|
2739
|
-
const final = prop.type;
|
|
2740
|
-
const tags = final.tags;
|
|
2741
|
-
switch (final.designType) {
|
|
2742
|
-
case "string": return tags?.has("isoDate") ? "isoDate" : tags?.has("date") ? "date" : "string";
|
|
2743
|
-
case "number":
|
|
2744
|
-
if (tags?.has("timestamp")) return "timestamp";
|
|
2745
|
-
if (tags?.has("int") || INTEGER_NUMBER_META.some((k) => prop.metadata.has(k))) return "integer";
|
|
2746
|
-
return "number";
|
|
2747
|
-
case "decimal": return "decimal";
|
|
2748
|
-
case "boolean": return "boolean";
|
|
2749
|
-
default: return;
|
|
2750
|
-
}
|
|
2751
|
-
}
|
|
2752
|
-
/** Kinds of every non-null leaf of a (possibly nested) union; `null` when one is not a scalar. */
|
|
2753
|
-
function collectUnionKinds(items, out) {
|
|
2754
|
-
for (const item of items) {
|
|
2755
|
-
if (item.type.kind === "union") {
|
|
2756
|
-
collectUnionKinds(item.type.items, out);
|
|
2757
|
-
continue;
|
|
2758
|
-
}
|
|
2759
|
-
if (item.type.kind === "") {
|
|
2760
|
-
const dt = item.type.designType;
|
|
2761
|
-
if (dt === "null" || dt === "undefined") continue;
|
|
2762
|
-
out.add(scalarValueKind(item));
|
|
2763
|
-
continue;
|
|
2764
|
-
}
|
|
2765
|
-
out.add(void 0);
|
|
2766
|
-
}
|
|
2767
|
-
}
|
|
2768
|
-
/**
|
|
2769
|
-
* Scalar storage kind of the column's value — what the server's filter guard
|
|
2770
|
-
* checks. A scalar maps to its kind; a union that agrees on one kind across
|
|
2771
|
-
* its non-null members (`'a' | 'b'`, `1 | 2`, `number.timestamp | null`)
|
|
2772
|
-
* takes it; arrays, objects, JSON and mixed unions have none (`undefined`),
|
|
2773
|
-
* which means no value coercion.
|
|
2774
|
-
*/
|
|
2775
|
-
function inferValueKind(prop) {
|
|
2776
|
-
const kind = prop.type.kind;
|
|
2777
|
-
if (kind === "") return scalarValueKind(prop);
|
|
2778
|
-
if (kind !== "union") return void 0;
|
|
2779
|
-
const kinds = /* @__PURE__ */ new Set();
|
|
2780
|
-
collectUnionKinds(prop.type.items, kinds);
|
|
2781
|
-
if (kinds.size !== 1) return void 0;
|
|
2782
|
-
return [...kinds][0];
|
|
2783
|
-
}
|
|
2784
|
-
//#endregion
|
|
2785
3029
|
//#region src/table/value-kind.ts
|
|
2786
3030
|
/** Storage kinds that hold plain numbers (a `timestamp` is epoch ms, but filters treat it as a date). */
|
|
2787
3031
|
const NUMERIC_VALUE_KINDS = new Set([
|
|
@@ -2912,4 +3156,4 @@ function resolvePath(model, table, view) {
|
|
|
2912
3156
|
return path.length > 0 ? path : void 0;
|
|
2913
3157
|
}
|
|
2914
3158
|
//#endregion
|
|
2915
|
-
export { DB_AMOUNT_CURRENCY, DB_AMOUNT_CURRENCY_REF, DB_COLUMN_PRECISION, DB_HTTP_PATH, DB_REL_FK, DB_UNIT, DB_UNIT_REF, DEFAULT_COL_SPAN, DEFAULT_ROW_SPAN, EXPECT_MAX_LENGTH, META_DEFAULT, META_DESCRIPTION, META_ID, META_LABEL, META_READONLY, META_REQUIRED, META_SENSITIVE, NUMERIC_VALUE_KINDS, StaticFieldResolver, UI_DICT_ATTR, UI_DICT_DESCR, UI_DICT_FILTERABLE, UI_DICT_LABEL, UI_DICT_SEARCHABLE, UI_DICT_SORTABLE, UI_FORM_ACTION, UI_FORM_ATTR, UI_FORM_AUTOCOMPLETE, UI_FORM_CLASSES, UI_FORM_COMPONENT, UI_FORM_DISABLED, UI_FORM_FN_ATTR, UI_FORM_FN_CLASSES, UI_FORM_FN_DESCRIPTION, UI_FORM_FN_DISABLED, UI_FORM_FN_HIDDEN, UI_FORM_FN_HINT, UI_FORM_FN_LABEL, UI_FORM_FN_OPTIONS, UI_FORM_FN_PLACEHOLDER, UI_FORM_FN_PREFIX, UI_FORM_FN_READONLY, UI_FORM_FN_STYLES, UI_FORM_FN_SUBMIT_DISABLED, UI_FORM_FN_SUBMIT_TEXT, UI_FORM_FN_TITLE, UI_FORM_FN_VALUE, UI_FORM_GRID_COL_SPAN, UI_FORM_GRID_ROW_SPAN, UI_FORM_HIDDEN, UI_FORM_HINT, UI_FORM_LABEL_SINGULAR, UI_FORM_OPTIONS, UI_FORM_ORDER, UI_FORM_PLACEHOLDER, UI_FORM_PREFIX, UI_FORM_PREFIX_ICON, UI_FORM_PREFIX_REF, UI_FORM_STYLES, UI_FORM_SUBMIT_TEXT, UI_FORM_SUFFIX, UI_FORM_SUFFIX_ICON, UI_FORM_SUFFIX_REF, UI_FORM_TYPE, UI_FORM_VALIDATE, UI_NAV_GROUP, UI_NAV_HIDDEN, UI_NAV_ORDER, UI_TABLE_ATTR, UI_TABLE_CLASSES, UI_TABLE_COMPONENT, UI_TABLE_EXCLUDE, UI_TABLE_FN_ATTR, UI_TABLE_FN_CLASSES, UI_TABLE_FN_PREFIX, UI_TABLE_FN_STYLES, UI_TABLE_ORDER, UI_TABLE_SELECT_WITH, UI_TABLE_STYLES, UI_TABLE_TYPE, UI_TABLE_WIDTH, UI_TYPE, ValueHelpClient, WF_ACTION_WITH_DATA, applyFormChanges, asArray, buildDescendantErrorCounts, buildFormDiff, buildFormRebase, buildGridClasses, buildModelRoutes, buildUnionVariants, collectDirtyPaths, createFieldValidator, createFormData, createFormDef, createFormValueResolver, createTableDef, deepClone, deepEqual, defaultResolver, deleteByPath, detectUnionVariant, enforceScale, escapeRegex, extractFieldLiteralOptions, extractLiteralOptions, extractMeasurement, extractValueHelp, fetchDistinctValues, formatDecimalForDisplay, getByPath, getColumn, getCurrencyDecimals, getCurrencyDisplayParts, getDecimalSeparator, getDeclaredFormActions, getDefaultClientFactory, getDefaultValidatorPlugins, getFieldMeta, getFilterableColumns, getFormValidator, getMetaEntry, getResolver, getSortableColumns, getThousandsSeparator, groupInteger, hasComputedAnnotations, hasFieldMeta, invalidateValueHelpCache, isArrayField, isFieldHidden, isObjectField, isPathDirty, isPickerDeclined, isPureLiteralUnion, isTupleField, isUnionField, iteratePathAncestors, joinDecimalString, joinPath, mergeErrorMaps, navigateHrefFor, omitPaths, optKey, optLabel, optionLabel, optionValue, parseColSpan, parseDecimalInput, parseRowSpan, parseStaticAttrs, parseStaticOptions, resetDefaultClientFactory, resetMetaCache, resetValueHelpCache, resolveAttrs, resolveFieldProp, resolveFormProp, resolveGridSpec, resolveOptions, resolveSingularLabel, resolveStatic, resolveValueHelp, setByPath, setDefaultClientFactory, setDefaultValidatorPlugins, setMetaCacheIdentity, setResolver, setValueHelpCacheTtl, splitDecimalString, str, unionVariantChanged, valueHelpDictPaths };
|
|
3159
|
+
export { DB_AMOUNT_CURRENCY, DB_AMOUNT_CURRENCY_REF, DB_COLUMN_PRECISION, DB_HTTP_PATH, DB_REL_FK, DB_UNIT, DB_UNIT_REF, DEFAULT_COL_SPAN, DEFAULT_ROW_SPAN, EXPECT_MAX_LENGTH, META_DEFAULT, META_DESCRIPTION, META_ID, META_LABEL, META_READONLY, META_REQUIRED, META_SENSITIVE, NUMERIC_VALUE_KINDS, StaticFieldResolver, UI_DICT_ATTR, UI_DICT_DESCR, UI_DICT_FILTERABLE, UI_DICT_LABEL, UI_DICT_SEARCHABLE, UI_DICT_SORTABLE, UI_FORM_ACTION, UI_FORM_ATTR, UI_FORM_AUTOCOMPLETE, UI_FORM_CLASSES, UI_FORM_COMPONENT, UI_FORM_DISABLED, UI_FORM_FN_ATTR, UI_FORM_FN_CLASSES, UI_FORM_FN_DESCRIPTION, UI_FORM_FN_DISABLED, UI_FORM_FN_HIDDEN, UI_FORM_FN_HINT, UI_FORM_FN_LABEL, UI_FORM_FN_OPTIONS, UI_FORM_FN_PLACEHOLDER, UI_FORM_FN_PREFIX, UI_FORM_FN_READONLY, UI_FORM_FN_STYLES, UI_FORM_FN_SUBMIT_DISABLED, UI_FORM_FN_SUBMIT_TEXT, UI_FORM_FN_TITLE, UI_FORM_FN_VALUE, UI_FORM_GRID_COL_SPAN, UI_FORM_GRID_ROW_SPAN, UI_FORM_HIDDEN, UI_FORM_HINT, UI_FORM_LABEL_SINGULAR, UI_FORM_OPTIONS, UI_FORM_ORDER, UI_FORM_PLACEHOLDER, UI_FORM_PREFIX, UI_FORM_PREFIX_ICON, UI_FORM_PREFIX_REF, UI_FORM_STYLES, UI_FORM_SUBMIT_TEXT, UI_FORM_SUFFIX, UI_FORM_SUFFIX_ICON, UI_FORM_SUFFIX_REF, UI_FORM_TYPE, UI_FORM_VALIDATE, UI_NAV_GROUP, UI_NAV_HIDDEN, UI_NAV_ORDER, UI_TABLE_ATTR, UI_TABLE_CLASSES, UI_TABLE_COMPONENT, UI_TABLE_EXCLUDE, UI_TABLE_FN_ATTR, UI_TABLE_FN_CLASSES, UI_TABLE_FN_PREFIX, UI_TABLE_FN_STYLES, UI_TABLE_ORDER, UI_TABLE_SELECT_WITH, UI_TABLE_STYLES, UI_TABLE_TYPE, UI_TABLE_WIDTH, UI_TYPE, ValueHelpClient, WF_ACTION_WITH_DATA, applyFormChanges, asArray, buildDescendantErrorCounts, buildFormDiff, buildFormRebase, buildGridClasses, buildModelRoutes, buildUnionVariants, collectDirtyPaths, createFieldValidator, createFormData, createFormDef, createFormValueResolver, createTableDef, deepClone, deepEqual, defaultResolver, deleteByPath, detectUnionVariant, enforceScale, escapeRegex, extractFieldLiteralOptions, extractLiteralOptions, extractMeasurement, extractValueHelp, fetchDistinctValues, formatDecimalForDisplay, getByPath, getColumn, getCurrencyDecimals, getCurrencyDisplayParts, getDecimalSeparator, getDeclaredFormActions, getDefaultClientFactory, getDefaultValidatorPlugins, getFieldMeta, getFilterableColumns, getFormValidator, getMetaCacheIdentity, getMetaEntry, getMetaTableDef, getResolver, getSortableColumns, getThousandsSeparator, groupInteger, hasComputedAnnotations, hasFieldMeta, invalidateValueHelpCache, isArrayField, isFieldHidden, isObjectField, isPathDirty, isPickerDeclined, isPureLiteralUnion, isTupleField, isUnionField, iteratePathAncestors, joinDecimalString, joinPath, mergeErrorMaps, navigateHrefFor, omitPaths, onMetaCacheReset, optKey, optLabel, optionLabel, optionValue, parseColSpan, parseDecimalInput, parseRowSpan, parseStaticAttrs, parseStaticOptions, resetDefaultClientFactory, resetMetaCache, resetValueHelpCache, resolveAttrs, resolveFieldProp, resolveFormProp, resolveGridSpec, resolveOptions, resolveSingularLabel, resolveStatic, resolveValueHelp, retainMetaEntry, setByPath, setDefaultClientFactory, setDefaultValidatorPlugins, setMetaCacheIdentity, setMetaCacheMaxEntries, setResolver, setValueHelpCacheTtl, splitDecimalString, str, unionVariantChanged, valueHelpDictPaths };
|