@metaobjectsdev/codegen-ts 0.7.0-rc.1 → 0.7.0-rc.11

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 (88) hide show
  1. package/README.md +30 -0
  2. package/dist/column-mapper.d.ts +16 -0
  3. package/dist/column-mapper.d.ts.map +1 -1
  4. package/dist/column-mapper.js +71 -1
  5. package/dist/column-mapper.js.map +1 -1
  6. package/dist/generators/docs-file.d.ts +8 -0
  7. package/dist/generators/docs-file.d.ts.map +1 -0
  8. package/dist/generators/docs-file.js +52 -0
  9. package/dist/generators/docs-file.js.map +1 -0
  10. package/dist/generators/entity-file.d.ts +15 -0
  11. package/dist/generators/entity-file.d.ts.map +1 -1
  12. package/dist/generators/entity-file.js +2 -1
  13. package/dist/generators/entity-file.js.map +1 -1
  14. package/dist/generators/index.d.ts +2 -0
  15. package/dist/generators/index.d.ts.map +1 -1
  16. package/dist/generators/index.js +2 -0
  17. package/dist/generators/index.js.map +1 -1
  18. package/dist/generators/prompt-render-file.d.ts.map +1 -1
  19. package/dist/generators/prompt-render-file.js +30 -12
  20. package/dist/generators/prompt-render-file.js.map +1 -1
  21. package/dist/generators/queries-file.d.ts +1 -1
  22. package/dist/generators/queries-file.d.ts.map +1 -1
  23. package/dist/generators/queries-file.js +11 -3
  24. package/dist/generators/queries-file.js.map +1 -1
  25. package/dist/generators/routes-file-hono.d.ts +21 -0
  26. package/dist/generators/routes-file-hono.d.ts.map +1 -0
  27. package/dist/generators/routes-file-hono.js +38 -0
  28. package/dist/generators/routes-file-hono.js.map +1 -0
  29. package/dist/index.d.ts +2 -2
  30. package/dist/index.d.ts.map +1 -1
  31. package/dist/index.js +1 -1
  32. package/dist/index.js.map +1 -1
  33. package/dist/metaobjects-config.d.ts +9 -2
  34. package/dist/metaobjects-config.d.ts.map +1 -1
  35. package/dist/metaobjects-config.js.map +1 -1
  36. package/dist/payload-codegen.d.ts +8 -0
  37. package/dist/payload-codegen.d.ts.map +1 -1
  38. package/dist/payload-codegen.js +33 -3
  39. package/dist/payload-codegen.js.map +1 -1
  40. package/dist/source-detect.d.ts +10 -0
  41. package/dist/source-detect.d.ts.map +1 -0
  42. package/dist/source-detect.js +30 -0
  43. package/dist/source-detect.js.map +1 -0
  44. package/dist/templates/docs-file.d.ts +48 -0
  45. package/dist/templates/docs-file.d.ts.map +1 -0
  46. package/dist/templates/docs-file.js +451 -0
  47. package/dist/templates/docs-file.js.map +1 -0
  48. package/dist/templates/drizzle-schema.js +27 -3
  49. package/dist/templates/drizzle-schema.js.map +1 -1
  50. package/dist/templates/entity-file.d.ts +15 -1
  51. package/dist/templates/entity-file.d.ts.map +1 -1
  52. package/dist/templates/entity-file.js +15 -5
  53. package/dist/templates/entity-file.js.map +1 -1
  54. package/dist/templates/inferred-types.d.ts +9 -0
  55. package/dist/templates/inferred-types.d.ts.map +1 -1
  56. package/dist/templates/inferred-types.js +88 -2
  57. package/dist/templates/inferred-types.js.map +1 -1
  58. package/dist/templates/routes-file-hono.d.ts +4 -0
  59. package/dist/templates/routes-file-hono.d.ts.map +1 -0
  60. package/dist/templates/routes-file-hono.js +119 -0
  61. package/dist/templates/routes-file-hono.js.map +1 -0
  62. package/dist/templates/value-object-file.d.ts +3 -0
  63. package/dist/templates/value-object-file.d.ts.map +1 -0
  64. package/dist/templates/value-object-file.js +27 -0
  65. package/dist/templates/value-object-file.js.map +1 -0
  66. package/dist/templates/zod-validators.d.ts +10 -0
  67. package/dist/templates/zod-validators.d.ts.map +1 -1
  68. package/dist/templates/zod-validators.js +108 -30
  69. package/dist/templates/zod-validators.js.map +1 -1
  70. package/package.json +5 -4
  71. package/src/column-mapper.ts +84 -0
  72. package/src/generators/docs-file.ts +64 -0
  73. package/src/generators/entity-file.ts +17 -1
  74. package/src/generators/index.ts +2 -0
  75. package/src/generators/prompt-render-file.ts +38 -12
  76. package/src/generators/queries-file.ts +13 -4
  77. package/src/generators/routes-file-hono.ts +48 -0
  78. package/src/index.ts +2 -2
  79. package/src/metaobjects-config.ts +9 -2
  80. package/src/payload-codegen.ts +34 -2
  81. package/src/source-detect.ts +28 -0
  82. package/src/templates/docs-file.ts +552 -0
  83. package/src/templates/drizzle-schema.ts +27 -3
  84. package/src/templates/entity-file.ts +36 -5
  85. package/src/templates/inferred-types.ts +117 -3
  86. package/src/templates/routes-file-hono.ts +142 -0
  87. package/src/templates/value-object-file.ts +30 -0
  88. package/src/templates/zod-validators.ts +121 -35
@@ -1,9 +1,34 @@
1
1
  // Inferred types template — emits Drizzle's InferSelectModel / InferInsertModel type aliases,
2
2
  // plus named union types for field.enum fields.
3
+ //
4
+ // Also emits the structural TS interface for value-only objects (metaobjects
5
+ // with no writable source.rdb). That path side-steps Drizzle entirely: the
6
+ // interface is computed directly from the field tree, with `name?: T` for
7
+ // optional fields (matching Zod's `.optional()` inference — `T | undefined` —
8
+ // without the superfluous `| null` that Drizzle nullable columns introduce).
3
9
 
4
- import { code, imp, type Code } from "ts-poet";
5
- import type { MetaObject } from "@metaobjectsdev/metadata";
6
- import { FIELD_SUBTYPE_ENUM } from "@metaobjectsdev/metadata";
10
+ import { code, imp, joinCode, type Code } from "ts-poet";
11
+ import type { MetaObject, MetaField } from "@metaobjectsdev/metadata";
12
+ import {
13
+ FIELD_SUBTYPE_ENUM,
14
+ FIELD_SUBTYPE_OBJECT,
15
+ FIELD_SUBTYPE_STRING,
16
+ FIELD_SUBTYPE_INT,
17
+ FIELD_SUBTYPE_SHORT,
18
+ FIELD_SUBTYPE_BYTE,
19
+ FIELD_SUBTYPE_LONG,
20
+ FIELD_SUBTYPE_DOUBLE,
21
+ FIELD_SUBTYPE_FLOAT,
22
+ FIELD_SUBTYPE_DECIMAL,
23
+ FIELD_SUBTYPE_CURRENCY,
24
+ FIELD_SUBTYPE_BOOLEAN,
25
+ FIELD_SUBTYPE_DATE,
26
+ FIELD_SUBTYPE_TIME,
27
+ FIELD_SUBTYPE_TIMESTAMP,
28
+ FIELD_SUBTYPE_CLASS,
29
+ FIELD_ATTR_REQUIRED,
30
+ FIELD_ATTR_OBJECT_REF,
31
+ } from "@metaobjectsdev/metadata";
7
32
  import { variableNameFromEntity, toPascalCase } from "../naming.js";
8
33
  import { enumValues } from "../enum-meta.js";
9
34
  import { renderDocsFor } from "./jsdoc.js";
@@ -53,3 +78,92 @@ export function renderEnumTypeAliases(entity: MetaObject): Code | null {
53
78
 
54
79
  return lines.length > 0 ? code`${lines.join("\n")}` : null;
55
80
  }
81
+
82
+ // ---------------------------------------------------------------------------
83
+ // Value-object interface emitter
84
+ // ---------------------------------------------------------------------------
85
+
86
+ const SCALAR_TS_BY_SUBTYPE: Record<string, string> = {
87
+ [FIELD_SUBTYPE_STRING]: "string",
88
+ [FIELD_SUBTYPE_CLASS]: "string",
89
+ [FIELD_SUBTYPE_INT]: "number",
90
+ [FIELD_SUBTYPE_SHORT]: "number",
91
+ [FIELD_SUBTYPE_BYTE]: "number",
92
+ [FIELD_SUBTYPE_LONG]: "number",
93
+ [FIELD_SUBTYPE_DOUBLE]: "number",
94
+ [FIELD_SUBTYPE_FLOAT]: "number",
95
+ [FIELD_SUBTYPE_DECIMAL]: "number",
96
+ [FIELD_SUBTYPE_CURRENCY]: "number",
97
+ [FIELD_SUBTYPE_BOOLEAN]: "boolean",
98
+ [FIELD_SUBTYPE_DATE]: "string",
99
+ [FIELD_SUBTYPE_TIME]: "string",
100
+ [FIELD_SUBTYPE_TIMESTAMP]: "string",
101
+ };
102
+
103
+ /** Type-alias name for a field.enum, mirroring renderEnumTypeAliases. */
104
+ function enumTypeAliasName(entity: MetaObject, field: MetaField): string {
105
+ const superField = field.resolveSuper();
106
+ return superField !== undefined
107
+ ? toPascalCase(superField.name)
108
+ : `${entity.name}${toPascalCase(field.name)}`;
109
+ }
110
+
111
+ /**
112
+ * One-line TS type expression for a field on a value-only object.
113
+ * Returns a `Code` so cross-module `field.object` refs can be hoisted via
114
+ * ts-poet `imp(...)` — matching how the Zod emitter hoists `<Ref>InsertSchema`.
115
+ */
116
+ function valueObjectFieldType(entity: MetaObject, field: MetaField): Code {
117
+ // field.object: import the referenced TS interface from its sibling module
118
+ // so ts-poet hoists the import. Mirrors zod-validators.ts's `<Ref>InsertSchema`
119
+ // import strategy, just for the type alias instead of the schema constant.
120
+ if (field.subType === FIELD_SUBTYPE_OBJECT) {
121
+ const ref = field.ownAttr(FIELD_ATTR_OBJECT_REF);
122
+ if (typeof ref === "string" && ref.length > 0) {
123
+ const refImp = imp(`${ref}@./${ref}.js`);
124
+ return field.isArray ? code`${refImp}[]` : code`${refImp}`;
125
+ }
126
+ return field.isArray ? code`unknown[]` : code`unknown`;
127
+ }
128
+
129
+ // field.enum: use the same type-alias name as renderEnumTypeAliases emits.
130
+ if (field.subType === FIELD_SUBTYPE_ENUM) {
131
+ const values = enumValues(field);
132
+ if (values !== undefined) {
133
+ const alias = enumTypeAliasName(entity, field);
134
+ return field.isArray ? code`${alias}[]` : code`${alias}`;
135
+ }
136
+ return field.isArray ? code`string[]` : code`string`;
137
+ }
138
+
139
+ const scalar = SCALAR_TS_BY_SUBTYPE[field.subType] ?? "unknown";
140
+ return field.isArray ? code`${scalar}[]` : code`${scalar}`;
141
+ }
142
+
143
+ /**
144
+ * Emit a structural `interface <Name> { ... }` for a value-only object.
145
+ *
146
+ * Optional fields use `name?: T` (matching the Zod `.optional()` inference
147
+ * `T | undefined`) instead of `name?: T | null`. Value objects never round-
148
+ * trip through Drizzle nullable columns, so the null-bridge is unnecessary
149
+ * here — and forces consumers into a residual cast at the call site.
150
+ */
151
+ export function renderValueObjectInterface(entity: MetaObject): Code {
152
+ const docs = renderDocsFor(entity);
153
+ const docsPrefix = docs ? `${docs}\n` : "";
154
+
155
+ const lines: Code[] = [];
156
+ for (const field of entity.fields()) {
157
+ const required = field.ownAttr(FIELD_ATTR_REQUIRED) === true;
158
+ const optional = required ? "" : "?";
159
+ const tsType = valueObjectFieldType(entity, field);
160
+ lines.push(code` ${field.name}${optional}: ${tsType};`);
161
+ }
162
+
163
+ // joinCode with "\n" interpolates each Code segment on its own line and
164
+ // keeps the imp() registrations intact so ts-poet hoists the imports.
165
+ return code`${docsPrefix}export interface ${entity.name} {
166
+ ${joinCode(lines, { on: "\n" })}
167
+ }
168
+ `;
169
+ }
@@ -0,0 +1,142 @@
1
+ // Hono route template — emits a per-entity routes file that delegates
2
+ // CRUD verbs to helpers from @metaobjectsdev/runtime-ts/hono.
3
+ //
4
+ // Hono parallel of templates/routes-file.ts. Two emit-shape differences
5
+ // vs the Fastify flavor, both reflecting Hono idioms rather than contract
6
+ // drift:
7
+ //
8
+ // 1) Exported function shape — `registerXxxRoutes(app, deps)` instead of
9
+ // `xxxRoutes(fastify)`. Workers/Bun consumers typically pass `db`
10
+ // through a per-request deps object (so a Worker can attach the
11
+ // D1 client pulled off bindings) rather than reaching for a
12
+ // module-level singleton. The Fastify flavor uses `import { db }`
13
+ // because Fastify apps are long-lived Node processes where a
14
+ // singleton is the norm. Both flavors talk to mountCrudRoutes /
15
+ // mountReadOnlyCrudRoutes with identical wire behavior.
16
+ //
17
+ // 2) apiPrefix is composed into the resource path (`${apiPrefix}${path}`)
18
+ // rather than wrapping the registration. Hono has no fastify.register
19
+ // / prefix-wrapping primitive; sub-apps via `app.route(prefix, sub)`
20
+ // exist but bloat the generated code. String concat is simpler and
21
+ // produces identical URL grammar.
22
+ //
23
+ // Dispatch logic mirrors Fastify:
24
+ // isProjection(entity) → mountReadOnlyCrudRoutes (GET list + GET :id)
25
+ // vanilla / write-through entity → mountCrudRoutes (all 5 CRUD verbs)
26
+
27
+ import { code, imp } from "ts-poet";
28
+ import type { MetaObject } from "@metaobjectsdev/metadata";
29
+ import { type RenderContext } from "../render-context.js";
30
+ import { entityModuleSpecifier } from "../import-path.js";
31
+ import { GENERATED_HEADER } from "../constants.js";
32
+ import { variableNameFromEntity } from "../naming.js";
33
+ import { isProjection } from "../projection/projection-detector.js";
34
+
35
+ export function renderRoutesFileHono(entity: MetaObject, ctx: RenderContext): string {
36
+ const entityName = entity.name;
37
+ const handlerName = `register${entityName}Routes`;
38
+
39
+ const entityFileSpec = entityModuleSpecifier(
40
+ ctx.selfTarget,
41
+ ctx.entityModuleTarget,
42
+ entity.package,
43
+ entityName,
44
+ ctx.extStyle,
45
+ );
46
+
47
+ const header =
48
+ `// ${GENERATED_HEADER} — DO NOT EDIT.\n` +
49
+ `// Source metadata: ${entityName} (${entity.fqn()})\n` +
50
+ `// Customize via ${entityName}.extra.ts in this directory (e.g., auth, additional handlers).\n`;
51
+
52
+ // Path composition: apiPrefix is a literal string in the URL.
53
+ const pathExpr = ctx.apiPrefix
54
+ ? `\`${ctx.apiPrefix}\${${entityName}.$path}\``
55
+ : `${entityName}.$path`;
56
+
57
+ // --- Projection path: read-only routes (GET list + GET :id) ---
58
+ if (isProjection(entity)) {
59
+ const camelName = entityName.charAt(0).toLowerCase() + entityName.slice(1);
60
+ const HonoSym = imp("t:Hono@hono");
61
+ const mountReadOnlyCrudRoutesSym = imp(
62
+ "mountReadOnlyCrudRoutes@@metaobjectsdev/runtime-ts/hono",
63
+ );
64
+
65
+ const literalImports = code`
66
+ import {
67
+ ${entityName},
68
+ ${camelName}View,
69
+ ${entityName}FilterAllowlist,
70
+ ${entityName}SortAllowlist,
71
+ } from ${JSON.stringify(entityFileSpec)};
72
+ `;
73
+
74
+ const body = code`
75
+ /**
76
+ * Mount read-only REST endpoints for ${entityName} (projection — view-backed, no writes).
77
+ *
78
+ * Exposes GET list + GET :id only. POST/PATCH/DELETE return 405.
79
+ * Customize: register this as-is, or import individual route helpers from
80
+ * @metaobjectsdev/runtime-ts/hono.
81
+ */
82
+ // biome-ignore lint/suspicious/noExplicitAny: consumer-defined Hono bindings/variables
83
+ export function ${handlerName}(app: ${HonoSym}<any, any, any>, deps: { db: unknown }): void {
84
+ ${mountReadOnlyCrudRoutesSym}({
85
+ app,
86
+ path: ${pathExpr},
87
+ db: deps.db,
88
+ view: ${camelName}View,
89
+ filterAllowlist: ${entityName}FilterAllowlist,
90
+ sortAllowlist: ${entityName}SortAllowlist,
91
+ dialect: ${JSON.stringify(ctx.dialect)},
92
+ });
93
+ }
94
+ `;
95
+
96
+ return header + literalImports.toString() + body.toString();
97
+ }
98
+
99
+ // --- Vanilla / write-through entity path: full CRUD routes ---
100
+ const tableVar = variableNameFromEntity(entityName);
101
+
102
+ const HonoSym = imp("t:Hono@hono");
103
+ const mountCrudRoutesSym = imp("mountCrudRoutes@@metaobjectsdev/runtime-ts/hono");
104
+
105
+ const literalImports = code`
106
+ import {
107
+ ${entityName},
108
+ ${tableVar},
109
+ ${entityName}InsertSchema,
110
+ ${entityName}UpdateSchema,
111
+ ${entityName}FilterAllowlist,
112
+ ${entityName}SortAllowlist,
113
+ } from ${JSON.stringify(entityFileSpec)};
114
+ `;
115
+
116
+ const body = code`
117
+ /**
118
+ * Mount the 5 standard REST endpoints for ${entityName} using Drizzle directly.
119
+ *
120
+ * Customize: register this as-is for stock CRUD, OR import the per-verb
121
+ * helpers (mountListRoute, mountGetRoute, ...) from
122
+ * @metaobjectsdev/runtime-ts/hono and mix with your own handlers
123
+ * (auth, side effects, etc.).
124
+ */
125
+ // biome-ignore lint/suspicious/noExplicitAny: consumer-defined Hono bindings/variables
126
+ export function ${handlerName}(app: ${HonoSym}<any, any, any>, deps: { db: unknown }): void {
127
+ ${mountCrudRoutesSym}({
128
+ app,
129
+ path: ${pathExpr},
130
+ db: deps.db,
131
+ table: ${tableVar},
132
+ insertSchema: ${entityName}InsertSchema,
133
+ updateSchema: ${entityName}UpdateSchema,
134
+ filterAllowlist: ${entityName}FilterAllowlist,
135
+ sortAllowlist: ${entityName}SortAllowlist,
136
+ dialect: ${JSON.stringify(ctx.dialect)},
137
+ });
138
+ }
139
+ `;
140
+
141
+ return header + literalImports.toString() + body.toString();
142
+ }
@@ -0,0 +1,30 @@
1
+ // Value-object file composer — emits a streamlined "value-only" module for
2
+ // metaobjects with no writable source.rdb child. Output is just the TS
3
+ // interface + the Zod schema (and enum type aliases when applicable); no
4
+ // Drizzle table, no Infer*Model aliases, no filter allowlists, no constants
5
+ // object.
6
+ //
7
+ // Dispatch: entity-file.ts routes here when hasWritableRdbSource(entity) is
8
+ // false. The entity may still have read-only source.* children (those are
9
+ // handled by the projection path before this one is reached).
10
+
11
+ import { joinCode, type Code } from "ts-poet";
12
+ import type { MetaObject } from "@metaobjectsdev/metadata";
13
+ import { renderValueObjectInterface, renderEnumTypeAliases } from "./inferred-types.js";
14
+ import { renderInsertSchemaOnly } from "./zod-validators.js";
15
+ import { GENERATED_HEADER } from "../constants.js";
16
+
17
+ export function renderValueObjectFile(obj: MetaObject): string {
18
+ const enumAliases = renderEnumTypeAliases(obj);
19
+ const sections: Code[] = [
20
+ renderValueObjectInterface(obj),
21
+ ...(enumAliases !== null ? [enumAliases] : []),
22
+ renderInsertSchemaOnly(obj),
23
+ ];
24
+ const body = joinCode(sections, { on: "\n" }).toString();
25
+ const header =
26
+ `// ${GENERATED_HEADER} — DO NOT EDIT.\n` +
27
+ `// Source metadata: ${obj.name} (${obj.fqn()})\n` +
28
+ `// Customize via ${obj.name}.extra.ts in this directory.\n`;
29
+ return header + body;
30
+ }
@@ -2,53 +2,102 @@
2
2
  // Auto-generated PKs are EXCLUDED from InsertSchema (caller doesn't provide them).
3
3
  // @autoSet fields: INSERT → .optional().transform(() => new Date().toISOString())
4
4
  // UPDATE → onCreate fields omitted entirely; onUpdate gets same transform
5
+ //
6
+ // field.object isArray:true objectRef:<Ref> — emits z.array(<Ref>InsertSchema)
7
+ // with a cross-module imp() so consumers passing the schema to
8
+ // zod-to-json-schema get a properly element-typed array. Without this, every
9
+ // object-array field collapsed to z.array(z.string()) and the JSON Schema sent
10
+ // downstream (e.g. to LLM tool_use input_schema) lost the nested object shape.
5
11
 
6
- import { code, imp, type Code } from "ts-poet";
12
+ import { code, joinCode, imp, type Code } from "ts-poet";
7
13
  import { MetaObject, MetaField } from "@metaobjectsdev/metadata";
8
14
  import {
9
15
  FIELD_SUBTYPE_STRING, FIELD_SUBTYPE_INT, FIELD_SUBTYPE_LONG, FIELD_SUBTYPE_CURRENCY,
10
16
  FIELD_SUBTYPE_BOOLEAN, FIELD_SUBTYPE_DOUBLE, FIELD_SUBTYPE_FLOAT,
11
17
  FIELD_SUBTYPE_DATE, FIELD_SUBTYPE_TIME, FIELD_SUBTYPE_TIMESTAMP,
12
- FIELD_SUBTYPE_ENUM,
18
+ FIELD_SUBTYPE_ENUM, FIELD_SUBTYPE_OBJECT,
13
19
  VALIDATOR_SUBTYPE_REQUIRED, VALIDATOR_SUBTYPE_LENGTH, VALIDATOR_SUBTYPE_REGEX,
14
20
  IDENTITY_ATTR_FIELDS, IDENTITY_ATTR_GENERATION,
15
21
  FIELD_ATTR_REQUIRED, FIELD_ATTR_MAX_LENGTH, FIELD_ATTR_DEFAULT,
16
- FIELD_ATTR_AUTO_SET, AUTO_SET_ON_CREATE, AUTO_SET_ON_UPDATE,
22
+ FIELD_ATTR_AUTO_SET, FIELD_ATTR_OBJECT_REF, AUTO_SET_ON_CREATE, AUTO_SET_ON_UPDATE,
17
23
  VALIDATOR_ATTR_MAX, VALIDATOR_ATTR_MIN, VALIDATOR_ATTR_PATTERN,
18
24
  GENERATION_INCREMENT, GENERATION_UUID,
19
25
  } from "@metaobjectsdev/metadata";
20
26
  import { enumValues, zodEnumExpr } from "../enum-meta.js";
21
27
  import { renderDocsFor } from "./jsdoc.js";
22
28
 
23
- export function renderZodValidators(obj: MetaObject): Code {
24
- const z = imp("z@zod");
29
+ /** Auto-generated PK field names that should be omitted from InsertSchema. */
30
+ function autoGenPkFieldNames(obj: MetaObject): Set<string> {
31
+ const out = new Set<string>();
25
32
  const primary = obj.primaryIdentity();
26
- const autoGenPkFields = new Set<string>();
27
33
  if (primary) {
28
34
  const generation = primary.ownAttr(IDENTITY_ATTR_GENERATION);
29
35
  if (generation === GENERATION_INCREMENT || generation === GENERATION_UUID) {
30
36
  const fields = primary.ownAttr(IDENTITY_ATTR_FIELDS);
31
37
  const fieldsList = Array.isArray(fields) ? fields : (typeof fields === "string" ? [fields] : []);
32
- for (const f of fieldsList) autoGenPkFields.add(String(f));
38
+ for (const f of fieldsList) out.add(String(f));
33
39
  }
34
40
  }
41
+ return out;
42
+ }
35
43
 
36
- const insertFieldLines: string[] = [];
37
- const updateFieldLines: string[] = [];
44
+ /**
45
+ * Emit ONLY the `<Name>InsertSchema`. Used by the value-object file emitter
46
+ * for metaobjects with no writable source.rdb — those have no PATCH/update
47
+ * semantics, so emitting an UpdateSchema would be misleading.
48
+ *
49
+ * The schema name is kept as `<Name>InsertSchema` even for pure value objects
50
+ * so consumer imports don't churn. A future polish PR could add a `<Name>Schema`
51
+ * alias for clarity.
52
+ */
53
+ export function renderInsertSchemaOnly(obj: MetaObject): Code {
54
+ const z = imp("z@zod");
55
+ const autoGenPkFields = autoGenPkFieldNames(obj);
56
+
57
+ const insertFieldLines: Code[] = [];
58
+ for (const child of obj.fields()) {
59
+ if (autoGenPkFields.has(child.name)) continue;
60
+
61
+ const autoSet = child.ownAttr(FIELD_ATTR_AUTO_SET);
62
+
63
+ if (autoSet === AUTO_SET_ON_CREATE || autoSet === AUTO_SET_ON_UPDATE) {
64
+ insertFieldLines.push(
65
+ code` ${child.name}: z.string().optional().transform(() => new Date().toISOString())`,
66
+ );
67
+ } else {
68
+ insertFieldLines.push(code` ${child.name}: ${zodFieldExpr(child)}`);
69
+ }
70
+ }
71
+
72
+ const insertSchemaName = `${obj.name}InsertSchema`;
73
+ const docs = renderDocsFor(obj);
74
+ const docsPrefix = docs ? `${docs}\n` : "";
75
+
76
+ return code`
77
+ ${docsPrefix}export const ${insertSchemaName} = ${z}.object({
78
+ ${joinCode(insertFieldLines, { on: ",\n" })}
79
+ });
80
+ `;
81
+ }
82
+
83
+ export function renderZodValidators(obj: MetaObject): Code {
84
+ const z = imp("z@zod");
85
+ const autoGenPkFields = autoGenPkFieldNames(obj);
86
+
87
+ const insertFieldLines: Code[] = [];
88
+ const updateFieldLines: Code[] = [];
38
89
  for (const child of obj.fields()) {
39
90
  if (autoGenPkFields.has(child.name)) continue;
40
91
 
41
92
  const autoSet = child.ownAttr(FIELD_ATTR_AUTO_SET);
42
93
 
43
94
  // Insert schema: @autoSet fields use transform (always override client input).
44
- // NOTE: use "z" as a literal string here — these lines are embedded in the
45
- // `code` template tag below which resolves the imp("z@zod") import.
46
95
  if (autoSet === AUTO_SET_ON_CREATE || autoSet === AUTO_SET_ON_UPDATE) {
47
96
  insertFieldLines.push(
48
- ` ${child.name}: z.string().optional().transform(() => new Date().toISOString())`,
97
+ code` ${child.name}: z.string().optional().transform(() => new Date().toISOString())`,
49
98
  );
50
99
  } else {
51
- insertFieldLines.push(` ${child.name}: ${zodFieldExpr(child)}`);
100
+ insertFieldLines.push(code` ${child.name}: ${zodFieldExpr(child)}`);
52
101
  }
53
102
 
54
103
  // Update schema: @autoSet onCreate → omit entirely; onUpdate → transform
@@ -56,13 +105,16 @@ export function renderZodValidators(obj: MetaObject): Code {
56
105
  // Omit: creation timestamps cannot be changed after creation
57
106
  } else if (autoSet === AUTO_SET_ON_UPDATE) {
58
107
  updateFieldLines.push(
59
- ` ${child.name}: z.string().optional().transform(() => new Date().toISOString())`,
108
+ code` ${child.name}: z.string().optional().transform(() => new Date().toISOString())`,
60
109
  );
61
110
  } else {
62
- // All non-autoSet fields are optional in the update schema (PATCH semantics)
63
- const expr = zodFieldExpr(child);
64
- const optionalExpr = expr.endsWith(".optional()") ? expr : `${expr}.optional()`;
65
- updateFieldLines.push(` ${child.name}: ${optionalExpr}`);
111
+ // All non-autoSet fields are optional in the update schema (PATCH semantics).
112
+ // zodFieldExpr already appends .optional() when the field is non-required
113
+ // OR has a default; only append once more when it didn't.
114
+ const baseExpr = zodFieldExpr(child);
115
+ updateFieldLines.push(
116
+ fieldWillBeOptional(child) ? code` ${child.name}: ${baseExpr}` : code` ${child.name}: ${baseExpr}.optional()`,
117
+ );
66
118
  }
67
119
  }
68
120
 
@@ -74,48 +126,82 @@ export function renderZodValidators(obj: MetaObject): Code {
74
126
 
75
127
  return code`
76
128
  ${docsPrefix}export const ${insertSchemaName} = ${z}.object({
77
- ${insertFieldLines.join(",\n")}
129
+ ${joinCode(insertFieldLines, { on: ",\n" })}
78
130
  });
79
131
 
80
132
  ${docsPrefix}export const ${updateSchemaName} = ${z}.object({
81
- ${updateFieldLines.join(",\n")}
133
+ ${joinCode(updateFieldLines, { on: ",\n" })}
82
134
  });
83
135
  `;
84
136
  }
85
137
 
86
- function zodFieldExpr(field: MetaField): string {
87
- let base: string;
138
+ function zodFieldExpr(field: MetaField): Code {
139
+ // FIELD_SUBTYPE_OBJECT: emit z.array(<Ref>InsertSchema) / <Ref>InsertSchema
140
+ // via an imp() so ts-poet hoists the cross-module import. Without this the
141
+ // field used to collapse to z.string() / z.array(z.string()) and downstream
142
+ // JSON Schema (e.g. LLM tool_use input_schema) lost the nested object shape.
143
+ if (field.subType === FIELD_SUBTYPE_OBJECT) {
144
+ const ref = field.ownAttr(FIELD_ATTR_OBJECT_REF);
145
+ if (typeof ref === "string" && ref.length > 0) {
146
+ const refImp = imp(`${ref}InsertSchema@./${ref}.js`);
147
+ let base: Code = code`${refImp}`;
148
+ if (field.isArray) base = code`z.array(${base})`;
149
+ return appendValidatorChain(base, field);
150
+ }
151
+ // No resolvable @objectRef — fall through to z.unknown(); downstream code
152
+ // can still pass a value through but loses validation.
153
+ let base: Code = code`z.unknown()`;
154
+ if (field.isArray) base = code`z.array(${base})`;
155
+ return appendValidatorChain(base, field);
156
+ }
157
+
158
+ let baseStr: string;
88
159
  switch (field.subType) {
89
160
  case FIELD_SUBTYPE_INT:
90
161
  case FIELD_SUBTYPE_CURRENCY:
91
162
  case FIELD_SUBTYPE_LONG:
92
- base = "z.number().int()";
163
+ baseStr = "z.number().int()";
93
164
  break;
94
165
  case FIELD_SUBTYPE_DOUBLE:
95
166
  case FIELD_SUBTYPE_FLOAT:
96
- base = "z.number()";
167
+ baseStr = "z.number()";
97
168
  break;
98
169
  case FIELD_SUBTYPE_BOOLEAN:
99
- base = "z.boolean()";
170
+ baseStr = "z.boolean()";
100
171
  break;
101
172
  case FIELD_SUBTYPE_DATE:
102
173
  case FIELD_SUBTYPE_TIME:
103
174
  case FIELD_SUBTYPE_TIMESTAMP:
104
- base = "z.string()";
175
+ baseStr = "z.string()";
105
176
  break;
106
177
  case FIELD_SUBTYPE_ENUM: {
107
178
  const values = enumValues(field);
108
- base = values !== undefined ? zodEnumExpr(values) : "z.string()";
179
+ baseStr = values !== undefined ? zodEnumExpr(values) : "z.string()";
109
180
  break;
110
181
  }
111
182
  case FIELD_SUBTYPE_STRING:
112
183
  default:
113
- base = "z.string()";
184
+ baseStr = "z.string()";
114
185
  break;
115
186
  }
116
187
 
117
- if (field.isArray) base = `z.array(${base})`;
188
+ if (field.isArray) baseStr = `z.array(${baseStr})`;
189
+ return appendValidatorChain(code`${baseStr}`, field);
190
+ }
191
+
192
+ /** Mirrors the optional-or-not decision inside appendValidatorChain so the update-schema
193
+ * caller can avoid stacking a second `.optional()` onto an already-optional expression. */
194
+ function fieldWillBeOptional(field: MetaField): boolean {
195
+ let isRequired = field.ownAttr(FIELD_ATTR_REQUIRED) === true;
196
+ for (const child of field.validators()) {
197
+ if (child.subType === VALIDATOR_SUBTYPE_REQUIRED) isRequired = true;
198
+ }
199
+ const hasDefault = field.ownAttr(FIELD_ATTR_DEFAULT) !== undefined;
200
+ return !isRequired || hasDefault;
201
+ }
118
202
 
203
+ /** Append .min/.max/.regex/.optional() based on field-level validators + required state. */
204
+ function appendValidatorChain(base: Code, field: MetaField): Code {
119
205
  let isRequired = field.ownAttr(FIELD_ATTR_REQUIRED) === true;
120
206
  let maxLen: number | undefined = field.ownAttr(FIELD_ATTR_MAX_LENGTH) as number | undefined;
121
207
  let minLen: number | undefined;
@@ -134,18 +220,18 @@ function zodFieldExpr(field: MetaField): string {
134
220
  }
135
221
  }
136
222
 
137
- let chain = base;
223
+ let chain: Code = base;
138
224
  if (field.subType === FIELD_SUBTYPE_STRING && !field.isArray) {
139
- if (minLen !== undefined) chain += `.min(${minLen})`;
140
- else if (isRequired) chain += `.min(1)`;
141
- if (maxLen !== undefined) chain += `.max(${maxLen})`;
142
- if (pattern !== undefined) chain += `.regex(new RegExp(${JSON.stringify(pattern)}))`;
225
+ if (minLen !== undefined) chain = code`${chain}.min(${minLen})`;
226
+ else if (isRequired) chain = code`${chain}.min(1)`;
227
+ if (maxLen !== undefined) chain = code`${chain}.max(${maxLen})`;
228
+ if (pattern !== undefined) chain = code`${chain}.regex(new RegExp(${JSON.stringify(pattern)}))`;
143
229
  }
144
230
 
145
231
  // Fields with DB-level defaults are optional in the InsertSchema: the caller
146
232
  // can omit them and the DB will fill in. Otherwise required-with-default
147
233
  // would force callers to repeat the default at every call site.
148
234
  const hasDefault = field.ownAttr(FIELD_ATTR_DEFAULT) !== undefined;
149
- if (!isRequired || hasDefault) chain += `.optional()`;
235
+ if (!isRequired || hasDefault) chain = code`${chain}.optional()`;
150
236
  return chain;
151
237
  }