@metaobjectsdev/codegen-ts 0.7.0-rc.9 → 0.8.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/generator.d.ts +9 -0
- package/dist/generator.d.ts.map +1 -1
- package/dist/generator.js.map +1 -1
- package/dist/generators/docs-data-builder.d.ts +16 -0
- package/dist/generators/docs-data-builder.d.ts.map +1 -0
- package/dist/generators/docs-data-builder.js +381 -0
- package/dist/generators/docs-data-builder.js.map +1 -0
- package/dist/generators/docs-data.d.ts +98 -0
- package/dist/generators/docs-data.d.ts.map +1 -0
- package/dist/generators/docs-data.js +43 -0
- package/dist/generators/docs-data.js.map +1 -0
- 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 +77 -0
- package/dist/generators/docs-file.js.map +1 -0
- package/dist/generators/entity-file.d.ts.map +1 -1
- package/dist/generators/entity-file.js +7 -0
- package/dist/generators/entity-file.js.map +1 -1
- package/dist/generators/index.d.ts +5 -0
- package/dist/generators/index.d.ts.map +1 -1
- package/dist/generators/index.js +4 -0
- package/dist/generators/index.js.map +1 -1
- package/dist/generators/output-prompt-file.d.ts +9 -0
- package/dist/generators/output-prompt-file.d.ts.map +1 -0
- package/dist/generators/output-prompt-file.js +51 -0
- package/dist/generators/output-prompt-file.js.map +1 -0
- package/dist/generators/template-generator.d.ts +41 -0
- package/dist/generators/template-generator.d.ts.map +1 -0
- package/dist/generators/template-generator.js +62 -0
- package/dist/generators/template-generator.js.map +1 -0
- package/dist/index.d.ts +7 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -1
- package/dist/index.js.map +1 -1
- package/dist/instance-artifacts.d.ts +29 -0
- package/dist/instance-artifacts.d.ts.map +1 -0
- package/dist/instance-artifacts.js +57 -0
- package/dist/instance-artifacts.js.map +1 -0
- package/dist/metaobjects-config.d.ts +10 -0
- package/dist/metaobjects-config.d.ts.map +1 -1
- package/dist/metaobjects-config.js +1 -0
- package/dist/metaobjects-config.js.map +1 -1
- package/dist/overwrite-policy.d.ts +39 -2
- package/dist/overwrite-policy.d.ts.map +1 -1
- package/dist/overwrite-policy.js +233 -13
- package/dist/overwrite-policy.js.map +1 -1
- package/dist/render-context.d.ts +4 -1
- package/dist/render-context.d.ts.map +1 -1
- package/dist/render-context.js +1 -0
- package/dist/render-context.js.map +1 -1
- package/dist/render-engine/framework-provider.d.ts +28 -0
- package/dist/render-engine/framework-provider.d.ts.map +1 -0
- package/dist/render-engine/framework-provider.js +104 -0
- package/dist/render-engine/framework-provider.js.map +1 -0
- package/dist/runner.d.ts +15 -1
- package/dist/runner.d.ts.map +1 -1
- package/dist/runner.js +45 -6
- package/dist/runner.js.map +1 -1
- package/dist/templates/docs-file.d.ts +17 -0
- package/dist/templates/docs-file.d.ts.map +1 -0
- package/dist/templates/docs-file.js +37 -0
- package/dist/templates/docs-file.js.map +1 -0
- package/dist/templates/entity-file.d.ts.map +1 -1
- package/dist/templates/entity-file.js +12 -0
- package/dist/templates/entity-file.js.map +1 -1
- package/dist/templates/fr010-field-mapping.d.ts +28 -0
- package/dist/templates/fr010-field-mapping.d.ts.map +1 -0
- package/dist/templates/fr010-field-mapping.js +170 -0
- package/dist/templates/fr010-field-mapping.js.map +1 -0
- package/dist/templates/output-format-spec-emitter.d.ts +4 -0
- package/dist/templates/output-format-spec-emitter.d.ts.map +1 -0
- package/dist/templates/output-format-spec-emitter.js +60 -0
- package/dist/templates/output-format-spec-emitter.js.map +1 -0
- package/dist/templates/output-parser.d.ts.map +1 -1
- package/dist/templates/output-parser.js +69 -4
- package/dist/templates/output-parser.js.map +1 -1
- package/dist/templates/output-prompt.d.ts +10 -0
- package/dist/templates/output-prompt.d.ts.map +1 -0
- package/dist/templates/output-prompt.js +75 -0
- package/dist/templates/output-prompt.js.map +1 -0
- package/dist/templates/recover-schema-emitter.d.ts +8 -0
- package/dist/templates/recover-schema-emitter.d.ts.map +1 -0
- package/dist/templates/recover-schema-emitter.js +64 -0
- package/dist/templates/recover-schema-emitter.js.map +1 -0
- package/package.json +5 -5
- package/src/generator.ts +9 -0
- package/src/generators/docs-data-builder.ts +470 -0
- package/src/generators/docs-data.ts +154 -0
- package/src/generators/docs-file.ts +87 -0
- package/src/generators/entity-file.ts +7 -0
- package/src/generators/index.ts +17 -0
- package/src/generators/output-prompt-file.ts +66 -0
- package/src/generators/template-generator.ts +106 -0
- package/src/index.ts +34 -2
- package/src/instance-artifacts.ts +61 -0
- package/src/metaobjects-config.ts +11 -0
- package/src/overwrite-policy.ts +325 -14
- package/src/render-context.ts +5 -1
- package/src/render-engine/framework-provider.ts +107 -0
- package/src/runner.ts +66 -6
- package/src/templates/docs-file.ts +51 -0
- package/src/templates/entity-file.ts +13 -0
- package/src/templates/fr010-field-mapping.ts +191 -0
- package/src/templates/output-format-spec-emitter.ts +97 -0
- package/src/templates/output-parser.ts +77 -2
- package/src/templates/output-prompt.ts +88 -0
- package/src/templates/recover-schema-emitter.ts +91 -0
- package/templates/docs/entity-page.md.mustache +54 -0
|
@@ -0,0 +1,470 @@
|
|
|
1
|
+
// Helper that turns a MetaObject (+ root) into the EntityDocData shape the
|
|
2
|
+
// templates consume. The previous hand-coded `renderDocsFile()` mixed data
|
|
3
|
+
// extraction with string emission; this module is the data-only half — the
|
|
4
|
+
// markdown structure now lives in templates/docs/entity-page.md.mustache.
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
type MetaObject,
|
|
8
|
+
type MetaField,
|
|
9
|
+
type MetaIdentity,
|
|
10
|
+
type MetaReferenceIdentity,
|
|
11
|
+
type MetaRoot,
|
|
12
|
+
TYPE_TEMPLATE,
|
|
13
|
+
TEMPLATE_ATTR_PAYLOAD_REF,
|
|
14
|
+
OBJECT_SUBTYPE_VALUE,
|
|
15
|
+
IDENTITY_SUBTYPE_PRIMARY,
|
|
16
|
+
IDENTITY_SUBTYPE_SECONDARY,
|
|
17
|
+
IDENTITY_SUBTYPE_REFERENCE,
|
|
18
|
+
IDENTITY_ATTR_GENERATION,
|
|
19
|
+
RELATIONSHIP_ATTR_CARDINALITY,
|
|
20
|
+
RELATIONSHIP_ATTR_OBJECT_REF,
|
|
21
|
+
RELATIONSHIP_SUBTYPE_COMPOSITION,
|
|
22
|
+
RELATIONSHIP_SUBTYPE_AGGREGATION,
|
|
23
|
+
RELATIONSHIP_SUBTYPE_ASSOCIATION,
|
|
24
|
+
FIELD_SUBTYPE_ENUM,
|
|
25
|
+
FIELD_SUBTYPE_OBJECT,
|
|
26
|
+
FIELD_SUBTYPE_STRING,
|
|
27
|
+
FIELD_SUBTYPE_CLASS,
|
|
28
|
+
FIELD_SUBTYPE_INT,
|
|
29
|
+
FIELD_SUBTYPE_SHORT,
|
|
30
|
+
FIELD_SUBTYPE_BYTE,
|
|
31
|
+
FIELD_SUBTYPE_LONG,
|
|
32
|
+
FIELD_SUBTYPE_DOUBLE,
|
|
33
|
+
FIELD_SUBTYPE_FLOAT,
|
|
34
|
+
FIELD_SUBTYPE_DECIMAL,
|
|
35
|
+
FIELD_SUBTYPE_CURRENCY,
|
|
36
|
+
FIELD_SUBTYPE_BOOLEAN,
|
|
37
|
+
FIELD_SUBTYPE_DATE,
|
|
38
|
+
FIELD_SUBTYPE_TIME,
|
|
39
|
+
FIELD_SUBTYPE_TIMESTAMP,
|
|
40
|
+
FIELD_ATTR_REQUIRED,
|
|
41
|
+
FIELD_ATTR_UNIQUE,
|
|
42
|
+
FIELD_ATTR_OBJECT_REF,
|
|
43
|
+
FIELD_ATTR_MAX_LENGTH,
|
|
44
|
+
FIELD_ATTR_DEFAULT,
|
|
45
|
+
VALIDATOR_SUBTYPE_LENGTH,
|
|
46
|
+
VALIDATOR_SUBTYPE_REGEX,
|
|
47
|
+
VALIDATOR_SUBTYPE_NUMERIC,
|
|
48
|
+
VALIDATOR_SUBTYPE_REQUIRED,
|
|
49
|
+
VALIDATOR_ATTR_PATTERN,
|
|
50
|
+
VALIDATOR_ATTR_MIN,
|
|
51
|
+
VALIDATOR_ATTR_MAX,
|
|
52
|
+
DOC_ATTR_DESCRIPTION,
|
|
53
|
+
stripPackage,
|
|
54
|
+
} from "@metaobjectsdev/metadata";
|
|
55
|
+
import { mapColumnType, type Dialect } from "../column-mapper.js";
|
|
56
|
+
import type { ColumnNamingStrategy } from "../metaobjects-config.js";
|
|
57
|
+
import { toPascalCase } from "../naming.js";
|
|
58
|
+
import { enumValues } from "../enum-meta.js";
|
|
59
|
+
import { hasWritableRdbSource } from "../source-detect.js";
|
|
60
|
+
import { GENERATED_HEADER } from "../constants.js";
|
|
61
|
+
import type {
|
|
62
|
+
EntityDocData,
|
|
63
|
+
StorageFieldDoc,
|
|
64
|
+
IdentityDoc,
|
|
65
|
+
RelationshipDoc,
|
|
66
|
+
UsedByDoc,
|
|
67
|
+
GeneratedFileDoc,
|
|
68
|
+
} from "./docs-data.js";
|
|
69
|
+
|
|
70
|
+
export interface BuildDocDataOpts {
|
|
71
|
+
dialect: Dialect;
|
|
72
|
+
columnNamingStrategy?: ColumnNamingStrategy;
|
|
73
|
+
loadedRoot: MetaRoot;
|
|
74
|
+
/** Set of generator names present in the pipeline; drives "Generated code". */
|
|
75
|
+
generatorNames?: ReadonlySet<string>;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const SCALAR_TS_BY_SUBTYPE: Record<string, string> = {
|
|
79
|
+
[FIELD_SUBTYPE_STRING]: "string",
|
|
80
|
+
[FIELD_SUBTYPE_CLASS]: "string",
|
|
81
|
+
[FIELD_SUBTYPE_INT]: "number",
|
|
82
|
+
[FIELD_SUBTYPE_SHORT]: "number",
|
|
83
|
+
[FIELD_SUBTYPE_BYTE]: "number",
|
|
84
|
+
[FIELD_SUBTYPE_LONG]: "number",
|
|
85
|
+
[FIELD_SUBTYPE_DOUBLE]: "number",
|
|
86
|
+
[FIELD_SUBTYPE_FLOAT]: "number",
|
|
87
|
+
[FIELD_SUBTYPE_DECIMAL]: "number",
|
|
88
|
+
[FIELD_SUBTYPE_CURRENCY]: "number",
|
|
89
|
+
[FIELD_SUBTYPE_BOOLEAN]: "boolean",
|
|
90
|
+
[FIELD_SUBTYPE_DATE]: "string",
|
|
91
|
+
[FIELD_SUBTYPE_TIME]: "string",
|
|
92
|
+
[FIELD_SUBTYPE_TIMESTAMP]: "string",
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
function enumTypeAliasName(entity: MetaObject, field: MetaField): string {
|
|
96
|
+
const superField = field.resolveSuper();
|
|
97
|
+
return superField !== undefined
|
|
98
|
+
? toPascalCase(superField.name)
|
|
99
|
+
: `${entity.name}${toPascalCase(field.name)}`;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function isFieldRequired(field: MetaField): boolean {
|
|
103
|
+
if (field.ownAttr(FIELD_ATTR_REQUIRED) === true) return true;
|
|
104
|
+
return field.validators().some((v) => v.subType === VALIDATOR_SUBTYPE_REQUIRED);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function tsTypeForStorage(
|
|
108
|
+
entity: MetaObject,
|
|
109
|
+
field: MetaField,
|
|
110
|
+
pkFieldNames: ReadonlySet<string>,
|
|
111
|
+
): string {
|
|
112
|
+
let base: string;
|
|
113
|
+
|
|
114
|
+
if (field.subType === FIELD_SUBTYPE_ENUM) {
|
|
115
|
+
const values = enumValues(field);
|
|
116
|
+
if (values !== undefined && values.length > 0) {
|
|
117
|
+
if (field.isArray) {
|
|
118
|
+
base = `${enumTypeAliasName(entity, field)}[]`;
|
|
119
|
+
} else {
|
|
120
|
+
base = values.map((v) => JSON.stringify(v)).join(" | ");
|
|
121
|
+
}
|
|
122
|
+
} else {
|
|
123
|
+
base = field.isArray ? "string[]" : "string";
|
|
124
|
+
}
|
|
125
|
+
} else if (field.subType === FIELD_SUBTYPE_OBJECT) {
|
|
126
|
+
const ref = field.ownAttr(FIELD_ATTR_OBJECT_REF);
|
|
127
|
+
const refName = typeof ref === "string" && ref.length > 0 ? ref : "unknown";
|
|
128
|
+
base = field.isArray ? `${refName}[]` : refName;
|
|
129
|
+
} else {
|
|
130
|
+
const scalar = SCALAR_TS_BY_SUBTYPE[field.subType] ?? "unknown";
|
|
131
|
+
base = field.isArray ? `${scalar}[]` : scalar;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const required = pkFieldNames.has(field.name) || isFieldRequired(field);
|
|
135
|
+
return required ? base : `${base} | null`;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function sqlColumnExpr(spec: ReturnType<typeof mapColumnType>): string {
|
|
139
|
+
const dbName = JSON.stringify(spec.dbName);
|
|
140
|
+
if (spec.fnOptions !== undefined && Object.keys(spec.fnOptions).length > 0) {
|
|
141
|
+
const parts: string[] = [];
|
|
142
|
+
for (const [k, v] of Object.entries(spec.fnOptions)) {
|
|
143
|
+
const lit = JSON.stringify(v);
|
|
144
|
+
if (Array.isArray(v)) {
|
|
145
|
+
parts.push(`${k}: ${lit} as const`);
|
|
146
|
+
} else {
|
|
147
|
+
parts.push(`${k}: ${lit}`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return `${spec.fnName}(${dbName}, { ${parts.join(", ")} })`;
|
|
151
|
+
}
|
|
152
|
+
return `${spec.fnName}(${dbName})`;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function constraintsCell(
|
|
156
|
+
entity: MetaObject,
|
|
157
|
+
field: MetaField,
|
|
158
|
+
pkFieldNames: Set<string>,
|
|
159
|
+
fkMap: Map<string, { targetEntity: string; targetField: string }>,
|
|
160
|
+
): string {
|
|
161
|
+
const parts: string[] = [];
|
|
162
|
+
|
|
163
|
+
if (pkFieldNames.has(field.name)) {
|
|
164
|
+
parts.push("primary key");
|
|
165
|
+
const primary = entity.primaryIdentity();
|
|
166
|
+
const gen = primary?.ownAttr(IDENTITY_ATTR_GENERATION);
|
|
167
|
+
if (typeof gen === "string") {
|
|
168
|
+
parts.push(`generation: \`${gen}\``);
|
|
169
|
+
}
|
|
170
|
+
} else if (isFieldRequired(field)) {
|
|
171
|
+
parts.push("required");
|
|
172
|
+
} else {
|
|
173
|
+
parts.push("optional");
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (field.ownAttr(FIELD_ATTR_UNIQUE) === true) {
|
|
177
|
+
parts.push("unique");
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (field.isArray) {
|
|
181
|
+
parts.push("JSON column");
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (field.subType === FIELD_SUBTYPE_ENUM && !field.isArray) {
|
|
185
|
+
const values = enumValues(field);
|
|
186
|
+
if (values !== undefined && values.length > 0) {
|
|
187
|
+
const list = values.map((v) => `'${v.replace(/'/g, "''")}'`).join(", ");
|
|
188
|
+
parts.push(`CHECK \`${field.column ?? field.name} IN (${list})\``);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// Walk validators once, bucket by subtype. We re-emit in the original
|
|
193
|
+
// emission order to preserve byte-identity with the docs-file-basic
|
|
194
|
+
// conformance fixture: regex pattern → maxLength-from-@maxLength →
|
|
195
|
+
// length-validator (min/max) → numeric-validator (min/max).
|
|
196
|
+
const maxLenAttr = field.ownAttr(FIELD_ATTR_MAX_LENGTH);
|
|
197
|
+
const regexParts: string[] = [];
|
|
198
|
+
const lengthParts: string[] = [];
|
|
199
|
+
const numericParts: string[] = [];
|
|
200
|
+
for (const v of field.validators()) {
|
|
201
|
+
if (v.subType === VALIDATOR_SUBTYPE_REGEX) {
|
|
202
|
+
const pattern = v.ownAttr(VALIDATOR_ATTR_PATTERN);
|
|
203
|
+
if (typeof pattern === "string" && pattern.length > 0) {
|
|
204
|
+
regexParts.push(`pattern \`${pattern}\``);
|
|
205
|
+
}
|
|
206
|
+
} else if (v.subType === VALIDATOR_SUBTYPE_LENGTH) {
|
|
207
|
+
const min = v.ownAttr(VALIDATOR_ATTR_MIN);
|
|
208
|
+
const max = v.ownAttr(VALIDATOR_ATTR_MAX);
|
|
209
|
+
if (typeof min === "number") lengthParts.push(`minLength: ${min}`);
|
|
210
|
+
if (typeof max === "number" && typeof maxLenAttr !== "number") lengthParts.push(`maxLength: ${max}`);
|
|
211
|
+
} else if (v.subType === VALIDATOR_SUBTYPE_NUMERIC) {
|
|
212
|
+
const min = v.ownAttr(VALIDATOR_ATTR_MIN);
|
|
213
|
+
const max = v.ownAttr(VALIDATOR_ATTR_MAX);
|
|
214
|
+
if (typeof min === "number") numericParts.push(`min: ${min}`);
|
|
215
|
+
if (typeof max === "number") numericParts.push(`max: ${max}`);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
parts.push(...regexParts);
|
|
219
|
+
if (typeof maxLenAttr === "number") {
|
|
220
|
+
parts.push(`maxLength: ${maxLenAttr}`);
|
|
221
|
+
}
|
|
222
|
+
parts.push(...lengthParts, ...numericParts);
|
|
223
|
+
|
|
224
|
+
const fk = fkMap.get(field.name);
|
|
225
|
+
if (fk !== undefined) {
|
|
226
|
+
parts.push(`references \`${fk.targetEntity}.${fk.targetField}\``);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const def = field.ownAttr(FIELD_ATTR_DEFAULT);
|
|
230
|
+
if (def !== undefined) {
|
|
231
|
+
parts.push(`default: \`${String(def)}\``);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const sup = field.resolveSuper();
|
|
235
|
+
if (sup !== undefined) {
|
|
236
|
+
parts.push(`extends \`${sup.name}\``);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
return parts.join(", ");
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function buildFkMap(
|
|
243
|
+
entity: MetaObject,
|
|
244
|
+
root: MetaRoot,
|
|
245
|
+
): Map<string, { targetEntity: string; targetField: string }> {
|
|
246
|
+
const out = new Map<string, { targetEntity: string; targetField: string }>();
|
|
247
|
+
for (const ref of entity.referenceIdentities()) {
|
|
248
|
+
const fkField = ref.fields[0];
|
|
249
|
+
const targetEntity = ref.targetEntity;
|
|
250
|
+
if (fkField === undefined || targetEntity === undefined) continue;
|
|
251
|
+
const targetField = ref.resolvedTargetPkField(root) ?? "id";
|
|
252
|
+
out.set(fkField, { targetEntity: stripPackage(targetEntity), targetField });
|
|
253
|
+
}
|
|
254
|
+
return out;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function sourceLine(entity: MetaObject): string | undefined {
|
|
258
|
+
const src = entity.source;
|
|
259
|
+
if (!src) return undefined;
|
|
260
|
+
if ("files" in src && src.files.length > 0) {
|
|
261
|
+
return src.files[0];
|
|
262
|
+
}
|
|
263
|
+
if (src.format === "code") {
|
|
264
|
+
return src.caller !== undefined ? `(code) ${src.caller}` : "(code)";
|
|
265
|
+
}
|
|
266
|
+
return undefined;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function entityDescription(entity: MetaObject): string | undefined {
|
|
270
|
+
const v = entity.attr(DOC_ATTR_DESCRIPTION);
|
|
271
|
+
return typeof v === "string" && v.length > 0 ? v : undefined;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function describeIdentity(id: MetaIdentity): string {
|
|
275
|
+
const fields = id.fields;
|
|
276
|
+
const fieldList = fields.length === 1
|
|
277
|
+
? `\`${fields[0]}\``
|
|
278
|
+
: `(${fields.map((f) => `\`${f}\``).join(", ")})`;
|
|
279
|
+
|
|
280
|
+
if (id.subType === IDENTITY_SUBTYPE_PRIMARY) {
|
|
281
|
+
const gen = id.ownAttr(IDENTITY_ATTR_GENERATION);
|
|
282
|
+
const genSuffix = typeof gen === "string" ? ` — generation: \`${gen}\`` : "";
|
|
283
|
+
return `**Primary key:** ${fieldList}${genSuffix}`;
|
|
284
|
+
}
|
|
285
|
+
if (id.subType === IDENTITY_SUBTYPE_SECONDARY) {
|
|
286
|
+
const uniqueText = id.unique ? "unique" : "non-unique";
|
|
287
|
+
return `**Secondary index:** ${fieldList} — ${uniqueText}`;
|
|
288
|
+
}
|
|
289
|
+
if (id.subType === IDENTITY_SUBTYPE_REFERENCE) {
|
|
290
|
+
// The subType discriminator guarantees the instance is a MetaReferenceIdentity;
|
|
291
|
+
// narrow to it so we can use its typed `referencesRaw` getter directly.
|
|
292
|
+
const raw = (id as MetaReferenceIdentity).referencesRaw;
|
|
293
|
+
if (typeof raw === "string" && raw.length > 0) {
|
|
294
|
+
return `**Reference:** ${fieldList} → \`${raw}\``;
|
|
295
|
+
}
|
|
296
|
+
return `**Reference:** ${fieldList}`;
|
|
297
|
+
}
|
|
298
|
+
return `**Identity (${id.subType}):** ${fieldList}`;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
function relationshipBullet(r: ReturnType<MetaObject["relationships"]>[number]): string {
|
|
302
|
+
const cardinality = r.ownAttr(RELATIONSHIP_ATTR_CARDINALITY);
|
|
303
|
+
const card = typeof cardinality === "string" ? cardinality : "?";
|
|
304
|
+
const targetRaw = r.ownAttr(RELATIONSHIP_ATTR_OBJECT_REF);
|
|
305
|
+
const target = typeof targetRaw === "string" ? stripPackage(targetRaw) : "?";
|
|
306
|
+
const subtype = r.subType;
|
|
307
|
+
let label: string;
|
|
308
|
+
switch (subtype) {
|
|
309
|
+
case RELATIONSHIP_SUBTYPE_COMPOSITION: label = "composition"; break;
|
|
310
|
+
case RELATIONSHIP_SUBTYPE_AGGREGATION: label = "aggregation"; break;
|
|
311
|
+
case RELATIONSHIP_SUBTYPE_ASSOCIATION: label = "association"; break;
|
|
312
|
+
default: label = subtype;
|
|
313
|
+
}
|
|
314
|
+
return `\`${r.name}\` — ${card} → \`${target}\` (${label})`;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/** Build the EntityDocData payload for one entity. The single public-API
|
|
318
|
+
* entry point exported by this module; the markdown template applies
|
|
319
|
+
* against this shape. */
|
|
320
|
+
export function buildEntityDocData(
|
|
321
|
+
entity: MetaObject,
|
|
322
|
+
opts: BuildDocDataOpts,
|
|
323
|
+
): EntityDocData {
|
|
324
|
+
const strategy = opts.columnNamingStrategy ?? "snake_case";
|
|
325
|
+
const root = opts.loadedRoot;
|
|
326
|
+
const primary = entity.primaryIdentity();
|
|
327
|
+
const pkFields = primary?.fields ?? [];
|
|
328
|
+
const pkFieldNames = new Set<string>(pkFields);
|
|
329
|
+
const fkMap = buildFkMap(entity, root);
|
|
330
|
+
|
|
331
|
+
// ---- Storage rows
|
|
332
|
+
const storageRows: StorageFieldDoc[] = entity.fields().map((field) => {
|
|
333
|
+
const spec = mapColumnType(field, opts.dialect, strategy);
|
|
334
|
+
const tsType = tsTypeForStorage(entity, field, pkFieldNames);
|
|
335
|
+
const tsTypeCell = tsType.split("|").map((s) => s.trim()).join(" \\| ");
|
|
336
|
+
const sqlExpr = sqlColumnExpr(spec);
|
|
337
|
+
const cons = constraintsCell(entity, field, pkFieldNames, fkMap);
|
|
338
|
+
const tsTypeCellStr = `\`${tsTypeCell}\``;
|
|
339
|
+
const sqlExprCellStr = `\`${sqlExpr}\``;
|
|
340
|
+
return {
|
|
341
|
+
name: field.name,
|
|
342
|
+
tsTypeCell: tsTypeCellStr,
|
|
343
|
+
sqlExprCell: sqlExprCellStr,
|
|
344
|
+
constraintsCell: cons,
|
|
345
|
+
rowLine: `| \`${field.name}\` | ${tsTypeCellStr} | ${sqlExprCellStr} | ${cons} |`,
|
|
346
|
+
};
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
const isValue = entity.subType === OBJECT_SUBTYPE_VALUE;
|
|
350
|
+
const hasStorage = !isValue && hasWritableRdbSource(entity);
|
|
351
|
+
|
|
352
|
+
// ---- Identities
|
|
353
|
+
const ids = entity.identities();
|
|
354
|
+
const identities: IdentityDoc[] | undefined = ids.length > 0
|
|
355
|
+
? ids.map((id) => ({ bullet: describeIdentity(id) }))
|
|
356
|
+
: undefined;
|
|
357
|
+
|
|
358
|
+
// ---- Relationships
|
|
359
|
+
const rels = entity.relationships();
|
|
360
|
+
const relationships: RelationshipDoc[] | undefined = rels.length > 0
|
|
361
|
+
? rels.map((r) => ({ bullet: relationshipBullet(r) }))
|
|
362
|
+
: undefined;
|
|
363
|
+
|
|
364
|
+
// ---- Validation
|
|
365
|
+
const lower = entity.name.charAt(0).toLowerCase() + entity.name.slice(1);
|
|
366
|
+
const validation = {
|
|
367
|
+
insertSchema: `${entity.name}InsertSchema`,
|
|
368
|
+
updateSchema: `${entity.name}UpdateSchema`,
|
|
369
|
+
entityFile: `${entity.name}.ts`,
|
|
370
|
+
lower,
|
|
371
|
+
};
|
|
372
|
+
|
|
373
|
+
// ---- UsedBy
|
|
374
|
+
const usedByMatches: UsedByDoc[] = [];
|
|
375
|
+
for (const child of root.ownChildren()) {
|
|
376
|
+
if (child.type !== TYPE_TEMPLATE) continue;
|
|
377
|
+
const ref = child.ownAttr(TEMPLATE_ATTR_PAYLOAD_REF);
|
|
378
|
+
if (typeof ref !== "string") continue;
|
|
379
|
+
if (stripPackage(ref) !== entity.name) continue;
|
|
380
|
+
usedByMatches.push({
|
|
381
|
+
bullet: `\`template.${child.subType} ${child.name}\` — uses \`${entity.name}\` as \`@payloadRef\``,
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
const usedBy = usedByMatches.length > 0 ? usedByMatches : undefined;
|
|
385
|
+
|
|
386
|
+
// ---- Generated
|
|
387
|
+
const gens = opts.generatorNames ?? new Set<string>();
|
|
388
|
+
const generated: GeneratedFileDoc[] = [];
|
|
389
|
+
generated.push({
|
|
390
|
+
filename: `${entity.name}.ts`,
|
|
391
|
+
description: "Drizzle table, Zod schemas, type aliases, enum literal unions.",
|
|
392
|
+
});
|
|
393
|
+
if (gens.has("queries-file") && !isValue) {
|
|
394
|
+
generated.push({
|
|
395
|
+
filename: `${entity.name}.queries.ts`,
|
|
396
|
+
description:
|
|
397
|
+
"typed CRUD helpers (find / list / create / update / delete; takes `db` as first param per ADR-0008).",
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
if (gens.has("routes-file") && !isValue) {
|
|
401
|
+
generated.push({
|
|
402
|
+
filename: `${entity.name}.routes.ts`,
|
|
403
|
+
description: `Fastify CRUD-5 route registration (\`register${entity.name}Routes\`).`,
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
if (gens.has("routes-file-hono") && !isValue) {
|
|
407
|
+
generated.push({
|
|
408
|
+
filename: `${entity.name}.routes.hono.ts`,
|
|
409
|
+
description: `Hono CRUD-5 route registration (\`register${entity.name}Routes\`).`,
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// Preamble header — built up exactly as the legacy emitter did.
|
|
414
|
+
const preambleLines: string[] = [];
|
|
415
|
+
const typeStr = `${entity.type}.${entity.subType}`;
|
|
416
|
+
preambleLines.push(`**Type:** \`${typeStr}\``);
|
|
417
|
+
const src = sourceLine(entity);
|
|
418
|
+
if (src !== undefined) preambleLines.push(`**Source:** \`${src}\``);
|
|
419
|
+
if (entity.package !== undefined && entity.package !== "") {
|
|
420
|
+
preambleLines.push(`**Package:** \`${entity.package}\``);
|
|
421
|
+
}
|
|
422
|
+
const preambleHeader = preambleLines.join("\n");
|
|
423
|
+
|
|
424
|
+
// Description quote — each line of the description prefixed with "> ".
|
|
425
|
+
const desc = entityDescription(entity);
|
|
426
|
+
let descriptionQuote: string | undefined;
|
|
427
|
+
if (desc !== undefined) {
|
|
428
|
+
descriptionQuote = desc.split("\n").map((l) => `> ${l}`.trimEnd()).join("\n");
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
const data: EntityDocData = {
|
|
432
|
+
generatedMarker: `<!-- ${GENERATED_HEADER} — DO NOT EDIT. -->`,
|
|
433
|
+
entity: {
|
|
434
|
+
name: entity.name,
|
|
435
|
+
type: typeStr,
|
|
436
|
+
},
|
|
437
|
+
preambleHeader,
|
|
438
|
+
validation,
|
|
439
|
+
generated,
|
|
440
|
+
};
|
|
441
|
+
|
|
442
|
+
if (desc !== undefined) data.entity.description = desc;
|
|
443
|
+
if (descriptionQuote !== undefined) data.descriptionQuote = descriptionQuote;
|
|
444
|
+
if (src !== undefined) data.entity.source = src;
|
|
445
|
+
if (entity.package !== undefined && entity.package !== "") {
|
|
446
|
+
data.entity.package = entity.package;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
if (hasStorage) {
|
|
450
|
+
data.storage = {
|
|
451
|
+
tableHeader: "| Field | TypeScript type | SQL column | Constraints |\n|---|---|---|---|",
|
|
452
|
+
rows: storageRows,
|
|
453
|
+
};
|
|
454
|
+
data.hasStorage = true;
|
|
455
|
+
}
|
|
456
|
+
if (identities !== undefined) {
|
|
457
|
+
data.identities = identities;
|
|
458
|
+
data.hasIdentities = true;
|
|
459
|
+
}
|
|
460
|
+
if (relationships !== undefined) {
|
|
461
|
+
data.relationships = relationships;
|
|
462
|
+
data.hasRelationships = true;
|
|
463
|
+
}
|
|
464
|
+
if (usedBy !== undefined) {
|
|
465
|
+
data.usedBy = usedBy;
|
|
466
|
+
data.hasUsedBy = true;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
return data;
|
|
470
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
// Data-dict shapes for the docs templates — the public contract template
|
|
2
|
+
// authors consume. Versioned per MO major; deprecated before removal.
|
|
3
|
+
//
|
|
4
|
+
// ## Stability contract (v1)
|
|
5
|
+
//
|
|
6
|
+
// Per the template-driven codegen design (D3 — data-shape stability), these
|
|
7
|
+
// types ARE a public API. Template authors who write custom Mustache files
|
|
8
|
+
// for `docs/entity-page.md` (or any of the partials) reference these keys.
|
|
9
|
+
//
|
|
10
|
+
// `EntityDocData` is the **Markdown-flavored** data shape — it intentionally
|
|
11
|
+
// mixes raw structural fields (entity, validation, generated) with
|
|
12
|
+
// **pre-rendered Markdown fragments** so cross-port walk functions (TS,
|
|
13
|
+
// Python, C#, Java, Kotlin) don't have to re-derive the same escaping rules
|
|
14
|
+
// (pipe-inside-cell escapes, backtick wrapping, identity bullets,
|
|
15
|
+
// description blockquotes). Fields whose JSDoc carries a `@markdown` tag
|
|
16
|
+
// encode Markdown-specific layout decisions and are stable for the v1
|
|
17
|
+
// contract; they are NOT useful for non-Markdown output (HTML, JSON, plain
|
|
18
|
+
// text).
|
|
19
|
+
//
|
|
20
|
+
// A consumer writing a custom Mustache template with **different table
|
|
21
|
+
// columns** or a **different output format** today must compose their own
|
|
22
|
+
// data from `MetaObject` directly — the v1 shape does not expose a
|
|
23
|
+
// structural-only layer. A future `EntityDocStructure` (raw fields only,
|
|
24
|
+
// no Markdown) may ship in v2 if a real adopter needs that surface; the
|
|
25
|
+
// split is deliberately deferred until then.
|
|
26
|
+
//
|
|
27
|
+
// ## Cross-port consistency
|
|
28
|
+
//
|
|
29
|
+
// Today's docsFile() refactor populates EntityDocData from MetaObject + the
|
|
30
|
+
// existing column-mapper / source-detect / enum-meta helpers. Cross-port
|
|
31
|
+
// implementations (C#, Java, Kotlin, Python) emit the same shape so a single
|
|
32
|
+
// set of Mustache templates can drive every port's docs codegen.
|
|
33
|
+
//
|
|
34
|
+
// ## Mustache idiom note
|
|
35
|
+
//
|
|
36
|
+
// Some sections carry both a list and a parallel `has*` boolean. The flag is
|
|
37
|
+
// only present to work around Mustache's lack of an "is non-empty array"
|
|
38
|
+
// primitive (`{{#identities}}` iterates but doesn't gate a wrapping section
|
|
39
|
+
// header). A future render engine version may let templates use
|
|
40
|
+
// `{{#identities.0}}` for the same effect, at which point the flag fields
|
|
41
|
+
// can be deprecated.
|
|
42
|
+
|
|
43
|
+
/** One row in the Storage table — fully-rendered as a single Markdown table
|
|
44
|
+
* row. The escaping rules for pipe-inside-cell are non-trivial and live in
|
|
45
|
+
* the data builder, not the template, so templates stay trivial and the
|
|
46
|
+
* cross-port walk functions don't have to re-derive the rules. */
|
|
47
|
+
export interface StorageFieldDoc {
|
|
48
|
+
name: string; // raw field name (without backticks)
|
|
49
|
+
/** @markdown — already escaped TS type, with backticks. */
|
|
50
|
+
tsTypeCell: string;
|
|
51
|
+
/** @markdown — already escaped SQL expression, wrapped in backticks. */
|
|
52
|
+
sqlExprCell: string;
|
|
53
|
+
/** @markdown — already-formatted constraints text. */
|
|
54
|
+
constraintsCell: string;
|
|
55
|
+
/** @markdown — pre-rendered full Markdown table row, e.g.
|
|
56
|
+
* "| `id` | `number` | `integer(\"id\")` | primary key |"
|
|
57
|
+
* Templates emit this verbatim via `{{{rowLine}}}`. */
|
|
58
|
+
rowLine: string;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface IdentityDoc {
|
|
62
|
+
/** @markdown — pre-formatted bullet text — e.g.
|
|
63
|
+
* "**Primary key:** `id` — generation: `increment`"
|
|
64
|
+
* (Carrying the fully-rendered string keeps the template trivial; the
|
|
65
|
+
* identity rendering rules are non-trivial and live in the builder.) */
|
|
66
|
+
bullet: string;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export interface RelationshipDoc {
|
|
70
|
+
/** @markdown — pre-formatted bullet text — e.g.
|
|
71
|
+
* "- `posts` — one-to-many → `Post` (composition)" */
|
|
72
|
+
bullet: string;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface UsedByDoc {
|
|
76
|
+
/** @markdown — pre-formatted bullet text. */
|
|
77
|
+
bullet: string;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface GeneratedFileDoc {
|
|
81
|
+
filename: string; // "Author.ts"
|
|
82
|
+
description: string; // "Drizzle table, Zod schemas, ..."
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export interface EntityDocData {
|
|
86
|
+
/** @markdown — auto-emitted by the templateGenerator; templates may also
|
|
87
|
+
* echo it for human readers. Format: `<!-- @generated by
|
|
88
|
+
* @metaobjectsdev/codegen-ts — DO NOT EDIT. -->`. */
|
|
89
|
+
generatedMarker: string;
|
|
90
|
+
|
|
91
|
+
/** The entity preamble — RAW (not Markdown-flavored). Custom non-Markdown
|
|
92
|
+
* templates can rely on these fields. */
|
|
93
|
+
entity: {
|
|
94
|
+
name: string; // "Author"
|
|
95
|
+
type: string; // "object.entity"
|
|
96
|
+
source?: string; // "meta.blog.json"
|
|
97
|
+
package?: string; // "acme::blog"
|
|
98
|
+
description?: string; // raw description text (may be multi-line)
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
/** @markdown — description as a blockquote (one `> ` per line). Present
|
|
102
|
+
* iff `entity.description` is present. Pre-rendered so multi-line
|
|
103
|
+
* descriptions don't have to be expressed as Mustache structural
|
|
104
|
+
* constructs. */
|
|
105
|
+
descriptionQuote?: string;
|
|
106
|
+
|
|
107
|
+
/** @markdown — multi-line preamble block: Type / Source? / Package?, one
|
|
108
|
+
* per line, in the exact order matching the legacy emitter. Always
|
|
109
|
+
* present. */
|
|
110
|
+
preambleHeader: string;
|
|
111
|
+
|
|
112
|
+
/** Storage section. Present iff the entity has a writable rdb source and
|
|
113
|
+
* is NOT object.value. */
|
|
114
|
+
storage?: {
|
|
115
|
+
/** @markdown — pre-rendered "| Field | ... |\n|---|---|---|---|" header
|
|
116
|
+
* pair. */
|
|
117
|
+
tableHeader: string;
|
|
118
|
+
rows: StorageFieldDoc[];
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
/** Identity section bullets — empty array iff section is omitted.
|
|
122
|
+
* See the "Mustache idiom note" at the top of this file for why this
|
|
123
|
+
* ships alongside a parallel `hasIdentities` boolean. */
|
|
124
|
+
identities?: IdentityDoc[];
|
|
125
|
+
/** Present-and-non-empty flag for the identities section. See the
|
|
126
|
+
* "Mustache idiom note" at the top of this file. */
|
|
127
|
+
hasIdentities?: boolean;
|
|
128
|
+
|
|
129
|
+
/** Relationships section — same list+flag pattern as identities. */
|
|
130
|
+
relationships?: RelationshipDoc[];
|
|
131
|
+
/** Present-and-non-empty flag for the relationships section. */
|
|
132
|
+
hasRelationships?: boolean;
|
|
133
|
+
|
|
134
|
+
/** Validation section — RAW (not Markdown-flavored). Always emitted.
|
|
135
|
+
* Custom non-Markdown templates can rely on these fields. */
|
|
136
|
+
validation: {
|
|
137
|
+
insertSchema: string; // "AuthorInsertSchema"
|
|
138
|
+
updateSchema: string; // "AuthorUpdateSchema"
|
|
139
|
+
entityFile: string; // "Author.ts"
|
|
140
|
+
lower: string; // "author" (lowercased first letter)
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
/** "Used by" — present iff any templates declare `@payloadRef` → this
|
|
144
|
+
* entity. Same list+flag pattern as identities. */
|
|
145
|
+
usedBy?: UsedByDoc[];
|
|
146
|
+
/** Present-and-non-empty flag for the usedBy section. */
|
|
147
|
+
hasUsedBy?: boolean;
|
|
148
|
+
|
|
149
|
+
/** Present flag for the storage section. */
|
|
150
|
+
hasStorage?: boolean;
|
|
151
|
+
|
|
152
|
+
/** Generated-code section — always emitted (at minimum the entity file). */
|
|
153
|
+
generated: GeneratedFileDoc[];
|
|
154
|
+
}
|