@atscript/ui 0.1.205 → 0.1.207

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/dist/form/create-form-def.cjs +40 -58
  2. package/dist/form/create-form-def.d.cts +9 -0
  3. package/dist/form/create-form-def.d.mts +9 -0
  4. package/dist/form/create-form-def.mjs +39 -57
  5. package/dist/form/db-column.cjs +37 -0
  6. package/dist/form/db-column.mjs +35 -0
  7. package/dist/form/path-utils.cjs +20 -5
  8. package/dist/form/path-utils.d.cts +8 -1
  9. package/dist/form/path-utils.d.mts +8 -1
  10. package/dist/form/path-utils.mjs +20 -6
  11. package/dist/form/types.d.cts +1 -0
  12. package/dist/form/types.d.mts +1 -0
  13. package/dist/index.cjs +18 -3
  14. package/dist/index.d.cts +8 -5
  15. package/dist/index.d.mts +8 -5
  16. package/dist/index.mjs +11 -8
  17. package/dist/plugin/annotations.cjs +33 -14
  18. package/dist/plugin/annotations.mjs +34 -15
  19. package/dist/plugin/value-help-validation.cjs +26 -6
  20. package/dist/plugin/value-help-validation.mjs +26 -7
  21. package/dist/shared/annotation-keys.cjs +3 -0
  22. package/dist/shared/annotation-keys.mjs +3 -1
  23. package/dist/shared/meta-cache.cjs +5 -4
  24. package/dist/shared/meta-cache.d.cts +3 -2
  25. package/dist/shared/meta-cache.d.mts +3 -2
  26. package/dist/shared/meta-cache.mjs +5 -4
  27. package/dist/shared/value-label.cjs +14 -2
  28. package/dist/shared/value-label.d.cts +23 -6
  29. package/dist/shared/value-label.d.mts +23 -6
  30. package/dist/shared/value-label.mjs +14 -3
  31. package/dist/table/create-table-def.cjs +7 -1
  32. package/dist/table/create-table-def.mjs +7 -2
  33. package/dist/table/types.d.cts +1 -1
  34. package/dist/table/types.d.mts +1 -1
  35. package/dist/value-help/client-store.cjs +43 -0
  36. package/dist/value-help/client-store.d.mts +1 -0
  37. package/dist/value-help/client-store.mjs +40 -0
  38. package/dist/value-help/display-value.cjs +30 -0
  39. package/dist/value-help/display-value.d.cts +45 -0
  40. package/dist/value-help/display-value.d.mts +45 -0
  41. package/dist/value-help/display-value.mjs +30 -0
  42. package/dist/value-help/extract-ref.cjs +15 -4
  43. package/dist/value-help/extract-ref.d.cts +6 -1
  44. package/dist/value-help/extract-ref.d.mts +6 -1
  45. package/dist/value-help/extract-ref.mjs +16 -6
  46. package/dist/value-help/index.d.mts +6 -3
  47. package/dist/value-help/options.cjs +49 -8
  48. package/dist/value-help/options.d.cts +41 -4
  49. package/dist/value-help/options.d.mts +41 -4
  50. package/dist/value-help/options.mjs +47 -9
  51. package/dist/value-help/resolve-options.cjs +9 -7
  52. package/dist/value-help/resolve-options.d.cts +8 -9
  53. package/dist/value-help/resolve-options.d.mts +8 -9
  54. package/dist/value-help/resolve-options.mjs +9 -7
  55. package/dist/value-help/resolve.cjs +39 -8
  56. package/dist/value-help/resolve.d.cts +22 -5
  57. package/dist/value-help/resolve.d.mts +22 -5
  58. package/dist/value-help/resolve.mjs +40 -9
  59. package/dist/value-help/source.cjs +72 -0
  60. package/dist/value-help/source.d.cts +55 -0
  61. package/dist/value-help/source.d.mts +55 -0
  62. package/dist/value-help/source.mjs +71 -0
  63. package/dist/value-help/texts.cjs +315 -0
  64. package/dist/value-help/texts.d.cts +53 -0
  65. package/dist/value-help/texts.d.mts +53 -0
  66. package/dist/value-help/texts.mjs +310 -0
  67. package/dist/value-help/types.d.cts +10 -0
  68. package/dist/value-help/types.d.mts +10 -0
  69. package/dist/value-help/value-help-client.cjs +21 -19
  70. package/dist/value-help/value-help-client.d.cts +9 -5
  71. package/dist/value-help/value-help-client.d.mts +9 -5
  72. package/dist/value-help/value-help-client.mjs +19 -17
  73. package/package.json +10 -10
@@ -4,6 +4,8 @@ const require_field_resolver = require("../shared/field-resolver.cjs");
4
4
  const require_messages = require("../messages.cjs");
5
5
  const require_extract_literals = require("../value-help/extract-literals.cjs");
6
6
  const require_extract_ref = require("../value-help/extract-ref.cjs");
7
+ const require_db_column = require("./db-column.cjs");
8
+ const require_path_utils = require("./path-utils.cjs");
7
9
  const require_skipped_nav = require("./skipped-nav.cjs");
8
10
  let _atscript_typescript_utils = require("@atscript/typescript/utils");
9
11
  let _atscript_db_meta = require("@atscript/db/meta");
@@ -24,7 +26,7 @@ const UI_TAGS = new Set([
24
26
  * an endless expansion: a tree's `children` items are the same def at every
25
27
  * depth, built once. A field def is reused only at the same path relative to
26
28
  * its object def, so a reused field always sits where it was built. Both are
27
- * per mode (`edit`), which shapes the defs.
29
+ * per {@link TWrite}, which shapes the defs.
28
30
  */
29
31
  const defsInProgress = [];
30
32
  const fieldsInProgress = [];
@@ -69,19 +71,19 @@ function unnamed(field) {
69
71
  * becomes a nested object field, since inlining it would never end.
70
72
  */
71
73
  function createFormDef(type, opts) {
72
- const edit = opts?.mode === "edit";
73
- const def = buildFormDef(type, edit, opts ?? {});
74
+ const write = opts?.mode === "edit" ? "edit" : "create";
75
+ const def = buildFormDef(type, write, opts ?? {});
74
76
  if (require_model_texts.isServedType(type)) def.served = true;
75
- if (edit) def.edit = true;
77
+ if (write === "edit") def.edit = true;
76
78
  return def;
77
79
  }
78
80
  /**
79
81
  * The def of an object (or a leaf) type. `root`: the caller's options, for the
80
82
  * form's own type only — nested objects get none of the root-table ones.
81
83
  */
82
- function buildFormDef(type, edit, root) {
84
+ function buildFormDef(type, write, root) {
83
85
  if (type.type.kind !== "object") {
84
- const rootField = createFieldDef("", type, edit);
86
+ const rootField = createFieldDef("", type, write);
85
87
  const fields = [rootField];
86
88
  return {
87
89
  type,
@@ -93,14 +95,14 @@ function buildFormDef(type, edit, root) {
93
95
  };
94
96
  }
95
97
  if (!root) {
96
- const reached = defsInProgress.find((e) => e.type === type && e.edit === edit);
98
+ const reached = defsInProgress.find((e) => e.type === type && e.write === write);
97
99
  if (reached) return reached.def;
98
100
  }
99
101
  const objectType = type;
100
102
  const flatMap = (0, _atscript_typescript_utils.flattenAnnotatedType)(objectType, { excludePhantomTypes: false });
101
103
  const opts = root && {
102
104
  ...root,
103
- versionColumn: root.versionColumn ?? (edit ? findVersionColumn(flatMap) : void 0)
105
+ versionColumn: root.versionColumn ?? (write === "edit" ? findVersionColumn(flatMap) : void 0)
104
106
  };
105
107
  const fields = [];
106
108
  const rootField = {
@@ -124,11 +126,11 @@ function buildFormDef(type, edit, root) {
124
126
  let collected;
125
127
  if (!root) defsInProgress.push({
126
128
  type,
127
- edit,
129
+ write,
128
130
  def
129
131
  });
130
132
  try {
131
- collected = collectFields(objectType, flatMap, edit, opts);
133
+ collected = collectFields(objectType, flatMap, write, opts);
132
134
  } finally {
133
135
  if (!root) defsInProgress.pop();
134
136
  }
@@ -141,7 +143,7 @@ function buildFormDef(type, edit, root) {
141
143
  return def;
142
144
  }
143
145
  /** Builds the top-level fields of an object def from its flattened props, unsorted. */
144
- function collectFields(objectType, flatMap, edit, opts) {
146
+ function collectFields(objectType, flatMap, write, opts) {
145
147
  const fields = [];
146
148
  const structuredPrefixes = /* @__PURE__ */ new Set();
147
149
  for (const [path, prop] of flatMap.entries()) {
@@ -159,7 +161,7 @@ function collectFields(objectType, flatMap, edit, opts) {
159
161
  if (originalProp.type.of.type.kind === "array" && !require_field_resolver.getFieldMeta(originalProp, "ui.form.component")) continue;
160
162
  }
161
163
  if (kind === "array" || kind === "object" || kind === "union" || kind === "tuple") structuredPrefixes.add(path + ".");
162
- const field = createFieldDef(path, originalProp, edit);
164
+ const field = createFieldDef(path, originalProp, write);
163
165
  if (require_extract_ref.isValueHelpPicker(originalProp)) field.picker = true;
164
166
  fields.push({
165
167
  field,
@@ -185,7 +187,7 @@ function markServerOwned(fields, type, metaFields) {
185
187
  if (own.size === 0 && !metaFields) return;
186
188
  const filled = /* @__PURE__ */ new Set();
187
189
  for (const { field } of fields) {
188
- const parent = parentPrefix(field.path);
190
+ const parent = require_path_utils.parentPrefix(field.path);
189
191
  for (const f of field.valueHelpInfo?.fill ?? []) filled.add(parent + f.local);
190
192
  }
191
193
  for (const c of fields) {
@@ -223,10 +225,6 @@ function servedReadOnly(metaFields, path) {
223
225
  function writtenByForm(field, filled) {
224
226
  return filled.has(field.path) || require_field_resolver.getFieldMeta(field.prop, "ui.form.fn.value") !== void 0 || require_field_resolver.getFieldMeta(field.prop, "ui.form.fn.readonly") !== void 0;
225
227
  }
226
- /** The path of the object holding `path`, with its trailing dot — `""` at the top. */
227
- function parentPrefix(path) {
228
- return path.slice(0, path.lastIndexOf(".") + 1);
229
- }
230
228
  /**
231
229
  * Scopes a binding's sibling names to the def (since 0.1.190). Host references
232
230
  * (`hostFilter`) and fills (`@ui.valueHelp.fill`) name fields of the interface
@@ -243,7 +241,7 @@ function keepWritableFills(fields) {
243
241
  for (const { field } of fields) {
244
242
  const info = field.valueHelpInfo;
245
243
  if (!info?.fill && !info?.hostFilter) continue;
246
- const parent = parentPrefix(field.path);
244
+ const parent = require_path_utils.parentPrefix(field.path);
247
245
  const hostFilter = parent && info.hostFilter ? info.hostFilter.map((h) => ({
248
246
  ...h,
249
247
  local: parent + h.local
@@ -265,36 +263,37 @@ function keepWritableFills(fields) {
265
263
  }
266
264
  }
267
265
  /**
268
- * Creates a FormFieldDef from any ATScript annotated type. `edit`: the form
269
- * edits a stored row (`CreateFormDefOptions.mode`).
266
+ * Creates a FormFieldDef from any ATScript annotated type. `write`: how the
267
+ * server writes it ({@link TWrite}, from `CreateFormDefOptions.mode`).
270
268
  *
271
269
  * An array, union or tuple reached again at the same path inside its own
272
270
  * build (an array item, a union variant or a tuple item of a recursive alias)
273
271
  * gets the field def under construction. Every other recursion passes an
274
272
  * object def, which `defsInProgress` catches, so the build always ends.
275
273
  */
276
- function createFieldDef(path, prop, edit) {
274
+ function createFieldDef(path, prop, write) {
277
275
  const kind = prop.type.kind;
278
- if (kind === "" || kind === "object") return buildFieldDef(path, prop, edit);
279
- const reached = fieldsInProgress.find((e) => e.prop === prop && e.path === path && e.edit === edit);
276
+ if (kind === "" || kind === "object") return buildFieldDef(path, prop, write);
277
+ const reached = fieldsInProgress.find((e) => e.prop === prop && e.path === path && e.write === write);
280
278
  if (reached) return reached.field;
281
279
  const building = {
282
280
  prop,
283
281
  path,
284
- edit,
282
+ write,
285
283
  field: {},
286
284
  copies: []
287
285
  };
288
286
  fieldsInProgress.push(building);
289
287
  try {
290
- const field = Object.assign(building.field, buildFieldDef(path, prop, edit));
288
+ const field = Object.assign(building.field, buildFieldDef(path, prop, write));
291
289
  for (const copy of building.copies) Object.assign(copy, field, { name: "" });
292
290
  return field;
293
291
  } finally {
294
292
  fieldsInProgress.pop();
295
293
  }
296
294
  }
297
- function buildFieldDef(path, prop, edit) {
295
+ function buildFieldDef(path, typeProp, write) {
296
+ const prop = write === "create" && path !== "" && !typeProp.optional && require_db_column.isDbDefaulted(typeProp) ? Object.create(typeProp, { optional: { value: true } }) : typeProp;
298
297
  const kind = prop.type.kind;
299
298
  const name = path.slice(path.lastIndexOf(".") + 1);
300
299
  const allStatic = !require_field_resolver.hasComputedAnnotations(prop);
@@ -302,7 +301,7 @@ function buildFieldDef(path, prop, edit) {
302
301
  const uiType = declaredType === "tags" && kind !== "array" ? void 0 : declaredType;
303
302
  const pushDown = require_field_resolver.getFieldMeta(prop, require_annotation_keys.UI_FORM_PUSH_DOWN) !== void 0;
304
303
  const valueHelpInfo = require_extract_ref.extractValueHelp(prop);
305
- const derived = isServerComputed(prop) || edit && path !== "" && isUpdateStamped(prop);
304
+ const derived = isServerComputed(prop) || write === "edit" && path !== "" && isUpdateStamped(prop);
306
305
  const base = {
307
306
  path,
308
307
  prop,
@@ -324,21 +323,21 @@ function buildFieldDef(path, prop, edit) {
324
323
  type: "array",
325
324
  customType: isTags ? "tags" : isMultiselect ? "multiselect" : customType,
326
325
  itemType: arrayType.of,
327
- itemField: labelledBy(createFieldDef("", arrayType.of, edit), prop)
326
+ itemField: labelledBy(createFieldDef("", arrayType.of, write), prop)
328
327
  };
329
328
  }
330
329
  if (kind === "object") return {
331
330
  ...base,
332
331
  type: "object",
333
332
  customType,
334
- objectDef: buildFormDef(prop, edit)
333
+ objectDef: buildFormDef(prop, write)
335
334
  };
336
335
  if (kind === "union") {
337
336
  if (require_extract_literals.isPureLiteralUnion(prop)) return {
338
337
  ...base,
339
338
  type: uiType ?? "select"
340
339
  };
341
- const unionVariants = createUnionVariants(prop, edit && soleValueMember(prop) !== void 0);
340
+ const unionVariants = createUnionVariants(prop, require_db_column.soleValueMember(prop) === void 0 ? void 0 : write);
342
341
  if (unionVariants.length > 1) return {
343
342
  ...base,
344
343
  type: "union",
@@ -367,7 +366,7 @@ function buildFieldDef(path, prop, edit) {
367
366
  ...base,
368
367
  type: "tuple",
369
368
  customType,
370
- itemFields: tupleType.items.map((item, i) => unnamed(createFieldDef(String(i), item, false)))
369
+ itemFields: tupleType.items.map((item, i) => unnamed(createFieldDef(String(i), item, void 0)))
371
370
  };
372
371
  }
373
372
  if (valueHelpInfo) return {
@@ -477,7 +476,7 @@ function isChildOfStructured(path, prefixes) {
477
476
  * Iterates top-level items directly — one variant per item.
478
477
  */
479
478
  function buildUnionVariants(typeDef) {
480
- return createUnionVariants(typeDef, false);
479
+ return createUnionVariants(typeDef, void 0);
481
480
  }
482
481
  /**
483
482
  * A scalar field inside `outer` (an array item, a union variant) also takes `outer`'s
@@ -487,11 +486,11 @@ function labelledBy(field, outer) {
487
486
  if (field.prop?.type.kind === "" && outer.metadata.has("ui.literalLabel")) field.labelsFrom = outer;
488
487
  return field;
489
488
  }
490
- function createUnionVariants(typeDef, edit) {
489
+ function createUnionVariants(typeDef, write) {
491
490
  const items = typeDef.type.items ?? [typeDef];
492
491
  const variants = [];
493
492
  for (const item of items) {
494
- const v = createVariant(item, edit);
493
+ const v = createVariant(item, write);
495
494
  if (v.itemField) labelledBy(v.itemField, typeDef);
496
495
  if (items.length > 1) v.label = `${String(variants.length + 1)}. ${v.label}`;
497
496
  variants.push(v);
@@ -499,7 +498,7 @@ function createUnionVariants(typeDef, edit) {
499
498
  return variants;
500
499
  }
501
500
  /** Creates a single union variant from an annotated type item. */
502
- function createVariant(def, edit) {
501
+ function createVariant(def, write) {
503
502
  const kind = def.type.kind;
504
503
  if (kind === "object") {
505
504
  const label = require_field_resolver.getFieldMeta(def, require_annotation_keys.META_LABEL);
@@ -510,8 +509,8 @@ function createVariant(def, edit) {
510
509
  labelText: label
511
510
  },
512
511
  type: def,
513
- def: buildFormDef(def, edit),
514
- itemField: hasComponent ? createFieldDef("", def, edit) : void 0
512
+ def: buildFormDef(def, write),
513
+ itemField: hasComponent ? createFieldDef("", def, write) : void 0
515
514
  };
516
515
  }
517
516
  if (kind === "") {
@@ -519,14 +518,14 @@ function createVariant(def, edit) {
519
518
  return {
520
519
  ...typeLabel(dt === "phantom" ? "item" : dt),
521
520
  type: def,
522
- itemField: createFieldDef("", def, edit),
521
+ itemField: createFieldDef("", def, write),
523
522
  designType: dt
524
523
  };
525
524
  }
526
525
  return {
527
526
  ...typeLabel(kind),
528
527
  type: def,
529
- itemField: createFieldDef("", def, edit)
528
+ itemField: createFieldDef("", def, write)
530
529
  };
531
530
  }
532
531
  /**
@@ -539,26 +538,9 @@ function isServerComputed(prop) {
539
538
  const meta = prop.metadata;
540
539
  return meta.has("db.column.derived") || meta.has("db.compute") || meta.has("db.compute.host");
541
540
  }
542
- /**
543
- * An update-stamped field (see `CreateFormDefOptions.mode`). Read off the prop,
544
- * or — as `@atscript/db` does — off the one value member of `T | null`, unless
545
- * the prop references another field.
546
- */
541
+ /** An update-stamped field (see `CreateFormDefOptions.mode`), read as {@link columnHas} reads it. */
547
542
  function isUpdateStamped(prop) {
548
- if ((0, _atscript_db_validator.isServerStampedOnUpdate)(prop.metadata)) return true;
549
- const member = prop.ref?.field ? void 0 : soleValueMember(prop);
550
- return member !== void 0 && (0, _atscript_db_validator.isServerStampedOnUpdate)(member.metadata);
551
- }
552
- /** The one non-`null` / `undefined` member of a (nested) union, if there is exactly one. */
553
- function soleValueMember(prop) {
554
- if (prop.type.kind !== "union") return void 0;
555
- const members = /* @__PURE__ */ new Set();
556
- const visit = (t) => {
557
- if (t.type.kind === "union") for (const item of t.type.items) visit(item);
558
- else if (!require_extract_literals.isEmptyMember(t)) members.add(t);
559
- };
560
- visit(prop);
561
- return members.size === 1 ? members.values().next().value : void 0;
543
+ return require_db_column.columnHas(prop, _atscript_db_validator.isServerStampedOnUpdate);
562
544
  }
563
545
  function capitalize(s) {
564
546
  return s.charAt(0).toUpperCase() + s.slice(1);
@@ -42,6 +42,15 @@ interface CreateFormDefOptions {
42
42
  * `@db.default.now`, or is an `@db.onUpdate.inc` counter (`0`). Since
43
43
  * 0.1.154; `@db.onUpdate.inc` since 0.1.159.
44
44
  *
45
+ * `"create"` makes a field the database fills when an insert leaves it out
46
+ * — a `@db.default*`, the fields `@atscript/db` makes optional on insert —
47
+ * an optional one: its def's `prop` is a view of the type's prop with
48
+ * `optional: true`, so it is offered, emptied and validated as an optional
49
+ * field, and a create leaves it out when empty. At the same places as the
50
+ * update stamps: any depth, but not a reference to another field, an array
51
+ * element, or below a tuple or a union of several types (the database fills
52
+ * none there). Since 0.1.206.
53
+ *
45
54
  * `"edit"` also sets `FormDef.edit`: an emptied optional field then holds
46
55
  * its empty value (`""`, `null` or `false`; business mode, see
47
56
  * `emptiedValue`), so a whole-record update clears it too. Since 0.1.205.
@@ -42,6 +42,15 @@ interface CreateFormDefOptions {
42
42
  * `@db.default.now`, or is an `@db.onUpdate.inc` counter (`0`). Since
43
43
  * 0.1.154; `@db.onUpdate.inc` since 0.1.159.
44
44
  *
45
+ * `"create"` makes a field the database fills when an insert leaves it out
46
+ * — a `@db.default*`, the fields `@atscript/db` makes optional on insert —
47
+ * an optional one: its def's `prop` is a view of the type's prop with
48
+ * `optional: true`, so it is offered, emptied and validated as an optional
49
+ * field, and a create leaves it out when empty. At the same places as the
50
+ * update stamps: any depth, but not a reference to another field, an array
51
+ * element, or below a tuple or a union of several types (the database fills
52
+ * none there). Since 0.1.206.
53
+ *
45
54
  * `"edit"` also sets `FormDef.edit`: an emptied optional field then holds
46
55
  * its empty value (`""`, `null` or `false`; business mode, see
47
56
  * `emptiedValue`), so a whole-record update clears it too. Since 0.1.205.
@@ -2,8 +2,10 @@ import { META_DESCRIPTION, META_LABEL, UI_FORM_COMPONENT, UI_FORM_PUSH_DOWN, UI_
2
2
  import { isServedType } from "../i18n/model-texts.mjs";
3
3
  import { getFieldMeta, hasComputedAnnotations } from "../shared/field-resolver.mjs";
4
4
  import { AS_UI_MESSAGES } from "../messages.mjs";
5
- import { isEmptyMember, isPureLiteralUnion } from "../value-help/extract-literals.mjs";
5
+ import { isPureLiteralUnion } from "../value-help/extract-literals.mjs";
6
6
  import { extractValueHelp, isValueHelpPicker } from "../value-help/extract-ref.mjs";
7
+ import { columnHas, isDbDefaulted, soleValueMember } from "./db-column.mjs";
8
+ import { parentPrefix } from "./path-utils.mjs";
7
9
  import { isSkippedNavField } from "./skipped-nav.mjs";
8
10
  import { flattenAnnotatedType } from "@atscript/typescript/utils";
9
11
  import { writableViewReadOnlyFields } from "@atscript/db/meta";
@@ -24,7 +26,7 @@ const UI_TAGS = new Set([
24
26
  * an endless expansion: a tree's `children` items are the same def at every
25
27
  * depth, built once. A field def is reused only at the same path relative to
26
28
  * its object def, so a reused field always sits where it was built. Both are
27
- * per mode (`edit`), which shapes the defs.
29
+ * per {@link TWrite}, which shapes the defs.
28
30
  */
29
31
  const defsInProgress = [];
30
32
  const fieldsInProgress = [];
@@ -69,19 +71,19 @@ function unnamed(field) {
69
71
  * becomes a nested object field, since inlining it would never end.
70
72
  */
71
73
  function createFormDef(type, opts) {
72
- const edit = opts?.mode === "edit";
73
- const def = buildFormDef(type, edit, opts ?? {});
74
+ const write = opts?.mode === "edit" ? "edit" : "create";
75
+ const def = buildFormDef(type, write, opts ?? {});
74
76
  if (isServedType(type)) def.served = true;
75
- if (edit) def.edit = true;
77
+ if (write === "edit") def.edit = true;
76
78
  return def;
77
79
  }
78
80
  /**
79
81
  * The def of an object (or a leaf) type. `root`: the caller's options, for the
80
82
  * form's own type only — nested objects get none of the root-table ones.
81
83
  */
82
- function buildFormDef(type, edit, root) {
84
+ function buildFormDef(type, write, root) {
83
85
  if (type.type.kind !== "object") {
84
- const rootField = createFieldDef("", type, edit);
86
+ const rootField = createFieldDef("", type, write);
85
87
  const fields = [rootField];
86
88
  return {
87
89
  type,
@@ -93,14 +95,14 @@ function buildFormDef(type, edit, root) {
93
95
  };
94
96
  }
95
97
  if (!root) {
96
- const reached = defsInProgress.find((e) => e.type === type && e.edit === edit);
98
+ const reached = defsInProgress.find((e) => e.type === type && e.write === write);
97
99
  if (reached) return reached.def;
98
100
  }
99
101
  const objectType = type;
100
102
  const flatMap = flattenAnnotatedType(objectType, { excludePhantomTypes: false });
101
103
  const opts = root && {
102
104
  ...root,
103
- versionColumn: root.versionColumn ?? (edit ? findVersionColumn(flatMap) : void 0)
105
+ versionColumn: root.versionColumn ?? (write === "edit" ? findVersionColumn(flatMap) : void 0)
104
106
  };
105
107
  const fields = [];
106
108
  const rootField = {
@@ -124,11 +126,11 @@ function buildFormDef(type, edit, root) {
124
126
  let collected;
125
127
  if (!root) defsInProgress.push({
126
128
  type,
127
- edit,
129
+ write,
128
130
  def
129
131
  });
130
132
  try {
131
- collected = collectFields(objectType, flatMap, edit, opts);
133
+ collected = collectFields(objectType, flatMap, write, opts);
132
134
  } finally {
133
135
  if (!root) defsInProgress.pop();
134
136
  }
@@ -141,7 +143,7 @@ function buildFormDef(type, edit, root) {
141
143
  return def;
142
144
  }
143
145
  /** Builds the top-level fields of an object def from its flattened props, unsorted. */
144
- function collectFields(objectType, flatMap, edit, opts) {
146
+ function collectFields(objectType, flatMap, write, opts) {
145
147
  const fields = [];
146
148
  const structuredPrefixes = /* @__PURE__ */ new Set();
147
149
  for (const [path, prop] of flatMap.entries()) {
@@ -159,7 +161,7 @@ function collectFields(objectType, flatMap, edit, opts) {
159
161
  if (originalProp.type.of.type.kind === "array" && !getFieldMeta(originalProp, "ui.form.component")) continue;
160
162
  }
161
163
  if (kind === "array" || kind === "object" || kind === "union" || kind === "tuple") structuredPrefixes.add(path + ".");
162
- const field = createFieldDef(path, originalProp, edit);
164
+ const field = createFieldDef(path, originalProp, write);
163
165
  if (isValueHelpPicker(originalProp)) field.picker = true;
164
166
  fields.push({
165
167
  field,
@@ -223,10 +225,6 @@ function servedReadOnly(metaFields, path) {
223
225
  function writtenByForm(field, filled) {
224
226
  return filled.has(field.path) || getFieldMeta(field.prop, "ui.form.fn.value") !== void 0 || getFieldMeta(field.prop, "ui.form.fn.readonly") !== void 0;
225
227
  }
226
- /** The path of the object holding `path`, with its trailing dot — `""` at the top. */
227
- function parentPrefix(path) {
228
- return path.slice(0, path.lastIndexOf(".") + 1);
229
- }
230
228
  /**
231
229
  * Scopes a binding's sibling names to the def (since 0.1.190). Host references
232
230
  * (`hostFilter`) and fills (`@ui.valueHelp.fill`) name fields of the interface
@@ -265,36 +263,37 @@ function keepWritableFills(fields) {
265
263
  }
266
264
  }
267
265
  /**
268
- * Creates a FormFieldDef from any ATScript annotated type. `edit`: the form
269
- * edits a stored row (`CreateFormDefOptions.mode`).
266
+ * Creates a FormFieldDef from any ATScript annotated type. `write`: how the
267
+ * server writes it ({@link TWrite}, from `CreateFormDefOptions.mode`).
270
268
  *
271
269
  * An array, union or tuple reached again at the same path inside its own
272
270
  * build (an array item, a union variant or a tuple item of a recursive alias)
273
271
  * gets the field def under construction. Every other recursion passes an
274
272
  * object def, which `defsInProgress` catches, so the build always ends.
275
273
  */
276
- function createFieldDef(path, prop, edit) {
274
+ function createFieldDef(path, prop, write) {
277
275
  const kind = prop.type.kind;
278
- if (kind === "" || kind === "object") return buildFieldDef(path, prop, edit);
279
- const reached = fieldsInProgress.find((e) => e.prop === prop && e.path === path && e.edit === edit);
276
+ if (kind === "" || kind === "object") return buildFieldDef(path, prop, write);
277
+ const reached = fieldsInProgress.find((e) => e.prop === prop && e.path === path && e.write === write);
280
278
  if (reached) return reached.field;
281
279
  const building = {
282
280
  prop,
283
281
  path,
284
- edit,
282
+ write,
285
283
  field: {},
286
284
  copies: []
287
285
  };
288
286
  fieldsInProgress.push(building);
289
287
  try {
290
- const field = Object.assign(building.field, buildFieldDef(path, prop, edit));
288
+ const field = Object.assign(building.field, buildFieldDef(path, prop, write));
291
289
  for (const copy of building.copies) Object.assign(copy, field, { name: "" });
292
290
  return field;
293
291
  } finally {
294
292
  fieldsInProgress.pop();
295
293
  }
296
294
  }
297
- function buildFieldDef(path, prop, edit) {
295
+ function buildFieldDef(path, typeProp, write) {
296
+ const prop = write === "create" && path !== "" && !typeProp.optional && isDbDefaulted(typeProp) ? Object.create(typeProp, { optional: { value: true } }) : typeProp;
298
297
  const kind = prop.type.kind;
299
298
  const name = path.slice(path.lastIndexOf(".") + 1);
300
299
  const allStatic = !hasComputedAnnotations(prop);
@@ -302,7 +301,7 @@ function buildFieldDef(path, prop, edit) {
302
301
  const uiType = declaredType === "tags" && kind !== "array" ? void 0 : declaredType;
303
302
  const pushDown = getFieldMeta(prop, UI_FORM_PUSH_DOWN) !== void 0;
304
303
  const valueHelpInfo = extractValueHelp(prop);
305
- const derived = isServerComputed(prop) || edit && path !== "" && isUpdateStamped(prop);
304
+ const derived = isServerComputed(prop) || write === "edit" && path !== "" && isUpdateStamped(prop);
306
305
  const base = {
307
306
  path,
308
307
  prop,
@@ -324,21 +323,21 @@ function buildFieldDef(path, prop, edit) {
324
323
  type: "array",
325
324
  customType: isTags ? "tags" : isMultiselect ? "multiselect" : customType,
326
325
  itemType: arrayType.of,
327
- itemField: labelledBy(createFieldDef("", arrayType.of, edit), prop)
326
+ itemField: labelledBy(createFieldDef("", arrayType.of, write), prop)
328
327
  };
329
328
  }
330
329
  if (kind === "object") return {
331
330
  ...base,
332
331
  type: "object",
333
332
  customType,
334
- objectDef: buildFormDef(prop, edit)
333
+ objectDef: buildFormDef(prop, write)
335
334
  };
336
335
  if (kind === "union") {
337
336
  if (isPureLiteralUnion(prop)) return {
338
337
  ...base,
339
338
  type: uiType ?? "select"
340
339
  };
341
- const unionVariants = createUnionVariants(prop, edit && soleValueMember(prop) !== void 0);
340
+ const unionVariants = createUnionVariants(prop, soleValueMember(prop) === void 0 ? void 0 : write);
342
341
  if (unionVariants.length > 1) return {
343
342
  ...base,
344
343
  type: "union",
@@ -367,7 +366,7 @@ function buildFieldDef(path, prop, edit) {
367
366
  ...base,
368
367
  type: "tuple",
369
368
  customType,
370
- itemFields: tupleType.items.map((item, i) => unnamed(createFieldDef(String(i), item, false)))
369
+ itemFields: tupleType.items.map((item, i) => unnamed(createFieldDef(String(i), item, void 0)))
371
370
  };
372
371
  }
373
372
  if (valueHelpInfo) return {
@@ -477,7 +476,7 @@ function isChildOfStructured(path, prefixes) {
477
476
  * Iterates top-level items directly — one variant per item.
478
477
  */
479
478
  function buildUnionVariants(typeDef) {
480
- return createUnionVariants(typeDef, false);
479
+ return createUnionVariants(typeDef, void 0);
481
480
  }
482
481
  /**
483
482
  * A scalar field inside `outer` (an array item, a union variant) also takes `outer`'s
@@ -487,11 +486,11 @@ function labelledBy(field, outer) {
487
486
  if (field.prop?.type.kind === "" && outer.metadata.has("ui.literalLabel")) field.labelsFrom = outer;
488
487
  return field;
489
488
  }
490
- function createUnionVariants(typeDef, edit) {
489
+ function createUnionVariants(typeDef, write) {
491
490
  const items = typeDef.type.items ?? [typeDef];
492
491
  const variants = [];
493
492
  for (const item of items) {
494
- const v = createVariant(item, edit);
493
+ const v = createVariant(item, write);
495
494
  if (v.itemField) labelledBy(v.itemField, typeDef);
496
495
  if (items.length > 1) v.label = `${String(variants.length + 1)}. ${v.label}`;
497
496
  variants.push(v);
@@ -499,7 +498,7 @@ function createUnionVariants(typeDef, edit) {
499
498
  return variants;
500
499
  }
501
500
  /** Creates a single union variant from an annotated type item. */
502
- function createVariant(def, edit) {
501
+ function createVariant(def, write) {
503
502
  const kind = def.type.kind;
504
503
  if (kind === "object") {
505
504
  const label = getFieldMeta(def, META_LABEL);
@@ -510,8 +509,8 @@ function createVariant(def, edit) {
510
509
  labelText: label
511
510
  },
512
511
  type: def,
513
- def: buildFormDef(def, edit),
514
- itemField: hasComponent ? createFieldDef("", def, edit) : void 0
512
+ def: buildFormDef(def, write),
513
+ itemField: hasComponent ? createFieldDef("", def, write) : void 0
515
514
  };
516
515
  }
517
516
  if (kind === "") {
@@ -519,14 +518,14 @@ function createVariant(def, edit) {
519
518
  return {
520
519
  ...typeLabel(dt === "phantom" ? "item" : dt),
521
520
  type: def,
522
- itemField: createFieldDef("", def, edit),
521
+ itemField: createFieldDef("", def, write),
523
522
  designType: dt
524
523
  };
525
524
  }
526
525
  return {
527
526
  ...typeLabel(kind),
528
527
  type: def,
529
- itemField: createFieldDef("", def, edit)
528
+ itemField: createFieldDef("", def, write)
530
529
  };
531
530
  }
532
531
  /**
@@ -539,26 +538,9 @@ function isServerComputed(prop) {
539
538
  const meta = prop.metadata;
540
539
  return meta.has("db.column.derived") || meta.has("db.compute") || meta.has("db.compute.host");
541
540
  }
542
- /**
543
- * An update-stamped field (see `CreateFormDefOptions.mode`). Read off the prop,
544
- * or — as `@atscript/db` does — off the one value member of `T | null`, unless
545
- * the prop references another field.
546
- */
541
+ /** An update-stamped field (see `CreateFormDefOptions.mode`), read as {@link columnHas} reads it. */
547
542
  function isUpdateStamped(prop) {
548
- if (isServerStampedOnUpdate(prop.metadata)) return true;
549
- const member = prop.ref?.field ? void 0 : soleValueMember(prop);
550
- return member !== void 0 && isServerStampedOnUpdate(member.metadata);
551
- }
552
- /** The one non-`null` / `undefined` member of a (nested) union, if there is exactly one. */
553
- function soleValueMember(prop) {
554
- if (prop.type.kind !== "union") return void 0;
555
- const members = /* @__PURE__ */ new Set();
556
- const visit = (t) => {
557
- if (t.type.kind === "union") for (const item of t.type.items) visit(item);
558
- else if (!isEmptyMember(t)) members.add(t);
559
- };
560
- visit(prop);
561
- return members.size === 1 ? members.values().next().value : void 0;
543
+ return columnHas(prop, isServerStampedOnUpdate);
562
544
  }
563
545
  function capitalize(s) {
564
546
  return s.charAt(0).toUpperCase() + s.slice(1);
@@ -0,0 +1,37 @@
1
+ const require_extract_literals = require("../value-help/extract-literals.cjs");
2
+ let _atscript_db_validator = require("@atscript/db/validator");
3
+ //#region src/form/db-column.ts
4
+ /** The one non-`null` / `undefined` member of a (nested) union, if there is exactly one. */
5
+ function soleValueMember(prop) {
6
+ if (prop.type.kind !== "union") return void 0;
7
+ const members = /* @__PURE__ */ new Set();
8
+ const visit = (t) => {
9
+ if (t.type.kind === "union") for (const item of t.type.items) visit(item);
10
+ else if (!require_extract_literals.isEmptyMember(t)) members.add(t);
11
+ };
12
+ visit(prop);
13
+ return members.size === 1 ? members.values().next().value : void 0;
14
+ }
15
+ /**
16
+ * Whether `test` holds for the column annotations of a field, read as
17
+ * `@atscript/db` reads them: the prop's own, or those of the one value member
18
+ * of `T | null` — unless the prop references another field, whose union keeps
19
+ * them.
20
+ */
21
+ function columnHas(prop, test) {
22
+ if (test(prop.metadata)) return true;
23
+ const member = prop.ref?.field ? void 0 : soleValueMember(prop);
24
+ return member !== void 0 && test(member.metadata);
25
+ }
26
+ /**
27
+ * Whether the database fills the field when an insert leaves it out: its column
28
+ * has a `@db.default*` (`@atscript/db`'s own insert rule) — see
29
+ * `CreateFormDefOptions.mode`.
30
+ */
31
+ function isDbDefaulted(prop) {
32
+ return columnHas(prop, _atscript_db_validator.isDefaultedOnInsert);
33
+ }
34
+ //#endregion
35
+ exports.columnHas = columnHas;
36
+ exports.isDbDefaulted = isDbDefaulted;
37
+ exports.soleValueMember = soleValueMember;