@atscript/ui 0.1.205 → 0.1.206

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.
@@ -4,6 +4,7 @@ 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");
7
8
  const require_skipped_nav = require("./skipped-nav.cjs");
8
9
  let _atscript_typescript_utils = require("@atscript/typescript/utils");
9
10
  let _atscript_db_meta = require("@atscript/db/meta");
@@ -24,7 +25,7 @@ const UI_TAGS = new Set([
24
25
  * an endless expansion: a tree's `children` items are the same def at every
25
26
  * depth, built once. A field def is reused only at the same path relative to
26
27
  * its object def, so a reused field always sits where it was built. Both are
27
- * per mode (`edit`), which shapes the defs.
28
+ * per {@link TWrite}, which shapes the defs.
28
29
  */
29
30
  const defsInProgress = [];
30
31
  const fieldsInProgress = [];
@@ -69,19 +70,19 @@ function unnamed(field) {
69
70
  * becomes a nested object field, since inlining it would never end.
70
71
  */
71
72
  function createFormDef(type, opts) {
72
- const edit = opts?.mode === "edit";
73
- const def = buildFormDef(type, edit, opts ?? {});
73
+ const write = opts?.mode === "edit" ? "edit" : "create";
74
+ const def = buildFormDef(type, write, opts ?? {});
74
75
  if (require_model_texts.isServedType(type)) def.served = true;
75
- if (edit) def.edit = true;
76
+ if (write === "edit") def.edit = true;
76
77
  return def;
77
78
  }
78
79
  /**
79
80
  * The def of an object (or a leaf) type. `root`: the caller's options, for the
80
81
  * form's own type only — nested objects get none of the root-table ones.
81
82
  */
82
- function buildFormDef(type, edit, root) {
83
+ function buildFormDef(type, write, root) {
83
84
  if (type.type.kind !== "object") {
84
- const rootField = createFieldDef("", type, edit);
85
+ const rootField = createFieldDef("", type, write);
85
86
  const fields = [rootField];
86
87
  return {
87
88
  type,
@@ -93,14 +94,14 @@ function buildFormDef(type, edit, root) {
93
94
  };
94
95
  }
95
96
  if (!root) {
96
- const reached = defsInProgress.find((e) => e.type === type && e.edit === edit);
97
+ const reached = defsInProgress.find((e) => e.type === type && e.write === write);
97
98
  if (reached) return reached.def;
98
99
  }
99
100
  const objectType = type;
100
101
  const flatMap = (0, _atscript_typescript_utils.flattenAnnotatedType)(objectType, { excludePhantomTypes: false });
101
102
  const opts = root && {
102
103
  ...root,
103
- versionColumn: root.versionColumn ?? (edit ? findVersionColumn(flatMap) : void 0)
104
+ versionColumn: root.versionColumn ?? (write === "edit" ? findVersionColumn(flatMap) : void 0)
104
105
  };
105
106
  const fields = [];
106
107
  const rootField = {
@@ -124,11 +125,11 @@ function buildFormDef(type, edit, root) {
124
125
  let collected;
125
126
  if (!root) defsInProgress.push({
126
127
  type,
127
- edit,
128
+ write,
128
129
  def
129
130
  });
130
131
  try {
131
- collected = collectFields(objectType, flatMap, edit, opts);
132
+ collected = collectFields(objectType, flatMap, write, opts);
132
133
  } finally {
133
134
  if (!root) defsInProgress.pop();
134
135
  }
@@ -141,7 +142,7 @@ function buildFormDef(type, edit, root) {
141
142
  return def;
142
143
  }
143
144
  /** Builds the top-level fields of an object def from its flattened props, unsorted. */
144
- function collectFields(objectType, flatMap, edit, opts) {
145
+ function collectFields(objectType, flatMap, write, opts) {
145
146
  const fields = [];
146
147
  const structuredPrefixes = /* @__PURE__ */ new Set();
147
148
  for (const [path, prop] of flatMap.entries()) {
@@ -159,7 +160,7 @@ function collectFields(objectType, flatMap, edit, opts) {
159
160
  if (originalProp.type.of.type.kind === "array" && !require_field_resolver.getFieldMeta(originalProp, "ui.form.component")) continue;
160
161
  }
161
162
  if (kind === "array" || kind === "object" || kind === "union" || kind === "tuple") structuredPrefixes.add(path + ".");
162
- const field = createFieldDef(path, originalProp, edit);
163
+ const field = createFieldDef(path, originalProp, write);
163
164
  if (require_extract_ref.isValueHelpPicker(originalProp)) field.picker = true;
164
165
  fields.push({
165
166
  field,
@@ -265,36 +266,37 @@ function keepWritableFills(fields) {
265
266
  }
266
267
  }
267
268
  /**
268
- * Creates a FormFieldDef from any ATScript annotated type. `edit`: the form
269
- * edits a stored row (`CreateFormDefOptions.mode`).
269
+ * Creates a FormFieldDef from any ATScript annotated type. `write`: how the
270
+ * server writes it ({@link TWrite}, from `CreateFormDefOptions.mode`).
270
271
  *
271
272
  * An array, union or tuple reached again at the same path inside its own
272
273
  * build (an array item, a union variant or a tuple item of a recursive alias)
273
274
  * gets the field def under construction. Every other recursion passes an
274
275
  * object def, which `defsInProgress` catches, so the build always ends.
275
276
  */
276
- function createFieldDef(path, prop, edit) {
277
+ function createFieldDef(path, prop, write) {
277
278
  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);
279
+ if (kind === "" || kind === "object") return buildFieldDef(path, prop, write);
280
+ const reached = fieldsInProgress.find((e) => e.prop === prop && e.path === path && e.write === write);
280
281
  if (reached) return reached.field;
281
282
  const building = {
282
283
  prop,
283
284
  path,
284
- edit,
285
+ write,
285
286
  field: {},
286
287
  copies: []
287
288
  };
288
289
  fieldsInProgress.push(building);
289
290
  try {
290
- const field = Object.assign(building.field, buildFieldDef(path, prop, edit));
291
+ const field = Object.assign(building.field, buildFieldDef(path, prop, write));
291
292
  for (const copy of building.copies) Object.assign(copy, field, { name: "" });
292
293
  return field;
293
294
  } finally {
294
295
  fieldsInProgress.pop();
295
296
  }
296
297
  }
297
- function buildFieldDef(path, prop, edit) {
298
+ function buildFieldDef(path, typeProp, write) {
299
+ const prop = write === "create" && path !== "" && !typeProp.optional && require_db_column.isDbDefaulted(typeProp) ? Object.create(typeProp, { optional: { value: true } }) : typeProp;
298
300
  const kind = prop.type.kind;
299
301
  const name = path.slice(path.lastIndexOf(".") + 1);
300
302
  const allStatic = !require_field_resolver.hasComputedAnnotations(prop);
@@ -302,7 +304,7 @@ function buildFieldDef(path, prop, edit) {
302
304
  const uiType = declaredType === "tags" && kind !== "array" ? void 0 : declaredType;
303
305
  const pushDown = require_field_resolver.getFieldMeta(prop, require_annotation_keys.UI_FORM_PUSH_DOWN) !== void 0;
304
306
  const valueHelpInfo = require_extract_ref.extractValueHelp(prop);
305
- const derived = isServerComputed(prop) || edit && path !== "" && isUpdateStamped(prop);
307
+ const derived = isServerComputed(prop) || write === "edit" && path !== "" && isUpdateStamped(prop);
306
308
  const base = {
307
309
  path,
308
310
  prop,
@@ -324,21 +326,21 @@ function buildFieldDef(path, prop, edit) {
324
326
  type: "array",
325
327
  customType: isTags ? "tags" : isMultiselect ? "multiselect" : customType,
326
328
  itemType: arrayType.of,
327
- itemField: labelledBy(createFieldDef("", arrayType.of, edit), prop)
329
+ itemField: labelledBy(createFieldDef("", arrayType.of, write), prop)
328
330
  };
329
331
  }
330
332
  if (kind === "object") return {
331
333
  ...base,
332
334
  type: "object",
333
335
  customType,
334
- objectDef: buildFormDef(prop, edit)
336
+ objectDef: buildFormDef(prop, write)
335
337
  };
336
338
  if (kind === "union") {
337
339
  if (require_extract_literals.isPureLiteralUnion(prop)) return {
338
340
  ...base,
339
341
  type: uiType ?? "select"
340
342
  };
341
- const unionVariants = createUnionVariants(prop, edit && soleValueMember(prop) !== void 0);
343
+ const unionVariants = createUnionVariants(prop, require_db_column.soleValueMember(prop) === void 0 ? void 0 : write);
342
344
  if (unionVariants.length > 1) return {
343
345
  ...base,
344
346
  type: "union",
@@ -367,7 +369,7 @@ function buildFieldDef(path, prop, edit) {
367
369
  ...base,
368
370
  type: "tuple",
369
371
  customType,
370
- itemFields: tupleType.items.map((item, i) => unnamed(createFieldDef(String(i), item, false)))
372
+ itemFields: tupleType.items.map((item, i) => unnamed(createFieldDef(String(i), item, void 0)))
371
373
  };
372
374
  }
373
375
  if (valueHelpInfo) return {
@@ -477,7 +479,7 @@ function isChildOfStructured(path, prefixes) {
477
479
  * Iterates top-level items directly — one variant per item.
478
480
  */
479
481
  function buildUnionVariants(typeDef) {
480
- return createUnionVariants(typeDef, false);
482
+ return createUnionVariants(typeDef, void 0);
481
483
  }
482
484
  /**
483
485
  * A scalar field inside `outer` (an array item, a union variant) also takes `outer`'s
@@ -487,11 +489,11 @@ function labelledBy(field, outer) {
487
489
  if (field.prop?.type.kind === "" && outer.metadata.has("ui.literalLabel")) field.labelsFrom = outer;
488
490
  return field;
489
491
  }
490
- function createUnionVariants(typeDef, edit) {
492
+ function createUnionVariants(typeDef, write) {
491
493
  const items = typeDef.type.items ?? [typeDef];
492
494
  const variants = [];
493
495
  for (const item of items) {
494
- const v = createVariant(item, edit);
496
+ const v = createVariant(item, write);
495
497
  if (v.itemField) labelledBy(v.itemField, typeDef);
496
498
  if (items.length > 1) v.label = `${String(variants.length + 1)}. ${v.label}`;
497
499
  variants.push(v);
@@ -499,7 +501,7 @@ function createUnionVariants(typeDef, edit) {
499
501
  return variants;
500
502
  }
501
503
  /** Creates a single union variant from an annotated type item. */
502
- function createVariant(def, edit) {
504
+ function createVariant(def, write) {
503
505
  const kind = def.type.kind;
504
506
  if (kind === "object") {
505
507
  const label = require_field_resolver.getFieldMeta(def, require_annotation_keys.META_LABEL);
@@ -510,8 +512,8 @@ function createVariant(def, edit) {
510
512
  labelText: label
511
513
  },
512
514
  type: def,
513
- def: buildFormDef(def, edit),
514
- itemField: hasComponent ? createFieldDef("", def, edit) : void 0
515
+ def: buildFormDef(def, write),
516
+ itemField: hasComponent ? createFieldDef("", def, write) : void 0
515
517
  };
516
518
  }
517
519
  if (kind === "") {
@@ -519,14 +521,14 @@ function createVariant(def, edit) {
519
521
  return {
520
522
  ...typeLabel(dt === "phantom" ? "item" : dt),
521
523
  type: def,
522
- itemField: createFieldDef("", def, edit),
524
+ itemField: createFieldDef("", def, write),
523
525
  designType: dt
524
526
  };
525
527
  }
526
528
  return {
527
529
  ...typeLabel(kind),
528
530
  type: def,
529
- itemField: createFieldDef("", def, edit)
531
+ itemField: createFieldDef("", def, write)
530
532
  };
531
533
  }
532
534
  /**
@@ -539,26 +541,9 @@ function isServerComputed(prop) {
539
541
  const meta = prop.metadata;
540
542
  return meta.has("db.column.derived") || meta.has("db.compute") || meta.has("db.compute.host");
541
543
  }
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
- */
544
+ /** An update-stamped field (see `CreateFormDefOptions.mode`), read as {@link columnHas} reads it. */
547
545
  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;
546
+ return require_db_column.columnHas(prop, _atscript_db_validator.isServerStampedOnUpdate);
562
547
  }
563
548
  function capitalize(s) {
564
549
  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,9 @@ 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";
7
8
  import { isSkippedNavField } from "./skipped-nav.mjs";
8
9
  import { flattenAnnotatedType } from "@atscript/typescript/utils";
9
10
  import { writableViewReadOnlyFields } from "@atscript/db/meta";
@@ -24,7 +25,7 @@ const UI_TAGS = new Set([
24
25
  * an endless expansion: a tree's `children` items are the same def at every
25
26
  * depth, built once. A field def is reused only at the same path relative to
26
27
  * its object def, so a reused field always sits where it was built. Both are
27
- * per mode (`edit`), which shapes the defs.
28
+ * per {@link TWrite}, which shapes the defs.
28
29
  */
29
30
  const defsInProgress = [];
30
31
  const fieldsInProgress = [];
@@ -69,19 +70,19 @@ function unnamed(field) {
69
70
  * becomes a nested object field, since inlining it would never end.
70
71
  */
71
72
  function createFormDef(type, opts) {
72
- const edit = opts?.mode === "edit";
73
- const def = buildFormDef(type, edit, opts ?? {});
73
+ const write = opts?.mode === "edit" ? "edit" : "create";
74
+ const def = buildFormDef(type, write, opts ?? {});
74
75
  if (isServedType(type)) def.served = true;
75
- if (edit) def.edit = true;
76
+ if (write === "edit") def.edit = true;
76
77
  return def;
77
78
  }
78
79
  /**
79
80
  * The def of an object (or a leaf) type. `root`: the caller's options, for the
80
81
  * form's own type only — nested objects get none of the root-table ones.
81
82
  */
82
- function buildFormDef(type, edit, root) {
83
+ function buildFormDef(type, write, root) {
83
84
  if (type.type.kind !== "object") {
84
- const rootField = createFieldDef("", type, edit);
85
+ const rootField = createFieldDef("", type, write);
85
86
  const fields = [rootField];
86
87
  return {
87
88
  type,
@@ -93,14 +94,14 @@ function buildFormDef(type, edit, root) {
93
94
  };
94
95
  }
95
96
  if (!root) {
96
- const reached = defsInProgress.find((e) => e.type === type && e.edit === edit);
97
+ const reached = defsInProgress.find((e) => e.type === type && e.write === write);
97
98
  if (reached) return reached.def;
98
99
  }
99
100
  const objectType = type;
100
101
  const flatMap = flattenAnnotatedType(objectType, { excludePhantomTypes: false });
101
102
  const opts = root && {
102
103
  ...root,
103
- versionColumn: root.versionColumn ?? (edit ? findVersionColumn(flatMap) : void 0)
104
+ versionColumn: root.versionColumn ?? (write === "edit" ? findVersionColumn(flatMap) : void 0)
104
105
  };
105
106
  const fields = [];
106
107
  const rootField = {
@@ -124,11 +125,11 @@ function buildFormDef(type, edit, root) {
124
125
  let collected;
125
126
  if (!root) defsInProgress.push({
126
127
  type,
127
- edit,
128
+ write,
128
129
  def
129
130
  });
130
131
  try {
131
- collected = collectFields(objectType, flatMap, edit, opts);
132
+ collected = collectFields(objectType, flatMap, write, opts);
132
133
  } finally {
133
134
  if (!root) defsInProgress.pop();
134
135
  }
@@ -141,7 +142,7 @@ function buildFormDef(type, edit, root) {
141
142
  return def;
142
143
  }
143
144
  /** Builds the top-level fields of an object def from its flattened props, unsorted. */
144
- function collectFields(objectType, flatMap, edit, opts) {
145
+ function collectFields(objectType, flatMap, write, opts) {
145
146
  const fields = [];
146
147
  const structuredPrefixes = /* @__PURE__ */ new Set();
147
148
  for (const [path, prop] of flatMap.entries()) {
@@ -159,7 +160,7 @@ function collectFields(objectType, flatMap, edit, opts) {
159
160
  if (originalProp.type.of.type.kind === "array" && !getFieldMeta(originalProp, "ui.form.component")) continue;
160
161
  }
161
162
  if (kind === "array" || kind === "object" || kind === "union" || kind === "tuple") structuredPrefixes.add(path + ".");
162
- const field = createFieldDef(path, originalProp, edit);
163
+ const field = createFieldDef(path, originalProp, write);
163
164
  if (isValueHelpPicker(originalProp)) field.picker = true;
164
165
  fields.push({
165
166
  field,
@@ -265,36 +266,37 @@ function keepWritableFills(fields) {
265
266
  }
266
267
  }
267
268
  /**
268
- * Creates a FormFieldDef from any ATScript annotated type. `edit`: the form
269
- * edits a stored row (`CreateFormDefOptions.mode`).
269
+ * Creates a FormFieldDef from any ATScript annotated type. `write`: how the
270
+ * server writes it ({@link TWrite}, from `CreateFormDefOptions.mode`).
270
271
  *
271
272
  * An array, union or tuple reached again at the same path inside its own
272
273
  * build (an array item, a union variant or a tuple item of a recursive alias)
273
274
  * gets the field def under construction. Every other recursion passes an
274
275
  * object def, which `defsInProgress` catches, so the build always ends.
275
276
  */
276
- function createFieldDef(path, prop, edit) {
277
+ function createFieldDef(path, prop, write) {
277
278
  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);
279
+ if (kind === "" || kind === "object") return buildFieldDef(path, prop, write);
280
+ const reached = fieldsInProgress.find((e) => e.prop === prop && e.path === path && e.write === write);
280
281
  if (reached) return reached.field;
281
282
  const building = {
282
283
  prop,
283
284
  path,
284
- edit,
285
+ write,
285
286
  field: {},
286
287
  copies: []
287
288
  };
288
289
  fieldsInProgress.push(building);
289
290
  try {
290
- const field = Object.assign(building.field, buildFieldDef(path, prop, edit));
291
+ const field = Object.assign(building.field, buildFieldDef(path, prop, write));
291
292
  for (const copy of building.copies) Object.assign(copy, field, { name: "" });
292
293
  return field;
293
294
  } finally {
294
295
  fieldsInProgress.pop();
295
296
  }
296
297
  }
297
- function buildFieldDef(path, prop, edit) {
298
+ function buildFieldDef(path, typeProp, write) {
299
+ const prop = write === "create" && path !== "" && !typeProp.optional && isDbDefaulted(typeProp) ? Object.create(typeProp, { optional: { value: true } }) : typeProp;
298
300
  const kind = prop.type.kind;
299
301
  const name = path.slice(path.lastIndexOf(".") + 1);
300
302
  const allStatic = !hasComputedAnnotations(prop);
@@ -302,7 +304,7 @@ function buildFieldDef(path, prop, edit) {
302
304
  const uiType = declaredType === "tags" && kind !== "array" ? void 0 : declaredType;
303
305
  const pushDown = getFieldMeta(prop, UI_FORM_PUSH_DOWN) !== void 0;
304
306
  const valueHelpInfo = extractValueHelp(prop);
305
- const derived = isServerComputed(prop) || edit && path !== "" && isUpdateStamped(prop);
307
+ const derived = isServerComputed(prop) || write === "edit" && path !== "" && isUpdateStamped(prop);
306
308
  const base = {
307
309
  path,
308
310
  prop,
@@ -324,21 +326,21 @@ function buildFieldDef(path, prop, edit) {
324
326
  type: "array",
325
327
  customType: isTags ? "tags" : isMultiselect ? "multiselect" : customType,
326
328
  itemType: arrayType.of,
327
- itemField: labelledBy(createFieldDef("", arrayType.of, edit), prop)
329
+ itemField: labelledBy(createFieldDef("", arrayType.of, write), prop)
328
330
  };
329
331
  }
330
332
  if (kind === "object") return {
331
333
  ...base,
332
334
  type: "object",
333
335
  customType,
334
- objectDef: buildFormDef(prop, edit)
336
+ objectDef: buildFormDef(prop, write)
335
337
  };
336
338
  if (kind === "union") {
337
339
  if (isPureLiteralUnion(prop)) return {
338
340
  ...base,
339
341
  type: uiType ?? "select"
340
342
  };
341
- const unionVariants = createUnionVariants(prop, edit && soleValueMember(prop) !== void 0);
343
+ const unionVariants = createUnionVariants(prop, soleValueMember(prop) === void 0 ? void 0 : write);
342
344
  if (unionVariants.length > 1) return {
343
345
  ...base,
344
346
  type: "union",
@@ -367,7 +369,7 @@ function buildFieldDef(path, prop, edit) {
367
369
  ...base,
368
370
  type: "tuple",
369
371
  customType,
370
- itemFields: tupleType.items.map((item, i) => unnamed(createFieldDef(String(i), item, false)))
372
+ itemFields: tupleType.items.map((item, i) => unnamed(createFieldDef(String(i), item, void 0)))
371
373
  };
372
374
  }
373
375
  if (valueHelpInfo) return {
@@ -477,7 +479,7 @@ function isChildOfStructured(path, prefixes) {
477
479
  * Iterates top-level items directly — one variant per item.
478
480
  */
479
481
  function buildUnionVariants(typeDef) {
480
- return createUnionVariants(typeDef, false);
482
+ return createUnionVariants(typeDef, void 0);
481
483
  }
482
484
  /**
483
485
  * A scalar field inside `outer` (an array item, a union variant) also takes `outer`'s
@@ -487,11 +489,11 @@ function labelledBy(field, outer) {
487
489
  if (field.prop?.type.kind === "" && outer.metadata.has("ui.literalLabel")) field.labelsFrom = outer;
488
490
  return field;
489
491
  }
490
- function createUnionVariants(typeDef, edit) {
492
+ function createUnionVariants(typeDef, write) {
491
493
  const items = typeDef.type.items ?? [typeDef];
492
494
  const variants = [];
493
495
  for (const item of items) {
494
- const v = createVariant(item, edit);
496
+ const v = createVariant(item, write);
495
497
  if (v.itemField) labelledBy(v.itemField, typeDef);
496
498
  if (items.length > 1) v.label = `${String(variants.length + 1)}. ${v.label}`;
497
499
  variants.push(v);
@@ -499,7 +501,7 @@ function createUnionVariants(typeDef, edit) {
499
501
  return variants;
500
502
  }
501
503
  /** Creates a single union variant from an annotated type item. */
502
- function createVariant(def, edit) {
504
+ function createVariant(def, write) {
503
505
  const kind = def.type.kind;
504
506
  if (kind === "object") {
505
507
  const label = getFieldMeta(def, META_LABEL);
@@ -510,8 +512,8 @@ function createVariant(def, edit) {
510
512
  labelText: label
511
513
  },
512
514
  type: def,
513
- def: buildFormDef(def, edit),
514
- itemField: hasComponent ? createFieldDef("", def, edit) : void 0
515
+ def: buildFormDef(def, write),
516
+ itemField: hasComponent ? createFieldDef("", def, write) : void 0
515
517
  };
516
518
  }
517
519
  if (kind === "") {
@@ -519,14 +521,14 @@ function createVariant(def, edit) {
519
521
  return {
520
522
  ...typeLabel(dt === "phantom" ? "item" : dt),
521
523
  type: def,
522
- itemField: createFieldDef("", def, edit),
524
+ itemField: createFieldDef("", def, write),
523
525
  designType: dt
524
526
  };
525
527
  }
526
528
  return {
527
529
  ...typeLabel(kind),
528
530
  type: def,
529
- itemField: createFieldDef("", def, edit)
531
+ itemField: createFieldDef("", def, write)
530
532
  };
531
533
  }
532
534
  /**
@@ -539,26 +541,9 @@ function isServerComputed(prop) {
539
541
  const meta = prop.metadata;
540
542
  return meta.has("db.column.derived") || meta.has("db.compute") || meta.has("db.compute.host");
541
543
  }
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
- */
544
+ /** An update-stamped field (see `CreateFormDefOptions.mode`), read as {@link columnHas} reads it. */
547
545
  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;
546
+ return columnHas(prop, isServerStampedOnUpdate);
562
547
  }
563
548
  function capitalize(s) {
564
549
  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;
@@ -0,0 +1,35 @@
1
+ import { isEmptyMember } from "../value-help/extract-literals.mjs";
2
+ import { isDefaultedOnInsert } from "@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 (!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, isDefaultedOnInsert);
33
+ }
34
+ //#endregion
35
+ export { columnHas, isDbDefaulted, soleValueMember };
@@ -2,6 +2,7 @@ const require_annotation_keys = require("../shared/annotation-keys.cjs");
2
2
  const require_field_resolver = require("../shared/field-resolver.cjs");
3
3
  const require_extract_ref = require("../value-help/extract-ref.cjs");
4
4
  const require_exact_number = require("../shared/exact-number.cjs");
5
+ const require_db_column = require("./db-column.cjs");
5
6
  let _atscript_typescript_utils = require("@atscript/typescript/utils");
6
7
  //#region src/form/path-utils.ts
7
8
  /**
@@ -133,15 +134,19 @@ function primitiveInitFallback(prop) {
133
134
  }
134
135
  function createFormData(type, resolver) {
135
136
  const resolve = resolver ?? defaultValueResolver;
136
- const pickers = /* @__PURE__ */ new Map();
137
+ const startUnset = /* @__PURE__ */ new Map();
137
138
  const unset = [];
139
+ const ambiguous = [];
140
+ const filledAt = (path) => !ambiguous.some((a) => a === "" || path === a || path.startsWith(`${a}.`));
138
141
  let value = (0, _atscript_typescript_utils.createDataFromAnnotatedType)(type, { mode: (prop, path) => {
139
142
  const resolved = resolve(prop, path);
140
143
  if (resolved !== void 0) return resolved;
141
- if (pickers.get(path) === prop) unset.push(path);
142
- else if (prop.type.kind === "object") {
143
- for (const [key, child] of prop.type.props) if (require_extract_ref.isValueHelpPicker(child)) pickers.set(joinPath(path, key), child);
144
- }
144
+ const kind = prop.type.kind;
145
+ if (startUnset.get(path) === prop) unset.push(path);
146
+ else if (kind === "object") {
147
+ const filled = filledAt(path);
148
+ for (const [key, child] of prop.type.props) if (require_extract_ref.isValueHelpPicker(child) || filled && require_db_column.isDbDefaulted(child)) startUnset.set(joinPath(path, key), child);
149
+ } else if (kind === "tuple" || kind === "union" && require_db_column.soleValueMember(prop) === void 0) ambiguous.push(path);
145
150
  } });
146
151
  if (value === void 0) value = primitiveInitFallback(type);
147
152
  const data = { value };
@@ -2,6 +2,7 @@ import { META_DEFAULT, UI_FORM_FN_VALUE } from "../shared/annotation-keys.mjs";
2
2
  import { resolveFieldProp } from "../shared/field-resolver.mjs";
3
3
  import { isValueHelpPicker } from "../value-help/extract-ref.mjs";
4
4
  import { isExactNumberText } from "../shared/exact-number.mjs";
5
+ import { isDbDefaulted, soleValueMember } from "./db-column.mjs";
5
6
  import { createDataFromAnnotatedType, detectDiscriminator } from "@atscript/typescript/utils";
6
7
  //#region src/form/path-utils.ts
7
8
  /**
@@ -133,15 +134,19 @@ function primitiveInitFallback(prop) {
133
134
  }
134
135
  function createFormData(type, resolver) {
135
136
  const resolve = resolver ?? defaultValueResolver;
136
- const pickers = /* @__PURE__ */ new Map();
137
+ const startUnset = /* @__PURE__ */ new Map();
137
138
  const unset = [];
139
+ const ambiguous = [];
140
+ const filledAt = (path) => !ambiguous.some((a) => a === "" || path === a || path.startsWith(`${a}.`));
138
141
  let value = createDataFromAnnotatedType(type, { mode: (prop, path) => {
139
142
  const resolved = resolve(prop, path);
140
143
  if (resolved !== void 0) return resolved;
141
- if (pickers.get(path) === prop) unset.push(path);
142
- else if (prop.type.kind === "object") {
143
- for (const [key, child] of prop.type.props) if (isValueHelpPicker(child)) pickers.set(joinPath(path, key), child);
144
- }
144
+ const kind = prop.type.kind;
145
+ if (startUnset.get(path) === prop) unset.push(path);
146
+ else if (kind === "object") {
147
+ const filled = filledAt(path);
148
+ for (const [key, child] of prop.type.props) if (isValueHelpPicker(child) || filled && isDbDefaulted(child)) startUnset.set(joinPath(path, key), child);
149
+ } else if (kind === "tuple" || kind === "union" && soleValueMember(prop) === void 0) ambiguous.push(path);
145
150
  } });
146
151
  if (value === void 0) value = primitiveInitFallback(type);
147
152
  const data = { value };
@@ -17,6 +17,7 @@ interface TFormAction {
17
17
  interface FormFieldDef {
18
18
  /** Dot-separated path relative to the parent data context. `''` = root. */
19
19
  path: string;
20
+ /** The prop the field edits — on a create form an optional view of a `@db.default*` one (see `CreateFormDefOptions.mode`). */
20
21
  prop: TAtscriptAnnotatedType;
21
22
  /**
22
23
  * Render-key the renderer dispatches on. For structured kinds (array,
@@ -17,6 +17,7 @@ interface TFormAction {
17
17
  interface FormFieldDef {
18
18
  /** Dot-separated path relative to the parent data context. `''` = root. */
19
19
  path: string;
20
+ /** The prop the field edits — on a create form an optional view of a `@db.default*` one (see `CreateFormDefOptions.mode`). */
20
21
  prop: TAtscriptAnnotatedType;
21
22
  /**
22
23
  * Render-key the renderer dispatches on. For structured kinds (array,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atscript/ui",
3
- "version": "0.1.205",
3
+ "version": "0.1.206",
4
4
  "description": "Framework-agnostic runtime for form and table definitions from atscript annotated types",
5
5
  "keywords": [
6
6
  "annotations",
@@ -52,19 +52,19 @@
52
52
  "access": "public"
53
53
  },
54
54
  "devDependencies": {
55
- "@atscript/core": "^0.2.31",
56
- "@atscript/db": "^0.1.209",
57
- "@atscript/db-client": "^0.1.209",
58
- "@atscript/typescript": "^0.2.31",
55
+ "@atscript/core": "^0.2.32",
56
+ "@atscript/db": "^0.1.210",
57
+ "@atscript/db-client": "^0.1.210",
58
+ "@atscript/typescript": "^0.2.32",
59
59
  "@uniqu/core": "^0.3.0",
60
- "unplugin-atscript": "^0.2.31",
60
+ "unplugin-atscript": "^0.2.32",
61
61
  "vitest": "npm:@voidzero-dev/vite-plus-test@0.1.14"
62
62
  },
63
63
  "peerDependencies": {
64
- "@atscript/core": "^0.2.31",
65
- "@atscript/db": "^0.1.209",
66
- "@atscript/db-client": "^0.1.209",
67
- "@atscript/typescript": "^0.2.31"
64
+ "@atscript/core": "^0.2.32",
65
+ "@atscript/db": "^0.1.210",
66
+ "@atscript/db-client": "^0.1.210",
67
+ "@atscript/typescript": "^0.2.32"
68
68
  },
69
69
  "scripts": {
70
70
  "build": "vp pack",