drizzle-docs-generator 0.5.6 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/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/package.json +5 -5
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,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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","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;AAmB7F,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;AAE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CAAO;AAE/B,CAAC;AAEHzB,EAAQ,MAAA;AAGR,QAAQ,GAAG,QAAQ,MAAM;AACvB,EAAAF,EAAA;AACF,CAAC;"}
|
|
@@ -1 +1 @@
|
|
|
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,
|
|
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,GAG9B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AAWnB,EAAS,OAAO,GAC1CoB,IAAS,KAAK,kBAAkBpB,EAAS,IAAI,GAC7CqB,IAAQrB,EAAS,YAAY,KAAK,IAAI;AAE5C,WAAO,GAAGkB,CAAS,IAAIE,CAAM,IAAID,CAAO,OAAOE,CAAK;AAAA,EACtD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOQ,kBAAkBL,GAA0C;AAClE,YAAQA,GAAA;AAAA,MACN,KAAK;AACH,eAAO;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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AA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAaQ,eAAerB,GAA8BW,GAAmC;AACtF,UAAMC,IAAY,KAAK,WAAWZ,EAAS,SAAS,GAC9CsB,IAAU,KAAK,WAAWtB,EAAS,OAAO,GAG1CuB,IAAuB,KAAK,qBAAqBvB,GAAUW,CAAM,GACjEa,IAAS,KAAK,kBAAkBxB,EAAS,MAAMuB,CAAoB,GACnEE,IAAQzB,EAAS,YAAY,KAAK,IAAI;AAE5C,WAAO,GAAGY,CAAS,IAAIY,CAAM,IAAIF,CAAO,OAAOG,CAAK;AAAA,EACtD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUQ,kBACNL,GACAG,IAAgC,IACxB;AACR,YAAQH,GAAA;AAAA,MACN,KAAK;AACH,eAAOG,IAAuB,WAAW;AAAA,MAC3C,KAAK;AACH,eAAO;AAAA,MACT,KAAK;AACH,eAAOA,IAAuB,WAAW;AAAA,MAC3C,KAAK;AACH,eAAO;AAAA,IAAA;AAAA,EAEb;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOQ,WAAWG,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;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "drizzle-docs-generator",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.1",
|
|
4
4
|
"description": "A CLI tool that generates DBML files from Drizzle ORM schema definitions.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cli",
|
|
@@ -38,16 +38,16 @@
|
|
|
38
38
|
"access": "public"
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"commander": "^14.0.
|
|
42
|
-
"drizzle-orm": "1.0.0-beta.12-
|
|
41
|
+
"commander": "^14.0.3",
|
|
42
|
+
"drizzle-orm": "1.0.0-beta.12-5845444",
|
|
43
43
|
"tsx": "^4.21.0",
|
|
44
44
|
"typescript": "^5.9.3"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"@types/node": "^24.10.7",
|
|
48
48
|
"@vitest/coverage-v8": "^4.0.18",
|
|
49
|
-
"oxfmt": "^0.
|
|
50
|
-
"oxlint": "^1.
|
|
49
|
+
"oxfmt": "^0.27.0",
|
|
50
|
+
"oxlint": "^1.42.0",
|
|
51
51
|
"vite": "^7.3.1",
|
|
52
52
|
"vite-plugin-dts": "^4.5.4",
|
|
53
53
|
"vitest": "^4.0.18"
|