drizzle-docs-generator 0.5.5 → 0.6.0
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.ja.md +2 -1
- package/README.md +2 -1
- package/dist/cli/index.js +54 -50
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/integration-test-utils.d.ts.map +1 -1
- package/dist/formatter/mermaid.d.ts +19 -3
- package/dist/formatter/mermaid.d.ts.map +1 -1
- package/dist/formatter/mermaid.js +37 -19
- package/dist/formatter/mermaid.js.map +1 -1
- package/dist/generator/common.d.ts +0 -1
- package/dist/generator/common.d.ts.map +1 -1
- package/dist/generator/common.js.map +1 -1
- package/dist/generator/mysql.d.ts.map +1 -1
- package/dist/generator/mysql.js +2 -3
- package/dist/generator/mysql.js.map +1 -1
- package/dist/generator/pg.d.ts.map +1 -1
- package/dist/generator/pg.js +9 -10
- package/dist/generator/pg.js.map +1 -1
- package/dist/generator/sqlite.d.ts.map +1 -1
- package/dist/generator/sqlite.js +5 -6
- package/dist/generator/sqlite.js.map +1 -1
- package/package.json +7 -4
package/README.ja.md
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# drizzle-docs-generator
|
|
2
2
|
|
|
3
|
-
[](https://
|
|
3
|
+
[](https://www.npmjs.com/package/drizzle-docs-generator)
|
|
4
|
+
[](https://codecov.io/gh/rikeda71/drizzle-docs-generator)
|
|
4
5
|
|
|
5
6
|
Drizzle ORM スキーマから DBML と Markdown ドキュメントを生成する CLI。JSDoc コメントを Note 句として出力できる。
|
|
6
7
|
|
package/README.md
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# drizzle-docs-generator
|
|
2
2
|
|
|
3
|
-
[](https://
|
|
3
|
+
[](https://www.npmjs.com/package/drizzle-docs-generator)
|
|
4
|
+
[](https://codecov.io/gh/rikeda71/drizzle-docs-generator)
|
|
4
5
|
|
|
5
6
|
CLI tool to generate DBML and Markdown documentation from Drizzle ORM schemas. Extracts JSDoc comments and outputs them as Note clauses.
|
|
6
7
|
|
package/dist/cli/index.js
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { Command as v } from "commander";
|
|
3
|
-
import { readFileSync as S, existsSync as m, mkdirSync as w, writeFileSync as
|
|
4
|
-
import { join as
|
|
3
|
+
import { readFileSync as S, existsSync as m, mkdirSync as w, writeFileSync as f, watch as G, lstatSync as O, readdirSync as j } from "node:fs";
|
|
4
|
+
import { join as u, dirname as g, resolve as F } from "node:path";
|
|
5
5
|
import { pathToFileURL as k } from "node:url";
|
|
6
6
|
import { PgGenerator as P, pgGenerate as z } from "../generator/pg.js";
|
|
7
|
-
import { MySqlGenerator as
|
|
8
|
-
import { SqliteGenerator as
|
|
7
|
+
import { MySqlGenerator as C, mysqlGenerate as R } from "../generator/mysql.js";
|
|
8
|
+
import { SqliteGenerator as T, sqliteGenerate as W } from "../generator/sqlite.js";
|
|
9
9
|
import { MarkdownFormatter as E } from "../formatter/markdown.js";
|
|
10
10
|
import { MermaidErDiagramFormatter as x } from "../formatter/mermaid.js";
|
|
11
|
-
import { register as
|
|
12
|
-
const
|
|
11
|
+
import { register as N } from "tsx/esm/api";
|
|
12
|
+
const U = N(), p = new v(), I = u(import.meta.dirname, "../../package.json"), $ = JSON.parse(S(I, "utf-8"));
|
|
13
13
|
p.name("drizzle-docs").description($.description).version($.version);
|
|
14
14
|
function b(r) {
|
|
15
15
|
switch (r) {
|
|
16
16
|
case "mysql":
|
|
17
|
-
return
|
|
17
|
+
return R;
|
|
18
18
|
case "sqlite":
|
|
19
|
-
return
|
|
19
|
+
return W;
|
|
20
20
|
default:
|
|
21
21
|
return z;
|
|
22
22
|
}
|
|
@@ -24,9 +24,9 @@ function b(r) {
|
|
|
24
24
|
function M(r) {
|
|
25
25
|
switch (r) {
|
|
26
26
|
case "mysql":
|
|
27
|
-
return
|
|
27
|
+
return C;
|
|
28
28
|
case "sqlite":
|
|
29
|
-
return
|
|
29
|
+
return T;
|
|
30
30
|
default:
|
|
31
31
|
return P;
|
|
32
32
|
}
|
|
@@ -35,19 +35,19 @@ function L(r, e) {
|
|
|
35
35
|
const o = [];
|
|
36
36
|
if (!m(r))
|
|
37
37
|
return o;
|
|
38
|
-
const t =
|
|
38
|
+
const t = u(r, "README.md");
|
|
39
39
|
m(t) && o.push(t);
|
|
40
|
-
for (const
|
|
41
|
-
const
|
|
42
|
-
m(
|
|
40
|
+
for (const a of e) {
|
|
41
|
+
const n = u(r, `${a}.md`);
|
|
42
|
+
m(n) && o.push(n);
|
|
43
43
|
}
|
|
44
44
|
return o;
|
|
45
45
|
}
|
|
46
46
|
async function B(r, e) {
|
|
47
|
-
const o = k(r).href, t = e.watch ? `?t=${Date.now()}` : "",
|
|
47
|
+
const o = k(r).href, t = e.watch ? `?t=${Date.now()}` : "", a = await import(o + t);
|
|
48
48
|
if (e.format === "markdown") {
|
|
49
|
-
const
|
|
50
|
-
schema:
|
|
49
|
+
const n = M(e.dialect), s = new n({
|
|
50
|
+
schema: a,
|
|
51
51
|
source: r
|
|
52
52
|
}).toIntermediateSchema();
|
|
53
53
|
if (!e.singleFile && e.output) {
|
|
@@ -57,7 +57,7 @@ async function B(r, e) {
|
|
|
57
57
|
return h(s, e);
|
|
58
58
|
} else
|
|
59
59
|
return b(e.dialect)({
|
|
60
|
-
schema:
|
|
60
|
+
schema: a,
|
|
61
61
|
source: r
|
|
62
62
|
});
|
|
63
63
|
}
|
|
@@ -66,8 +66,8 @@ function D(r) {
|
|
|
66
66
|
try {
|
|
67
67
|
const o = j(r, { withFileTypes: !0 });
|
|
68
68
|
for (const t of o) {
|
|
69
|
-
const
|
|
70
|
-
t.isDirectory() ? e.push(...D(
|
|
69
|
+
const a = u(r, t.name);
|
|
70
|
+
t.isDirectory() ? e.push(...D(a)) : t.isFile() && /\.(ts|js|mts|mjs|cts|cjs)$/.test(t.name) && e.push(a);
|
|
71
71
|
}
|
|
72
72
|
} catch (o) {
|
|
73
73
|
o instanceof Error && console.error(`Error reading directory ${r}: ${o.message}`);
|
|
@@ -91,7 +91,9 @@ function A(r, e, o) {
|
|
|
91
91
|
function h(r, e) {
|
|
92
92
|
const t = new E().format(r);
|
|
93
93
|
if (e.erDiagram) {
|
|
94
|
-
const
|
|
94
|
+
const n = new x({
|
|
95
|
+
includeColumns: e.columns
|
|
96
|
+
}).format(r);
|
|
95
97
|
return `${t}
|
|
96
98
|
|
|
97
99
|
---
|
|
@@ -99,7 +101,7 @@ function h(r, e) {
|
|
|
99
101
|
## ER Diagram
|
|
100
102
|
|
|
101
103
|
\`\`\`mermaid
|
|
102
|
-
${
|
|
104
|
+
${n}
|
|
103
105
|
\`\`\``;
|
|
104
106
|
}
|
|
105
107
|
return t;
|
|
@@ -107,11 +109,13 @@ ${a}
|
|
|
107
109
|
function q(r, e, o) {
|
|
108
110
|
w(e, { recursive: !0 });
|
|
109
111
|
const t = new E({ linkFormat: "file" });
|
|
110
|
-
let
|
|
112
|
+
let n = `${t.generateIndex(r)}
|
|
111
113
|
`;
|
|
112
114
|
if (o.erDiagram) {
|
|
113
|
-
const s = new x(
|
|
114
|
-
|
|
115
|
+
const s = new x({
|
|
116
|
+
includeColumns: o.columns
|
|
117
|
+
}).format(r);
|
|
118
|
+
n += `
|
|
115
119
|
---
|
|
116
120
|
|
|
117
121
|
## ER Diagram
|
|
@@ -121,10 +125,10 @@ ${s}
|
|
|
121
125
|
\`\`\`
|
|
122
126
|
`;
|
|
123
127
|
}
|
|
124
|
-
u(
|
|
128
|
+
f(u(e, "README.md"), n, "utf-8");
|
|
125
129
|
for (const c of r.tables) {
|
|
126
130
|
const s = t.generateTableDoc(c, r), i = `${c.name}.md`;
|
|
127
|
-
u(
|
|
131
|
+
f(u(e, i), `# ${c.name}
|
|
128
132
|
|
|
129
133
|
${s}
|
|
130
134
|
`, "utf-8");
|
|
@@ -132,7 +136,7 @@ ${s}
|
|
|
132
136
|
}
|
|
133
137
|
function V(r, e) {
|
|
134
138
|
const o = g(e);
|
|
135
|
-
w(o, { recursive: !0 }),
|
|
139
|
+
w(o, { recursive: !0 }), f(e, r.endsWith(`
|
|
136
140
|
`) ? r : r + `
|
|
137
141
|
`, "utf-8");
|
|
138
142
|
}
|
|
@@ -140,21 +144,21 @@ async function H(r, e) {
|
|
|
140
144
|
const o = J(r);
|
|
141
145
|
try {
|
|
142
146
|
const t = {};
|
|
143
|
-
for (const
|
|
144
|
-
const
|
|
147
|
+
for (const a of o) {
|
|
148
|
+
const n = k(a).href, c = e.watch ? `?t=${Date.now()}` : "";
|
|
145
149
|
try {
|
|
146
|
-
const s = await import(
|
|
150
|
+
const s = await import(n + c);
|
|
147
151
|
Object.assign(t, s);
|
|
148
152
|
} catch (s) {
|
|
149
|
-
throw s instanceof Error && (console.error(`Error importing ${
|
|
153
|
+
throw s instanceof Error && (console.error(`Error importing ${a}: ${s.message}`), console.error(`
|
|
150
154
|
Possible causes:`), console.error("- Syntax error in the schema file"), console.error("- Missing dependencies (make sure drizzle-orm is installed)"), console.error("- Circular dependencies between schema files")), s;
|
|
151
155
|
}
|
|
152
156
|
}
|
|
153
157
|
if (e.format === "markdown") {
|
|
154
|
-
const
|
|
155
|
-
if (!
|
|
158
|
+
const a = M(e.dialect), n = o[0];
|
|
159
|
+
if (!n)
|
|
156
160
|
throw new Error("No schema files found");
|
|
157
|
-
const c = o.length === 1 ?
|
|
161
|
+
const c = o.length === 1 ? n : g(n), i = new a({
|
|
158
162
|
schema: t,
|
|
159
163
|
source: c
|
|
160
164
|
}).toIntermediateSchema();
|
|
@@ -180,18 +184,18 @@ Use --force to overwrite existing files.`
|
|
|
180
184
|
console.log(l);
|
|
181
185
|
}
|
|
182
186
|
} else {
|
|
183
|
-
const
|
|
187
|
+
const a = A(t, o, e);
|
|
184
188
|
if (e.output) {
|
|
185
189
|
!e.force && m(e.output) && (console.error(
|
|
186
190
|
`Error: Output file already exists: ${e.output}
|
|
187
191
|
Use --force to overwrite existing files.`
|
|
188
192
|
), process.exit(1));
|
|
189
|
-
const
|
|
190
|
-
w(
|
|
191
|
-
`) ?
|
|
193
|
+
const n = g(e.output);
|
|
194
|
+
w(n, { recursive: !0 }), f(e.output, a.endsWith(`
|
|
195
|
+
`) ? a : a + `
|
|
192
196
|
`, "utf-8"), console.log(`DBML generated: ${e.output}`);
|
|
193
197
|
} else
|
|
194
|
-
console.log(
|
|
198
|
+
console.log(a);
|
|
195
199
|
}
|
|
196
200
|
} catch (t) {
|
|
197
201
|
t instanceof Error ? console.error(`Error generating output: ${t.message}`) : console.error("Error generating output:", t), process.exit(1);
|
|
@@ -201,25 +205,25 @@ function K(r, e) {
|
|
|
201
205
|
const o = F(process.cwd(), r);
|
|
202
206
|
console.log(`Watching for changes: ${o}`);
|
|
203
207
|
let t = null;
|
|
204
|
-
G(o, async (
|
|
205
|
-
|
|
208
|
+
G(o, async (a) => {
|
|
209
|
+
a === "change" && (t && clearTimeout(t), t = setTimeout(async () => {
|
|
206
210
|
console.log(`
|
|
207
211
|
File changed, regenerating...`);
|
|
208
212
|
try {
|
|
209
|
-
const
|
|
210
|
-
if (!e.output &&
|
|
211
|
-
console.log(
|
|
213
|
+
const n = await B(o, e), c = e.format === "markdown" ? "Markdown" : "DBML", s = e.format === "markdown" && !e.singleFile && e.output;
|
|
214
|
+
if (!e.output && n)
|
|
215
|
+
console.log(n);
|
|
212
216
|
else if (e.output) {
|
|
213
217
|
const i = s ? `${e.output}/` : e.output;
|
|
214
218
|
console.log(`${c} regenerated: ${i}`);
|
|
215
219
|
}
|
|
216
|
-
} catch (
|
|
217
|
-
|
|
220
|
+
} catch (n) {
|
|
221
|
+
n instanceof Error ? console.error(`Error: ${n.message}`) : console.error("Error:", n);
|
|
218
222
|
}
|
|
219
223
|
}, 100));
|
|
220
224
|
});
|
|
221
225
|
}
|
|
222
|
-
p.command("generate").description("Generate documentation from Drizzle schema files").argument("<schema>", "Path to Drizzle schema file or directory").option("-o, --output <path>", "Output file or directory path").option("-d, --dialect <dialect>", "Database dialect (postgresql, mysql, sqlite)", "postgresql").option("-f, --format <format>", "Output format (dbml, markdown)", "markdown").option("-w, --watch", "Watch for file changes and regenerate").option("--single-file", "Output Markdown as a single file (for markdown format)").option("--no-er-diagram", "Exclude ER diagram from Markdown output").option("--force", "Overwrite existing files without confirmation").action(async (r, e) => {
|
|
226
|
+
p.command("generate").description("Generate documentation from Drizzle schema files").argument("<schema>", "Path to Drizzle schema file or directory").option("-o, --output <path>", "Output file or directory path").option("-d, --dialect <dialect>", "Database dialect (postgresql, mysql, sqlite)", "postgresql").option("-f, --format <format>", "Output format (dbml, markdown)", "markdown").option("-w, --watch", "Watch for file changes and regenerate").option("--single-file", "Output Markdown as a single file (for markdown format)").option("--no-er-diagram", "Exclude ER diagram from Markdown output").option("--no-columns", "Exclude columns from Mermaid ER diagram").option("--force", "Overwrite existing files without confirmation").action(async (r, e) => {
|
|
223
227
|
const o = ["postgresql", "mysql", "sqlite"];
|
|
224
228
|
o.includes(e.dialect) || (console.error(
|
|
225
229
|
`Error: Invalid dialect "${e.dialect}". Valid options: ${o.join(", ")}`
|
|
@@ -227,10 +231,10 @@ p.command("generate").description("Generate documentation from Drizzle schema fi
|
|
|
227
231
|
const t = ["dbml", "markdown"];
|
|
228
232
|
t.includes(e.format) || (console.error(
|
|
229
233
|
`Error: Invalid format "${e.format}". Valid options: ${t.join(", ")}`
|
|
230
|
-
), process.exit(1)), e.format !== "markdown" && (e.singleFile && console.warn("Warning: --single-file is only applicable with --format markdown"), e.erDiagram || console.warn("Warning: --no-er-diagram is only applicable with --format markdown")), await H(r, e), e.watch && K(r, e);
|
|
234
|
+
), process.exit(1)), e.format !== "markdown" && (e.singleFile && console.warn("Warning: --single-file is only applicable with --format markdown"), e.erDiagram || console.warn("Warning: --no-er-diagram is only applicable with --format markdown"), e.columns || console.warn("Warning: --no-columns is only applicable with --format markdown")), await H(r, e), e.watch && K(r, e);
|
|
231
235
|
});
|
|
232
236
|
p.parse();
|
|
233
237
|
process.on("exit", () => {
|
|
234
|
-
|
|
238
|
+
U();
|
|
235
239
|
});
|
|
236
240
|
//# sourceMappingURL=index.js.map
|
package/dist/cli/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../src/cli/index.ts"],"sourcesContent":["#!/usr/bin/env node\n\n/**\n * CLI for drizzle-docs-generator\n *\n * Generates DBML files from Drizzle ORM schema definitions.\n */\n\nimport { Command } from \"commander\";\nimport {\n existsSync,\n lstatSync,\n mkdirSync,\n readdirSync,\n readFileSync,\n watch,\n writeFileSync,\n} from \"node:fs\";\nimport { dirname, join, resolve } from \"node:path\";\nimport { pathToFileURL } from \"node:url\";\nimport { pgGenerate } from \"../generator/pg\";\nimport { mysqlGenerate } from \"../generator/mysql\";\nimport { sqliteGenerate } from \"../generator/sqlite\";\nimport { PgGenerator } from \"../generator/pg\";\nimport { MySqlGenerator } from \"../generator/mysql\";\nimport { SqliteGenerator } from \"../generator/sqlite\";\nimport { MarkdownFormatter } from \"../formatter/markdown\";\nimport { MermaidErDiagramFormatter } from \"../formatter/mermaid\";\nimport { register } from \"tsx/esm/api\";\nimport type { IntermediateSchema } from \"../types\";\n\n// Register tsx loader to support TypeScript and extensionless imports\nconst unregister = register();\n\nconst program = new Command();\n\n// Use import.meta.dirname (Node 20.11+) to resolve package.json\n// This works correctly even after bundling\nconst packageJsonPath = join(import.meta.dirname, \"../../package.json\");\nconst packageJson = JSON.parse(readFileSync(packageJsonPath, \"utf-8\")) as {\n version: string;\n description: string;\n};\n\nprogram.name(\"drizzle-docs\").description(packageJson.description).version(packageJson.version);\n\ntype Dialect = \"postgresql\" | \"mysql\" | \"sqlite\";\ntype OutputFormat = \"dbml\" | \"markdown\";\n\ninterface GenerateCommandOptions {\n output?: string;\n dialect: Dialect;\n watch?: boolean;\n format: OutputFormat;\n singleFile?: boolean;\n erDiagram: boolean; // commander uses --no-er-diagram which sets erDiagram to false\n force?: boolean; // skip overwrite confirmation for existing files\n}\n\n/**\n * Get the generate function based on dialect\n */\nfunction getGenerateFunction(dialect: Dialect) {\n switch (dialect) {\n case \"mysql\":\n return mysqlGenerate;\n case \"sqlite\":\n return sqliteGenerate;\n case \"postgresql\":\n default:\n return pgGenerate;\n }\n}\n\n/**\n * Get the generator class based on dialect\n */\nfunction getGeneratorClass(dialect: Dialect) {\n switch (dialect) {\n case \"mysql\":\n return MySqlGenerator;\n case \"sqlite\":\n return SqliteGenerator;\n case \"postgresql\":\n default:\n return PgGenerator;\n }\n}\n\n/**\n * Check if output directory has existing files\n */\nfunction hasExistingFiles(outputDir: string, tableNames: string[]): string[] {\n const existingFiles: string[] = [];\n\n if (!existsSync(outputDir)) {\n return existingFiles;\n }\n\n const readmePath = join(outputDir, \"README.md\");\n if (existsSync(readmePath)) {\n existingFiles.push(readmePath);\n }\n\n for (const tableName of tableNames) {\n const tablePath = join(outputDir, `${tableName}.md`);\n if (existsSync(tablePath)) {\n existingFiles.push(tablePath);\n }\n }\n\n return existingFiles;\n}\n\n/**\n * Generate output from a schema file (for watch mode)\n * Returns the generated output string for single-file formats,\n * or writes multiple files directly for multi-file markdown\n */\nasync function generateFromSchema(\n schemaPath: string,\n options: GenerateCommandOptions,\n): Promise<string | undefined> {\n // Use file URL for dynamic import (required for ESM)\n const schemaUrl = pathToFileURL(schemaPath).href;\n\n // Dynamic import with cache busting for watch mode\n const cacheBuster = options.watch ? `?t=${Date.now()}` : \"\";\n const schemaModule = (await import(schemaUrl + cacheBuster)) as Record<string, unknown>;\n\n if (options.format === \"markdown\") {\n const GeneratorClass = getGeneratorClass(options.dialect);\n const generator = new GeneratorClass({\n schema: schemaModule,\n source: schemaPath,\n });\n const intermediateSchema = generator.toIntermediateSchema();\n\n // Handle multi-file output in watch mode\n if (!options.singleFile && options.output) {\n writeMarkdownMultipleFiles(intermediateSchema, options.output, options);\n return undefined;\n }\n\n return generateMarkdownOutput(intermediateSchema, options);\n } else {\n const generate = getGenerateFunction(options.dialect);\n return generate({\n schema: schemaModule,\n source: schemaPath,\n });\n }\n}\n\n/**\n * Find all TypeScript schema files in a directory\n */\nfunction findSchemaFiles(dirPath: string): string[] {\n const files: string[] = [];\n\n try {\n const entries = readdirSync(dirPath, { withFileTypes: true });\n\n for (const entry of entries) {\n const fullPath = join(dirPath, entry.name);\n\n if (entry.isDirectory()) {\n // Recursively search subdirectories\n files.push(...findSchemaFiles(fullPath));\n } else if (entry.isFile() && /\\.(ts|js|mts|mjs|cts|cjs)$/.test(entry.name)) {\n files.push(fullPath);\n }\n }\n } catch (error) {\n if (error instanceof Error) {\n console.error(`Error reading directory ${dirPath}: ${error.message}`);\n }\n }\n\n return files;\n}\n\n/**\n * Resolve schema path (file or directory)\n */\nfunction resolveSchemaPath(schema: string): string[] {\n const schemaPath = resolve(process.cwd(), schema);\n\n // Check if path exists\n if (!existsSync(schemaPath)) {\n console.error(`Error: Schema path not found: ${schemaPath}`);\n process.exit(1);\n }\n\n // Check if it's a directory\n const stats = lstatSync(schemaPath);\n if (stats.isDirectory()) {\n const schemaFiles = findSchemaFiles(schemaPath);\n if (schemaFiles.length === 0) {\n console.error(`Error: No schema files found in directory: ${schemaPath}`);\n process.exit(1);\n }\n return schemaFiles;\n }\n\n // Single file\n return [schemaPath];\n}\n\n/**\n * Generate DBML format output\n */\nfunction generateDbmlOutput(\n mergedSchema: Record<string, unknown>,\n schemaPaths: string[],\n options: GenerateCommandOptions,\n): string {\n const generate = getGenerateFunction(options.dialect);\n return (\n generate({\n schema: mergedSchema,\n source: schemaPaths[0],\n }) || \"\"\n );\n}\n\n/**\n * Generate Markdown format output\n */\nfunction generateMarkdownOutput(\n intermediateSchema: IntermediateSchema,\n options: GenerateCommandOptions,\n): string {\n const markdownFormatter = new MarkdownFormatter();\n const markdown = markdownFormatter.format(intermediateSchema);\n\n // Include ER diagram unless --no-er-diagram is specified\n if (options.erDiagram) {\n const mermaidFormatter = new MermaidErDiagramFormatter();\n const erDiagram = mermaidFormatter.format(intermediateSchema);\n\n return `${markdown}\\n\\n---\\n\\n## ER Diagram\\n\\n\\`\\`\\`mermaid\\n${erDiagram}\\n\\`\\`\\``;\n }\n\n return markdown;\n}\n\n/**\n * Write Markdown to multiple files (one per table)\n */\nfunction writeMarkdownMultipleFiles(\n intermediateSchema: IntermediateSchema,\n outputDir: string,\n options: GenerateCommandOptions,\n): void {\n // Ensure output directory exists\n mkdirSync(outputDir, { recursive: true });\n\n const markdownFormatter = new MarkdownFormatter({ linkFormat: \"file\" });\n\n // Write README.md with index\n const index = markdownFormatter.generateIndex(intermediateSchema);\n let readme = `${index}\\n`;\n\n // Add ER diagram to README unless disabled\n if (options.erDiagram) {\n const mermaidFormatter = new MermaidErDiagramFormatter();\n const erDiagram = mermaidFormatter.format(intermediateSchema);\n readme += `\\n---\\n\\n## ER Diagram\\n\\n\\`\\`\\`mermaid\\n${erDiagram}\\n\\`\\`\\`\\n`;\n }\n\n writeFileSync(join(outputDir, \"README.md\"), readme, \"utf-8\");\n\n // Write individual table files\n for (const table of intermediateSchema.tables) {\n const tableDoc = markdownFormatter.generateTableDoc(table, intermediateSchema);\n const fileName = `${table.name}.md`;\n writeFileSync(join(outputDir, fileName), `# ${table.name}\\n\\n${tableDoc}\\n`, \"utf-8\");\n }\n}\n\n/**\n * Write single Markdown file\n */\nfunction writeSingleMarkdownFile(content: string, outputPath: string): void {\n const dir = dirname(outputPath);\n mkdirSync(dir, { recursive: true });\n writeFileSync(outputPath, content.endsWith(\"\\n\") ? content : content + \"\\n\", \"utf-8\");\n}\n\n/**\n * Run the generate command\n */\nasync function runGenerate(schema: string, options: GenerateCommandOptions): Promise<void> {\n const schemaPaths = resolveSchemaPath(schema);\n\n try {\n // Merge all schema modules\n const mergedSchema: Record<string, unknown> = {};\n\n for (const schemaPath of schemaPaths) {\n const schemaUrl = pathToFileURL(schemaPath).href;\n const cacheBuster = options.watch ? `?t=${Date.now()}` : \"\";\n\n try {\n const schemaModule = (await import(schemaUrl + cacheBuster)) as Record<string, unknown>;\n Object.assign(mergedSchema, schemaModule);\n } catch (error) {\n if (error instanceof Error) {\n console.error(`Error importing ${schemaPath}: ${error.message}`);\n console.error(\"\\nPossible causes:\");\n console.error(\"- Syntax error in the schema file\");\n console.error(\"- Missing dependencies (make sure drizzle-orm is installed)\");\n console.error(\"- Circular dependencies between schema files\");\n }\n throw error;\n }\n }\n\n if (options.format === \"markdown\") {\n // Generate Markdown format\n const GeneratorClass = getGeneratorClass(options.dialect);\n // For multiple files, pass the directory path to extract comments from all files\n const firstFilePath = schemaPaths[0];\n if (!firstFilePath) {\n throw new Error(\"No schema files found\");\n }\n const sourcePath = schemaPaths.length === 1 ? firstFilePath : dirname(firstFilePath);\n const generator = new GeneratorClass({\n schema: mergedSchema,\n source: sourcePath,\n });\n const intermediateSchema = generator.toIntermediateSchema();\n\n if (options.singleFile) {\n // Single file Markdown output\n const content = generateMarkdownOutput(intermediateSchema, options);\n\n if (options.output) {\n // Check for existing file if --force is not specified\n if (!options.force && existsSync(options.output)) {\n console.error(\n `Error: Output file already exists: ${options.output}\\nUse --force to overwrite existing files.`,\n );\n process.exit(1);\n }\n writeSingleMarkdownFile(content, options.output);\n console.log(`Markdown generated: ${options.output}`);\n } else {\n console.log(content);\n }\n } else {\n // Multiple files Markdown output\n if (!options.output) {\n // If no output specified, default to stdout with single file format\n const content = generateMarkdownOutput(intermediateSchema, options);\n console.log(content);\n } else {\n // Check for existing files if --force is not specified\n if (!options.force) {\n const tableNames = intermediateSchema.tables.map((t) => t.name);\n const existingFiles = hasExistingFiles(options.output, tableNames);\n if (existingFiles.length > 0) {\n console.error(\n `Error: The following files already exist:\\n${existingFiles.map((f) => ` - ${f}`).join(\"\\n\")}\\nUse --force to overwrite existing files.`,\n );\n process.exit(1);\n }\n }\n writeMarkdownMultipleFiles(intermediateSchema, options.output, options);\n console.log(`Markdown generated: ${options.output}/`);\n }\n }\n } else {\n // Generate DBML format\n const dbml = generateDbmlOutput(mergedSchema, schemaPaths, options);\n\n if (options.output) {\n // Check for existing file if --force is not specified\n if (!options.force && existsSync(options.output)) {\n console.error(\n `Error: Output file already exists: ${options.output}\\nUse --force to overwrite existing files.`,\n );\n process.exit(1);\n }\n const dir = dirname(options.output);\n mkdirSync(dir, { recursive: true });\n writeFileSync(options.output, dbml.endsWith(\"\\n\") ? dbml : dbml + \"\\n\", \"utf-8\");\n console.log(`DBML generated: ${options.output}`);\n } else {\n console.log(dbml);\n }\n }\n } catch (error) {\n if (error instanceof Error) {\n console.error(`Error generating output: ${error.message}`);\n } else {\n console.error(\"Error generating output:\", error);\n }\n process.exit(1);\n }\n}\n\n/**\n * Watch mode: regenerate on file changes\n */\nfunction watchSchema(schema: string, options: GenerateCommandOptions): void {\n const schemaPath = resolve(process.cwd(), schema);\n\n console.log(`Watching for changes: ${schemaPath}`);\n\n let debounceTimer: NodeJS.Timeout | null = null;\n\n watch(schemaPath, async (eventType) => {\n if (eventType === \"change\") {\n // Debounce to avoid multiple triggers\n if (debounceTimer) {\n clearTimeout(debounceTimer);\n }\n\n debounceTimer = setTimeout(async () => {\n console.log(\"\\nFile changed, regenerating...\");\n try {\n const output = await generateFromSchema(schemaPath, options);\n const formatLabel = options.format === \"markdown\" ? \"Markdown\" : \"DBML\";\n const isMultiFile =\n options.format === \"markdown\" && !options.singleFile && options.output;\n\n if (!options.output && output) {\n console.log(output);\n } else if (options.output) {\n const outputLabel = isMultiFile ? `${options.output}/` : options.output;\n console.log(`${formatLabel} regenerated: ${outputLabel}`);\n }\n } catch (error) {\n if (error instanceof Error) {\n console.error(`Error: ${error.message}`);\n } else {\n console.error(\"Error:\", error);\n }\n }\n }, 100);\n }\n });\n}\n\nprogram\n .command(\"generate\")\n .description(\"Generate documentation from Drizzle schema files\")\n .argument(\"<schema>\", \"Path to Drizzle schema file or directory\")\n .option(\"-o, --output <path>\", \"Output file or directory path\")\n .option(\"-d, --dialect <dialect>\", \"Database dialect (postgresql, mysql, sqlite)\", \"postgresql\")\n .option(\"-f, --format <format>\", \"Output format (dbml, markdown)\", \"markdown\")\n .option(\"-w, --watch\", \"Watch for file changes and regenerate\")\n .option(\"--single-file\", \"Output Markdown as a single file (for markdown format)\")\n .option(\"--no-er-diagram\", \"Exclude ER diagram from Markdown output\")\n .option(\"--force\", \"Overwrite existing files without confirmation\")\n .action(async (schema: string, options: GenerateCommandOptions) => {\n // Validate dialect\n const validDialects: Dialect[] = [\"postgresql\", \"mysql\", \"sqlite\"];\n if (!validDialects.includes(options.dialect)) {\n console.error(\n `Error: Invalid dialect \"${options.dialect}\". Valid options: ${validDialects.join(\", \")}`,\n );\n process.exit(1);\n }\n\n // Validate format\n const validFormats: OutputFormat[] = [\"dbml\", \"markdown\"];\n if (!validFormats.includes(options.format)) {\n console.error(\n `Error: Invalid format \"${options.format}\". Valid options: ${validFormats.join(\", \")}`,\n );\n process.exit(1);\n }\n\n // Warn if --single-file or --no-er-diagram used with non-markdown format\n if (options.format !== \"markdown\") {\n if (options.singleFile) {\n console.warn(\"Warning: --single-file is only applicable with --format markdown\");\n }\n if (!options.erDiagram) {\n console.warn(\"Warning: --no-er-diagram is only applicable with --format markdown\");\n }\n }\n\n // Initial generation\n await runGenerate(schema, options);\n\n // Start watch mode if requested\n if (options.watch) {\n watchSchema(schema, options);\n }\n });\n\nprogram.parse();\n\n// Cleanup: Unregister tsx loader when process exits\nprocess.on(\"exit\", () => {\n unregister();\n});\n"],"names":["unregister","register","program","Command","packageJsonPath","join","packageJson","readFileSync","getGenerateFunction","dialect","mysqlGenerate","sqliteGenerate","pgGenerate","getGeneratorClass","MySqlGenerator","SqliteGenerator","PgGenerator","hasExistingFiles","outputDir","tableNames","existingFiles","existsSync","readmePath","tableName","tablePath","generateFromSchema","schemaPath","options","schemaUrl","pathToFileURL","cacheBuster","schemaModule","GeneratorClass","intermediateSchema","writeMarkdownMultipleFiles","generateMarkdownOutput","findSchemaFiles","dirPath","files","entries","readdirSync","entry","fullPath","error","resolveSchemaPath","schema","resolve","lstatSync","schemaFiles","generateDbmlOutput","mergedSchema","schemaPaths","markdown","MarkdownFormatter","erDiagram","MermaidErDiagramFormatter","mkdirSync","markdownFormatter","readme","writeFileSync","table","tableDoc","fileName","writeSingleMarkdownFile","content","outputPath","dir","dirname","runGenerate","firstFilePath","sourcePath","t","f","dbml","watchSchema","debounceTimer","watch","eventType","output","formatLabel","isMultiFile","outputLabel","validDialects","validFormats"],"mappings":";;;;;;;;;;;AAgCA,MAAMA,IAAaC,EAAA,GAEbC,IAAU,IAAIC,EAAA,GAIdC,IAAkBC,EAAK,YAAY,SAAS,oBAAoB,GAChEC,IAAc,KAAK,MAAMC,EAAaH,GAAiB,OAAO,CAAC;AAKrEF,EAAQ,KAAK,cAAc,EAAE,YAAYI,EAAY,WAAW,EAAE,QAAQA,EAAY,OAAO;AAkB7F,SAASE,EAAoBC,GAAkB;AAC7C,UAAQA,GAAA;AAAA,IACN,KAAK;AACH,aAAOC;AAAA,IACT,KAAK;AACH,aAAOC;AAAA,IAET;AACE,aAAOC;AAAA,EAAA;AAEb;AAKA,SAASC,EAAkBJ,GAAkB;AAC3C,UAAQA,GAAA;AAAA,IACN,KAAK;AACH,aAAOK;AAAA,IACT,KAAK;AACH,aAAOC;AAAA,IAET;AACE,aAAOC;AAAA,EAAA;AAEb;AAKA,SAASC,EAAiBC,GAAmBC,GAAgC;AAC3E,QAAMC,IAA0B,CAAA;AAEhC,MAAI,CAACC,EAAWH,CAAS;AACvB,WAAOE;AAGT,QAAME,IAAajB,EAAKa,GAAW,WAAW;AAC9C,EAAIG,EAAWC,CAAU,KACvBF,EAAc,KAAKE,CAAU;AAG/B,aAAWC,KAAaJ,GAAY;AAClC,UAAMK,IAAYnB,EAAKa,GAAW,GAAGK,CAAS,KAAK;AACnD,IAAIF,EAAWG,CAAS,KACtBJ,EAAc,KAAKI,CAAS;AAAA,EAEhC;AAEA,SAAOJ;AACT;AAOA,eAAeK,EACbC,GACAC,GAC6B;AAE7B,QAAMC,IAAYC,EAAcH,CAAU,EAAE,MAGtCI,IAAcH,EAAQ,QAAQ,MAAM,KAAK,KAAK,KAAK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AzC,EACG,QAAQ,UAAU,EAClB,YAAY,kDAAkD,EAC9D,SAAS,YAAY,0CAA0C,EAC/D,OAAO,uBAAuB,+BAA+B,EAC7D,OAAO,2BAA2B,gDAAgD,YAAY,EAC9F,OAAO,yBAAyB,kCAAkC,UAAU,EAC5E,OAAO,eAAe,uCAAuC,EAC7D,OAAO,iBAAiB,wDAAwD,EAChF,OAAO,mBAAmB,yCAAyC,EACnE,OAAO,WAAW,+CAA+C,EACjE,OAAO,OAAO2C,GAAgBlB,MAAoC;AAEjE,QAAMuD,IAA2B,CAAC,cAAc,SAAS,QAAQ;AACjE,EAAKA,EAAc,SAASvD,EAAQ,OAAO,MACzC,QAAQ;AAAA,IACN,2BAA2BA,EAAQ,OAAO,qBAAqBuD,EAAc,KAAK,IAAI,CAAC;AAAA,EAAA,GAEzF,QAAQ,KAAK,CAAC;AAIhB,QAAMC,IAA+B,CAAC,QAAQ,UAAU;AACxD,EAAKA,EAAa,SAASxD,EAAQ,MAAM,MACvC,QAAQ;AAAA,IACN,0BAA0BA,EAAQ,MAAM,qBAAqBwD,EAAa,KAAK,IAAI,CAAC;AAAA,EAAA,GAEtF,QAAQ,KAAK,CAAC,IAIZxD,EAAQ,WAAW,eACjBA,EAAQ,cACV,QAAQ,KAAK,kEAAkE,GAE5EA,EAAQ,aACX,QAAQ,KAAK,oEAAoE,IAKrF,MAAMyC,EAAYvB,GAAQlB,CAAO,GAG7BA,EAAQ,SACV+C,EAAY7B,GAAQlB,CAAO;AAE/B,CAAC;AAEHzB,EAAQ,MAAA;AAGR,QAAQ,GAAG,QAAQ,MAAM;AACvB,EAAAF,EAAA;AACF,CAAC;"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/cli/index.ts"],"sourcesContent":["#!/usr/bin/env node\n\n/**\n * CLI for drizzle-docs-generator\n *\n * Generates DBML files from Drizzle ORM schema definitions.\n */\n\nimport { Command } from \"commander\";\nimport {\n existsSync,\n lstatSync,\n mkdirSync,\n readdirSync,\n readFileSync,\n watch,\n writeFileSync,\n} from \"node:fs\";\nimport { dirname, join, resolve } from \"node:path\";\nimport { pathToFileURL } from \"node:url\";\nimport { pgGenerate } from \"../generator/pg\";\nimport { mysqlGenerate } from \"../generator/mysql\";\nimport { sqliteGenerate } from \"../generator/sqlite\";\nimport { PgGenerator } from \"../generator/pg\";\nimport { MySqlGenerator } from \"../generator/mysql\";\nimport { SqliteGenerator } from \"../generator/sqlite\";\nimport { MarkdownFormatter } from \"../formatter/markdown\";\nimport { MermaidErDiagramFormatter } from \"../formatter/mermaid\";\nimport { register } from \"tsx/esm/api\";\nimport type { IntermediateSchema } from \"../types\";\n\n// Register tsx loader to support TypeScript and extensionless imports\nconst unregister = register();\n\nconst program = new Command();\n\n// Use import.meta.dirname (Node 20.11+) to resolve package.json\n// This works correctly even after bundling\nconst packageJsonPath = join(import.meta.dirname, \"../../package.json\");\nconst packageJson = JSON.parse(readFileSync(packageJsonPath, \"utf-8\")) as {\n version: string;\n description: string;\n};\n\nprogram.name(\"drizzle-docs\").description(packageJson.description).version(packageJson.version);\n\ntype Dialect = \"postgresql\" | \"mysql\" | \"sqlite\";\ntype OutputFormat = \"dbml\" | \"markdown\";\n\ninterface GenerateCommandOptions {\n output?: string;\n dialect: Dialect;\n watch?: boolean;\n format: OutputFormat;\n singleFile?: boolean;\n erDiagram: boolean; // commander uses --no-er-diagram which sets erDiagram to false\n columns: boolean; // commander uses --no-columns which sets columns to false\n force?: boolean; // skip overwrite confirmation for existing files\n}\n\n/**\n * Get the generate function based on dialect\n */\nfunction getGenerateFunction(dialect: Dialect) {\n switch (dialect) {\n case \"mysql\":\n return mysqlGenerate;\n case \"sqlite\":\n return sqliteGenerate;\n case \"postgresql\":\n default:\n return pgGenerate;\n }\n}\n\n/**\n * Get the generator class based on dialect\n */\nfunction getGeneratorClass(dialect: Dialect) {\n switch (dialect) {\n case \"mysql\":\n return MySqlGenerator;\n case \"sqlite\":\n return SqliteGenerator;\n case \"postgresql\":\n default:\n return PgGenerator;\n }\n}\n\n/**\n * Check if output directory has existing files\n */\nfunction hasExistingFiles(outputDir: string, tableNames: string[]): string[] {\n const existingFiles: string[] = [];\n\n if (!existsSync(outputDir)) {\n return existingFiles;\n }\n\n const readmePath = join(outputDir, \"README.md\");\n if (existsSync(readmePath)) {\n existingFiles.push(readmePath);\n }\n\n for (const tableName of tableNames) {\n const tablePath = join(outputDir, `${tableName}.md`);\n if (existsSync(tablePath)) {\n existingFiles.push(tablePath);\n }\n }\n\n return existingFiles;\n}\n\n/**\n * Generate output from a schema file (for watch mode)\n * Returns the generated output string for single-file formats,\n * or writes multiple files directly for multi-file markdown\n */\nasync function generateFromSchema(\n schemaPath: string,\n options: GenerateCommandOptions,\n): Promise<string | undefined> {\n // Use file URL for dynamic import (required for ESM)\n const schemaUrl = pathToFileURL(schemaPath).href;\n\n // Dynamic import with cache busting for watch mode\n const cacheBuster = options.watch ? `?t=${Date.now()}` : \"\";\n const schemaModule = (await import(schemaUrl + cacheBuster)) as Record<string, unknown>;\n\n if (options.format === \"markdown\") {\n const GeneratorClass = getGeneratorClass(options.dialect);\n const generator = new GeneratorClass({\n schema: schemaModule,\n source: schemaPath,\n });\n const intermediateSchema = generator.toIntermediateSchema();\n\n // Handle multi-file output in watch mode\n if (!options.singleFile && options.output) {\n writeMarkdownMultipleFiles(intermediateSchema, options.output, options);\n return undefined;\n }\n\n return generateMarkdownOutput(intermediateSchema, options);\n } else {\n const generate = getGenerateFunction(options.dialect);\n return generate({\n schema: schemaModule,\n source: schemaPath,\n });\n }\n}\n\n/**\n * Find all TypeScript schema files in a directory\n */\nfunction findSchemaFiles(dirPath: string): string[] {\n const files: string[] = [];\n\n try {\n const entries = readdirSync(dirPath, { withFileTypes: true });\n\n for (const entry of entries) {\n const fullPath = join(dirPath, entry.name);\n\n if (entry.isDirectory()) {\n // Recursively search subdirectories\n files.push(...findSchemaFiles(fullPath));\n } else if (entry.isFile() && /\\.(ts|js|mts|mjs|cts|cjs)$/.test(entry.name)) {\n files.push(fullPath);\n }\n }\n } catch (error) {\n if (error instanceof Error) {\n console.error(`Error reading directory ${dirPath}: ${error.message}`);\n }\n }\n\n return files;\n}\n\n/**\n * Resolve schema path (file or directory)\n */\nfunction resolveSchemaPath(schema: string): string[] {\n const schemaPath = resolve(process.cwd(), schema);\n\n // Check if path exists\n if (!existsSync(schemaPath)) {\n console.error(`Error: Schema path not found: ${schemaPath}`);\n process.exit(1);\n }\n\n // Check if it's a directory\n const stats = lstatSync(schemaPath);\n if (stats.isDirectory()) {\n const schemaFiles = findSchemaFiles(schemaPath);\n if (schemaFiles.length === 0) {\n console.error(`Error: No schema files found in directory: ${schemaPath}`);\n process.exit(1);\n }\n return schemaFiles;\n }\n\n // Single file\n return [schemaPath];\n}\n\n/**\n * Generate DBML format output\n */\nfunction generateDbmlOutput(\n mergedSchema: Record<string, unknown>,\n schemaPaths: string[],\n options: GenerateCommandOptions,\n): string {\n const generate = getGenerateFunction(options.dialect);\n return (\n generate({\n schema: mergedSchema,\n source: schemaPaths[0],\n }) || \"\"\n );\n}\n\n/**\n * Generate Markdown format output\n */\nfunction generateMarkdownOutput(\n intermediateSchema: IntermediateSchema,\n options: GenerateCommandOptions,\n): string {\n const markdownFormatter = new MarkdownFormatter();\n const markdown = markdownFormatter.format(intermediateSchema);\n\n // Include ER diagram unless --no-er-diagram is specified\n if (options.erDiagram) {\n const mermaidFormatter = new MermaidErDiagramFormatter({\n includeColumns: options.columns,\n });\n const erDiagram = mermaidFormatter.format(intermediateSchema);\n\n return `${markdown}\\n\\n---\\n\\n## ER Diagram\\n\\n\\`\\`\\`mermaid\\n${erDiagram}\\n\\`\\`\\``;\n }\n\n return markdown;\n}\n\n/**\n * Write Markdown to multiple files (one per table)\n */\nfunction writeMarkdownMultipleFiles(\n intermediateSchema: IntermediateSchema,\n outputDir: string,\n options: GenerateCommandOptions,\n): void {\n // Ensure output directory exists\n mkdirSync(outputDir, { recursive: true });\n\n const markdownFormatter = new MarkdownFormatter({ linkFormat: \"file\" });\n\n // Write README.md with index\n const index = markdownFormatter.generateIndex(intermediateSchema);\n let readme = `${index}\\n`;\n\n // Add ER diagram to README unless disabled\n if (options.erDiagram) {\n const mermaidFormatter = new MermaidErDiagramFormatter({\n includeColumns: options.columns,\n });\n const erDiagram = mermaidFormatter.format(intermediateSchema);\n readme += `\\n---\\n\\n## ER Diagram\\n\\n\\`\\`\\`mermaid\\n${erDiagram}\\n\\`\\`\\`\\n`;\n }\n\n writeFileSync(join(outputDir, \"README.md\"), readme, \"utf-8\");\n\n // Write individual table files\n for (const table of intermediateSchema.tables) {\n const tableDoc = markdownFormatter.generateTableDoc(table, intermediateSchema);\n const fileName = `${table.name}.md`;\n writeFileSync(join(outputDir, fileName), `# ${table.name}\\n\\n${tableDoc}\\n`, \"utf-8\");\n }\n}\n\n/**\n * Write single Markdown file\n */\nfunction writeSingleMarkdownFile(content: string, outputPath: string): void {\n const dir = dirname(outputPath);\n mkdirSync(dir, { recursive: true });\n writeFileSync(outputPath, content.endsWith(\"\\n\") ? content : content + \"\\n\", \"utf-8\");\n}\n\n/**\n * Run the generate command\n */\nasync function runGenerate(schema: string, options: GenerateCommandOptions): Promise<void> {\n const schemaPaths = resolveSchemaPath(schema);\n\n try {\n // Merge all schema modules\n const mergedSchema: Record<string, unknown> = {};\n\n for (const schemaPath of schemaPaths) {\n const schemaUrl = pathToFileURL(schemaPath).href;\n const cacheBuster = options.watch ? `?t=${Date.now()}` : \"\";\n\n try {\n const schemaModule = (await import(schemaUrl + cacheBuster)) as Record<string, unknown>;\n Object.assign(mergedSchema, schemaModule);\n } catch (error) {\n if (error instanceof Error) {\n console.error(`Error importing ${schemaPath}: ${error.message}`);\n console.error(\"\\nPossible causes:\");\n console.error(\"- Syntax error in the schema file\");\n console.error(\"- Missing dependencies (make sure drizzle-orm is installed)\");\n console.error(\"- Circular dependencies between schema files\");\n }\n throw error;\n }\n }\n\n if (options.format === \"markdown\") {\n // Generate Markdown format\n const GeneratorClass = getGeneratorClass(options.dialect);\n // For multiple files, pass the directory path to extract comments from all files\n const firstFilePath = schemaPaths[0];\n if (!firstFilePath) {\n throw new Error(\"No schema files found\");\n }\n const sourcePath = schemaPaths.length === 1 ? firstFilePath : dirname(firstFilePath);\n const generator = new GeneratorClass({\n schema: mergedSchema,\n source: sourcePath,\n });\n const intermediateSchema = generator.toIntermediateSchema();\n\n if (options.singleFile) {\n // Single file Markdown output\n const content = generateMarkdownOutput(intermediateSchema, options);\n\n if (options.output) {\n // Check for existing file if --force is not specified\n if (!options.force && existsSync(options.output)) {\n console.error(\n `Error: Output file already exists: ${options.output}\\nUse --force to overwrite existing files.`,\n );\n process.exit(1);\n }\n writeSingleMarkdownFile(content, options.output);\n console.log(`Markdown generated: ${options.output}`);\n } else {\n console.log(content);\n }\n } else {\n // Multiple files Markdown output\n if (!options.output) {\n // If no output specified, default to stdout with single file format\n const content = generateMarkdownOutput(intermediateSchema, options);\n console.log(content);\n } else {\n // Check for existing files if --force is not specified\n if (!options.force) {\n const tableNames = intermediateSchema.tables.map((t) => t.name);\n const existingFiles = hasExistingFiles(options.output, tableNames);\n if (existingFiles.length > 0) {\n console.error(\n `Error: The following files already exist:\\n${existingFiles.map((f) => ` - ${f}`).join(\"\\n\")}\\nUse --force to overwrite existing files.`,\n );\n process.exit(1);\n }\n }\n writeMarkdownMultipleFiles(intermediateSchema, options.output, options);\n console.log(`Markdown generated: ${options.output}/`);\n }\n }\n } else {\n // Generate DBML format\n const dbml = generateDbmlOutput(mergedSchema, schemaPaths, options);\n\n if (options.output) {\n // Check for existing file if --force is not specified\n if (!options.force && existsSync(options.output)) {\n console.error(\n `Error: Output file already exists: ${options.output}\\nUse --force to overwrite existing files.`,\n );\n process.exit(1);\n }\n const dir = dirname(options.output);\n mkdirSync(dir, { recursive: true });\n writeFileSync(options.output, dbml.endsWith(\"\\n\") ? dbml : dbml + \"\\n\", \"utf-8\");\n console.log(`DBML generated: ${options.output}`);\n } else {\n console.log(dbml);\n }\n }\n } catch (error) {\n if (error instanceof Error) {\n console.error(`Error generating output: ${error.message}`);\n } else {\n console.error(\"Error generating output:\", error);\n }\n process.exit(1);\n }\n}\n\n/**\n * Watch mode: regenerate on file changes\n */\nfunction watchSchema(schema: string, options: GenerateCommandOptions): void {\n const schemaPath = resolve(process.cwd(), schema);\n\n console.log(`Watching for changes: ${schemaPath}`);\n\n let debounceTimer: NodeJS.Timeout | null = null;\n\n watch(schemaPath, async (eventType) => {\n if (eventType === \"change\") {\n // Debounce to avoid multiple triggers\n if (debounceTimer) {\n clearTimeout(debounceTimer);\n }\n\n debounceTimer = setTimeout(async () => {\n console.log(\"\\nFile changed, regenerating...\");\n try {\n const output = await generateFromSchema(schemaPath, options);\n const formatLabel = options.format === \"markdown\" ? \"Markdown\" : \"DBML\";\n const isMultiFile =\n options.format === \"markdown\" && !options.singleFile && options.output;\n\n if (!options.output && output) {\n console.log(output);\n } else if (options.output) {\n const outputLabel = isMultiFile ? `${options.output}/` : options.output;\n console.log(`${formatLabel} regenerated: ${outputLabel}`);\n }\n } catch (error) {\n if (error instanceof Error) {\n console.error(`Error: ${error.message}`);\n } else {\n console.error(\"Error:\", error);\n }\n }\n }, 100);\n }\n });\n}\n\nprogram\n .command(\"generate\")\n .description(\"Generate documentation from Drizzle schema files\")\n .argument(\"<schema>\", \"Path to Drizzle schema file or directory\")\n .option(\"-o, --output <path>\", \"Output file or directory path\")\n .option(\"-d, --dialect <dialect>\", \"Database dialect (postgresql, mysql, sqlite)\", \"postgresql\")\n .option(\"-f, --format <format>\", \"Output format (dbml, markdown)\", \"markdown\")\n .option(\"-w, --watch\", \"Watch for file changes and regenerate\")\n .option(\"--single-file\", \"Output Markdown as a single file (for markdown format)\")\n .option(\"--no-er-diagram\", \"Exclude ER diagram from Markdown output\")\n .option(\"--no-columns\", \"Exclude columns from Mermaid ER diagram\")\n .option(\"--force\", \"Overwrite existing files without confirmation\")\n .action(async (schema: string, options: GenerateCommandOptions) => {\n // Validate dialect\n const validDialects: Dialect[] = [\"postgresql\", \"mysql\", \"sqlite\"];\n if (!validDialects.includes(options.dialect)) {\n console.error(\n `Error: Invalid dialect \"${options.dialect}\". Valid options: ${validDialects.join(\", \")}`,\n );\n process.exit(1);\n }\n\n // Validate format\n const validFormats: OutputFormat[] = [\"dbml\", \"markdown\"];\n if (!validFormats.includes(options.format)) {\n console.error(\n `Error: Invalid format \"${options.format}\". Valid options: ${validFormats.join(\", \")}`,\n );\n process.exit(1);\n }\n\n // Warn if --single-file, --no-er-diagram, or --no-columns used with non-markdown format\n if (options.format !== \"markdown\") {\n if (options.singleFile) {\n console.warn(\"Warning: --single-file is only applicable with --format markdown\");\n }\n if (!options.erDiagram) {\n console.warn(\"Warning: --no-er-diagram is only applicable with --format markdown\");\n }\n if (!options.columns) {\n console.warn(\"Warning: --no-columns is only applicable with --format markdown\");\n }\n }\n\n // Initial generation\n await runGenerate(schema, options);\n\n // Start watch mode if requested\n if (options.watch) {\n watchSchema(schema, options);\n }\n });\n\nprogram.parse();\n\n// Cleanup: Unregister tsx loader when process exits\nprocess.on(\"exit\", () => {\n unregister();\n});\n"],"names":["unregister","register","program","Command","packageJsonPath","join","packageJson","readFileSync","getGenerateFunction","dialect","mysqlGenerate","sqliteGenerate","pgGenerate","getGeneratorClass","MySqlGenerator","SqliteGenerator","PgGenerator","hasExistingFiles","outputDir","tableNames","existingFiles","existsSync","readmePath","tableName","tablePath","generateFromSchema","schemaPath","options","schemaUrl","pathToFileURL","cacheBuster","schemaModule","GeneratorClass","intermediateSchema","writeMarkdownMultipleFiles","generateMarkdownOutput","findSchemaFiles","dirPath","files","entries","readdirSync","entry","fullPath","error","resolveSchemaPath","schema","resolve","lstatSync","schemaFiles","generateDbmlOutput","mergedSchema","schemaPaths","markdown","MarkdownFormatter","erDiagram","MermaidErDiagramFormatter","mkdirSync","markdownFormatter","readme","writeFileSync","table","tableDoc","fileName","writeSingleMarkdownFile","content","outputPath","dir","dirname","runGenerate","firstFilePath","sourcePath","t","f","dbml","watchSchema","debounceTimer","watch","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AAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAIrF,eAAO,MAAM,YAAY,QAA8C,CAAC;AACxE,eAAO,MAAM,YAAY,QAAwC,CAAC;AAClE,eAAO,MAAM,YAAY,QAAwC,CAAC;AAClE,eAAO,MAAM,eAAe,QAA2C,CAAC;AACxE,eAAO,MAAM,eAAe,QAA2C,CAAC;AACxE,eAAO,MAAM,gBAAgB,QAA4C,CAAC;AAC1E,eAAO,MAAM,gBAAgB,QAA4C,CAAC;AAG1E,eAAO,MAAM,eAAe,QAA2D,CAAC;AAGxF,eAAO,MAAM,eAAe,
|
|
1
|
+
{"version":3,"file":"integration-test-utils.d.ts","sourceRoot":"","sources":["../../src/cli/integration-test-utils.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAIrF,eAAO,MAAM,YAAY,QAA8C,CAAC;AACxE,eAAO,MAAM,YAAY,QAAwC,CAAC;AAClE,eAAO,MAAM,YAAY,QAAwC,CAAC;AAClE,eAAO,MAAM,eAAe,QAA2C,CAAC;AACxE,eAAO,MAAM,eAAe,QAA2C,CAAC;AACxE,eAAO,MAAM,gBAAgB,QAA4C,CAAC;AAC1E,eAAO,MAAM,gBAAgB,QAA4C,CAAC;AAG1E,eAAO,MAAM,eAAe,QAA2D,CAAC;AAGxF,eAAO,MAAM,eAAe,UAQ3B,CAAC;AAEF;;;GAGG;AACH,wBAAgB,oBAAoB,IAAI,IAAI,CAa3C;AAGD,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,EAAE,YAAY,EAAE,aAAa,EAAE,CAAC;AACtE,OAAO,EAAE,IAAI,EAAE,CAAC"}
|
|
@@ -8,7 +8,12 @@ export interface MermaidFormatterOptions extends FormatterOptions {
|
|
|
8
8
|
* Whether to include column types in the diagram
|
|
9
9
|
* @default true
|
|
10
10
|
*/
|
|
11
|
-
includeColumnTypes
|
|
11
|
+
includeColumnTypes: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Whether to include columns in the diagram
|
|
14
|
+
* @default true
|
|
15
|
+
*/
|
|
16
|
+
includeColumns: boolean;
|
|
12
17
|
}
|
|
13
18
|
/**
|
|
14
19
|
* MermaidErDiagramFormatter generates Mermaid ER diagram syntax from IntermediateSchema
|
|
@@ -36,9 +41,9 @@ export declare class MermaidErDiagramFormatter implements OutputFormatter {
|
|
|
36
41
|
/**
|
|
37
42
|
* Create a new MermaidErDiagramFormatter
|
|
38
43
|
*
|
|
39
|
-
* @param options - Formatter options
|
|
44
|
+
* @param options - Formatter options (all fields are optional and default to true)
|
|
40
45
|
*/
|
|
41
|
-
constructor(options?: MermaidFormatterOptions);
|
|
46
|
+
constructor(options?: Partial<MermaidFormatterOptions>);
|
|
42
47
|
/**
|
|
43
48
|
* Format the intermediate schema into a Mermaid ER diagram
|
|
44
49
|
*
|
|
@@ -58,6 +63,12 @@ export declare class MermaidErDiagramFormatter implements OutputFormatter {
|
|
|
58
63
|
* Collect foreign key columns from relations for FK marker assignment
|
|
59
64
|
*/
|
|
60
65
|
private collectForeignKeyColumns;
|
|
66
|
+
/**
|
|
67
|
+
* Check if a foreign key is nullable
|
|
68
|
+
*
|
|
69
|
+
* A foreign key is nullable if all of its columns are nullable
|
|
70
|
+
*/
|
|
71
|
+
private isForeignKeyNullable;
|
|
61
72
|
/**
|
|
62
73
|
* Format a table definition to Mermaid syntax
|
|
63
74
|
*/
|
|
@@ -77,14 +88,19 @@ export declare class MermaidErDiagramFormatter implements OutputFormatter {
|
|
|
77
88
|
*
|
|
78
89
|
* Mermaid ER diagram relation syntax:
|
|
79
90
|
* - ||--|| : one-to-one
|
|
91
|
+
* - ||--o| : one-to-one, but target is optional
|
|
80
92
|
* - ||--o{ : one-to-many
|
|
81
93
|
* - }o--|| : many-to-one
|
|
94
|
+
* - }o--o| : many-to-one, but source is optional
|
|
82
95
|
* - }o--o{ : many-to-many
|
|
83
96
|
*/
|
|
84
97
|
private formatRelation;
|
|
85
98
|
/**
|
|
86
99
|
* Convert IntermediateRelationType to Mermaid relation symbol
|
|
87
100
|
*
|
|
101
|
+
* @param type - The relation type
|
|
102
|
+
* @param isForeignKeyNullable - Whether the foreign key column(s) are nullable
|
|
103
|
+
*
|
|
88
104
|
* @see https://mermaid.js.org/syntax/entityRelationshipDiagram.html#relationship-syntax
|
|
89
105
|
*/
|
|
90
106
|
private getRelationSymbol;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mermaid.d.ts","sourceRoot":"","sources":["../../src/formatter/mermaid.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,kBAAkB,EAInB,MAAM,UAAU,CAAC;AAClB,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAEjE;;GAEG;AACH,MAAM,WAAW,uBAAwB,SAAQ,gBAAgB;IAC/D;;;OAGG;IACH,kBAAkB,CAAC,EAAE,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"mermaid.d.ts","sourceRoot":"","sources":["../../src/formatter/mermaid.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,kBAAkB,EAInB,MAAM,UAAU,CAAC;AAClB,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAEjE;;GAEG;AACH,MAAM,WAAW,uBAAwB,SAAQ,gBAAgB;IAC/D;;;OAGG;IACH,kBAAkB,EAAE,OAAO,CAAC;IAE5B;;;OAGG;IACH,cAAc,EAAE,OAAO,CAAC;CACzB;AAaD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,qBAAa,yBAA0B,YAAW,eAAe;IAC/D,OAAO,CAAC,OAAO,CAAoC;IAEnD;;;;OAIG;gBACS,OAAO,GAAE,OAAO,CAAC,uBAAuB,CAAM;IAI1D;;;;;OAKG;IACH,MAAM,CAAC,MAAM,EAAE,kBAAkB,GAAG,MAAM;IA4B1C;;;;;;OAMG;IACH,aAAa,CAAC,MAAM,EAAE,kBAAkB,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM;IAiDpE;;OAEG;IACH,OAAO,CAAC,wBAAwB;IAgBhC;;;;OAIG;IACH,OAAO,CAAC,oBAAoB;IAc5B;;OAEG;IACH,OAAO,CAAC,WAAW;IAwBnB;;OAEG;IACH,OAAO,CAAC,YAAY;IAmCpB;;;;OAIG;IACH,OAAO,CAAC,YAAY;IAiDpB;;;;;;;;;;OAUG;IACH,OAAO,CAAC,cAAc;IAYtB;;;;;;;OAOG;IACH,OAAO,CAAC,iBAAiB;IAgBzB;;;;OAIG;IACH,OAAO,CAAC,UAAU;IAUlB;;OAEG;IACH,OAAO,CAAC,YAAY;CAGrB"}
|
|
@@ -2,14 +2,15 @@ const u = {
|
|
|
2
2
|
includeComments: !0,
|
|
3
3
|
includeIndexes: !0,
|
|
4
4
|
includeConstraints: !0,
|
|
5
|
-
includeColumnTypes: !0
|
|
5
|
+
includeColumnTypes: !0,
|
|
6
|
+
includeColumns: !0
|
|
6
7
|
};
|
|
7
8
|
class f {
|
|
8
9
|
options;
|
|
9
10
|
/**
|
|
10
11
|
* Create a new MermaidErDiagramFormatter
|
|
11
12
|
*
|
|
12
|
-
* @param options - Formatter options
|
|
13
|
+
* @param options - Formatter options (all fields are optional and default to true)
|
|
13
14
|
*/
|
|
14
15
|
constructor(e = {}) {
|
|
15
16
|
this.options = { ...u, ...e };
|
|
@@ -23,7 +24,7 @@ class f {
|
|
|
23
24
|
format(e) {
|
|
24
25
|
const t = ["erDiagram"], n = this.collectForeignKeyColumns(e.relations);
|
|
25
26
|
for (const o of e.relations) {
|
|
26
|
-
const i = this.formatRelation(o);
|
|
27
|
+
const i = this.formatRelation(o, e.tables);
|
|
27
28
|
i && t.push(` ${i}`);
|
|
28
29
|
}
|
|
29
30
|
e.relations.length > 0 && e.tables.length > 0 && t.push("");
|
|
@@ -47,17 +48,17 @@ class f {
|
|
|
47
48
|
const o = /* @__PURE__ */ new Set([t]), i = [];
|
|
48
49
|
for (const s of e.relations)
|
|
49
50
|
(s.fromTable === t || s.toTable === t) && (o.add(s.fromTable), o.add(s.toTable), i.push(s));
|
|
50
|
-
const a = e.tables.filter((s) => o.has(s.name)),
|
|
51
|
+
const a = e.tables.filter((s) => o.has(s.name)), r = this.collectForeignKeyColumns(i), l = ["erDiagram"];
|
|
51
52
|
for (const s of i) {
|
|
52
|
-
const
|
|
53
|
-
|
|
53
|
+
const m = this.formatRelation(s, a);
|
|
54
|
+
m && l.push(` ${m}`);
|
|
54
55
|
}
|
|
55
|
-
i.length > 0 && a.length > 0 &&
|
|
56
|
+
i.length > 0 && a.length > 0 && l.push("");
|
|
56
57
|
for (const s of a) {
|
|
57
|
-
const
|
|
58
|
-
|
|
58
|
+
const m = this.formatTable(s, r);
|
|
59
|
+
l.push(...m);
|
|
59
60
|
}
|
|
60
|
-
return
|
|
61
|
+
return l.join(`
|
|
61
62
|
`);
|
|
62
63
|
}
|
|
63
64
|
/**
|
|
@@ -72,15 +73,27 @@ class f {
|
|
|
72
73
|
}
|
|
73
74
|
return t;
|
|
74
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* Check if a foreign key is nullable
|
|
78
|
+
*
|
|
79
|
+
* A foreign key is nullable if all of its columns are nullable
|
|
80
|
+
*/
|
|
81
|
+
isForeignKeyNullable(e, t) {
|
|
82
|
+
const n = t.find((o) => o.name === e.fromTable);
|
|
83
|
+
return n ? e.fromColumns.every((o) => n.columns.find((a) => a.name === o)?.nullable ?? !1) : !1;
|
|
84
|
+
}
|
|
75
85
|
/**
|
|
76
86
|
* Format a table definition to Mermaid syntax
|
|
77
87
|
*/
|
|
78
88
|
formatTable(e, t) {
|
|
79
|
-
const n = [], o = this.escapeName(e.name)
|
|
89
|
+
const n = [], o = this.escapeName(e.name);
|
|
90
|
+
if (!this.options.includeColumns)
|
|
91
|
+
return n.push(` ${o}`), n;
|
|
92
|
+
const i = t.get(e.name) || /* @__PURE__ */ new Set();
|
|
80
93
|
n.push(` ${o} {`);
|
|
81
94
|
for (const a of e.columns) {
|
|
82
|
-
const
|
|
83
|
-
n.push(` ${
|
|
95
|
+
const r = this.formatColumn(a, i);
|
|
96
|
+
n.push(` ${r}`);
|
|
84
97
|
}
|
|
85
98
|
return n.push(" }"), n;
|
|
86
99
|
}
|
|
@@ -146,27 +159,32 @@ class f {
|
|
|
146
159
|
*
|
|
147
160
|
* Mermaid ER diagram relation syntax:
|
|
148
161
|
* - ||--|| : one-to-one
|
|
162
|
+
* - ||--o| : one-to-one, but target is optional
|
|
149
163
|
* - ||--o{ : one-to-many
|
|
150
164
|
* - }o--|| : many-to-one
|
|
165
|
+
* - }o--o| : many-to-one, but source is optional
|
|
151
166
|
* - }o--o{ : many-to-many
|
|
152
167
|
*/
|
|
153
|
-
formatRelation(e) {
|
|
154
|
-
const
|
|
155
|
-
return `${
|
|
168
|
+
formatRelation(e, t) {
|
|
169
|
+
const n = this.escapeName(e.fromTable), o = this.escapeName(e.toTable), i = this.isForeignKeyNullable(e, t), a = this.getRelationSymbol(e.type, i), r = e.fromColumns.join(", ");
|
|
170
|
+
return `${n} ${a} ${o} : "${r}"`;
|
|
156
171
|
}
|
|
157
172
|
/**
|
|
158
173
|
* Convert IntermediateRelationType to Mermaid relation symbol
|
|
159
174
|
*
|
|
175
|
+
* @param type - The relation type
|
|
176
|
+
* @param isForeignKeyNullable - Whether the foreign key column(s) are nullable
|
|
177
|
+
*
|
|
160
178
|
* @see https://mermaid.js.org/syntax/entityRelationshipDiagram.html#relationship-syntax
|
|
161
179
|
*/
|
|
162
|
-
getRelationSymbol(e) {
|
|
180
|
+
getRelationSymbol(e, t = !1) {
|
|
163
181
|
switch (e) {
|
|
164
182
|
case "one-to-one":
|
|
165
|
-
return "||--||";
|
|
183
|
+
return t ? "||--o|" : "||--||";
|
|
166
184
|
case "one-to-many":
|
|
167
185
|
return "||--o{";
|
|
168
186
|
case "many-to-one":
|
|
169
|
-
return "}o--||";
|
|
187
|
+
return t ? "}o--o|" : "}o--||";
|
|
170
188
|
case "many-to-many":
|
|
171
189
|
return "}o--o{";
|
|
172
190
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mermaid.js","sources":["../../src/formatter/mermaid.ts"],"sourcesContent":["import type {\n IntermediateSchema,\n TableDefinition,\n ColumnDefinition,\n RelationDefinition,\n} from \"../types\";\nimport type { OutputFormatter, FormatterOptions } from \"./types\";\n\n/**\n * Options for MermaidErDiagramFormatter\n */\nexport interface MermaidFormatterOptions extends FormatterOptions {\n /**\n * Whether to include column types in the diagram\n * @default true\n */\n includeColumnTypes?: boolean;\n}\n\n/**\n * Default formatter options\n */\nconst DEFAULT_OPTIONS: Required<MermaidFormatterOptions> = {\n includeComments: true,\n includeIndexes: true,\n includeConstraints: true,\n includeColumnTypes: true,\n};\n\n/**\n * MermaidErDiagramFormatter generates Mermaid ER diagram syntax from IntermediateSchema\n *\n * This formatter creates Mermaid-compatible ER diagrams that can be rendered\n * in GitHub, GitLab, or any Mermaid-supporting platform.\n *\n * @example\n * ```typescript\n * const formatter = new MermaidErDiagramFormatter();\n * const mermaid = formatter.format(schema);\n * // Output:\n * // erDiagram\n * // users ||--o{ posts : \"author_id\"\n * // users {\n * // int id PK\n * // varchar username\n * // }\n * ```\n *\n * @see https://mermaid.js.org/syntax/entityRelationshipDiagram.html\n */\nexport class MermaidErDiagramFormatter implements OutputFormatter {\n private options: Required<MermaidFormatterOptions>;\n\n /**\n * Create a new MermaidErDiagramFormatter\n *\n * @param options - Formatter options\n */\n constructor(options: MermaidFormatterOptions = {}) {\n this.options = { ...DEFAULT_OPTIONS, ...options };\n }\n\n /**\n * Format the intermediate schema into a Mermaid ER diagram\n *\n * @param schema - The intermediate schema to format\n * @returns Mermaid ER diagram string\n */\n format(schema: IntermediateSchema): string {\n const lines: string[] = [\"erDiagram\"];\n\n // Collect foreign key columns for FK markers\n const fkColumns = this.collectForeignKeyColumns(schema.relations);\n\n // Generate relations first (at the top of the diagram)\n for (const relation of schema.relations) {\n const relationLine = this.formatRelation(relation);\n if (relationLine) {\n lines.push(` ${relationLine}`);\n }\n }\n\n // Add blank line between relations and tables if there are both\n if (schema.relations.length > 0 && schema.tables.length > 0) {\n lines.push(\"\");\n }\n\n // Generate tables\n for (const table of schema.tables) {\n const tableLines = this.formatTable(table, fkColumns);\n lines.push(...tableLines);\n }\n\n return lines.join(\"\\n\");\n }\n\n /**\n * Format a focused ER diagram showing only a specific table and its related tables\n *\n * @param schema - The intermediate schema\n * @param tableName - The name of the table to focus on\n * @returns Mermaid ER diagram string showing only the focused table and its relations\n */\n formatFocused(schema: IntermediateSchema, tableName: string): string {\n // Find the target table\n const targetTable = schema.tables.find((t) => t.name === tableName);\n if (!targetTable) {\n return \"erDiagram\";\n }\n\n // Find related tables through relations\n const relatedTableNames = new Set<string>([tableName]);\n const relevantRelations: RelationDefinition[] = [];\n\n for (const relation of schema.relations) {\n if (relation.fromTable === tableName || relation.toTable === tableName) {\n relatedTableNames.add(relation.fromTable);\n relatedTableNames.add(relation.toTable);\n relevantRelations.push(relation);\n }\n }\n\n // Filter tables to only include related ones\n const relevantTables = schema.tables.filter((t) => relatedTableNames.has(t.name));\n\n // Collect foreign key columns for FK markers\n const fkColumns = this.collectForeignKeyColumns(relevantRelations);\n\n const lines: string[] = [\"erDiagram\"];\n\n // Generate relations\n for (const relation of relevantRelations) {\n const relationLine = this.formatRelation(relation);\n if (relationLine) {\n lines.push(` ${relationLine}`);\n }\n }\n\n // Add blank line between relations and tables if there are both\n if (relevantRelations.length > 0 && relevantTables.length > 0) {\n lines.push(\"\");\n }\n\n // Generate tables\n for (const table of relevantTables) {\n const tableLines = this.formatTable(table, fkColumns);\n lines.push(...tableLines);\n }\n\n return lines.join(\"\\n\");\n }\n\n /**\n * Collect foreign key columns from relations for FK marker assignment\n */\n private collectForeignKeyColumns(relations: RelationDefinition[]): Map<string, Set<string>> {\n const fkColumns = new Map<string, Set<string>>();\n\n for (const relation of relations) {\n // The \"from\" side of the relation has the FK column\n if (!fkColumns.has(relation.fromTable)) {\n fkColumns.set(relation.fromTable, new Set());\n }\n for (const col of relation.fromColumns) {\n fkColumns.get(relation.fromTable)!.add(col);\n }\n }\n\n return fkColumns;\n }\n\n /**\n * Format a table definition to Mermaid syntax\n */\n private formatTable(table: TableDefinition, fkColumns: Map<string, Set<string>>): string[] {\n const lines: string[] = [];\n const tableName = this.escapeName(table.name);\n const tableFkColumns = fkColumns.get(table.name) || new Set();\n\n lines.push(` ${tableName} {`);\n\n for (const column of table.columns) {\n const columnLine = this.formatColumn(column, tableFkColumns);\n lines.push(` ${columnLine}`);\n }\n\n lines.push(\" }\");\n\n return lines;\n }\n\n /**\n * Format a column definition to Mermaid syntax\n */\n private formatColumn(column: ColumnDefinition, fkColumns: Set<string>): string {\n const parts: string[] = [];\n\n // Column type (simplified for Mermaid)\n if (this.options.includeColumnTypes) {\n parts.push(this.simplifyType(column.type));\n }\n\n // Column name\n parts.push(this.escapeName(column.name));\n\n // PK/FK markers\n const markers: string[] = [];\n if (column.primaryKey) {\n markers.push(\"PK\");\n }\n if (fkColumns.has(column.name)) {\n markers.push(\"FK\");\n }\n if (column.unique && !column.primaryKey) {\n markers.push(\"UK\");\n }\n\n if (markers.length > 0) {\n parts.push(markers.join(\",\"));\n }\n\n // Add comment as Mermaid comment (quoted string after markers)\n if (this.options.includeComments && column.comment) {\n parts.push(`\"${this.escapeString(column.comment)}\"`);\n }\n\n return parts.join(\" \");\n }\n\n /**\n * Simplify SQL type for Mermaid display\n *\n * Mermaid ER diagrams work best with simple type names\n */\n private simplifyType(type: string): string {\n // Remove parentheses content for cleaner display (e.g., varchar(255) -> varchar)\n const simplified = type.replace(/\\([^)]*\\)/g, \"\").toLowerCase();\n\n // Map common types to shorter versions\n const typeMap: Record<string, string> = {\n integer: \"int\",\n bigint: \"bigint\",\n smallint: \"smallint\",\n serial: \"serial\",\n bigserial: \"bigserial\",\n text: \"text\",\n varchar: \"varchar\",\n char: \"char\",\n boolean: \"boolean\",\n timestamp: \"timestamp\",\n timestamptz: \"timestamptz\",\n \"timestamp with time zone\": \"timestamptz\",\n \"time with time zone\": \"timetz\",\n date: \"date\",\n time: \"time\",\n timetz: \"timetz\",\n json: \"json\",\n jsonb: \"jsonb\",\n uuid: \"uuid\",\n real: \"real\",\n float: \"float\",\n double: \"double\",\n \"double precision\": \"double\",\n decimal: \"decimal\",\n numeric: \"numeric\",\n blob: \"blob\",\n bytea: \"bytea\",\n // MySQL types\n tinyint: \"tinyint\",\n mediumint: \"mediumint\",\n // MySQL unsigned types (u-prefix for distinction)\n \"tinyint unsigned\": \"utinyint\",\n \"smallint unsigned\": \"usmallint\",\n \"mediumint unsigned\": \"umediumint\",\n \"int unsigned\": \"uint\",\n \"bigint unsigned\": \"ubigint\",\n \"float unsigned\": \"ufloat\",\n \"double unsigned\": \"udouble\",\n };\n\n return typeMap[simplified] || simplified;\n }\n\n /**\n * Format a relation definition to Mermaid syntax\n *\n * Mermaid ER diagram relation syntax:\n * - ||--|| : one-to-one\n * - ||--o{ : one-to-many\n * - }o--|| : many-to-one\n * - }o--o{ : many-to-many\n */\n private formatRelation(relation: RelationDefinition): string {\n const fromTable = this.escapeName(relation.fromTable);\n const toTable = this.escapeName(relation.toTable);\n const symbol = this.getRelationSymbol(relation.type);\n const label = relation.fromColumns.join(\", \");\n\n return `${fromTable} ${symbol} ${toTable} : \"${label}\"`;\n }\n\n /**\n * Convert IntermediateRelationType to Mermaid relation symbol\n *\n * @see https://mermaid.js.org/syntax/entityRelationshipDiagram.html#relationship-syntax\n */\n private getRelationSymbol(type: RelationDefinition[\"type\"]): string {\n switch (type) {\n case \"one-to-one\":\n return \"||--||\";\n case \"one-to-many\":\n return \"||--o{\";\n case \"many-to-one\":\n return \"}o--||\";\n case \"many-to-many\":\n return \"}o--o{\";\n }\n }\n\n /**\n * Escape a name for Mermaid if it contains special characters\n *\n * Mermaid entity names should be alphanumeric with underscores\n */\n private escapeName(name: string): string {\n // Mermaid accepts alphanumeric and underscores\n // For names with special characters, we need to quote them\n if (/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(name)) {\n return name;\n }\n // Replace special characters with underscores for Mermaid compatibility\n return name.replace(/[^a-zA-Z0-9_]/g, \"_\");\n }\n\n /**\n * Escape a string for use in Mermaid quoted strings\n */\n private escapeString(str: string): string {\n return str.replace(/\\\\/g, \"\\\\\\\\\").replace(/\"/g, '\\\\\"').replace(/\\n/g, \" \");\n }\n}\n"],"names":["DEFAULT_OPTIONS","MermaidErDiagramFormatter","options","schema","lines","fkColumns","relation","relationLine","table","tableLines","tableName","t","relatedTableNames","relevantRelations","relevantTables","relations","col","tableFkColumns","column","columnLine","parts","markers","type","simplified","fromTable","toTable","symbol","label","name","str"],"mappings":"AAsBA,MAAMA,IAAqD;AAAA,EACzD,iBAAiB;AAAA,EACjB,gBAAgB;AAAA,EAChB,oBAAoB;AAAA,EACpB,oBAAoB;AACtB;AAuBO,MAAMC,EAAqD;AAAA,EACxD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOR,YAAYC,IAAmC,IAAI;AACjD,SAAK,UAAU,EAAE,GAAGF,GAAiB,GAAGE,EAAA;AAAA,EAC1C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,OAAOC,GAAoC;AACzC,UAAMC,IAAkB,CAAC,WAAW,GAG9BC,IAAY,KAAK,yBAAyBF,EAAO,SAAS;AAGhE,eAAWG,KAAYH,EAAO,WAAW;AACvC,YAAMI,IAAe,KAAK,eAAeD,CAAQ;AACjD,MAAIC,KACFH,EAAM,KAAK,OAAOG,CAAY,EAAE;AAAA,IAEpC;AAGA,IAAIJ,EAAO,UAAU,SAAS,KAAKA,EAAO,OAAO,SAAS,KACxDC,EAAM,KAAK,EAAE;AAIf,eAAWI,KAASL,EAAO,QAAQ;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O;AAAA,MACT,KAAK;AACH,eAAO;AAAA,MACT,KAAK;AACH,eAAO;AAAA,MACT,KAAK;AACH,eAAO;AAAA,IAAA;AAAA,EAEb;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOQ,WAAWM,GAAsB;AAGvC,WAAI,2BAA2B,KAAKA,CAAI,IAC/BA,IAGFA,EAAK,QAAQ,kBAAkB,GAAG;AAAA,EAC3C;AAAA;AAAA;AAAA;AAAA,EAKQ,aAAaC,GAAqB;AACxC,WAAOA,EAAI,QAAQ,OAAO,MAAM,EAAE,QAAQ,MAAM,KAAK,EAAE,QAAQ,OAAO,GAAG;AAAA,EAC3E;AACF;"}
|
|
1
|
+
{"version":3,"file":"mermaid.js","sources":["../../src/formatter/mermaid.ts"],"sourcesContent":["import type {\n IntermediateSchema,\n TableDefinition,\n ColumnDefinition,\n RelationDefinition,\n} from \"../types\";\nimport type { OutputFormatter, FormatterOptions } from \"./types\";\n\n/**\n * Options for MermaidErDiagramFormatter\n */\nexport interface MermaidFormatterOptions extends FormatterOptions {\n /**\n * Whether to include column types in the diagram\n * @default true\n */\n includeColumnTypes: boolean;\n\n /**\n * Whether to include columns in the diagram\n * @default true\n */\n includeColumns: boolean;\n}\n\n/**\n * Default formatter options\n */\nconst DEFAULT_OPTIONS: Required<MermaidFormatterOptions> = {\n includeComments: true,\n includeIndexes: true,\n includeConstraints: true,\n includeColumnTypes: true,\n includeColumns: true,\n};\n\n/**\n * MermaidErDiagramFormatter generates Mermaid ER diagram syntax from IntermediateSchema\n *\n * This formatter creates Mermaid-compatible ER diagrams that can be rendered\n * in GitHub, GitLab, or any Mermaid-supporting platform.\n *\n * @example\n * ```typescript\n * const formatter = new MermaidErDiagramFormatter();\n * const mermaid = formatter.format(schema);\n * // Output:\n * // erDiagram\n * // users ||--o{ posts : \"author_id\"\n * // users {\n * // int id PK\n * // varchar username\n * // }\n * ```\n *\n * @see https://mermaid.js.org/syntax/entityRelationshipDiagram.html\n */\nexport class MermaidErDiagramFormatter implements OutputFormatter {\n private options: Required<MermaidFormatterOptions>;\n\n /**\n * Create a new MermaidErDiagramFormatter\n *\n * @param options - Formatter options (all fields are optional and default to true)\n */\n constructor(options: Partial<MermaidFormatterOptions> = {}) {\n this.options = { ...DEFAULT_OPTIONS, ...options };\n }\n\n /**\n * Format the intermediate schema into a Mermaid ER diagram\n *\n * @param schema - The intermediate schema to format\n * @returns Mermaid ER diagram string\n */\n format(schema: IntermediateSchema): string {\n const lines: string[] = [\"erDiagram\"];\n\n // Collect foreign key columns for FK markers\n const fkColumns = this.collectForeignKeyColumns(schema.relations);\n\n // Generate relations first (at the top of the diagram)\n for (const relation of schema.relations) {\n const relationLine = this.formatRelation(relation, schema.tables);\n if (relationLine) {\n lines.push(` ${relationLine}`);\n }\n }\n\n // Add blank line between relations and tables if there are both\n if (schema.relations.length > 0 && schema.tables.length > 0) {\n lines.push(\"\");\n }\n\n // Generate tables\n for (const table of schema.tables) {\n const tableLines = this.formatTable(table, fkColumns);\n lines.push(...tableLines);\n }\n\n return lines.join(\"\\n\");\n }\n\n /**\n * Format a focused ER diagram showing only a specific table and its related tables\n *\n * @param schema - The intermediate schema\n * @param tableName - The name of the table to focus on\n * @returns Mermaid ER diagram string showing only the focused table and its relations\n */\n formatFocused(schema: IntermediateSchema, tableName: string): string {\n // Find the target table\n const targetTable = schema.tables.find((t) => t.name === tableName);\n if (!targetTable) {\n return \"erDiagram\";\n }\n\n // Find related tables through relations\n const relatedTableNames = new Set<string>([tableName]);\n const relevantRelations: RelationDefinition[] = [];\n\n for (const relation of schema.relations) {\n if (relation.fromTable === tableName || relation.toTable === tableName) {\n relatedTableNames.add(relation.fromTable);\n relatedTableNames.add(relation.toTable);\n relevantRelations.push(relation);\n }\n }\n\n // Filter tables to only include related ones\n const relevantTables = schema.tables.filter((t) => relatedTableNames.has(t.name));\n\n // Collect foreign key columns for FK markers\n const fkColumns = this.collectForeignKeyColumns(relevantRelations);\n\n const lines: string[] = [\"erDiagram\"];\n\n // Generate relations\n for (const relation of relevantRelations) {\n const relationLine = this.formatRelation(relation, relevantTables);\n if (relationLine) {\n lines.push(` ${relationLine}`);\n }\n }\n\n // Add blank line between relations and tables if there are both\n if (relevantRelations.length > 0 && relevantTables.length > 0) {\n lines.push(\"\");\n }\n\n // Generate tables\n for (const table of relevantTables) {\n const tableLines = this.formatTable(table, fkColumns);\n lines.push(...tableLines);\n }\n\n return lines.join(\"\\n\");\n }\n\n /**\n * Collect foreign key columns from relations for FK marker assignment\n */\n private collectForeignKeyColumns(relations: RelationDefinition[]): Map<string, Set<string>> {\n const fkColumns = new Map<string, Set<string>>();\n\n for (const relation of relations) {\n // The \"from\" side of the relation has the FK column\n if (!fkColumns.has(relation.fromTable)) {\n fkColumns.set(relation.fromTable, new Set());\n }\n for (const col of relation.fromColumns) {\n fkColumns.get(relation.fromTable)!.add(col);\n }\n }\n\n return fkColumns;\n }\n\n /**\n * Check if a foreign key is nullable\n *\n * A foreign key is nullable if all of its columns are nullable\n */\n private isForeignKeyNullable(relation: RelationDefinition, tables: TableDefinition[]): boolean {\n // Find the table that contains the foreign key columns\n const fromTable = tables.find((t) => t.name === relation.fromTable);\n if (!fromTable) {\n return false;\n }\n\n // Check if all foreign key columns are nullable\n return relation.fromColumns.every((colName) => {\n const column = fromTable.columns.find((c) => c.name === colName);\n return column?.nullable ?? false;\n });\n }\n\n /**\n * Format a table definition to Mermaid syntax\n */\n private formatTable(table: TableDefinition, fkColumns: Map<string, Set<string>>): string[] {\n const lines: string[] = [];\n const tableName = this.escapeName(table.name);\n\n // If columns are not included, return table name only (without braces)\n if (!this.options.includeColumns) {\n lines.push(` ${tableName}`);\n return lines;\n }\n\n const tableFkColumns = fkColumns.get(table.name) || new Set();\n\n lines.push(` ${tableName} {`);\n\n for (const column of table.columns) {\n const columnLine = this.formatColumn(column, tableFkColumns);\n lines.push(` ${columnLine}`);\n }\n\n lines.push(\" }\");\n\n return lines;\n }\n\n /**\n * Format a column definition to Mermaid syntax\n */\n private formatColumn(column: ColumnDefinition, fkColumns: Set<string>): string {\n const parts: string[] = [];\n\n // Column type (simplified for Mermaid)\n if (this.options.includeColumnTypes) {\n parts.push(this.simplifyType(column.type));\n }\n\n // Column name\n parts.push(this.escapeName(column.name));\n\n // PK/FK markers\n const markers: string[] = [];\n if (column.primaryKey) {\n markers.push(\"PK\");\n }\n if (fkColumns.has(column.name)) {\n markers.push(\"FK\");\n }\n if (column.unique && !column.primaryKey) {\n markers.push(\"UK\");\n }\n\n if (markers.length > 0) {\n parts.push(markers.join(\",\"));\n }\n\n // Add comment as Mermaid comment (quoted string after markers)\n if (this.options.includeComments && column.comment) {\n parts.push(`\"${this.escapeString(column.comment)}\"`);\n }\n\n return parts.join(\" \");\n }\n\n /**\n * Simplify SQL type for Mermaid display\n *\n * Mermaid ER diagrams work best with simple type names\n */\n private simplifyType(type: string): string {\n // Remove parentheses content for cleaner display (e.g., varchar(255) -> varchar)\n const simplified = type.replace(/\\([^)]*\\)/g, \"\").toLowerCase();\n\n // Map common types to shorter versions\n const typeMap: Record<string, string> = {\n integer: \"int\",\n bigint: \"bigint\",\n smallint: \"smallint\",\n serial: \"serial\",\n bigserial: \"bigserial\",\n text: \"text\",\n varchar: \"varchar\",\n char: \"char\",\n boolean: \"boolean\",\n timestamp: \"timestamp\",\n timestamptz: \"timestamptz\",\n \"timestamp with time zone\": \"timestamptz\",\n \"time with time zone\": \"timetz\",\n date: \"date\",\n time: \"time\",\n timetz: \"timetz\",\n json: \"json\",\n jsonb: \"jsonb\",\n uuid: \"uuid\",\n real: \"real\",\n float: \"float\",\n double: \"double\",\n \"double precision\": \"double\",\n decimal: \"decimal\",\n numeric: \"numeric\",\n blob: \"blob\",\n bytea: \"bytea\",\n // MySQL types\n tinyint: \"tinyint\",\n mediumint: \"mediumint\",\n // MySQL unsigned types (u-prefix for distinction)\n \"tinyint unsigned\": \"utinyint\",\n \"smallint unsigned\": \"usmallint\",\n \"mediumint unsigned\": \"umediumint\",\n \"int unsigned\": \"uint\",\n \"bigint unsigned\": \"ubigint\",\n \"float unsigned\": \"ufloat\",\n \"double unsigned\": \"udouble\",\n };\n\n return typeMap[simplified] || simplified;\n }\n\n /**\n * Format a relation definition to Mermaid syntax\n *\n * Mermaid ER diagram relation syntax:\n * - ||--|| : one-to-one\n * - ||--o| : one-to-one, but target is optional\n * - ||--o{ : one-to-many\n * - }o--|| : many-to-one\n * - }o--o| : many-to-one, but source is optional\n * - }o--o{ : many-to-many\n */\n private formatRelation(relation: RelationDefinition, tables: TableDefinition[]): string {\n const fromTable = this.escapeName(relation.fromTable);\n const toTable = this.escapeName(relation.toTable);\n\n // Check if the foreign key columns are nullable\n const isForeignKeyNullable = this.isForeignKeyNullable(relation, tables);\n const symbol = this.getRelationSymbol(relation.type, isForeignKeyNullable);\n const label = relation.fromColumns.join(\", \");\n\n return `${fromTable} ${symbol} ${toTable} : \"${label}\"`;\n }\n\n /**\n * Convert IntermediateRelationType to Mermaid relation symbol\n *\n * @param type - The relation type\n * @param isForeignKeyNullable - Whether the foreign key column(s) are nullable\n *\n * @see https://mermaid.js.org/syntax/entityRelationshipDiagram.html#relationship-syntax\n */\n private getRelationSymbol(\n type: RelationDefinition[\"type\"],\n isForeignKeyNullable: boolean = false,\n ): string {\n switch (type) {\n case \"one-to-one\":\n return isForeignKeyNullable ? \"||--o|\" : \"||--||\";\n case \"one-to-many\":\n return \"||--o{\";\n case \"many-to-one\":\n return isForeignKeyNullable ? \"}o--o|\" : \"}o--||\";\n case \"many-to-many\":\n return \"}o--o{\";\n }\n }\n\n /**\n * Escape a name for Mermaid if it contains special characters\n *\n * Mermaid entity names should be alphanumeric with underscores\n */\n private escapeName(name: string): string {\n // Mermaid accepts alphanumeric and underscores\n // For names with special characters, we need to quote them\n if (/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(name)) {\n return name;\n }\n // Replace special characters with underscores for Mermaid compatibility\n return name.replace(/[^a-zA-Z0-9_]/g, \"_\");\n }\n\n /**\n * Escape a string for use in Mermaid quoted strings\n */\n private escapeString(str: string): string {\n return str.replace(/\\\\/g, \"\\\\\\\\\").replace(/\"/g, '\\\\\"').replace(/\\n/g, \" \");\n }\n}\n"],"names":["DEFAULT_OPTIONS","MermaidErDiagramFormatter","options","schema","lines","fkColumns","relation","relationLine","table","tableLines","tableName","t","relatedTableNames","relevantRelations","relevantTables","relations","col","tables","fromTable","colName","c","tableFkColumns","column","columnLine","parts","markers","type","simplified","toTable","isForeignKeyNullable","symbol","label","name","str"],"mappings":"AA4BA,MAAMA,IAAqD;AAAA,EACzD,iBAAiB;AAAA,EACjB,gBAAgB;AAAA,EAChB,oBAAoB;AAAA,EACpB,oBAAoB;AAAA,EACpB,gBAAgB;AAClB;AAuBO,MAAMC,EAAqD;AAAA,EACxD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOR,YAAYC,IAA4C,IAAI;AAC1D,SAAK,UAAU,EAAE,GAAGF,GAAiB,GAAGE,EAAA;AAAA,EAC1C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,OAAOC,GAAoC;AACzC,UAAMC,IAAkB,CAAC,WAAW,GAG9BC,IAAY,KAAK,yBAAyBF,EAAO,SAAS;AAGhE,eAAWG,KAAYH,EAAO,WAAW;AACvC,YAAMI,IAAe,KAAK,eAAeD,GAAUH,EAAO,MAAM;AAChE,MAAII,KACFH,EAAM,KAAK,OAAOG,CAAY,EAAE;AAAA,IAEpC;AAGA,IAAIJ,EAAO,UAAU,SAAS,KAAKA,EAAO,OAAO,SAAS,KACxDC,EAAM,KAAK,EAAE;AAIf,eAAWI,KAASL,EAAO,QAAQ;AACjC,YAAMM,IAAa,KAAK,YAAYD,GAAOH,CAAS;AACpD,MAAAD,EAAM,KAAK,GAAGK,CAAU;AAAA,IAC1B;AAEA,WAAOL,EAAM,KAAK;AAAA,CAAI;AAAA,EACxB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,cAAcD,GAA4BO,GAA2B;AAGnE,QAAI,C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;;;;;OAKG;IACH,SAAS,CAAC,4BAA4B,CACpC,WAAW,EAAE,WAAW,GAAG,SAAS,GACnC,oBAAoB,EAAE;IA0CzB;;;;;OAKG;IACH,SAAS,CAAC,4BAA4B,CAAC,EAAE,EAAE,gBAAgB,GAAG,oBAAoB;IAelF;;;;;OAKG;IACH,SAAS,CAAC,uBAAuB,CAAC,GAAG,EAAE,YAAY,GAAG,kBAAkB;IA4BxE;;;;;;OAMG;IACH,SAAS,CAAC,sBAAsB,IAAI,cAAc,EAAE;CAKrD;AAED;;;;;;;;GAQG;AACH,wBAAgB,aAAa,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,IAAI,CAKrE"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"common.js","sources":["../../src/generator/common.ts"],"sourcesContent":["import {\n type AnyColumn,\n type Table,\n getTableColumns,\n getTableName,\n is,\n Relation,\n} from \"drizzle-orm\";\nimport type { TableRelationalConfig } from \"drizzle-orm/relations\";\nimport { PgTable, getTableConfig as getPgTableConfig } from \"drizzle-orm/pg-core\";\nimport { MySqlTable, getTableConfig as getMySqlTableConfig } from \"drizzle-orm/mysql-core\";\nimport { SQLiteTable, getTableConfig as getSqliteTableConfig } from \"drizzle-orm/sqlite-core\";\nimport { writeFileSync, mkdirSync } from \"node:fs\";\nimport { dirname, resolve } from \"node:path\";\nimport { DbmlFormatter } from \"../formatter/dbml\";\n\nimport type {\n GeneratedRef,\n GenerateOptions,\n IntermediateSchema,\n TableDefinition,\n ColumnDefinition,\n IndexDefinition,\n ConstraintDefinition,\n RelationDefinition,\n EnumDefinition,\n DatabaseType,\n IntermediateRelationType,\n} from \"../types\";\nimport { extractComments, type SchemaComments } from \"../parser/comments\";\nimport { extractRelations, type SchemaRelations } from \"../parser/relations\";\nimport { V0RelationAdapter } from \"../adapter/v0-adapter\";\nimport { V1RelationAdapter } from \"../adapter/v1-adapter\";\nimport type { UnifiedRelation } from \"../adapter/types\";\n\n/**\n * Configuration for different database dialects\n */\nexport interface DialectConfig {\n escapeName: (name: string) => string;\n isIncrement: (column: AnyColumn) => boolean;\n}\n\n/**\n * Configuration for an index definition\n */\ninterface IndexConfig {\n config: {\n columns: Array<{ name: string }>;\n name?: string;\n unique: boolean;\n using?: string;\n };\n}\n\n/**\n * Configuration for a primary key constraint\n */\ninterface PrimaryKeyConfig {\n columns: Array<{ name: string }>;\n name?: string;\n}\n\n/**\n * Configuration for a unique constraint\n */\ninterface UniqueConstraintConfig {\n columns: Array<{ name: string }>;\n name?: string;\n}\n\n/**\n * Configuration for a foreign key constraint\n */\nexport interface ForeignKeyConfig {\n reference: () => {\n columns: Array<{ name: string }>;\n foreignColumns: Array<{ name: string }>;\n foreignTable: Table;\n };\n name?: string;\n onDelete?: string;\n onUpdate?: string;\n}\n\n/**\n * Table configuration extracted from Drizzle tables\n */\nexport interface TableConfig {\n indexes: IndexConfig[];\n primaryKeys: PrimaryKeyConfig[];\n uniqueConstraints: UniqueConstraintConfig[];\n foreignKeys: ForeignKeyConfig[];\n}\n\n/**\n * Base generator class for DBML generation\n */\nexport abstract class BaseGenerator<\n TSchema extends Record<string, unknown> = Record<string, unknown>,\n> {\n protected schema: TSchema;\n protected generatedRefs: GeneratedRef[] = [];\n protected comments: SchemaComments | undefined;\n protected parsedRelations: SchemaRelations | undefined;\n protected source: string | undefined;\n protected abstract dialectConfig: DialectConfig;\n\n /**\n * Create a new generator instance\n * @param options - Configuration options including schema, source code, and comments\n */\n constructor(options: GenerateOptions<TSchema>) {\n this.schema = options.schema;\n this.source = options.source;\n\n // Initialize comments from options\n if (options.comments) {\n this.comments = options.comments;\n } else if (this.source) {\n this.comments = extractComments(this.source);\n }\n\n // Extract relations from source for v0 API detection\n if (this.source) {\n this.parsedRelations = extractRelations(this.source);\n }\n }\n\n /**\n * Generate complete DBML output from the schema\n *\n * Creates DBML representation including:\n * - Table definitions with columns, indexes, and constraints\n * - Foreign key references (from table config or relations)\n * - Comments for tables and columns\n *\n * @returns The complete DBML string\n */\n generate(): string {\n const schema = this.toIntermediateSchema();\n const formatter = new DbmlFormatter();\n return formatter.format(schema);\n }\n\n /**\n * Get all tables from schema\n *\n * Extracts all Drizzle table objects from the schema by checking each value\n * with isTable() method.\n *\n * @returns Array of table objects\n */\n protected getTables(): Table[] {\n const tables: Table[] = [];\n for (const value of Object.values(this.schema)) {\n if (this.isTable(value)) {\n tables.push(value as Table);\n }\n }\n return tables;\n }\n\n /**\n * Check if a value is a Drizzle table\n *\n * Validates whether a value is a table instance from any supported dialect\n * (PostgreSQL, MySQL, or SQLite).\n *\n * @param value - The value to check\n * @returns True if value is a Drizzle table\n */\n protected isTable(value: unknown): boolean {\n return is(value, PgTable) || is(value, MySqlTable) || is(value, SQLiteTable);\n }\n\n /**\n * Check if a value is a v1 relation entry (from defineRelations())\n *\n * Uses official Drizzle v1 types: TableRelationalConfig with Relation instances.\n * Validates the structure has 'table', 'name', and 'relations' properties with valid types.\n *\n * @param value - The value to check\n * @returns True if value is a v1 relation entry\n */\n protected isV1RelationEntry(value: unknown): value is TableRelationalConfig {\n if (typeof value !== \"object\" || value === null) {\n return false;\n }\n const obj = value as Record<string, unknown>;\n if (\n !(\"table\" in obj) ||\n !(\"name\" in obj) ||\n typeof obj.name !== \"string\" ||\n !(\"relations\" in obj) ||\n typeof obj.relations !== \"object\" ||\n obj.relations === null\n ) {\n return false;\n }\n // Check that 'relations' contains Relation instances (using drizzle-orm is())\n const relations = obj.relations as Record<string, unknown>;\n const relationValues = Object.values(relations);\n // Empty relations object is valid, but if there are entries, they must be Relations\n if (relationValues.length > 0) {\n return relationValues.some((rel) => is(rel, Relation));\n }\n // If no relations defined, also check table is valid\n return this.isTable(obj.table);\n }\n\n /**\n * Get all v1 relation entries from schema (defineRelations() API)\n *\n * Handles both individual entries and the full defineRelations() result object.\n * Extracts relation configurations that use the modern v1 API with official types.\n *\n * @returns Array of v1 relation entries\n */\n protected getV1RelationEntries(): TableRelationalConfig[] {\n const entries: TableRelationalConfig[] = [];\n for (const value of Object.values(this.schema)) {\n // Check if it's an individual relation entry\n if (this.isV1RelationEntry(value)) {\n entries.push(value);\n }\n // Check if it's the full defineRelations() result object\n // (an object where each value is a relation entry)\n else if (this.isV1DefineRelationsResult(value)) {\n for (const entry of Object.values(value as Record<string, unknown>)) {\n if (this.isV1RelationEntry(entry)) {\n entries.push(entry);\n }\n }\n }\n }\n return entries;\n }\n\n /**\n * Check if a value is a full v1 defineRelations() result object\n *\n * This is an object where all values are TableRelationalConfig objects.\n * Used to detect the full result of calling defineRelations() in v1.\n *\n * @param value - The value to check\n * @returns True if value is a full defineRelations() result\n */\n protected isV1DefineRelationsResult(value: unknown): boolean {\n if (typeof value !== \"object\" || value === null || Array.isArray(value)) {\n return false;\n }\n const obj = value as Record<string, unknown>;\n const values = Object.values(obj);\n // Must have at least one entry and all must be relation entries\n return values.length > 0 && values.every((v) => this.isV1RelationEntry(v));\n }\n\n /**\n * Get table configuration from a Drizzle table\n *\n * Extracts indexes, primary keys, unique constraints, and foreign keys\n * from the table using the appropriate dialect-specific config getter.\n *\n * @param table - The Drizzle table to get configuration from\n * @returns Table configuration or undefined if dialect is not supported\n */\n protected getTableConfig(table: Table): TableConfig | undefined {\n // Detect dialect and use appropriate config getter\n // Cast is required at boundary due to Drizzle ORM's internal SQL<unknown> | Column union types\n if (is(table, PgTable)) {\n const config = getPgTableConfig(table);\n return this.mapTableConfig(config as Parameters<typeof this.mapTableConfig>[0]);\n }\n if (is(table, MySqlTable)) {\n const config = getMySqlTableConfig(table);\n return this.mapTableConfig(config as Parameters<typeof this.mapTableConfig>[0]);\n }\n if (is(table, SQLiteTable)) {\n const config = getSqliteTableConfig(table);\n return this.mapTableConfig(config as Parameters<typeof this.mapTableConfig>[0]);\n }\n return undefined;\n }\n\n /**\n * Map Drizzle's internal table config to our typed TableConfig\n *\n * Note: Drizzle ORM's columns type is `SQL<unknown> | IndexedColumn` union,\n * so we use `{ name?: string }` and cast to access the name property.\n * This is unavoidable due to Drizzle's internal type structure.\n */\n private mapTableConfig(config: {\n indexes: Array<{\n config: {\n columns: Array<{ name?: string }>;\n name?: string;\n unique?: boolean;\n using?: string;\n };\n }>;\n primaryKeys: Array<{ columns: Array<{ name?: string }>; name?: string }>;\n uniqueConstraints: Array<{ columns: Array<{ name?: string }>; name?: string }>;\n foreignKeys: Array<{\n reference: () => {\n columns: Array<{ name: string }>;\n foreignColumns: Array<{ name: string }>;\n foreignTable: Table;\n };\n name?: string;\n onDelete?: string;\n onUpdate?: string;\n }>;\n }): TableConfig {\n return {\n indexes: (config.indexes || []).map((idx) => ({\n config: {\n columns: idx.config.columns\n .filter((c): c is { name: string } => typeof c.name === \"string\")\n .map((c) => ({ name: c.name })),\n name: idx.config.name,\n unique: idx.config.unique ?? false,\n using: idx.config.using,\n },\n })),\n primaryKeys: (config.primaryKeys || []).map((pk) => ({\n columns: pk.columns\n .filter((c): c is { name: string } => typeof c.name === \"string\")\n .map((c) => ({ name: c.name })),\n name: pk.name,\n })),\n uniqueConstraints: (config.uniqueConstraints || []).map((uc) => ({\n columns: uc.columns\n .filter((c): c is { name: string } => typeof c.name === \"string\")\n .map((c) => ({ name: c.name })),\n name: uc.name,\n })),\n foreignKeys: (config.foreignKeys || []).map((fk) => ({\n reference: fk.reference,\n name: fk.name,\n onDelete: fk.onDelete,\n onUpdate: fk.onUpdate,\n })),\n };\n }\n\n /**\n * Get the default value for a column\n *\n * Extracts and formats the default value from a column, handling SQL expressions,\n * objects, and primitive values. Returns undefined if no default value exists.\n *\n * @param column - The column to get the default value from\n * @returns Formatted default value string or undefined\n */\n protected getDefaultValue(column: AnyColumn): string | undefined {\n if (!column.hasDefault) {\n return undefined;\n }\n\n const defaultValue = column.default;\n\n if (defaultValue === null) {\n return \"null\";\n }\n\n if (defaultValue === undefined) {\n return undefined;\n }\n\n // Handle SQL expressions\n if (typeof defaultValue === \"object\" && defaultValue !== null) {\n if (\"queryChunks\" in defaultValue) {\n // It's a SQL template\n const chunks = (defaultValue as { queryChunks: unknown[] }).queryChunks;\n const sqlParts: string[] = [];\n for (const chunk of chunks) {\n if (typeof chunk === \"string\") {\n sqlParts.push(chunk);\n } else if (typeof chunk === \"object\" && chunk !== null && \"value\" in chunk) {\n sqlParts.push(String((chunk as { value: unknown }).value));\n }\n }\n return sqlParts.join(\"\");\n }\n if (\"sql\" in defaultValue) {\n return (defaultValue as { sql: string }).sql;\n }\n // JSON object\n return JSON.stringify(defaultValue);\n }\n\n // Handle primitives\n if (typeof defaultValue === \"string\") {\n // Use '' escape (SQL standard)\n const escaped = defaultValue.replace(/'/g, \"''\");\n return `'${escaped}'`;\n }\n if (typeof defaultValue === \"number\" || typeof defaultValue === \"boolean\") {\n return String(defaultValue);\n }\n\n return undefined;\n }\n\n /**\n * Collect foreign keys from table configuration\n *\n * Parses all foreign key definitions from the table config and adds them\n * to the generatedRefs collection for later output.\n *\n * @param tableName - The name of the source table\n * @param foreignKeys - Array of foreign key definitions from table config\n */\n protected collectForeignKeysFromConfig(tableName: string, foreignKeys: ForeignKeyConfig[]): void {\n for (const fk of foreignKeys) {\n const ref = this.parseForeignKey(tableName, fk);\n this.generatedRefs.push(ref);\n }\n }\n\n /**\n * Parse a foreign key into a GeneratedRef\n *\n * Extracts foreign key information (source/target tables and columns, actions)\n * and converts it to a GeneratedRef object for DBML output.\n *\n * @param tableName - The name of the source table\n * @param fk - The foreign key definition to parse\n * @returns GeneratedRef object\n */\n protected parseForeignKey(tableName: string, fk: ForeignKeyConfig): GeneratedRef {\n const reference = fk.reference();\n const fromColumns = reference.columns.map((c) => c.name);\n const toColumns = reference.foreignColumns.map((c) => c.name);\n const toTable = getTableName(reference.foreignTable);\n\n return {\n fromTable: tableName,\n fromColumns,\n toTable,\n toColumns,\n type: \">\",\n onDelete: fk.onDelete,\n onUpdate: fk.onUpdate,\n };\n }\n\n /**\n * Detect if relation definitions are present in the schema\n *\n * Checks for both v1 (defineRelations()) and v0 (relations()) API usage.\n * Returns true if any relation definitions are found.\n *\n * @returns True if relations are defined, false otherwise\n */\n private hasRelationDefinitions(): boolean {\n // Check for v1 relation entries (defineRelations() API)\n const v1Entries = this.getV1RelationEntries();\n if (v1Entries.length > 0) {\n return true;\n }\n\n // Check for v0 relation definitions (relations() API)\n if (this.parsedRelations && this.parsedRelations.relations.length > 0) {\n return true;\n }\n\n return false;\n }\n\n /**\n * Create the appropriate relation adapter based on schema contents\n *\n * Detects whether v1 or v0 relations are present and returns the\n * corresponding adapter implementation.\n *\n * @returns RelationAdapter instance (V1RelationAdapter or V0RelationAdapter)\n */\n private createRelationAdapter() {\n const v1Entries = this.getV1RelationEntries();\n if (v1Entries.length > 0) {\n return new V1RelationAdapter(v1Entries);\n }\n return new V0RelationAdapter(this.schema, this.parsedRelations);\n }\n\n /**\n * Convert a UnifiedRelation to a RelationDefinition\n *\n * Maps the unified relation format from adapters to the intermediate\n * schema's RelationDefinition format.\n *\n * @param unified - The unified relation from adapter\n * @returns RelationDefinition for intermediate schema\n */\n private unifiedRelationToDefinition(unified: UnifiedRelation): RelationDefinition {\n return {\n fromTable: unified.sourceTable,\n fromColumns: unified.sourceColumns,\n toTable: unified.targetTable,\n toColumns: unified.targetColumns,\n type: unified.relationType,\n onDelete: unified.onDelete,\n onUpdate: unified.onUpdate,\n };\n }\n\n /**\n * Convert the Drizzle schema to an intermediate schema representation\n *\n * Creates a database-agnostic intermediate representation that can be\n * used to generate various output formats (DBML, Markdown, JSON, etc.)\n *\n * @returns The intermediate schema representation\n */\n toIntermediateSchema(): IntermediateSchema {\n const tables = this.getTables();\n\n // Determine database type from the first table\n const databaseType = this.getDatabaseType(tables[0]);\n\n // Convert tables to intermediate format\n const tableDefinitions: TableDefinition[] = tables.map((table) =>\n this.tableToDefinition(table),\n );\n\n // Collect relations (auto-detect relation definitions vs foreign keys)\n let relations: RelationDefinition[] = [];\n\n if (this.hasRelationDefinitions()) {\n // Use adapter to extract relations in unified format (v1 or v0 API)\n const adapter = this.createRelationAdapter();\n const unifiedRelations = adapter.extract();\n relations = unifiedRelations.map((unified) => this.unifiedRelationToDefinition(unified));\n } else {\n // Fall back to foreign keys from table configs\n // Reset generatedRefs to collect fresh relations\n this.generatedRefs = [];\n for (const table of tables) {\n const tableName = getTableName(table);\n const tableConfig = this.getTableConfig(table);\n if (tableConfig && tableConfig.foreignKeys.length > 0) {\n this.collectForeignKeysFromConfig(tableName, tableConfig.foreignKeys);\n }\n }\n // Convert GeneratedRefs to RelationDefinitions\n relations = this.generatedRefs.map((ref) => this.refToRelationDefinition(ref));\n }\n\n // Collect enums (override in subclasses for dialect-specific behavior)\n const enums: EnumDefinition[] = this.collectEnumDefinitions();\n\n return {\n databaseType,\n tables: tableDefinitions,\n relations,\n enums,\n };\n }\n\n /**\n * Determine the database type from a Drizzle table\n *\n * @param table - The table to check (can be undefined for empty schemas)\n * @returns The database type\n */\n protected getDatabaseType(table: Table | undefined): DatabaseType {\n if (!table) {\n // Default to postgresql if no tables\n return \"postgresql\";\n }\n if (is(table, PgTable)) {\n return \"postgresql\";\n }\n if (is(table, MySqlTable)) {\n return \"mysql\";\n }\n if (is(table, SQLiteTable)) {\n return \"sqlite\";\n }\n // Fallback\n return \"postgresql\";\n }\n\n /**\n * Convert a Drizzle table to a TableDefinition\n *\n * @param table - The Drizzle table to convert\n * @returns The table definition\n */\n protected tableToDefinition(table: Table): TableDefinition {\n const tableName = getTableName(table);\n const columns = getTableColumns(table);\n const tableConfig = this.getTableConfig(table);\n\n // Convert columns\n const columnDefinitions: ColumnDefinition[] = Object.values(columns).map((column) =>\n this.columnToDefinition(column, tableName),\n );\n\n // Convert indexes\n const indexDefinitions: IndexDefinition[] = this.extractIndexDefinitions(tableConfig);\n\n // Convert constraints\n const constraintDefinitions: ConstraintDefinition[] =\n this.extractConstraintDefinitions(tableConfig);\n\n // Get table comment\n const tableComment = this.comments?.tables[tableName]?.comment;\n\n return {\n name: tableName,\n comment: tableComment,\n columns: columnDefinitions,\n indexes: indexDefinitions,\n constraints: constraintDefinitions,\n };\n }\n\n /**\n * Convert a Drizzle column to a ColumnDefinition\n *\n * @param column - The Drizzle column to convert\n * @param tableName - The name of the table containing the column\n * @returns The column definition\n */\n protected columnToDefinition(column: AnyColumn, tableName: string): ColumnDefinition {\n const columnComment = this.comments?.tables[tableName]?.columns[column.name]?.comment;\n const defaultValue = this.getDefaultValue(column);\n\n return {\n name: column.name,\n type: column.getSQLType(),\n nullable: !column.notNull,\n defaultValue,\n primaryKey: column.primary,\n unique: column.isUnique,\n autoIncrement: this.dialectConfig.isIncrement(column) || undefined,\n comment: columnComment,\n };\n }\n\n /**\n * Extract index definitions from table config\n *\n * @param tableConfig - The table configuration\n * @returns Array of index definitions\n */\n protected extractIndexDefinitions(tableConfig: TableConfig | undefined): IndexDefinition[] {\n if (!tableConfig) {\n return [];\n }\n\n const indexes: IndexDefinition[] = [];\n\n for (const idx of tableConfig.indexes) {\n const columns = idx.config.columns.map((c) => c.name);\n if (columns.length > 0) {\n indexes.push({\n name: idx.config.name || `idx_${columns.join(\"_\")}`,\n columns,\n unique: idx.config.unique,\n type: idx.config.using,\n });\n }\n }\n\n return indexes;\n }\n\n /**\n * Extract constraint definitions from table config\n *\n * @param tableConfig - The table configuration\n * @returns Array of constraint definitions\n */\n protected extractConstraintDefinitions(\n tableConfig: TableConfig | undefined,\n ): ConstraintDefinition[] {\n if (!tableConfig) {\n return [];\n }\n\n const constraints: ConstraintDefinition[] = [];\n\n // Primary keys\n for (const pk of tableConfig.primaryKeys) {\n const columns = pk.columns.map((c) => c.name);\n if (columns.length > 0) {\n constraints.push({\n name: pk.name || `pk_${columns.join(\"_\")}`,\n type: \"primary_key\",\n columns,\n });\n }\n }\n\n // Unique constraints\n for (const uc of tableConfig.uniqueConstraints) {\n const columns = uc.columns.map((c) => c.name);\n if (columns.length > 0) {\n constraints.push({\n name: uc.name || `uq_${columns.join(\"_\")}`,\n type: \"unique\",\n columns,\n });\n }\n }\n\n // Foreign keys\n for (const fk of tableConfig.foreignKeys) {\n const fkDef = this.parseForeignKeyForConstraint(fk);\n if (fkDef) {\n constraints.push(fkDef);\n }\n }\n\n return constraints;\n }\n\n /**\n * Parse a foreign key into a ConstraintDefinition\n *\n * @param fk - The foreign key definition\n * @returns ConstraintDefinition\n */\n protected parseForeignKeyForConstraint(fk: ForeignKeyConfig): ConstraintDefinition {\n const reference = fk.reference();\n const columns = reference.columns.map((c) => c.name);\n const referencedColumns = reference.foreignColumns.map((c) => c.name);\n const referencedTable = getTableName(reference.foreignTable);\n\n return {\n name: fk.name || `fk_${columns.join(\"_\")}_${referencedTable}`,\n type: \"foreign_key\",\n columns,\n referencedTable,\n referencedColumns,\n };\n }\n\n /**\n * Convert a GeneratedRef to a RelationDefinition\n *\n * @param ref - The generated reference\n * @returns The relation definition\n */\n protected refToRelationDefinition(ref: GeneratedRef): RelationDefinition {\n // Map DBML ref type to IntermediateRelationType\n let relationType: IntermediateRelationType;\n switch (ref.type) {\n case \"-\":\n relationType = \"one-to-one\";\n break;\n case \">\":\n relationType = \"many-to-one\";\n break;\n case \"<\":\n relationType = \"one-to-many\";\n break;\n default:\n relationType = \"many-to-one\";\n }\n\n return {\n fromTable: ref.fromTable,\n fromColumns: ref.fromColumns,\n toTable: ref.toTable,\n toColumns: ref.toColumns,\n type: relationType,\n onDelete: ref.onDelete,\n onUpdate: ref.onUpdate,\n };\n }\n\n /**\n * Collect enum definitions from the schema\n *\n * Override in subclasses for dialect-specific enum handling (e.g., PostgreSQL)\n *\n * @returns Array of enum definitions (empty by default)\n */\n protected collectEnumDefinitions(): EnumDefinition[] {\n // Default implementation returns empty array\n // Override in PgGenerator for PostgreSQL enum support\n return [];\n }\n}\n\n/**\n * Write DBML content to a file\n *\n * Creates the directory if it doesn't exist and writes the DBML content\n * to the specified file path.\n *\n * @param filePath - The path to write the DBML file to\n * @param content - The DBML content to write\n */\nexport function writeDbmlFile(filePath: string, content: string): void {\n const resolvedPath = resolve(filePath);\n const dir = dirname(resolvedPath);\n mkdirSync(dir, { recursive: true });\n writeFileSync(resolvedPath, content, \"utf-8\");\n}\n"],"names":["BaseGenerator","options","extractComments","extractRelations","schema","DbmlFormatter","tables","value","is","PgTable","MySqlTable","SQLiteTable","obj","relations","relationValues","rel","Relation","entries","entry","values","v","table","config","getPgTableConfig","getMySqlTableConfig","getSqliteTableConfig","idx","c","pk","uc","fk","column","defaultValue","chunks","sqlParts","chunk","tableName","foreignKeys","ref","reference","fromColumns","toColumns","toTable","getTableName","v1Entries","V1RelationAdapter","V0RelationAdapter","unified","databaseType","tableDefinitions","tableConfig","enums","columns","getTableColumns","columnDefinitions","indexDefinitions","constraintDefinitions","tableComment","columnComment","indexes","constraints","fkDef","referencedColumns","referencedTable","relationType","writeDbmlFile","filePath","content","resolvedPath","resolve","dir","dirname","mkdirSync","writeFileSync"],"mappings":";;;;;;;;;;;AAkGO,MAAeA,EAEpB;AAAA,EACU;AAAA,EACA,gBAAgC,CAAA;AAAA,EAChC;AAAA,EACA;AAAA,EACA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOV,YAAYC,GAAmC;AAC7C,SAAK,SAASA,EAAQ,QACtB,KAAK,SAASA,EAAQ,QAGlBA,EAAQ,WACV,KAAK,WAAWA,EAAQ,WACf,KAAK,WACd,KAAK,WAAWC,EAAgB,KAAK,MAAM,IAIzC,KAAK,WACP,KAAK,kBAAkBC,EAAiB,KAAK,MAAM;AAAA,EAEvD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYA,WAAmB;AACjB,UAAMC,IAAS,KAAK,qBAAA;AAEpB,WADkB,IAAIC,EAAA,EACL,OAAOD,CAAM;AAAA,EAChC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUU,YAAqB;AAC7B,UAAME,IAAkB,CAAA;AACxB,eAAWC,KAAS,OAAO,OAAO,KAAK,MAAM;AAC3C,MAAI,KAAK,QAAQA,CAAK,KACpBD,EAAO,KAAKC,CAAc;AAG9B,WAAOD;AAAA,EAC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AAkBpB,EAAaJ,EAAU,YAAY;AAE3D,WAAO;AAAA,MACL,MAAMT,EAAG,QAAQ,MAAMsB,EAAQ,KAAK,GAAG,CAAC,IAAIW,CAAe;AAAA,MAC3D,MAAM;AAAA,MACN,SAAAX;AAAA,MACA,iBAAAW;AAAA,MACA,mBAAAD;AAAA,IAAA;AAAA,EAEJ;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQU,wBAAwBxB,GAAuC;AAEvE,QAAI0B;AACJ,YAAQ1B,EAAI,MAAA;AAAA,MACV,KAAK;AACH,QAAA0B,IAAe;AACf;AAAA,MACF,KAAK;AACH,QAAAA,IAAe;AACf;AAAA,MACF,KAAK;AACH,QAAAA,IAAe;AACf;AAAA,MACF;AACE,QAAAA,IAAe;AAAA,IAAA;AAGnB,WAAO;AAAA,MACL,WAAW1B,EAAI;AAAA,MACf,aAAaA,EAAI;AAAA,MACjB,SAASA,EAAI;AAAA,MACb,WAAWA,EAAI;AAAA,MACf,MAAM0B;AAAA,MACN,UAAU1B,EAAI;AAAA,MACd,UAAUA,EAAI;AAAA,IAAA;AAAA,EAElB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASU,yBAA2C;AAGnD,WAAO,CAAA;AAAA,EACT;AACF;AAWO,SAAS2B,EAAcC,GAAkBC,GAAuB;AACrE,QAAMC,IAAeC,EAAQH,CAAQ,GAC/BI,IAAMC,EAAQH,CAAY;AAChC,EAAAI,EAAUF,GAAK,EAAE,WAAW,GAAA,CAAM,GAClCG,EAAcL,GAAcD,GAAS,OAAO;AAC9C;"}
|
|
1
|
+
{"version":3,"file":"common.js","sources":["../../src/generator/common.ts"],"sourcesContent":["import {\n type AnyColumn,\n type Table,\n getTableColumns,\n getTableName,\n is,\n Relation,\n} from \"drizzle-orm\";\nimport type { TableRelationalConfig } from \"drizzle-orm/relations\";\nimport { PgTable, getTableConfig as getPgTableConfig } from \"drizzle-orm/pg-core\";\nimport { MySqlTable, getTableConfig as getMySqlTableConfig } from \"drizzle-orm/mysql-core\";\nimport { SQLiteTable, getTableConfig as getSqliteTableConfig } from \"drizzle-orm/sqlite-core\";\nimport { writeFileSync, mkdirSync } from \"node:fs\";\nimport { dirname, resolve } from \"node:path\";\nimport { DbmlFormatter } from \"../formatter/dbml\";\n\nimport type {\n GeneratedRef,\n GenerateOptions,\n IntermediateSchema,\n TableDefinition,\n ColumnDefinition,\n IndexDefinition,\n ConstraintDefinition,\n RelationDefinition,\n EnumDefinition,\n DatabaseType,\n IntermediateRelationType,\n} from \"../types\";\nimport { extractComments, type SchemaComments } from \"../parser/comments\";\nimport { extractRelations, type SchemaRelations } from \"../parser/relations\";\nimport { V0RelationAdapter } from \"../adapter/v0-adapter\";\nimport { V1RelationAdapter } from \"../adapter/v1-adapter\";\nimport type { UnifiedRelation } from \"../adapter/types\";\n\n/**\n * Configuration for different database dialects\n */\nexport interface DialectConfig {\n isIncrement: (column: AnyColumn) => boolean;\n}\n\n/**\n * Configuration for an index definition\n */\ninterface IndexConfig {\n config: {\n columns: Array<{ name: string }>;\n name?: string;\n unique: boolean;\n using?: string;\n };\n}\n\n/**\n * Configuration for a primary key constraint\n */\ninterface PrimaryKeyConfig {\n columns: Array<{ name: string }>;\n name?: string;\n}\n\n/**\n * Configuration for a unique constraint\n */\ninterface UniqueConstraintConfig {\n columns: Array<{ name: string }>;\n name?: string;\n}\n\n/**\n * Configuration for a foreign key constraint\n */\nexport interface ForeignKeyConfig {\n reference: () => {\n columns: Array<{ name: string }>;\n foreignColumns: Array<{ name: string }>;\n foreignTable: Table;\n };\n name?: string;\n onDelete?: string;\n onUpdate?: string;\n}\n\n/**\n * Table configuration extracted from Drizzle tables\n */\nexport interface TableConfig {\n indexes: IndexConfig[];\n primaryKeys: PrimaryKeyConfig[];\n uniqueConstraints: UniqueConstraintConfig[];\n foreignKeys: ForeignKeyConfig[];\n}\n\n/**\n * Base generator class for DBML generation\n */\nexport abstract class BaseGenerator<\n TSchema extends Record<string, unknown> = Record<string, unknown>,\n> {\n protected schema: TSchema;\n protected generatedRefs: GeneratedRef[] = [];\n protected comments: SchemaComments | undefined;\n protected parsedRelations: SchemaRelations | undefined;\n protected source: string | undefined;\n protected abstract dialectConfig: DialectConfig;\n\n /**\n * Create a new generator instance\n * @param options - Configuration options including schema, source code, and comments\n */\n constructor(options: GenerateOptions<TSchema>) {\n this.schema = options.schema;\n this.source = options.source;\n\n // Initialize comments from options\n if (options.comments) {\n this.comments = options.comments;\n } else if (this.source) {\n this.comments = extractComments(this.source);\n }\n\n // Extract relations from source for v0 API detection\n if (this.source) {\n this.parsedRelations = extractRelations(this.source);\n }\n }\n\n /**\n * Generate complete DBML output from the schema\n *\n * Creates DBML representation including:\n * - Table definitions with columns, indexes, and constraints\n * - Foreign key references (from table config or relations)\n * - Comments for tables and columns\n *\n * @returns The complete DBML string\n */\n generate(): string {\n const schema = this.toIntermediateSchema();\n const formatter = new DbmlFormatter();\n return formatter.format(schema);\n }\n\n /**\n * Get all tables from schema\n *\n * Extracts all Drizzle table objects from the schema by checking each value\n * with isTable() method.\n *\n * @returns Array of table objects\n */\n protected getTables(): Table[] {\n const tables: Table[] = [];\n for (const value of Object.values(this.schema)) {\n if (this.isTable(value)) {\n tables.push(value as Table);\n }\n }\n return tables;\n }\n\n /**\n * Check if a value is a Drizzle table\n *\n * Validates whether a value is a table instance from any supported dialect\n * (PostgreSQL, MySQL, or SQLite).\n *\n * @param value - The value to check\n * @returns True if value is a Drizzle table\n */\n protected isTable(value: unknown): boolean {\n return is(value, PgTable) || is(value, MySqlTable) || is(value, SQLiteTable);\n }\n\n /**\n * Check if a value is a v1 relation entry (from defineRelations())\n *\n * Uses official Drizzle v1 types: TableRelationalConfig with Relation instances.\n * Validates the structure has 'table', 'name', and 'relations' properties with valid types.\n *\n * @param value - The value to check\n * @returns True if value is a v1 relation entry\n */\n protected isV1RelationEntry(value: unknown): value is TableRelationalConfig {\n if (typeof value !== \"object\" || value === null) {\n return false;\n }\n const obj = value as Record<string, unknown>;\n if (\n !(\"table\" in obj) ||\n !(\"name\" in obj) ||\n typeof obj.name !== \"string\" ||\n !(\"relations\" in obj) ||\n typeof obj.relations !== \"object\" ||\n obj.relations === null\n ) {\n return false;\n }\n // Check that 'relations' contains Relation instances (using drizzle-orm is())\n const relations = obj.relations as Record<string, unknown>;\n const relationValues = Object.values(relations);\n // Empty relations object is valid, but if there are entries, they must be Relations\n if (relationValues.length > 0) {\n return relationValues.some((rel) => is(rel, Relation));\n }\n // If no relations defined, also check table is valid\n return this.isTable(obj.table);\n }\n\n /**\n * Get all v1 relation entries from schema (defineRelations() API)\n *\n * Handles both individual entries and the full defineRelations() result object.\n * Extracts relation configurations that use the modern v1 API with official types.\n *\n * @returns Array of v1 relation entries\n */\n protected getV1RelationEntries(): TableRelationalConfig[] {\n const entries: TableRelationalConfig[] = [];\n for (const value of Object.values(this.schema)) {\n // Check if it's an individual relation entry\n if (this.isV1RelationEntry(value)) {\n entries.push(value);\n }\n // Check if it's the full defineRelations() result object\n // (an object where each value is a relation entry)\n else if (this.isV1DefineRelationsResult(value)) {\n for (const entry of Object.values(value as Record<string, unknown>)) {\n if (this.isV1RelationEntry(entry)) {\n entries.push(entry);\n }\n }\n }\n }\n return entries;\n }\n\n /**\n * Check if a value is a full v1 defineRelations() result object\n *\n * This is an object where all values are TableRelationalConfig objects.\n * Used to detect the full result of calling defineRelations() in v1.\n *\n * @param value - The value to check\n * @returns True if value is a full defineRelations() result\n */\n protected isV1DefineRelationsResult(value: unknown): boolean {\n if (typeof value !== \"object\" || value === null || Array.isArray(value)) {\n return false;\n }\n const obj = value as Record<string, unknown>;\n const values = Object.values(obj);\n // Must have at least one entry and all must be relation entries\n return values.length > 0 && values.every((v) => this.isV1RelationEntry(v));\n }\n\n /**\n * Get table configuration from a Drizzle table\n *\n * Extracts indexes, primary keys, unique constraints, and foreign keys\n * from the table using the appropriate dialect-specific config getter.\n *\n * @param table - The Drizzle table to get configuration from\n * @returns Table configuration or undefined if dialect is not supported\n */\n protected getTableConfig(table: Table): TableConfig | undefined {\n // Detect dialect and use appropriate config getter\n // Cast is required at boundary due to Drizzle ORM's internal SQL<unknown> | Column union types\n if (is(table, PgTable)) {\n const config = getPgTableConfig(table);\n return this.mapTableConfig(config as Parameters<typeof this.mapTableConfig>[0]);\n }\n if (is(table, MySqlTable)) {\n const config = getMySqlTableConfig(table);\n return this.mapTableConfig(config as Parameters<typeof this.mapTableConfig>[0]);\n }\n if (is(table, SQLiteTable)) {\n const config = getSqliteTableConfig(table);\n return this.mapTableConfig(config as Parameters<typeof this.mapTableConfig>[0]);\n }\n return undefined;\n }\n\n /**\n * Map Drizzle's internal table config to our typed TableConfig\n *\n * Note: Drizzle ORM's columns type is `SQL<unknown> | IndexedColumn` union,\n * so we use `{ name?: string }` and cast to access the name property.\n * This is unavoidable due to Drizzle's internal type structure.\n */\n private mapTableConfig(config: {\n indexes: Array<{\n config: {\n columns: Array<{ name?: string }>;\n name?: string;\n unique?: boolean;\n using?: string;\n };\n }>;\n primaryKeys: Array<{ columns: Array<{ name?: string }>; name?: string }>;\n uniqueConstraints: Array<{ columns: Array<{ name?: string }>; name?: string }>;\n foreignKeys: Array<{\n reference: () => {\n columns: Array<{ name: string }>;\n foreignColumns: Array<{ name: string }>;\n foreignTable: Table;\n };\n name?: string;\n onDelete?: string;\n onUpdate?: string;\n }>;\n }): TableConfig {\n return {\n indexes: (config.indexes || []).map((idx) => ({\n config: {\n columns: idx.config.columns\n .filter((c): c is { name: string } => typeof c.name === \"string\")\n .map((c) => ({ name: c.name })),\n name: idx.config.name,\n unique: idx.config.unique ?? false,\n using: idx.config.using,\n },\n })),\n primaryKeys: (config.primaryKeys || []).map((pk) => ({\n columns: pk.columns\n .filter((c): c is { name: string } => typeof c.name === \"string\")\n .map((c) => ({ name: c.name })),\n name: pk.name,\n })),\n uniqueConstraints: (config.uniqueConstraints || []).map((uc) => ({\n columns: uc.columns\n .filter((c): c is { name: string } => typeof c.name === \"string\")\n .map((c) => ({ name: c.name })),\n name: uc.name,\n })),\n foreignKeys: (config.foreignKeys || []).map((fk) => ({\n reference: fk.reference,\n name: fk.name,\n onDelete: fk.onDelete,\n onUpdate: fk.onUpdate,\n })),\n };\n }\n\n /**\n * Get the default value for a column\n *\n * Extracts and formats the default value from a column, handling SQL expressions,\n * objects, and primitive values. Returns undefined if no default value exists.\n *\n * @param column - The column to get the default value from\n * @returns Formatted default value string or undefined\n */\n protected getDefaultValue(column: AnyColumn): string | undefined {\n if (!column.hasDefault) {\n return undefined;\n }\n\n const defaultValue = column.default;\n\n if (defaultValue === null) {\n return \"null\";\n }\n\n if (defaultValue === undefined) {\n return undefined;\n }\n\n // Handle SQL expressions\n if (typeof defaultValue === \"object\" && defaultValue !== null) {\n if (\"queryChunks\" in defaultValue) {\n // It's a SQL template\n const chunks = (defaultValue as { queryChunks: unknown[] }).queryChunks;\n const sqlParts: string[] = [];\n for (const chunk of chunks) {\n if (typeof chunk === \"string\") {\n sqlParts.push(chunk);\n } else if (typeof chunk === \"object\" && chunk !== null && \"value\" in chunk) {\n sqlParts.push(String((chunk as { value: unknown }).value));\n }\n }\n return sqlParts.join(\"\");\n }\n if (\"sql\" in defaultValue) {\n return (defaultValue as { sql: string }).sql;\n }\n // JSON object\n return JSON.stringify(defaultValue);\n }\n\n // Handle primitives\n if (typeof defaultValue === \"string\") {\n // Use '' escape (SQL standard)\n const escaped = defaultValue.replace(/'/g, \"''\");\n return `'${escaped}'`;\n }\n if (typeof defaultValue === \"number\" || typeof defaultValue === \"boolean\") {\n return String(defaultValue);\n }\n\n return undefined;\n }\n\n /**\n * Collect foreign keys from table configuration\n *\n * Parses all foreign key definitions from the table config and adds them\n * to the generatedRefs collection for later output.\n *\n * @param tableName - The name of the source table\n * @param foreignKeys - Array of foreign key definitions from table config\n */\n protected collectForeignKeysFromConfig(tableName: string, foreignKeys: ForeignKeyConfig[]): void {\n for (const fk of foreignKeys) {\n const ref = this.parseForeignKey(tableName, fk);\n this.generatedRefs.push(ref);\n }\n }\n\n /**\n * Parse a foreign key into a GeneratedRef\n *\n * Extracts foreign key information (source/target tables and columns, actions)\n * and converts it to a GeneratedRef object for DBML output.\n *\n * @param tableName - The name of the source table\n * @param fk - The foreign key definition to parse\n * @returns GeneratedRef object\n */\n protected parseForeignKey(tableName: string, fk: ForeignKeyConfig): GeneratedRef {\n const reference = fk.reference();\n const fromColumns = reference.columns.map((c) => c.name);\n const toColumns = reference.foreignColumns.map((c) => c.name);\n const toTable = getTableName(reference.foreignTable);\n\n return {\n fromTable: tableName,\n fromColumns,\n toTable,\n toColumns,\n type: \">\",\n onDelete: fk.onDelete,\n onUpdate: fk.onUpdate,\n };\n }\n\n /**\n * Detect if relation definitions are present in the schema\n *\n * Checks for both v1 (defineRelations()) and v0 (relations()) API usage.\n * Returns true if any relation definitions are found.\n *\n * @returns True if relations are defined, false otherwise\n */\n private hasRelationDefinitions(): boolean {\n // Check for v1 relation entries (defineRelations() API)\n const v1Entries = this.getV1RelationEntries();\n if (v1Entries.length > 0) {\n return true;\n }\n\n // Check for v0 relation definitions (relations() API)\n if (this.parsedRelations && this.parsedRelations.relations.length > 0) {\n return true;\n }\n\n return false;\n }\n\n /**\n * Create the appropriate relation adapter based on schema contents\n *\n * Detects whether v1 or v0 relations are present and returns the\n * corresponding adapter implementation.\n *\n * @returns RelationAdapter instance (V1RelationAdapter or V0RelationAdapter)\n */\n private createRelationAdapter() {\n const v1Entries = this.getV1RelationEntries();\n if (v1Entries.length > 0) {\n return new V1RelationAdapter(v1Entries);\n }\n return new V0RelationAdapter(this.schema, this.parsedRelations);\n }\n\n /**\n * Convert a UnifiedRelation to a RelationDefinition\n *\n * Maps the unified relation format from adapters to the intermediate\n * schema's RelationDefinition format.\n *\n * @param unified - The unified relation from adapter\n * @returns RelationDefinition for intermediate schema\n */\n private unifiedRelationToDefinition(unified: UnifiedRelation): RelationDefinition {\n return {\n fromTable: unified.sourceTable,\n fromColumns: unified.sourceColumns,\n toTable: unified.targetTable,\n toColumns: unified.targetColumns,\n type: unified.relationType,\n onDelete: unified.onDelete,\n onUpdate: unified.onUpdate,\n };\n }\n\n /**\n * Convert the Drizzle schema to an intermediate schema representation\n *\n * Creates a database-agnostic intermediate representation that can be\n * used to generate various output formats (DBML, Markdown, JSON, etc.)\n *\n * @returns The intermediate schema representation\n */\n toIntermediateSchema(): IntermediateSchema {\n const tables = this.getTables();\n\n // Determine database type from the first table\n const databaseType = this.getDatabaseType(tables[0]);\n\n // Convert tables to intermediate format\n const tableDefinitions: TableDefinition[] = tables.map((table) =>\n this.tableToDefinition(table),\n );\n\n // Collect relations (auto-detect relation definitions vs foreign keys)\n let relations: RelationDefinition[] = [];\n\n if (this.hasRelationDefinitions()) {\n // Use adapter to extract relations in unified format (v1 or v0 API)\n const adapter = this.createRelationAdapter();\n const unifiedRelations = adapter.extract();\n relations = unifiedRelations.map((unified) => this.unifiedRelationToDefinition(unified));\n } else {\n // Fall back to foreign keys from table configs\n // Reset generatedRefs to collect fresh relations\n this.generatedRefs = [];\n for (const table of tables) {\n const tableName = getTableName(table);\n const tableConfig = this.getTableConfig(table);\n if (tableConfig && tableConfig.foreignKeys.length > 0) {\n this.collectForeignKeysFromConfig(tableName, tableConfig.foreignKeys);\n }\n }\n // Convert GeneratedRefs to RelationDefinitions\n relations = this.generatedRefs.map((ref) => this.refToRelationDefinition(ref));\n }\n\n // Collect enums (override in subclasses for dialect-specific behavior)\n const enums: EnumDefinition[] = this.collectEnumDefinitions();\n\n return {\n databaseType,\n tables: tableDefinitions,\n relations,\n enums,\n };\n }\n\n /**\n * Determine the database type from a Drizzle table\n *\n * @param table - The table to check (can be undefined for empty schemas)\n * @returns The database type\n */\n protected getDatabaseType(table: Table | undefined): DatabaseType {\n if (!table) {\n // Default to postgresql if no tables\n return \"postgresql\";\n }\n if (is(table, PgTable)) {\n return \"postgresql\";\n }\n if (is(table, MySqlTable)) {\n return \"mysql\";\n }\n if (is(table, SQLiteTable)) {\n return \"sqlite\";\n }\n // Fallback\n return \"postgresql\";\n }\n\n /**\n * Convert a Drizzle table to a TableDefinition\n *\n * @param table - The Drizzle table to convert\n * @returns The table definition\n */\n protected tableToDefinition(table: Table): TableDefinition {\n const tableName = getTableName(table);\n const columns = getTableColumns(table);\n const tableConfig = this.getTableConfig(table);\n\n // Convert columns\n const columnDefinitions: ColumnDefinition[] = Object.values(columns).map((column) =>\n this.columnToDefinition(column, tableName),\n );\n\n // Convert indexes\n const indexDefinitions: IndexDefinition[] = this.extractIndexDefinitions(tableConfig);\n\n // Convert constraints\n const constraintDefinitions: ConstraintDefinition[] =\n this.extractConstraintDefinitions(tableConfig);\n\n // Get table comment\n const tableComment = this.comments?.tables[tableName]?.comment;\n\n return {\n name: tableName,\n comment: tableComment,\n columns: columnDefinitions,\n indexes: indexDefinitions,\n constraints: constraintDefinitions,\n };\n }\n\n /**\n * Convert a Drizzle column to a ColumnDefinition\n *\n * @param column - The Drizzle column to convert\n * @param tableName - The name of the table containing the column\n * @returns The column definition\n */\n protected columnToDefinition(column: AnyColumn, tableName: string): ColumnDefinition {\n const columnComment = this.comments?.tables[tableName]?.columns[column.name]?.comment;\n const defaultValue = this.getDefaultValue(column);\n\n return {\n name: column.name,\n type: column.getSQLType(),\n nullable: !column.notNull,\n defaultValue,\n primaryKey: column.primary,\n unique: column.isUnique,\n autoIncrement: this.dialectConfig.isIncrement(column) || undefined,\n comment: columnComment,\n };\n }\n\n /**\n * Extract index definitions from table config\n *\n * @param tableConfig - The table configuration\n * @returns Array of index definitions\n */\n protected extractIndexDefinitions(tableConfig: TableConfig | undefined): IndexDefinition[] {\n if (!tableConfig) {\n return [];\n }\n\n const indexes: IndexDefinition[] = [];\n\n for (const idx of tableConfig.indexes) {\n const columns = idx.config.columns.map((c) => c.name);\n if (columns.length > 0) {\n indexes.push({\n name: idx.config.name || `idx_${columns.join(\"_\")}`,\n columns,\n unique: idx.config.unique,\n type: idx.config.using,\n });\n }\n }\n\n return indexes;\n }\n\n /**\n * Extract constraint definitions from table config\n *\n * @param tableConfig - The table configuration\n * @returns Array of constraint definitions\n */\n protected extractConstraintDefinitions(\n tableConfig: TableConfig | undefined,\n ): ConstraintDefinition[] {\n if (!tableConfig) {\n return [];\n }\n\n const constraints: ConstraintDefinition[] = [];\n\n // Primary keys\n for (const pk of tableConfig.primaryKeys) {\n const columns = pk.columns.map((c) => c.name);\n if (columns.length > 0) {\n constraints.push({\n name: pk.name || `pk_${columns.join(\"_\")}`,\n type: \"primary_key\",\n columns,\n });\n }\n }\n\n // Unique constraints\n for (const uc of tableConfig.uniqueConstraints) {\n const columns = uc.columns.map((c) => c.name);\n if (columns.length > 0) {\n constraints.push({\n name: uc.name || `uq_${columns.join(\"_\")}`,\n type: \"unique\",\n columns,\n });\n }\n }\n\n // Foreign keys\n for (const fk of tableConfig.foreignKeys) {\n const fkDef = this.parseForeignKeyForConstraint(fk);\n if (fkDef) {\n constraints.push(fkDef);\n }\n }\n\n return constraints;\n }\n\n /**\n * Parse a foreign key into a ConstraintDefinition\n *\n * @param fk - The foreign key definition\n * @returns ConstraintDefinition\n */\n protected parseForeignKeyForConstraint(fk: ForeignKeyConfig): ConstraintDefinition {\n const reference = fk.reference();\n const columns = reference.columns.map((c) => c.name);\n const referencedColumns = reference.foreignColumns.map((c) => c.name);\n const referencedTable = getTableName(reference.foreignTable);\n\n return {\n name: fk.name || `fk_${columns.join(\"_\")}_${referencedTable}`,\n type: \"foreign_key\",\n columns,\n referencedTable,\n referencedColumns,\n };\n }\n\n /**\n * Convert a GeneratedRef to a RelationDefinition\n *\n * @param ref - The generated reference\n * @returns The relation definition\n */\n protected refToRelationDefinition(ref: GeneratedRef): RelationDefinition {\n // Map DBML ref type to IntermediateRelationType\n let relationType: IntermediateRelationType;\n switch (ref.type) {\n case \"-\":\n relationType = \"one-to-one\";\n break;\n case \">\":\n relationType = \"many-to-one\";\n break;\n case \"<\":\n relationType = \"one-to-many\";\n break;\n default:\n relationType = \"many-to-one\";\n }\n\n return {\n fromTable: ref.fromTable,\n fromColumns: ref.fromColumns,\n toTable: ref.toTable,\n toColumns: ref.toColumns,\n type: relationType,\n onDelete: ref.onDelete,\n onUpdate: ref.onUpdate,\n };\n }\n\n /**\n * Collect enum definitions from the schema\n *\n * Override in subclasses for dialect-specific enum handling (e.g., PostgreSQL)\n *\n * @returns Array of enum definitions (empty by default)\n */\n protected collectEnumDefinitions(): EnumDefinition[] {\n // Default implementation returns empty array\n // Override in PgGenerator for PostgreSQL enum support\n return [];\n }\n}\n\n/**\n * Write DBML content to a file\n *\n * Creates the directory if it doesn't exist and writes the DBML content\n * to the specified file path.\n *\n * @param filePath - The path to write the DBML file to\n * @param content - The DBML content to write\n */\nexport function writeDbmlFile(filePath: string, content: string): void {\n const resolvedPath = resolve(filePath);\n const dir = dirname(resolvedPath);\n mkdirSync(dir, { recursive: true });\n writeFileSync(resolvedPath, content, \"utf-8\");\n}\n"],"names":["BaseGenerator","options","extractComments","extractRelations","schema","DbmlFormatter","tables","value","is","PgTable","MySqlTable","SQLiteTable","obj","relations","relationValues","rel","Relation","entries","entry","values","v","table","config","getPgTableConfig","getMySqlTableConfig","getSqliteTableConfig","idx","c","pk","uc","fk","column","defaultValue","chunks","sqlParts","chunk","tableName","foreignKeys","ref","reference","fromColumns","toColumns","toTable","getTableName","v1Entries","V1RelationAdapter","V0RelationAdapter","unified","databaseType","tableDefinitions","tableConfig","enums","columns","getTableColumns","columnDefinitions","indexDefinitions","constraintDefinitions","tableComment","columnComment","indexes","constraints","fkDef","referencedColumns","referencedTable","relationType","writeDbmlFile","filePath","content","resolvedPath","resolve","dir","dirname","mkdirSync","writeFileSync"],"mappings":";;;;;;;;;;;AAiGO,MAAeA,EAEpB;AAAA,EACU;AAAA,EACA,gBAAgC,CAAA;AAAA,EAChC;AAAA,EACA;AAAA,EACA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOV,YAAYC,GAAmC;AAC7C,SAAK,SAASA,EAAQ,QACtB,KAAK,SAASA,EAAQ,QAGlBA,EAAQ,WACV,KAAK,WAAWA,E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;AAAA;AAAA;AAAA,EASU,yBAA2C;AAGnD,WAAO,CAAA;AAAA,EACT;AACF;AAWO,SAAS2B,EAAcC,GAAkBC,GAAuB;AACrE,QAAMC,IAAeC,EAAQH,CAAQ,GAC/BI,IAAMC,EAAQH,CAAY;AAChC,EAAAI,EAAUF,GAAK,EAAE,WAAW,GAAA,CAAM,GAClCG,EAAcL,GAAcD,GAAS,OAAO;AAC9C;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mysql.d.ts","sourceRoot":"","sources":["../../src/generator/mysql.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAiB,KAAK,aAAa,EAAE,MAAM,UAAU,CAAC;AAC5E,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAEhD;;;;;GAKG;AACH,qBAAa,cAAc,CACzB,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CACjE,SAAQ,aAAa,CAAC,OAAO,CAAC;IAC9B,SAAS,CAAC,aAAa,EAAE,aAAa,
|
|
1
|
+
{"version":3,"file":"mysql.d.ts","sourceRoot":"","sources":["../../src/generator/mysql.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAiB,KAAK,aAAa,EAAE,MAAM,UAAU,CAAC;AAC5E,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAEhD;;;;;GAKG;AACH,qBAAa,cAAc,CACzB,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CACjE,SAAQ,aAAa,CAAC,OAAO,CAAC;IAC9B,SAAS,CAAC,aAAa,EAAE,aAAa,CAKpC;CACH;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,aAAa,CAAC,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACnE,OAAO,EAAE,eAAe,CAAC,OAAO,CAAC,GAChC,MAAM,CASR"}
|
package/dist/generator/mysql.js
CHANGED
|
@@ -1,16 +1,15 @@
|
|
|
1
1
|
import { BaseGenerator as n, writeDbmlFile as a } from "./common.js";
|
|
2
2
|
class o extends n {
|
|
3
3
|
dialectConfig = {
|
|
4
|
-
escapeName: (r) => `\`${r}\``,
|
|
5
4
|
isIncrement: (r) => r.autoIncrement === !0
|
|
6
5
|
};
|
|
7
6
|
}
|
|
8
|
-
function
|
|
7
|
+
function l(e) {
|
|
9
8
|
const t = new o(e).generate();
|
|
10
9
|
return e.out && a(e.out, t), t;
|
|
11
10
|
}
|
|
12
11
|
export {
|
|
13
12
|
o as MySqlGenerator,
|
|
14
|
-
|
|
13
|
+
l as mysqlGenerate
|
|
15
14
|
};
|
|
16
15
|
//# sourceMappingURL=mysql.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mysql.js","sources":["../../src/generator/mysql.ts"],"sourcesContent":["import type { AnyColumn } from \"drizzle-orm\";\nimport { BaseGenerator, writeDbmlFile, type DialectConfig } from \"./common\";\nimport type { GenerateOptions } from \"../types\";\n\n/**\n * MySQL-specific DBML generator\n *\n * Supports JSDoc comment extraction via `sourceFile` or `comments` options.\n * Comments are included as DBML Note clauses for tables and columns.\n */\nexport class MySqlGenerator<\n TSchema extends Record<string, unknown> = Record<string, unknown>,\n> extends BaseGenerator<TSchema> {\n protected dialectConfig: DialectConfig = {\n
|
|
1
|
+
{"version":3,"file":"mysql.js","sources":["../../src/generator/mysql.ts"],"sourcesContent":["import type { AnyColumn } from \"drizzle-orm\";\nimport { BaseGenerator, writeDbmlFile, type DialectConfig } from \"./common\";\nimport type { GenerateOptions } from \"../types\";\n\n/**\n * MySQL-specific DBML generator\n *\n * Supports JSDoc comment extraction via `sourceFile` or `comments` options.\n * Comments are included as DBML Note clauses for tables and columns.\n */\nexport class MySqlGenerator<\n TSchema extends Record<string, unknown> = Record<string, unknown>,\n> extends BaseGenerator<TSchema> {\n protected dialectConfig: DialectConfig = {\n isIncrement: (column: AnyColumn) => {\n // MySQL uses autoincrement property\n return (column as unknown as { autoIncrement?: boolean }).autoIncrement === true;\n },\n };\n}\n\n/**\n * Generate DBML from a MySQL Drizzle schema\n *\n * @example\n * ```ts\n * import { mysqlTable, serial, text, varchar } from \"drizzle-orm/mysql-core\";\n * import { mysqlGenerate } from \"drizzle-docs-generator\";\n *\n * const users = mysqlTable(\"users\", {\n * id: serial(\"id\").primaryKey(),\n * name: text(\"name\").notNull(),\n * email: varchar(\"email\", { length: 255 }).unique(),\n * });\n *\n * const dbml = mysqlGenerate({ schema: { users } });\n * console.log(dbml);\n * ```\n *\n * To include JSDoc comments as DBML Note clauses, use the `source` option:\n * ```typescript\n * const dbml = mysqlGenerate({ schema: { users }, source: \"./schema.ts\" });\n * ```\n */\nexport function mysqlGenerate<TSchema extends Record<string, unknown>>(\n options: GenerateOptions<TSchema>,\n): string {\n const generator = new MySqlGenerator(options);\n const dbml = generator.generate();\n\n if (options.out) {\n writeDbmlFile(options.out, dbml);\n }\n\n return dbml;\n}\n"],"names":["MySqlGenerator","BaseGenerator","column","mysqlGenerate","options","dbml","writeDbmlFile"],"mappings":";AAUO,MAAMA,UAEHC,EAAuB;AAAA,EACrB,gBAA+B;AAAA,IACvC,aAAa,CAACC,MAEJA,EAAkD,kBAAkB;AAAA,EAC9E;AAEJ;AAyBO,SAASC,EACdC,GACQ;AAER,QAAMC,IADY,IAAIL,EAAeI,CAAO,EACrB,SAAA;AAEvB,SAAIA,EAAQ,OACVE,EAAcF,EAAQ,KAAKC,CAAI,GAG1BA;AACT;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pg.d.ts","sourceRoot":"","sources":["../../src/generator/pg.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAiB,KAAK,aAAa,EAAE,MAAM,UAAU,CAAC;AAC5E,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAEhE;;;;;GAKG;AACH,qBAAa,WAAW,CACtB,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CACjE,SAAQ,aAAa,CAAC,OAAO,CAAC;IAC9B,SAAS,CAAC,aAAa,EAAE,aAAa,
|
|
1
|
+
{"version":3,"file":"pg.d.ts","sourceRoot":"","sources":["../../src/generator/pg.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAiB,KAAK,aAAa,EAAE,MAAM,UAAU,CAAC;AAC5E,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAEhE;;;;;GAKG;AACH,qBAAa,WAAW,CACtB,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CACjE,SAAQ,aAAa,CAAC,OAAO,CAAC;IAC9B,SAAS,CAAC,aAAa,EAAE,aAAa,CAKpC;IAEF;;OAEG;IACH,OAAO,CAAC,YAAY;IAsBpB;;;;;;;OAOG;cACgB,sBAAsB,IAAI,cAAc,EAAE;CAa9D;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,UAAU,CAAC,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAChE,OAAO,EAAE,eAAe,CAAC,OAAO,CAAC,GAChC,MAAM,CASR"}
|
package/dist/generator/pg.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { getTableColumns as
|
|
2
|
-
import { PgEnumColumn as
|
|
3
|
-
import { BaseGenerator as
|
|
4
|
-
class i extends
|
|
1
|
+
import { getTableColumns as u } from "drizzle-orm";
|
|
2
|
+
import { PgEnumColumn as c } from "drizzle-orm/pg-core";
|
|
3
|
+
import { BaseGenerator as l, writeDbmlFile as a } from "./common.js";
|
|
4
|
+
class i extends l {
|
|
5
5
|
dialectConfig = {
|
|
6
|
-
escapeName: (e) => `"${e}"`,
|
|
7
6
|
isIncrement: (e) => e.getSQLType().toLowerCase().includes("serial")
|
|
8
7
|
};
|
|
9
8
|
/**
|
|
@@ -12,9 +11,9 @@ class i extends a {
|
|
|
12
11
|
collectEnums() {
|
|
13
12
|
const e = /* @__PURE__ */ new Map(), n = this.getTables();
|
|
14
13
|
for (const s of n) {
|
|
15
|
-
const r =
|
|
14
|
+
const r = u(s);
|
|
16
15
|
for (const m of Object.values(r))
|
|
17
|
-
if (m instanceof
|
|
16
|
+
if (m instanceof c) {
|
|
18
17
|
const o = m.enum;
|
|
19
18
|
o && !e.has(o.enumName) && e.set(o.enumName, o.enumValues);
|
|
20
19
|
}
|
|
@@ -39,12 +38,12 @@ class i extends a {
|
|
|
39
38
|
return n;
|
|
40
39
|
}
|
|
41
40
|
}
|
|
42
|
-
function
|
|
41
|
+
function p(t) {
|
|
43
42
|
const n = new i(t).generate();
|
|
44
|
-
return t.out &&
|
|
43
|
+
return t.out && a(t.out, n), n;
|
|
45
44
|
}
|
|
46
45
|
export {
|
|
47
46
|
i as PgGenerator,
|
|
48
|
-
|
|
47
|
+
p as pgGenerate
|
|
49
48
|
};
|
|
50
49
|
//# sourceMappingURL=pg.js.map
|
package/dist/generator/pg.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pg.js","sources":["../../src/generator/pg.ts"],"sourcesContent":["import { type AnyColumn, getTableColumns } from \"drizzle-orm\";\nimport { PgEnumColumn } from \"drizzle-orm/pg-core\";\nimport { BaseGenerator, writeDbmlFile, type DialectConfig } from \"./common\";\nimport type { GenerateOptions, EnumDefinition } from \"../types\";\n\n/**\n * PostgreSQL-specific DBML generator\n *\n * Supports JSDoc comment extraction via `sourceFile` or `comments` options.\n * Comments are included as DBML Note clauses for tables and columns.\n */\nexport class PgGenerator<\n TSchema extends Record<string, unknown> = Record<string, unknown>,\n> extends BaseGenerator<TSchema> {\n protected dialectConfig: DialectConfig = {\n
|
|
1
|
+
{"version":3,"file":"pg.js","sources":["../../src/generator/pg.ts"],"sourcesContent":["import { type AnyColumn, getTableColumns } from \"drizzle-orm\";\nimport { PgEnumColumn } from \"drizzle-orm/pg-core\";\nimport { BaseGenerator, writeDbmlFile, type DialectConfig } from \"./common\";\nimport type { GenerateOptions, EnumDefinition } from \"../types\";\n\n/**\n * PostgreSQL-specific DBML generator\n *\n * Supports JSDoc comment extraction via `sourceFile` or `comments` options.\n * Comments are included as DBML Note clauses for tables and columns.\n */\nexport class PgGenerator<\n TSchema extends Record<string, unknown> = Record<string, unknown>,\n> extends BaseGenerator<TSchema> {\n protected dialectConfig: DialectConfig = {\n isIncrement: (column: AnyColumn) => {\n const sqlType = column.getSQLType().toLowerCase();\n return sqlType.includes(\"serial\");\n },\n };\n\n /**\n * Collect all enum types from the schema\n */\n private collectEnums(): Map<string, string[]> {\n const enums = new Map<string, string[]>();\n const tables = this.getTables();\n\n for (const table of tables) {\n const columns = getTableColumns(table);\n\n for (const column of Object.values(columns)) {\n if (column instanceof PgEnumColumn) {\n const enumObj = (\n column as unknown as { enum: { enumName: string; enumValues: string[] } }\n ).enum;\n if (enumObj && !enums.has(enumObj.enumName)) {\n enums.set(enumObj.enumName, enumObj.enumValues);\n }\n }\n }\n }\n\n return enums;\n }\n\n /**\n * Collect enum definitions for intermediate schema\n *\n * Overrides the base implementation to extract PostgreSQL enum types\n * from enum columns in the schema.\n *\n * @returns Array of enum definitions\n */\n protected override collectEnumDefinitions(): EnumDefinition[] {\n const enums = this.collectEnums();\n const enumDefinitions: EnumDefinition[] = [];\n\n for (const [name, values] of enums) {\n enumDefinitions.push({\n name,\n values,\n });\n }\n\n return enumDefinitions;\n }\n}\n\n/**\n * Generate DBML from a PostgreSQL Drizzle schema\n *\n * @example\n * ```ts\n * import { pgTable, serial, text, varchar } from \"drizzle-orm/pg-core\";\n * import { pgGenerate } from \"drizzle-docs-generator\";\n *\n * const users = pgTable(\"users\", {\n * id: serial(\"id\").primaryKey(),\n * name: text(\"name\").notNull(),\n * email: varchar(\"email\", { length: 255 }).unique(),\n * });\n *\n * const dbml = pgGenerate({ schema: { users } });\n * console.log(dbml);\n * ```\n *\n * To include JSDoc comments as DBML Note clauses, use the `source` option:\n * ```typescript\n * const dbml = pgGenerate({ schema: { users }, source: \"./schema.ts\" });\n * ```\n */\nexport function pgGenerate<TSchema extends Record<string, unknown>>(\n options: GenerateOptions<TSchema>,\n): string {\n const generator = new PgGenerator(options);\n const dbml = generator.generate();\n\n if (options.out) {\n writeDbmlFile(options.out, dbml);\n }\n\n return dbml;\n}\n"],"names":["PgGenerator","BaseGenerator","column","enums","tables","table","columns","getTableColumns","PgEnumColumn","enumObj","enumDefinitions","name","values","pgGenerate","options","dbml","writeDbmlFile"],"mappings":";;;AAWO,MAAMA,UAEHC,EAAuB;AAAA,EACrB,gBAA+B;AAAA,IACvC,aAAa,CAACC,MACIA,EAAO,WAAA,EAAa,YAAA,EACrB,SAAS,QAAQ;AAAA,EAClC;AAAA;AAAA;AAAA;AAAA,EAMM,eAAsC;AAC5C,UAAMC,wBAAY,IAAA,GACZC,IAAS,KAAK,UAAA;AAEpB,eAAWC,KAASD,GAAQ;AAC1B,YAAME,IAAUC,EAAgBF,CAAK;AAErC,iBAAWH,KAAU,OAAO,OAAOI,CAAO;AACxC,YAAIJ,aAAkBM,GAAc;AAClC,gBAAMC,IACJP,EACA;AACF,UAAIO,KAAW,CAACN,EAAM,IAAIM,EAAQ,QAAQ,KACxCN,EAAM,IAAIM,EAAQ,UAAUA,EAAQ,UAAU;AAAA,QAElD;AAAA,IAEJ;AAEA,WAAON;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUmB,yBAA2C;AAC5D,UAAMA,IAAQ,KAAK,aAAA,GACbO,IAAoC,CAAA;AAE1C,eAAW,CAACC,GAAMC,CAAM,KAAKT;AAC3B,MAAAO,EAAgB,KAAK;AAAA,QACnB,MAAAC;AAAA,QACA,QAAAC;AAAA,MAAA,CACD;AAGH,WAAOF;AAAA,EACT;AACF;AAyBO,SAASG,EACdC,GACQ;AAER,QAAMC,IADY,IAAIf,EAAYc,CAAO,EAClB,SAAA;AAEvB,SAAIA,EAAQ,OACVE,EAAcF,EAAQ,KAAKC,CAAI,GAG1BA;AACT;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sqlite.d.ts","sourceRoot":"","sources":["../../src/generator/sqlite.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAiB,KAAK,aAAa,EAAE,MAAM,UAAU,CAAC;AAC5E,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAEhD;;;;;GAKG;AACH,qBAAa,eAAe,CAC1B,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CACjE,SAAQ,aAAa,CAAC,OAAO,CAAC;IAC9B,SAAS,CAAC,aAAa,EAAE,aAAa,
|
|
1
|
+
{"version":3,"file":"sqlite.d.ts","sourceRoot":"","sources":["../../src/generator/sqlite.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAiB,KAAK,aAAa,EAAE,MAAM,UAAU,CAAC;AAC5E,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAEhD;;;;;GAKG;AACH,qBAAa,eAAe,CAC1B,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CACjE,SAAQ,aAAa,CAAC,OAAO,CAAC;IAC9B,SAAS,CAAC,aAAa,EAAE,aAAa,CAMpC;CACH;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,cAAc,CAAC,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACpE,OAAO,EAAE,eAAe,CAAC,OAAO,CAAC,GAChC,MAAM,CASR"}
|
package/dist/generator/sqlite.js
CHANGED
|
@@ -1,16 +1,15 @@
|
|
|
1
1
|
import { BaseGenerator as n, writeDbmlFile as a } from "./common.js";
|
|
2
2
|
class o extends n {
|
|
3
3
|
dialectConfig = {
|
|
4
|
-
|
|
5
|
-
isIncrement: (e) => e.getSQLType().toLowerCase() === "integer" && e.primary
|
|
4
|
+
isIncrement: (r) => r.getSQLType().toLowerCase() === "integer" && r.primary
|
|
6
5
|
};
|
|
7
6
|
}
|
|
8
|
-
function
|
|
9
|
-
const t = new o(
|
|
10
|
-
return
|
|
7
|
+
function s(e) {
|
|
8
|
+
const t = new o(e).generate();
|
|
9
|
+
return e.out && a(e.out, t), t;
|
|
11
10
|
}
|
|
12
11
|
export {
|
|
13
12
|
o as SqliteGenerator,
|
|
14
|
-
|
|
13
|
+
s as sqliteGenerate
|
|
15
14
|
};
|
|
16
15
|
//# sourceMappingURL=sqlite.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sqlite.js","sources":["../../src/generator/sqlite.ts"],"sourcesContent":["import type { AnyColumn } from \"drizzle-orm\";\nimport { BaseGenerator, writeDbmlFile, type DialectConfig } from \"./common\";\nimport type { GenerateOptions } from \"../types\";\n\n/**\n * SQLite-specific DBML generator\n *\n * Supports JSDoc comment extraction via `sourceFile` or `comments` options.\n * Comments are included as DBML Note clauses for tables and columns.\n */\nexport class SqliteGenerator<\n TSchema extends Record<string, unknown> = Record<string, unknown>,\n> extends BaseGenerator<TSchema> {\n protected dialectConfig: DialectConfig = {\n
|
|
1
|
+
{"version":3,"file":"sqlite.js","sources":["../../src/generator/sqlite.ts"],"sourcesContent":["import type { AnyColumn } from \"drizzle-orm\";\nimport { BaseGenerator, writeDbmlFile, type DialectConfig } from \"./common\";\nimport type { GenerateOptions } from \"../types\";\n\n/**\n * SQLite-specific DBML generator\n *\n * Supports JSDoc comment extraction via `sourceFile` or `comments` options.\n * Comments are included as DBML Note clauses for tables and columns.\n */\nexport class SqliteGenerator<\n TSchema extends Record<string, unknown> = Record<string, unknown>,\n> extends BaseGenerator<TSchema> {\n protected dialectConfig: DialectConfig = {\n isIncrement: (column: AnyColumn) => {\n // SQLite uses INTEGER PRIMARY KEY for autoincrement\n const sqlType = column.getSQLType().toLowerCase();\n return sqlType === \"integer\" && column.primary;\n },\n };\n}\n\n/**\n * Generate DBML from a SQLite Drizzle schema\n *\n * @example\n * ```ts\n * import { sqliteTable, integer, text } from \"drizzle-orm/sqlite-core\";\n * import { sqliteGenerate } from \"drizzle-docs-generator\";\n *\n * const users = sqliteTable(\"users\", {\n * id: integer(\"id\").primaryKey(),\n * name: text(\"name\").notNull(),\n * email: text(\"email\").unique(),\n * });\n *\n * const dbml = sqliteGenerate({ schema: { users } });\n * console.log(dbml);\n * ```\n *\n * To include JSDoc comments as DBML Note clauses, use the `source` option:\n * ```typescript\n * const dbml = sqliteGenerate({ schema: { users }, source: \"./schema.ts\" });\n * ```\n */\nexport function sqliteGenerate<TSchema extends Record<string, unknown>>(\n options: GenerateOptions<TSchema>,\n): string {\n const generator = new SqliteGenerator(options);\n const dbml = generator.generate();\n\n if (options.out) {\n writeDbmlFile(options.out, dbml);\n }\n\n return dbml;\n}\n"],"names":["SqliteGenerator","BaseGenerator","column","sqliteGenerate","options","dbml","writeDbmlFile"],"mappings":";AAUO,MAAMA,UAEHC,EAAuB;AAAA,EACrB,gBAA+B;AAAA,IACvC,aAAa,CAACC,MAEIA,EAAO,WAAA,EAAa,YAAA,MACjB,aAAaA,EAAO;AAAA,EACzC;AAEJ;AAyBO,SAASC,EACdC,GACQ;AAER,QAAMC,IADY,IAAIL,EAAgBI,CAAO,EACtB,SAAA;AAEvB,SAAIA,EAAQ,OACVE,EAAcF,EAAQ,KAAKC,CAAI,GAG1BA;AACT;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "drizzle-docs-generator",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "A CLI tool that generates DBML files from Drizzle ORM schema definitions.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cli",
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
"documentation",
|
|
10
10
|
"drizzle",
|
|
11
11
|
"drizzle-orm",
|
|
12
|
+
"mermaid",
|
|
12
13
|
"schema",
|
|
13
14
|
"typescript"
|
|
14
15
|
],
|
|
@@ -38,17 +39,18 @@
|
|
|
38
39
|
},
|
|
39
40
|
"dependencies": {
|
|
40
41
|
"commander": "^14.0.2",
|
|
41
|
-
"drizzle-orm": "1.0.0-beta.12-
|
|
42
|
+
"drizzle-orm": "1.0.0-beta.12-566a3bc",
|
|
42
43
|
"tsx": "^4.21.0",
|
|
43
44
|
"typescript": "^5.9.3"
|
|
44
45
|
},
|
|
45
46
|
"devDependencies": {
|
|
46
47
|
"@types/node": "^24.10.7",
|
|
47
|
-
"
|
|
48
|
+
"@vitest/coverage-v8": "^4.0.18",
|
|
49
|
+
"oxfmt": "^0.26.0",
|
|
48
50
|
"oxlint": "^1.39.0",
|
|
49
51
|
"vite": "^7.3.1",
|
|
50
52
|
"vite-plugin-dts": "^4.5.4",
|
|
51
|
-
"vitest": "^4.0.
|
|
53
|
+
"vitest": "^4.0.18"
|
|
52
54
|
},
|
|
53
55
|
"peerDependencies": {
|
|
54
56
|
"drizzle-orm": ">=0.44.0 || >=1.0.0-beta.0"
|
|
@@ -62,6 +64,7 @@
|
|
|
62
64
|
"test": "vitest",
|
|
63
65
|
"test:run": "vitest run",
|
|
64
66
|
"test:integration": "rm -rf src/cli/__integration_test_output__ && vitest run --config vitest.integration.config.ts",
|
|
67
|
+
"test:coverage": "vitest run --coverage",
|
|
65
68
|
"lint": "oxlint",
|
|
66
69
|
"format": "oxfmt --write",
|
|
67
70
|
"format:check": "oxfmt --check",
|