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