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

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