📄 src/models/bin/index.ts
D-OPEN SOVEREIGN

Documentation & Insights

Code generator runner.

Usage: tsx src/bin/index.ts ./generated-src

Reads the enriched schema JSONs and versions.json from the workspace
sibling `@the_library/db_schemas` package and emits one model file per
schema plus an index barrel into the destination folder.
1/*
2 * Copyright (c) 2026 DataPond D-Library Pty Ltd. ("The Sanctuary")
3 * Non-Profit Public Library — The World Library
4 * 
5 * D-SAFE Certification issued by POND Enterprise.
6 * Published under the D-Open Code Sovereign Licence v1.0 framework
7 * DataPond D-Library All exclusive Right reserved on modifiying the code - CC Attribution to data pond, Non modifiable, Non Commercial.
8 * https://registry.world.bibliotech.com/licence
9 * Source code donated to DataPond D-Library by it's author: data pond.
10 * 
11 * Technical Guardian ("The Shield"): Pond Enterprise Pty Ltd. (ACN 694 747 987)
12 * All liability claims about D-SAFE certification issues coming from the published D-Safe direction must be addressed with Pond Enterprise Pty Ltd.
13 * Code Modification automatically voids the certified liability protection provided by Pond Enterprise.
14 */
15
16/**
17 * Code generator runner.
18 *
19 * Usage: tsx src/bin/index.ts ./generated-src
20 *
21 * Reads the enriched schema JSONs and versions.json from the workspace
22 * sibling `@the_library/db_schemas` package and emits one model file per
23 * schema plus an index barrel into the destination folder.
24 */
25import { readdirSync, readFileSync, writeFileSync, mkdirSync } from "node:fs";
26import { resolve, dirname, join } from "node:path";
27import { fileURLToPath } from "node:url";
28import { generateModelSource, generateIndexSource, generateCrosswalkWorkerSource, generateCrosswalkClientSource, type GeneratorContext } from "../schemaGenerator.ts";
29import type { ModelDefinition } from "../registry.ts";
30import { resolveLayout, type VersionsFile } from "../binary.ts";
31
32const here = dirname(fileURLToPath(import.meta.url));
33const schemaPkgRoot = resolve(here, "../../../../db_schemas");
34const schemaSrcDir = join(schemaPkgRoot, "generated-src");
35const versionsPath = join(schemaPkgRoot, "dist", "versions.json");
36
37const destination = process.argv[2];
38if (!destination) {
39    console.error("Usage: tsx src/bin/index.ts <destination-folder>");
40    process.exit(1);
41}
42const destDir = resolve(process.cwd(), destination);
43mkdirSync(destDir, { recursive: true });
44
45// ─── Load inputs ─────────────────────────────────────────────────────────────
46
47const versionsFile = JSON.parse(readFileSync(versionsPath, 'utf-8')) as VersionsFile;
48const layout = resolveLayout(versionsFile.current, versionsFile);
49
50const schemaFiles = readdirSync(schemaSrcDir)
51    .filter(f => f.endsWith('.json'))
52    .sort();
53
54const schemas = new Map<string, ModelDefinition>();
55for (const file of schemaFiles) {
56    const schema = JSON.parse(readFileSync(join(schemaSrcDir, file), 'utf-8')) as ModelDefinition;
57    schemas.set(schema.name, schema);
58}
59
60console.log(`[models_v2] loaded ${schemas.size} schemas (layout v${versionsFile.current}, hash ${layout.hash})`);
61
62// ─── Generate ────────────────────────────────────────────────────────────────
63
64const ctx: GeneratorContext = { schemas, layout };
65const all = Array.from(schemas.values()).sort((a, b) => a.objectId - b.objectId);
66
67for (const schema of all) {
68    const source = generateModelSource(schema, ctx);
69    const file = join(destDir, `${schema.name}.ts`);
70    writeFileSync(file, source);
71    console.log(`[models_v2] generated ${schema.name}.ts (objectId ${schema.objectId})`);
72}
73
74writeFileSync(join(destDir, 'index.ts'), generateIndexSource(all));
75console.log(`[models_v2] generated index.ts — done.`);
76
77const workerDir = join(destDir, 'workers');
78mkdirSync(workerDir, { recursive: true });
79writeFileSync(join(workerDir, 'ExternalIdCrosswalkWorker.ts'), generateCrosswalkWorkerSource(all));
80writeFileSync(join(destDir, 'ExternalIdCrosswalk.ts'), generateCrosswalkClientSource());
81console.log(`[models_v2] generated crosswalk artifacts — done.`);
82