@cuongph.dev/dbdocgen 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 dbdocgen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,437 @@
1
+ # dbdocgen
2
+
3
+ Generate database documentation from SQL schema files.
4
+
5
+ `dbdocgen` parses a SQL schema (DDL), normalizes it into a structured metadata model, and exports documentation in multiple formats — Excel, Markdown, HTML, Mermaid ER diagram, and Word — from a single CLI command or programmatic API.
6
+
7
+ The database schema is the **single source of truth**. All table names, column types, constraints, and relationships come directly from the parsed SQL file.
8
+
9
+ ## Features
10
+
11
+ - Parse `schema.sql` (PostgreSQL, MySQL/MariaDB primary support)
12
+ - Export to 5 formats: Excel, Markdown, HTML, Mermaid, Word
13
+ - A5:SQL-style layout: per-table definition tables with metadata headers
14
+ - Excel workbook with **Overview** sheet (summary + hyperlinks) and one sheet per table
15
+ - HTML with **index page** and per-table detail pages (PK/FK highlighting)
16
+ - Localized output labels: English (`en`) and Japanese (`jp`)
17
+ - Timestamped output directory per run (`./output/db_doc_gen_{yymmddhhmm}`)
18
+ - Step-by-step progress logging during generation
19
+ - Config file support via [cosmiconfig](https://github.com/cosmiconfig/cosmiconfig)
20
+ - Programmatic API for integration into CI or custom tooling
21
+
22
+ ## Requirements
23
+
24
+ - Node.js >= 20
25
+ - pnpm (for development)
26
+
27
+ ## Installation
28
+
29
+ ```bash
30
+ # Global install
31
+ npm install -g @cuongph.dev/dbdocgen
32
+
33
+ # Project dev dependency
34
+ pnpm add -D @cuongph.dev/dbdocgen
35
+ ```
36
+
37
+ ## Quick Start
38
+
39
+ ```bash
40
+ # 1. Create a config file
41
+ dbdocgen init
42
+
43
+ # 2. Edit dbdocgen.config.json — set your schema path
44
+
45
+ # 3. Generate documentation (output → ./output/db_doc_gen_{yymmddhhmm})
46
+ dbdocgen generate
47
+
48
+ # Or pass options directly
49
+ dbdocgen generate --schema ./database/schema.sql --out ./docs/db
50
+ ```
51
+
52
+ ### Development (from source)
53
+
54
+ ```bash
55
+ git clone <repo>
56
+ cd db_document_tool
57
+ pnpm install
58
+ pnpm dev generate --schema ./database/schema.sql
59
+ ```
60
+
61
+ ## CLI Reference
62
+
63
+ ### `generate`
64
+
65
+ Generate database documentation from a SQL schema file.
66
+
67
+ ```bash
68
+ dbdocgen generate [options]
69
+ ```
70
+
71
+ | Option | Description |
72
+ | ------ | ----------- |
73
+ | `--schema <path>` | Path to the SQL schema file (default: from config or `./schema.sql`) |
74
+ | `--out <path>` | Output directory (default: `./output/db_doc_gen_{yymmddhhmm}`) |
75
+ | `--format <list>` | Comma-separated formats: `excel`, `markdown`, `html`, `diagram`, `word` (default: all) |
76
+ | `--config <path>` | Config file path (default: auto-detected) |
77
+
78
+ **Progress output example:**
79
+
80
+ ```
81
+ [dbdocgen] Loading configuration...
82
+ [dbdocgen] Configuration loaded
83
+ schema: ./database/schema.sql
84
+ outDir: ./output/db_doc_gen_2606281947
85
+ formats: excel, markdown, html, diagram, word
86
+ language: en
87
+ [dbdocgen] Reading schema file
88
+ [dbdocgen] Parsing schema
89
+ dialect: auto-detect
90
+ [dbdocgen] Schema parsed
91
+ tables: 53
92
+ warnings: 1
93
+ dialect: mysql
94
+ [dbdocgen] Exporting excel output
95
+ ...
96
+ [dbdocgen] Generation complete
97
+ ```
98
+
99
+ ### `init`
100
+
101
+ Create a default `dbdocgen.config.json` in the current directory.
102
+
103
+ ```bash
104
+ dbdocgen init
105
+ dbdocgen init --force # overwrite existing config
106
+ ```
107
+
108
+ ### `validate`
109
+
110
+ Parse and validate a SQL schema file without generating documentation.
111
+
112
+ ```bash
113
+ dbdocgen validate
114
+ dbdocgen validate --schema ./database/schema.sql
115
+ ```
116
+
117
+ ### `clean`
118
+
119
+ Remove the output directory.
120
+
121
+ ```bash
122
+ dbdocgen clean --out ./docs/db
123
+ ```
124
+
125
+ ### `config show`
126
+
127
+ Print the resolved configuration (merges config file + CLI defaults).
128
+
129
+ ```bash
130
+ dbdocgen config show
131
+ dbdocgen config show --config ./custom.config.json
132
+ ```
133
+
134
+ ### `config validate`
135
+
136
+ Validate the config file against the schema.
137
+
138
+ ```bash
139
+ dbdocgen config validate
140
+ ```
141
+
142
+ ### `info`
143
+
144
+ Show version and supported features.
145
+
146
+ ```bash
147
+ dbdocgen info
148
+ ```
149
+
150
+ ## Configuration
151
+
152
+ dbdocgen discovers config files automatically via cosmiconfig. Supported names (in project root):
153
+
154
+ | File | Format |
155
+ | ---- | ------ |
156
+ | `dbdocgen.config.json` | JSON |
157
+ | `dbdocgen.config.js` | JavaScript (ESM) |
158
+ | `.dbdocgenrc` | JSON or YAML |
159
+
160
+ CLI options override config file values.
161
+
162
+ ### Example `dbdocgen.config.json`
163
+
164
+ ```json
165
+ {
166
+ "schema": "./database/schema.sql",
167
+ "dialect": "mysql",
168
+ "output": {
169
+ "formats": ["excel", "markdown", "html", "diagram", "word"],
170
+ "language": "en"
171
+ }
172
+ }
173
+ ```
174
+
175
+ ### Config fields
176
+
177
+ | Field | Type | Default | Description |
178
+ | ----- | ---- | ------- | ----------- |
179
+ | `schema` | `string` | `"./schema.sql"` | Path to the SQL schema file |
180
+ | `dialect` | `string` | auto-detected | SQL dialect hint: `postgres`, `mysql`, `mariadb`, `sqlite`, `mssql` |
181
+ | `output.formats` | `string[]` | all formats | Which output formats to generate |
182
+ | `output.language` | `string` | `"en"` | Label language: `en` or `jp` |
183
+
184
+ > **Note:** `outDir` is not set in config. Each `generate` run uses `--out` if provided, otherwise a new timestamped directory under `./output/`.
185
+
186
+ ## Output Structure
187
+
188
+ Each `generate` run writes to a dedicated directory:
189
+
190
+ ```
191
+ output/db_doc_gen_2606281947/
192
+ ├── database_dictionary.xlsx # Excel — Overview + one sheet per table
193
+ ├── database_document.docx # Word document (all tables)
194
+ ├── er_diagram.mmd # Mermaid ER diagram
195
+ ├── tables/
196
+ │ ├── users.md # Per-table Markdown
197
+ │ └── orders.md
198
+ └── html/
199
+ ├── index.html # Table list with links
200
+ └── tables/
201
+ ├── users.html # Per-table HTML
202
+ └── orders.html
203
+ ```
204
+
205
+ ### Output formats
206
+
207
+ | Format | Output file(s) | Description |
208
+ | ------ | -------------- | ----------- |
209
+ | `excel` | `database_dictionary.xlsx` | A5:SQL-style workbook (see below) |
210
+ | `markdown` | `tables/<name>.md` | Per-table Markdown with metadata + column table |
211
+ | `html` | `html/index.html`, `html/tables/<name>.html` | Index page + per-table pages with PK/FK badges |
212
+ | `diagram` | `er_diagram.mmd` | Mermaid `erDiagram` source |
213
+ | `word` | `database_document.docx` | Word document with all tables |
214
+
215
+ ### A5:SQL-style column definition
216
+
217
+ All text exporters (Excel, Markdown, HTML, Word) use the same six-column definition table:
218
+
219
+ | Physical Name | Logical Name | Type | Required | Default Value | Notes |
220
+ | ------------- | ------------ | ---- | -------- | ------------- | ----- |
221
+ | `id` | | `bigint` | Yes | `-` | PK |
222
+ | `user_id` | | `bigint` | Yes | `-` | FK |
223
+
224
+ Column headers are localized based on `output.language` (e.g. Japanese: 物理名 | 論理名 | 型 | 必須 | デフォルト値 | 備考).
225
+
226
+ ### Excel workbook layout
227
+
228
+ **Overview sheet**
229
+
230
+ - Title bar with document name
231
+ - Summary: dialect, table count, relationship count
232
+ - Table list with columns: `#`, Table, Logical Name, Columns, Primary Key, Foreign Keys, Indexes
233
+ - Hyperlinks from table names to their detail sheets
234
+ - Auto-filter enabled on the table list
235
+
236
+ **Per-table sheet**
237
+
238
+ - Title bar with table physical name
239
+ - `← Overview` back-link
240
+ - Metadata block: physical name, logical name, column count, PK, FK, indexes
241
+ - Column definition table with PK rows highlighted (yellow) and FK rows (blue)
242
+ - PK/FK markers in the Notes column
243
+
244
+ ### HTML layout
245
+
246
+ - `html/index.html` — summary cards + sortable table list with links to each table
247
+ - `html/tables/<name>.html` — metadata, column table, PK/FK row highlighting, back link to index
248
+
249
+ ### Output languages
250
+
251
+ | Language | Config value | Notes |
252
+ | -------- | ------------ | ----- |
253
+ | English | `"en"` (default) | All labels in English |
254
+ | Japanese | `"jp"` | Column headers and metadata labels in Japanese |
255
+
256
+ Set via config or ensure `output.language` is set before generation:
257
+
258
+ ```json
259
+ { "output": { "language": "jp" } }
260
+ ```
261
+
262
+ ### Logical names and comments
263
+
264
+ **Logical Name** and **Notes** columns reflect SQL `COMMENT` annotations from the schema. If your schema has no comments, these fields show `(none)`.
265
+
266
+ To populate them, add comments in your DDL:
267
+
268
+ ```sql
269
+ CREATE TABLE users (
270
+ id BIGINT NOT NULL COMMENT 'User ID',
271
+ name VARCHAR(255) COMMENT 'Display name'
272
+ ) COMMENT='Application users';
273
+ ```
274
+
275
+ ## Programmatic API
276
+
277
+ ```ts
278
+ import { generateDbDocs } from "@cuongph.dev/dbdocgen";
279
+
280
+ const doc = await generateDbDocs({
281
+ schema: "./database/schema.sql",
282
+ outDir: "./docs/db",
283
+ dialect: "mysql", // optional — auto-detected if omitted
284
+ output: {
285
+ formats: ["excel", "markdown", "html", "diagram", "word"],
286
+ language: "en"
287
+ },
288
+ onProgress: (event) => {
289
+ console.log(event.step, event.message, event.detail);
290
+ }
291
+ });
292
+
293
+ console.log(doc.tables); // TableDoc[]
294
+ console.log(doc.warnings); // WarningDoc[]
295
+ ```
296
+
297
+ ### Exported symbols
298
+
299
+ ```ts
300
+ import {
301
+ generateDbDocs,
302
+ parseSqlSchema,
303
+ loadConfig,
304
+ exportExcelDictionary,
305
+ exportMarkdownDocs,
306
+ exportHtmlDocs,
307
+ exportMermaidDiagram,
308
+ exportWordDocument,
309
+ renderMermaid,
310
+ databaseDocSchema,
311
+ version
312
+ } from "@cuongph.dev/dbdocgen";
313
+
314
+ import type {
315
+ DbdocgenConfig,
316
+ DatabaseDoc,
317
+ TableDoc,
318
+ ColumnDoc,
319
+ GenerateDbDocsOptions,
320
+ OutputFormat
321
+ } from "@cuongph.dev/dbdocgen";
322
+ ```
323
+
324
+ ### Use individual exporters
325
+
326
+ ```ts
327
+ import { parseSqlSchema, exportExcelDictionary } from "@cuongph.dev/dbdocgen";
328
+ import { readFile } from "node:fs/promises";
329
+
330
+ const sql = await readFile("./schema.sql", "utf8");
331
+ const doc = await parseSqlSchema(sql, { dialect: "mysql" });
332
+
333
+ await exportExcelDictionary(doc, { outDir: "./out", language: "jp" });
334
+ ```
335
+
336
+ ## How It Works
337
+
338
+ ```
339
+ schema.sql
340
+
341
+
342
+ SQL Parser (node-sql-parser)
343
+
344
+
345
+ Normalized Metadata Model (DatabaseDoc)
346
+
347
+ ├──► Excel Exporter → database_dictionary.xlsx
348
+ ├──► Markdown Exporter → tables/*.md
349
+ ├──► HTML Exporter → html/index.html + html/tables/*.html
350
+ ├──► Mermaid Exporter → er_diagram.mmd
351
+ └──► Word Exporter → database_document.docx
352
+ ```
353
+
354
+ ### Metadata model
355
+
356
+ The internal `DatabaseDoc` structure contains:
357
+
358
+ - `dialect` — detected SQL dialect
359
+ - `tables[]` — table name, comment, columns, primary keys, foreign keys, indexes
360
+ - `relationships[]` — FK relationships derived from schema
361
+ - `indexes[]` — standalone index definitions
362
+ - `warnings[]` — parser limitations, unsupported syntax
363
+
364
+ Schema facts (names, types, constraints) are never modified after parsing.
365
+
366
+ ## Tech Stack
367
+
368
+ | Layer | Technology |
369
+ | ----- | ---------- |
370
+ | Language | TypeScript |
371
+ | Runtime | Node.js >= 20 |
372
+ | Package manager | pnpm |
373
+ | Build | tsup (ESM + CJS) |
374
+ | CLI | commander |
375
+ | Config | cosmiconfig |
376
+ | Validation | zod |
377
+ | SQL parser | node-sql-parser |
378
+ | Excel | exceljs |
379
+ | Word | docx |
380
+ | Tests | vitest |
381
+ | Lint / format | eslint, prettier |
382
+
383
+ ## Project Structure
384
+
385
+ ```
386
+ src/
387
+ ├── cli/index.ts # CLI entry point
388
+ ├── core/
389
+ │ ├── config/ # Config schema, loader, defaults
390
+ │ ├── model/ # DatabaseDoc types + Zod validation
391
+ │ └── pipeline/ # generateDbDocs orchestration
392
+ ├── parsers/sql/ # SQL parser + normalizer
393
+ ├── exporters/
394
+ │ ├── excel/ # A5-style Excel workbook
395
+ │ ├── markdown/ # Per-table Markdown
396
+ │ ├── html/ # Index + per-table HTML
397
+ │ ├── diagram/ # Mermaid ER diagram
398
+ │ ├── word/ # Word document
399
+ │ └── shared/ # i18n output labels
400
+ └── index.ts # Public API barrel
401
+ ```
402
+
403
+ ## Development
404
+
405
+ ```bash
406
+ pnpm install # install dependencies
407
+ pnpm dev generate # run CLI from source (tsx)
408
+ pnpm test # run tests
409
+ pnpm typecheck # TypeScript check
410
+ pnpm build # build dist/
411
+ pnpm lint # eslint
412
+ pnpm format # prettier
413
+ ```
414
+
415
+ ## Limitations
416
+
417
+ | Limitation | Notes |
418
+ | ---------- | ----- |
419
+ | Static SQL only | No live database connection |
420
+ | Single schema file | No multi-file glob support yet |
421
+ | Dialect coverage | PostgreSQL and MySQL/MariaDB are primary targets |
422
+ | No incremental gen | Each run regenerates all output files |
423
+ | No web UI | Outputs are static files |
424
+ | Comments required for logical names | Logical Name fields need SQL `COMMENT` annotations |
425
+
426
+ ## Roadmap
427
+
428
+ | Version | Planned |
429
+ | ------- | ------- |
430
+ | v0.3 | Live DB introspection |
431
+ | v0.4 | Framework-specific source plugins |
432
+ | v0.5 | Optional OpenAPI plugin |
433
+ | v1.0 | Stable plugin API, npm public release |
434
+
435
+ ## License
436
+
437
+ MIT