@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.
- package/README.md +30 -0
- package/dist/column-mapper.d.ts +16 -0
- package/dist/column-mapper.d.ts.map +1 -1
- package/dist/column-mapper.js +71 -1
- package/dist/column-mapper.js.map +1 -1
- package/dist/generators/docs-file.d.ts +8 -0
- package/dist/generators/docs-file.d.ts.map +1 -0
- package/dist/generators/docs-file.js +52 -0
- package/dist/generators/docs-file.js.map +1 -0
- package/dist/generators/entity-file.d.ts +15 -0
- package/dist/generators/entity-file.d.ts.map +1 -1
- package/dist/generators/entity-file.js +2 -1
- package/dist/generators/entity-file.js.map +1 -1
- package/dist/generators/index.d.ts +2 -0
- package/dist/generators/index.d.ts.map +1 -1
- package/dist/generators/index.js +2 -0
- package/dist/generators/index.js.map +1 -1
- package/dist/generators/prompt-render-file.d.ts.map +1 -1
- package/dist/generators/prompt-render-file.js +30 -12
- package/dist/generators/prompt-render-file.js.map +1 -1
- package/dist/generators/queries-file.d.ts +1 -1
- package/dist/generators/queries-file.d.ts.map +1 -1
- package/dist/generators/queries-file.js +11 -3
- package/dist/generators/queries-file.js.map +1 -1
- package/dist/generators/routes-file-hono.d.ts +21 -0
- package/dist/generators/routes-file-hono.d.ts.map +1 -0
- package/dist/generators/routes-file-hono.js +38 -0
- package/dist/generators/routes-file-hono.js.map +1 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/metaobjects-config.d.ts +9 -2
- package/dist/metaobjects-config.d.ts.map +1 -1
- package/dist/metaobjects-config.js.map +1 -1
- package/dist/payload-codegen.d.ts +8 -0
- package/dist/payload-codegen.d.ts.map +1 -1
- package/dist/payload-codegen.js +33 -3
- package/dist/payload-codegen.js.map +1 -1
- package/dist/source-detect.d.ts +10 -0
- package/dist/source-detect.d.ts.map +1 -0
- package/dist/source-detect.js +30 -0
- package/dist/source-detect.js.map +1 -0
- package/dist/templates/docs-file.d.ts +48 -0
- package/dist/templates/docs-file.d.ts.map +1 -0
- package/dist/templates/docs-file.js +451 -0
- package/dist/templates/docs-file.js.map +1 -0
- package/dist/templates/drizzle-schema.js +27 -3
- package/dist/templates/drizzle-schema.js.map +1 -1
- package/dist/templates/entity-file.d.ts +15 -1
- package/dist/templates/entity-file.d.ts.map +1 -1
- package/dist/templates/entity-file.js +15 -5
- package/dist/templates/entity-file.js.map +1 -1
- package/dist/templates/inferred-types.d.ts +9 -0
- package/dist/templates/inferred-types.d.ts.map +1 -1
- package/dist/templates/inferred-types.js +88 -2
- package/dist/templates/inferred-types.js.map +1 -1
- package/dist/templates/routes-file-hono.d.ts +4 -0
- package/dist/templates/routes-file-hono.d.ts.map +1 -0
- package/dist/templates/routes-file-hono.js +119 -0
- package/dist/templates/routes-file-hono.js.map +1 -0
- package/dist/templates/value-object-file.d.ts +3 -0
- package/dist/templates/value-object-file.d.ts.map +1 -0
- package/dist/templates/value-object-file.js +27 -0
- package/dist/templates/value-object-file.js.map +1 -0
- package/dist/templates/zod-validators.d.ts +10 -0
- package/dist/templates/zod-validators.d.ts.map +1 -1
- package/dist/templates/zod-validators.js +108 -30
- package/dist/templates/zod-validators.js.map +1 -1
- package/package.json +5 -4
- package/src/column-mapper.ts +84 -0
- package/src/generators/docs-file.ts +64 -0
- package/src/generators/entity-file.ts +17 -1
- package/src/generators/index.ts +2 -0
- package/src/generators/prompt-render-file.ts +38 -12
- package/src/generators/queries-file.ts +13 -4
- package/src/generators/routes-file-hono.ts +48 -0
- package/src/index.ts +2 -2
- package/src/metaobjects-config.ts +9 -2
- package/src/payload-codegen.ts +34 -2
- package/src/source-detect.ts +28 -0
- package/src/templates/docs-file.ts +552 -0
- package/src/templates/drizzle-schema.ts +27 -3
- package/src/templates/entity-file.ts +36 -5
- package/src/templates/inferred-types.ts +117 -3
- package/src/templates/routes-file-hono.ts +142 -0
- package/src/templates/value-object-file.ts +30 -0
- package/src/templates/zod-validators.ts +121 -35
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metaobjectsdev/codegen-ts",
|
|
3
|
-
"version": "0.7.0-rc.
|
|
3
|
+
"version": "0.7.0-rc.11",
|
|
4
4
|
"description": "TypeScript codegen engine for MetaObjects — emits Drizzle, Zod, and Fastify artifacts.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"access": "public"
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"@metaobjectsdev/metadata": "0.7.0-rc.
|
|
41
|
+
"@metaobjectsdev/metadata": "0.7.0-rc.11",
|
|
42
42
|
"@biomejs/js-api": "^0.7.0",
|
|
43
43
|
"@biomejs/wasm-nodejs": "^1.9.4",
|
|
44
44
|
"ts-poet": "^6.10.0"
|
|
@@ -49,10 +49,11 @@
|
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
51
|
"@biomejs/biome": "^1.9.0",
|
|
52
|
-
"@metaobjectsdev/codegen-ts-react": "0.7.0-rc.
|
|
53
|
-
"@metaobjectsdev/render": "0.7.0-rc.
|
|
52
|
+
"@metaobjectsdev/codegen-ts-react": "0.7.0-rc.11",
|
|
53
|
+
"@metaobjectsdev/render": "0.7.0-rc.11",
|
|
54
54
|
"bun-types": "latest",
|
|
55
55
|
"drizzle-orm": "^0.36.0",
|
|
56
|
+
"hono": "^4.6.0",
|
|
56
57
|
"typescript": "^5.6.0",
|
|
57
58
|
"zod": "^3.23.0"
|
|
58
59
|
}
|
package/src/column-mapper.ts
CHANGED
|
@@ -23,6 +23,7 @@ import {
|
|
|
23
23
|
FIELD_ATTR_REQUIRED,
|
|
24
24
|
FIELD_ATTR_UNIQUE,
|
|
25
25
|
FIELD_ATTR_DEFAULT,
|
|
26
|
+
FIELD_ATTR_OBJECT_REF,
|
|
26
27
|
VALIDATOR_ATTR_MAX,
|
|
27
28
|
} from "@metaobjectsdev/metadata";
|
|
28
29
|
import { columnNameFromField } from "./naming.js";
|
|
@@ -63,6 +64,36 @@ function isSqlExprDefault(value: string): boolean {
|
|
|
63
64
|
return SQL_EXPR_PATTERNS.some((re) => re.test(value));
|
|
64
65
|
}
|
|
65
66
|
|
|
67
|
+
/**
|
|
68
|
+
* For an isArray:true field stored in SQLite as text(...,{mode:"json"}), return
|
|
69
|
+
* the TS element type used in the emitted .$type<E[]>() chain. Returns undefined
|
|
70
|
+
* when the field's subType doesn't have a stable scalar TS mapping (e.g.,
|
|
71
|
+
* field.object — leave the inferred `unknown[]` so the consumer can layer a
|
|
72
|
+
* richer schema on top).
|
|
73
|
+
*/
|
|
74
|
+
function sqliteJsonArrayElementTsType(subType: string): string | undefined {
|
|
75
|
+
switch (subType) {
|
|
76
|
+
case FIELD_SUBTYPE_STRING:
|
|
77
|
+
case FIELD_SUBTYPE_ENUM:
|
|
78
|
+
case FIELD_SUBTYPE_CLASS:
|
|
79
|
+
case FIELD_SUBTYPE_DATE:
|
|
80
|
+
case FIELD_SUBTYPE_TIME:
|
|
81
|
+
case FIELD_SUBTYPE_TIMESTAMP:
|
|
82
|
+
case FIELD_SUBTYPE_DECIMAL:
|
|
83
|
+
return "string";
|
|
84
|
+
case FIELD_SUBTYPE_INT:
|
|
85
|
+
case FIELD_SUBTYPE_LONG:
|
|
86
|
+
case FIELD_SUBTYPE_CURRENCY:
|
|
87
|
+
case FIELD_SUBTYPE_DOUBLE:
|
|
88
|
+
case FIELD_SUBTYPE_FLOAT:
|
|
89
|
+
return "number";
|
|
90
|
+
case FIELD_SUBTYPE_BOOLEAN:
|
|
91
|
+
return "boolean";
|
|
92
|
+
default:
|
|
93
|
+
return undefined;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
66
97
|
/** Map a recognized SQL expression to its canonical raw form (uppercase keywords). */
|
|
67
98
|
function canonicalizeSqlExpr(value: string): string {
|
|
68
99
|
const lower = value.toLowerCase();
|
|
@@ -93,6 +124,17 @@ export interface ColumnSpec {
|
|
|
93
124
|
leadingComment?: string;
|
|
94
125
|
/** Optional CHECK constraint expression for the column (e.g., `status IN ('A', 'B')`). */
|
|
95
126
|
checkConstraint?: string;
|
|
127
|
+
/**
|
|
128
|
+
* Optional `.$type<...>()` chain target. Renderer (drizzle-schema.ts) emits
|
|
129
|
+
* `.$type<TS[]>()` ahead of the modifiers chain, using ts-poet `imp()` for
|
|
130
|
+
* objectRef variants so the cross-module type import auto-hoists.
|
|
131
|
+
* `kind: "scalar"` covers string[]/number[]/boolean[] — no import needed.
|
|
132
|
+
* `kind: "objectRef"` covers SourceLens[]/Dissent[]/etc. — `module` is the
|
|
133
|
+
* relative import path to that entity's emitted module.
|
|
134
|
+
*/
|
|
135
|
+
dollarTypeRef?:
|
|
136
|
+
| { kind: "scalar"; tsType: "string" | "number" | "boolean" }
|
|
137
|
+
| { kind: "objectRef"; name: string; module: string };
|
|
96
138
|
}
|
|
97
139
|
|
|
98
140
|
/** Resolve max length from validator.length child or @maxLength attr.
|
|
@@ -220,12 +262,36 @@ export function mapColumnType(
|
|
|
220
262
|
}
|
|
221
263
|
}
|
|
222
264
|
|
|
265
|
+
// Enum literal types: pass the values as `{ enum: [...] as const }` to
|
|
266
|
+
// Drizzle's text(...) so the inferred column type is a literal union
|
|
267
|
+
// ("a" | "b" | ...) instead of bare `string`. Skip when isArray — JSON
|
|
268
|
+
// arrays use { mode: "json" }, and the enum members go through Zod
|
|
269
|
+
// validation at the Insert/Update layer instead. Mirrors the Zod
|
|
270
|
+
// emission, which already uses z.enum([...]).
|
|
271
|
+
if (subType === FIELD_SUBTYPE_ENUM && !isArray && fnName === "text") {
|
|
272
|
+
const values = enumValues(field);
|
|
273
|
+
if (values !== undefined && values.length > 0) {
|
|
274
|
+
fnOptions = { ...(fnOptions ?? {}), enum: values };
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
223
278
|
const modifiers: string[] = [];
|
|
224
279
|
|
|
225
280
|
if (dialect === "postgres" && isArray) {
|
|
226
281
|
modifiers.push(".array()");
|
|
227
282
|
}
|
|
228
283
|
|
|
284
|
+
// SQLite stores arrays as JSON in a text column; Drizzle's text(...,{mode:"json"})
|
|
285
|
+
// infers the column as `unknown` without a $type<T>() annotation. Emit the
|
|
286
|
+
// chain via spec.dollarTypeRef so the renderer can hoist a type-only import
|
|
287
|
+
// for object refs (SourceLens[], Dissent[], etc.). Scalars (string/number/
|
|
288
|
+
// boolean) need no import. Postgres uses .array() above which is already
|
|
289
|
+
// element-typed by Drizzle.
|
|
290
|
+
// Determined ABOVE the modifiers chain so the renderer can position
|
|
291
|
+
// `.$type<>()` ahead of `.notNull()` etc.
|
|
292
|
+
// Note: dollarTypeRef is read alongside (and rendered ahead of) `modifiers`
|
|
293
|
+
// by `renderColumn` — see drizzle-schema.ts.
|
|
294
|
+
|
|
229
295
|
if (isRequired(field)) {
|
|
230
296
|
modifiers.push(".notNull()");
|
|
231
297
|
}
|
|
@@ -258,6 +324,23 @@ export function mapColumnType(
|
|
|
258
324
|
}
|
|
259
325
|
}
|
|
260
326
|
|
|
327
|
+
// SQLite isArray columns route through {mode:"json"} above; compute the
|
|
328
|
+
// $type<E[]>() target so the renderer can hoist any cross-module imports.
|
|
329
|
+
let dollarTypeRef: ColumnSpec["dollarTypeRef"];
|
|
330
|
+
if (dialect === "sqlite" && isArray) {
|
|
331
|
+
if (subType === FIELD_SUBTYPE_OBJECT) {
|
|
332
|
+
const ref = field.ownAttr(FIELD_ATTR_OBJECT_REF);
|
|
333
|
+
if (typeof ref === "string" && ref.length > 0) {
|
|
334
|
+
dollarTypeRef = { kind: "objectRef", name: ref, module: `./${ref}.js` };
|
|
335
|
+
}
|
|
336
|
+
} else {
|
|
337
|
+
const scalar = sqliteJsonArrayElementTsType(subType);
|
|
338
|
+
if (scalar !== undefined) {
|
|
339
|
+
dollarTypeRef = { kind: "scalar", tsType: scalar as "string" | "number" | "boolean" };
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
261
344
|
const result: ColumnSpec = {
|
|
262
345
|
fnName,
|
|
263
346
|
dbName,
|
|
@@ -267,6 +350,7 @@ export function mapColumnType(
|
|
|
267
350
|
};
|
|
268
351
|
if (fnOptions !== undefined) result.fnOptions = fnOptions;
|
|
269
352
|
if (defaultExpr !== undefined) result.defaultExpr = defaultExpr;
|
|
353
|
+
if (dollarTypeRef !== undefined) result.dollarTypeRef = dollarTypeRef;
|
|
270
354
|
if (leadingComment !== undefined) result.leadingComment = leadingComment;
|
|
271
355
|
|
|
272
356
|
// Enum fields: emit a CHECK constraint listing the valid member values.
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// docsFile() — emits `<Entity>.md` next to each generated entity module.
|
|
2
|
+
//
|
|
3
|
+
// Markdown is port-agnostic: the same metadata produces the same documentation
|
|
4
|
+
// page regardless of dialect, output layout, or runtime target. The page covers
|
|
5
|
+
// description / type / source / package preamble; storage table + identity +
|
|
6
|
+
// relationships for table-backed entities; validation entry points; "used by"
|
|
7
|
+
// template cross-references; and the generated-code surface (which sibling
|
|
8
|
+
// files codegen produces). See `templates/docs-file.ts` for the body.
|
|
9
|
+
|
|
10
|
+
import type { MetaObject } from "@metaobjectsdev/metadata";
|
|
11
|
+
import { perEntity, type Generator, type GeneratorFactory } from "../generator.js";
|
|
12
|
+
import { renderDocsFile } from "../templates/docs-file.js";
|
|
13
|
+
import { entityOutputPath } from "../import-path.js";
|
|
14
|
+
|
|
15
|
+
export interface DocsFileOpts {
|
|
16
|
+
filter?: (entity: MetaObject) => boolean;
|
|
17
|
+
target?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const docsFile = function docsFile(opts?: DocsFileOpts): Generator {
|
|
21
|
+
const generator: Generator = {
|
|
22
|
+
name: "docs-file",
|
|
23
|
+
generate: perEntity((entity, ctx) => {
|
|
24
|
+
if (!ctx.renderContext) {
|
|
25
|
+
throw new Error("docs-file: renderContext is required (provided by runGen)");
|
|
26
|
+
}
|
|
27
|
+
const rc = ctx.renderContext;
|
|
28
|
+
const generatorNames = readGeneratorNames(ctx);
|
|
29
|
+
return {
|
|
30
|
+
path: entityOutputPath(
|
|
31
|
+
ctx.config.outputLayout ?? "flat",
|
|
32
|
+
entity.package,
|
|
33
|
+
`${entity.name}.md`,
|
|
34
|
+
),
|
|
35
|
+
content: renderDocsFile(entity, {
|
|
36
|
+
dialect: rc.dialect,
|
|
37
|
+
columnNamingStrategy: rc.columnNamingStrategy,
|
|
38
|
+
loadedRoot: rc.loadedRoot,
|
|
39
|
+
generatorNames,
|
|
40
|
+
}),
|
|
41
|
+
};
|
|
42
|
+
}),
|
|
43
|
+
};
|
|
44
|
+
if (opts?.filter) {
|
|
45
|
+
generator.filter = opts.filter;
|
|
46
|
+
}
|
|
47
|
+
if (opts?.target) {
|
|
48
|
+
generator.target = opts.target;
|
|
49
|
+
}
|
|
50
|
+
return generator;
|
|
51
|
+
} as GeneratorFactory<DocsFileOpts>;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* `GenContext` does not currently carry the full generator list — that lives on
|
|
55
|
+
* the resolved config inside the runner. Rather than thread a new field through
|
|
56
|
+
* just to populate one optional markdown section, we always list every
|
|
57
|
+
* potential companion. The "Generated code" section becomes "files that may be
|
|
58
|
+
* generated alongside this one" — adopters cross-reference their own
|
|
59
|
+
* `metaobjects.config.ts` to confirm which they actually wired in. This matches
|
|
60
|
+
* the spec guidance "list them all and let the reader figure out which exist."
|
|
61
|
+
*/
|
|
62
|
+
function readGeneratorNames(_ctx: unknown): ReadonlySet<string> {
|
|
63
|
+
return new Set(["queries-file", "routes-file", "routes-file-hono"]);
|
|
64
|
+
}
|
|
@@ -7,9 +7,25 @@ import { entityOutputPath } from "../import-path.js";
|
|
|
7
7
|
export interface EntityFileOpts {
|
|
8
8
|
filter?: (entity: MetaObject) => boolean;
|
|
9
9
|
target?: string;
|
|
10
|
+
/**
|
|
11
|
+
* Whether generated entity files include the Fastify-flavored
|
|
12
|
+
* `<Entity>FilterAllowlist` + `<Entity>SortAllowlist` blocks (and the
|
|
13
|
+
* `@metaobjectsdev/runtime-ts/drizzle-fastify` type-only imports they
|
|
14
|
+
* require). Default `true` for back-compat.
|
|
15
|
+
*
|
|
16
|
+
* Set to `false` for Worker/Lambda-style consumers that don't mount
|
|
17
|
+
* Fastify-style server routes — the generated entity file then has no
|
|
18
|
+
* `runtime-ts/drizzle-fastify` imports at all and `@metaobjectsdev/runtime-ts`
|
|
19
|
+
* can be omitted from the consumer's dependency tree.
|
|
20
|
+
*
|
|
21
|
+
* The client-side `<Entity>Filter` type is always emitted — it's a pure
|
|
22
|
+
* client-side type with no runtime-ts dependency.
|
|
23
|
+
*/
|
|
24
|
+
allowlists?: boolean;
|
|
10
25
|
}
|
|
11
26
|
|
|
12
27
|
export const entityFile = function entityFile(opts?: EntityFileOpts): Generator {
|
|
28
|
+
const allowlists = opts?.allowlists ?? true;
|
|
13
29
|
const generator: Generator = {
|
|
14
30
|
name: "entity-file",
|
|
15
31
|
emitsEntityModule: true,
|
|
@@ -19,7 +35,7 @@ export const entityFile = function entityFile(opts?: EntityFileOpts): Generator
|
|
|
19
35
|
}
|
|
20
36
|
return {
|
|
21
37
|
path: entityOutputPath(ctx.config.outputLayout ?? "flat", entity.package, `${entity.name}.ts`),
|
|
22
|
-
content: await formatTs(renderEntityFile(entity, ctx.renderContext)),
|
|
38
|
+
content: await formatTs(renderEntityFile(entity, ctx.renderContext, { allowlists })),
|
|
23
39
|
};
|
|
24
40
|
}),
|
|
25
41
|
};
|
package/src/generators/index.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
export { entityFile, type EntityFileOpts } from "./entity-file.js";
|
|
2
2
|
export { queriesFile, type QueriesFileOpts } from "./queries-file.js";
|
|
3
3
|
export { routesFile, type RoutesFileOpts } from "./routes-file.js";
|
|
4
|
+
export { routesFileHono, type RoutesFileHonoOpts } from "./routes-file-hono.js";
|
|
4
5
|
export { barrel, type BarrelOpts } from "./barrel.js";
|
|
5
6
|
export { mermaidErDiagram, type MermaidErOptions } from "./mermaid-er.js";
|
|
6
7
|
export { promptRender, type PromptRenderOpts } from "./prompt-render-file.js";
|
|
7
8
|
export { outputParser, type OutputParserOpts } from "./output-parser-file.js";
|
|
9
|
+
export { docsFile, type DocsFileOpts } from "./docs-file.js";
|
|
@@ -16,9 +16,10 @@ import {
|
|
|
16
16
|
oncePerRun,
|
|
17
17
|
} from "../generator.js";
|
|
18
18
|
import {
|
|
19
|
-
|
|
19
|
+
generatePayloadInterfacesBatch,
|
|
20
20
|
generateRenderHandle,
|
|
21
21
|
} from "../payload-codegen.js";
|
|
22
|
+
import { GENERATED_HEADER } from "../constants.js";
|
|
22
23
|
|
|
23
24
|
export interface PromptRenderOpts {
|
|
24
25
|
/** Output file path relative to the target's outDir. Default: "prompts.ts". */
|
|
@@ -27,6 +28,17 @@ export interface PromptRenderOpts {
|
|
|
27
28
|
target?: string;
|
|
28
29
|
}
|
|
29
30
|
|
|
31
|
+
// Hoisted into the emitted file once. generateRenderHandle() emits this line
|
|
32
|
+
// per-handle (for the standalone scenario); we strip its per-handle copies.
|
|
33
|
+
const RENDER_IMPORT = `import { render, type Provider } from "@metaobjectsdev/render";`;
|
|
34
|
+
|
|
35
|
+
// Matches the `import type { ... } from "./payloads.js";` that generateRenderHandle
|
|
36
|
+
// emits for the standalone two-file scenario. In the single-file output here the
|
|
37
|
+
// payload interfaces are already defined above, so the import is dead.
|
|
38
|
+
function isStandalonePayloadImport(line: string): boolean {
|
|
39
|
+
return line.startsWith("import type {") && line.includes('"./payloads.js"');
|
|
40
|
+
}
|
|
41
|
+
|
|
30
42
|
export const promptRender = function promptRender(opts?: PromptRenderOpts): Generator {
|
|
31
43
|
const outFile = opts?.outFile ?? "prompts.ts";
|
|
32
44
|
const generator: Generator = {
|
|
@@ -41,21 +53,35 @@ export const promptRender = function promptRender(opts?: PromptRenderOpts): Gene
|
|
|
41
53
|
return [];
|
|
42
54
|
}
|
|
43
55
|
|
|
44
|
-
const parts: string[] = [];
|
|
45
|
-
|
|
46
|
-
|
|
56
|
+
const parts: string[] = [`// ${GENERATED_HEADER} — DO NOT EDIT.`];
|
|
57
|
+
|
|
58
|
+
// Hoist the @metaobjectsdev/render import once (only when prompts emit handles).
|
|
59
|
+
if (prompts.length > 0) {
|
|
60
|
+
parts.push(RENDER_IMPORT);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Emit payload interfaces with a single shared dedupe set so a lens
|
|
64
|
+
// referenced by multiple payloads appears exactly once.
|
|
65
|
+
const payloadInterfaces = generatePayloadInterfacesBatch(
|
|
66
|
+
ctx.loadedRoot,
|
|
67
|
+
payloads.map((p) => p.name),
|
|
68
|
+
);
|
|
69
|
+
if (payloadInterfaces.length > 0) {
|
|
70
|
+
parts.push(payloadInterfaces);
|
|
47
71
|
}
|
|
48
|
-
|
|
49
|
-
//
|
|
50
|
-
//
|
|
51
|
-
//
|
|
72
|
+
|
|
73
|
+
// Append each render handle with its per-handle imports stripped — both
|
|
74
|
+
// the now-hoisted render/Provider import and the standalone payloads.js
|
|
75
|
+
// import. Also drop any leading blank lines left behind by the strip so
|
|
76
|
+
// the joined output doesn't accumulate double blank gaps between parts.
|
|
52
77
|
for (const t of prompts) {
|
|
53
|
-
const
|
|
78
|
+
const lines = generateRenderHandle(ctx.loadedRoot, t.name)
|
|
54
79
|
.split("\n")
|
|
55
|
-
.filter((line) =>
|
|
56
|
-
|
|
57
|
-
parts.push(
|
|
80
|
+
.filter((line) => line !== RENDER_IMPORT && !isStandalonePayloadImport(line));
|
|
81
|
+
while (lines.length > 0 && lines[0]!.trim() === "") lines.shift();
|
|
82
|
+
parts.push(lines.join("\n"));
|
|
58
83
|
}
|
|
84
|
+
|
|
59
85
|
return [{
|
|
60
86
|
path: outFile,
|
|
61
87
|
content: parts.filter((s) => s.length > 0).map((s) => s.trimEnd()).join("\n\n") + "\n",
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { OBJECT_SUBTYPE_VALUE, type MetaObject } from "@metaobjectsdev/metadata";
|
|
2
2
|
import { perEntity, type Generator, type GeneratorFactory } from "../generator.js";
|
|
3
3
|
import { renderQueriesFile } from "../templates/queries-file.js";
|
|
4
4
|
import { formatTs } from "../format.js";
|
|
@@ -9,9 +9,21 @@ export interface QueriesFileOpts {
|
|
|
9
9
|
target?: string;
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
+
// object.value records have no primary identity, so the rendered queries module
|
|
13
|
+
// emits findById/updateById/deleteById against a non-existent column. Skipping
|
|
14
|
+
// value subtypes is unconditional — the user-supplied filter (if any) is applied
|
|
15
|
+
// on top via boolean AND.
|
|
16
|
+
const skipValueTypes = (e: MetaObject): boolean => e.subType !== OBJECT_SUBTYPE_VALUE;
|
|
17
|
+
|
|
12
18
|
export const queriesFile = function queriesFile(opts?: QueriesFileOpts): Generator {
|
|
19
|
+
const userFilter = opts?.filter;
|
|
20
|
+
const filter: (e: MetaObject) => boolean = userFilter
|
|
21
|
+
? (e) => skipValueTypes(e) && userFilter(e)
|
|
22
|
+
: skipValueTypes;
|
|
23
|
+
|
|
13
24
|
const generator: Generator = {
|
|
14
25
|
name: "queries-file",
|
|
26
|
+
filter,
|
|
15
27
|
generate: perEntity(async (entity, ctx) => {
|
|
16
28
|
if (!ctx.renderContext) {
|
|
17
29
|
throw new Error("queries-file: renderContext is required (provided by runGen)");
|
|
@@ -22,9 +34,6 @@ export const queriesFile = function queriesFile(opts?: QueriesFileOpts): Generat
|
|
|
22
34
|
};
|
|
23
35
|
}),
|
|
24
36
|
};
|
|
25
|
-
if (opts?.filter) {
|
|
26
|
-
generator.filter = opts.filter;
|
|
27
|
-
}
|
|
28
37
|
if (opts?.target) {
|
|
29
38
|
generator.target = opts.target;
|
|
30
39
|
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { MetaObject } from "@metaobjectsdev/metadata";
|
|
2
|
+
import { perEntity, type Generator, type GeneratorFactory } from "../generator.js";
|
|
3
|
+
import { renderRoutesFileHono } from "../templates/routes-file-hono.js";
|
|
4
|
+
import { formatTs } from "../format.js";
|
|
5
|
+
import { entityOutputPath } from "../import-path.js";
|
|
6
|
+
|
|
7
|
+
export interface RoutesFileHonoOpts {
|
|
8
|
+
filter?: (entity: MetaObject) => boolean;
|
|
9
|
+
target?: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Hono variant of routesFile() — emits `<Entity>.routes.hono.ts` mounting
|
|
14
|
+
* the same five CRUD verbs (GET list / GET :id / POST / PATCH+PUT / DELETE)
|
|
15
|
+
* against `@metaobjectsdev/runtime-ts/hono` rather than `…/drizzle-fastify`.
|
|
16
|
+
*
|
|
17
|
+
* Same cross-port wire contract (envelope shape, status codes, filter +
|
|
18
|
+
* sort + withCount semantics) as the Fastify flavor. Workers / Bun / Node
|
|
19
|
+
* consumers running Hono can replace hand-written route registration with
|
|
20
|
+
* this generator output one entity at a time.
|
|
21
|
+
*
|
|
22
|
+
* Per-entity opt-out via `@emitRoutes: false` is honored. If the user
|
|
23
|
+
* supplies their own filter, both must pass (AND).
|
|
24
|
+
*/
|
|
25
|
+
export const routesFileHono = function routesFileHono(opts?: RoutesFileHonoOpts): Generator {
|
|
26
|
+
const userFilter = opts?.filter ?? (() => true);
|
|
27
|
+
const generator: Generator = {
|
|
28
|
+
name: "routes-file-hono",
|
|
29
|
+
filter: (e: MetaObject) => e.ownAttr("emitRoutes") !== false && userFilter(e),
|
|
30
|
+
generate: perEntity(async (entity, ctx) => {
|
|
31
|
+
if (!ctx.renderContext) {
|
|
32
|
+
throw new Error("routes-file-hono: renderContext is required (provided by runGen)");
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
path: entityOutputPath(
|
|
36
|
+
ctx.config.outputLayout ?? "flat",
|
|
37
|
+
entity.package,
|
|
38
|
+
`${entity.name}.routes.hono.ts`,
|
|
39
|
+
),
|
|
40
|
+
content: await formatTs(renderRoutesFileHono(entity, ctx.renderContext)),
|
|
41
|
+
};
|
|
42
|
+
}),
|
|
43
|
+
};
|
|
44
|
+
if (opts?.target) {
|
|
45
|
+
generator.target = opts.target;
|
|
46
|
+
}
|
|
47
|
+
return generator;
|
|
48
|
+
} as GeneratorFactory<RoutesFileHonoOpts>;
|
package/src/index.ts
CHANGED
|
@@ -9,7 +9,7 @@ export type { RunGenOpts, RunGenResult } from "./runner.js";
|
|
|
9
9
|
export type { Generator, GenContext, EmittedFile, GeneratorFactory } from "./generator.js";
|
|
10
10
|
export { perEntity, oncePerRun } from "./generator.js";
|
|
11
11
|
|
|
12
|
-
export type { MetaobjectsGenConfig, NormalizedMetaobjectsGenConfig, ResolvedGenConfig, Dialect, ExtStyle, ColumnNamingStrategy } from "./metaobjects-config.js";
|
|
12
|
+
export type { MetaobjectsGenConfig, NormalizedMetaobjectsGenConfig, ResolvedGenConfig, Dialect, ExtStyle, ColumnNamingStrategy, MetaDataTypeProvider } from "./metaobjects-config.js";
|
|
13
13
|
export { defineConfig, normalizeConfig } from "./metaobjects-config.js";
|
|
14
14
|
|
|
15
15
|
export type { ColumnSpec, DefaultExpr } from "./column-mapper.js";
|
|
@@ -44,4 +44,4 @@ export { emitViewDdl } from "./projection/view-ddl-emit.js";
|
|
|
44
44
|
export type { EmitOptions as ViewDdlEmitOptions } from "./projection/view-ddl-emit.js";
|
|
45
45
|
export type { JoinNode, JoinTree, SelectColumn, SelectSpec, ViewSpec } from "./projection/view-spec.js";
|
|
46
46
|
// Prompt construction (FR-004): typed payload + render-handle codegen.
|
|
47
|
-
export { generatePayloadInterfaces, generateRenderHandle } from "./payload-codegen.js";
|
|
47
|
+
export { generatePayloadInterfaces, generatePayloadInterfacesBatch, generateRenderHandle } from "./payload-codegen.js";
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { DEFAULT_COLUMN_NAMING_STRATEGY, type ColumnNamingStrategy } from "@metaobjectsdev/metadata";
|
|
1
|
+
import { DEFAULT_COLUMN_NAMING_STRATEGY, type ColumnNamingStrategy, type MetaDataTypeProvider } from "@metaobjectsdev/metadata";
|
|
2
2
|
import type { Generator } from "./generator.js";
|
|
3
3
|
import type { ExtStyle } from "./render-context.js";
|
|
4
4
|
import type { OutputLayout, ResolvedTarget } from "./import-path.js";
|
|
5
5
|
|
|
6
6
|
export type Dialect = "sqlite" | "postgres";
|
|
7
7
|
/** Re-exported from metadata so codegen-ts consumers see one canonical type. */
|
|
8
|
-
export type { ColumnNamingStrategy } from "@metaobjectsdev/metadata";
|
|
8
|
+
export type { ColumnNamingStrategy, MetaDataTypeProvider } from "@metaobjectsdev/metadata";
|
|
9
9
|
export type { ExtStyle };
|
|
10
10
|
export type { OutputLayout };
|
|
11
11
|
export type { ResolvedTarget };
|
|
@@ -41,6 +41,13 @@ export interface MetaobjectsGenConfig extends ResolvedGenConfig {
|
|
|
41
41
|
targets?: Record<string, TargetConfig>;
|
|
42
42
|
/** importBase for the default target (top-level outDir). */
|
|
43
43
|
importBase?: string;
|
|
44
|
+
/**
|
|
45
|
+
* Consumer-supplied {@link MetaDataTypeProvider}s. Threaded to `loadMemory`
|
|
46
|
+
* by the CLI's gen/migrate commands so a project can register its own
|
|
47
|
+
* subtypes/attrs (e.g. a `template.toolcall` subtype) without forking the
|
|
48
|
+
* loader. Composed AFTER the default core+forge bundle.
|
|
49
|
+
*/
|
|
50
|
+
providers?: readonly MetaDataTypeProvider[];
|
|
44
51
|
}
|
|
45
52
|
|
|
46
53
|
/** MetaobjectsGenConfig after applying defaults. All fields required.
|
package/src/payload-codegen.ts
CHANGED
|
@@ -17,6 +17,7 @@ import {
|
|
|
17
17
|
TYPE_TEMPLATE,
|
|
18
18
|
FIELD_SUBTYPE_OBJECT,
|
|
19
19
|
FIELD_ATTR_OBJECT_REF,
|
|
20
|
+
FIELD_ATTR_REQUIRED,
|
|
20
21
|
TEMPLATE_ATTR_PAYLOAD_REF,
|
|
21
22
|
TEMPLATE_ATTR_TEXT_REF,
|
|
22
23
|
TEMPLATE_ATTR_FORMAT,
|
|
@@ -55,7 +56,13 @@ function fieldTsType(field: MetaData): { type: string; refVo?: string } {
|
|
|
55
56
|
if (typeof ref === "string") result.refVo = ref;
|
|
56
57
|
return result;
|
|
57
58
|
}
|
|
58
|
-
|
|
59
|
+
const scalar = SCALAR_TS[field.subType] ?? "unknown";
|
|
60
|
+
return { type: field.isArray ? `${scalar}[]` : scalar };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** True iff the field's @required is explicitly set to true. */
|
|
64
|
+
function isFieldRequired(field: MetaData): boolean {
|
|
65
|
+
return field.ownAttr(FIELD_ATTR_REQUIRED) === true;
|
|
59
66
|
}
|
|
60
67
|
|
|
61
68
|
function emitInterface(root: MetaData, voName: string, emitted: Set<string>, out: string[]): void {
|
|
@@ -67,7 +74,15 @@ function emitInterface(root: MetaData, voName: string, emitted: Set<string>, out
|
|
|
67
74
|
const refs: string[] = [];
|
|
68
75
|
for (const f of vo.children().filter((c) => c.type === TYPE_FIELD)) {
|
|
69
76
|
const { type, refVo } = fieldTsType(f);
|
|
70
|
-
|
|
77
|
+
// Required fields: `name: T;`
|
|
78
|
+
// Optional fields: `name?: T | null;` — the `| null` lets values from
|
|
79
|
+
// Drizzle entity rows (which return `null` for nullable columns) flow
|
|
80
|
+
// straight in. Without it, TS treats undefined-vs-null as a hard error
|
|
81
|
+
// at the entity → payload boundary.
|
|
82
|
+
const isRequired = isFieldRequired(f);
|
|
83
|
+
const tsType = isRequired ? type : `${type} | null`;
|
|
84
|
+
const optional = isRequired ? "" : "?";
|
|
85
|
+
lines.push(` ${f.name}${optional}: ${tsType};`);
|
|
71
86
|
if (refVo) refs.push(refVo);
|
|
72
87
|
}
|
|
73
88
|
lines.push("}");
|
|
@@ -82,6 +97,23 @@ export function generatePayloadInterfaces(root: MetaData, voName: string): strin
|
|
|
82
97
|
return out.join("\n\n") + "\n";
|
|
83
98
|
}
|
|
84
99
|
|
|
100
|
+
/**
|
|
101
|
+
* Emit interfaces for several payloads at once, using a single shared dedupe
|
|
102
|
+
* set so nested types (e.g. lens projections referenced by multiple payloads)
|
|
103
|
+
* appear exactly once in the combined output.
|
|
104
|
+
*
|
|
105
|
+
* Returns the empty string when `voNames` is empty.
|
|
106
|
+
*/
|
|
107
|
+
export function generatePayloadInterfacesBatch(root: MetaData, voNames: readonly string[]): string {
|
|
108
|
+
if (voNames.length === 0) return "";
|
|
109
|
+
const out: string[] = [];
|
|
110
|
+
const emitted = new Set<string>();
|
|
111
|
+
for (const name of voNames) {
|
|
112
|
+
emitInterface(root, name, emitted, out);
|
|
113
|
+
}
|
|
114
|
+
return out.length === 0 ? "" : out.join("\n\n") + "\n";
|
|
115
|
+
}
|
|
116
|
+
|
|
85
117
|
function pascal(s: string): string {
|
|
86
118
|
return s.length > 0 ? s[0]!.toUpperCase() + s.slice(1) : s;
|
|
87
119
|
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// Source-detect helpers — discriminate table-backed entities from value-only
|
|
2
|
+
// objects (and other in-memory / transit shapes) by inspecting source.* children.
|
|
3
|
+
//
|
|
4
|
+
// Used by the entity-file composer to pick a streamlined "value-only" emission
|
|
5
|
+
// path for metaobjects that declare no writable relational source. Pure
|
|
6
|
+
// metadata-driven, not a typeId discriminator: any object subtype can opt out
|
|
7
|
+
// of Drizzle table emission simply by omitting source.rdb.
|
|
8
|
+
|
|
9
|
+
import { MetaSource } from "@metaobjectsdev/metadata";
|
|
10
|
+
import { TYPE_SOURCE, SOURCE_SUBTYPE_RDB } from "@metaobjectsdev/metadata";
|
|
11
|
+
import type { MetaObject } from "@metaobjectsdev/metadata";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* True when the entity declares at least one writable source.rdb child.
|
|
15
|
+
* Discriminates table-backed entities (full Drizzle file: table + Insert/Update
|
|
16
|
+
* schemas + filter allowlists + constants) from value-only objects (TS
|
|
17
|
+
* interface + Zod schema only). Absence of source.rdb means in-memory /
|
|
18
|
+
* transit data — no migration, no ORM table to point at.
|
|
19
|
+
*/
|
|
20
|
+
export function hasWritableRdbSource(entity: MetaObject): boolean {
|
|
21
|
+
for (const child of entity.ownChildren()) {
|
|
22
|
+
if (child.type !== TYPE_SOURCE) continue;
|
|
23
|
+
if (child.subType !== SOURCE_SUBTYPE_RDB) continue;
|
|
24
|
+
if (!(child instanceof MetaSource)) continue;
|
|
25
|
+
if (child.isWritable()) return true;
|
|
26
|
+
}
|
|
27
|
+
return false;
|
|
28
|
+
}
|