db-model-router 1.0.5 → 1.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (136) hide show
  1. package/README.md +150 -11
  2. package/TODO.md +0 -14
  3. package/db-manager/.dbmanager.sqlite +0 -0
  4. package/db-manager/README.md +223 -0
  5. package/db-manager/adapter-proxy.js +361 -0
  6. package/db-manager/demo/cockroachdb.env +6 -0
  7. package/db-manager/demo/demo.sqlite +0 -0
  8. package/db-manager/demo/dynamodb.env +7 -0
  9. package/db-manager/demo/mongodb.env +4 -0
  10. package/db-manager/demo/mssql.env +6 -0
  11. package/db-manager/demo/mysql.env +6 -0
  12. package/db-manager/demo/oracle.env +6 -0
  13. package/db-manager/demo/postgres.env +6 -0
  14. package/db-manager/demo/redis.env +4 -0
  15. package/db-manager/demo/seeds/cockroachdb.sql +32 -0
  16. package/db-manager/demo/seeds/mssql.sql +32 -0
  17. package/db-manager/demo/seeds/mysql.sql +32 -0
  18. package/db-manager/demo/seeds/oracle.sql +43 -0
  19. package/db-manager/demo/seeds/postgres.sql +32 -0
  20. package/db-manager/demo/seeds/sqlite3.sql +32 -0
  21. package/db-manager/demo/sqlite3.env +2 -0
  22. package/db-manager/metadata-db.js +170 -0
  23. package/db-manager/public/.gitkeep +1 -0
  24. package/db-manager/public/css/style.css +1413 -0
  25. package/db-manager/public/js/app.js +1370 -0
  26. package/db-manager/routes/api.js +388 -0
  27. package/db-manager/routes/views.js +61 -0
  28. package/db-manager/server.js +39 -0
  29. package/db-manager/utils/build-filter-config.js +18 -0
  30. package/db-manager/utils/csv-export.js +59 -0
  31. package/db-manager/utils/export-filename.js +39 -0
  32. package/db-manager/utils/filter-tables.js +20 -0
  33. package/db-manager/utils/parse-filters.js +93 -0
  34. package/db-manager/utils/sort-state.js +35 -0
  35. package/db-manager/views/.gitkeep +1 -0
  36. package/db-manager/views/dashboard.ejs +53 -0
  37. package/db-manager/views/history.ejs +52 -0
  38. package/db-manager/views/index.ejs +35 -0
  39. package/db-manager/views/layout.ejs +31 -0
  40. package/db-manager/views/partials/data-panel.ejs +74 -0
  41. package/db-manager/views/partials/header.ejs +36 -0
  42. package/db-manager/views/partials/sidebar.ejs +30 -0
  43. package/db-manager/views/query.ejs +58 -0
  44. package/dbmr.schema.json +23 -45
  45. package/demo/.env.example +1 -0
  46. package/demo/app.js +3 -1
  47. package/demo/commons/db.js +11 -0
  48. package/demo/commons/migrate.js +3 -0
  49. package/demo/commons/modules.js +18 -0
  50. package/demo/commons/password.js +36 -0
  51. package/demo/commons/webhook.js +81 -0
  52. package/demo/dbmr.schema.json +22 -46
  53. package/demo/middleware/authenticate.js +14 -0
  54. package/demo/middleware/hasPermission.js +30 -0
  55. package/demo/middleware/tenantIsolation.js +17 -0
  56. package/demo/migrations/20260509170349_create_saas_tables.sql +69 -0
  57. package/demo/migrations/{20260430155809_create_tables.sql → 20260509170349_create_tables.sql} +11 -25
  58. package/demo/models/addresses.js +5 -3
  59. package/demo/models/cart_items.js +5 -3
  60. package/demo/models/carts.js +5 -3
  61. package/demo/models/categories.js +5 -3
  62. package/demo/models/coupons.js +5 -3
  63. package/demo/models/index.js +43 -0
  64. package/demo/models/order_items.js +4 -2
  65. package/demo/models/orders.js +5 -3
  66. package/demo/models/payments.js +5 -3
  67. package/demo/models/product_images.js +4 -2
  68. package/demo/models/product_reviews.js +5 -3
  69. package/demo/models/product_variants.js +5 -3
  70. package/demo/models/products.js +5 -3
  71. package/demo/models/role_permissions.js +17 -0
  72. package/demo/models/roles.js +17 -0
  73. package/demo/models/shipments.js +5 -3
  74. package/demo/models/tenants.js +18 -0
  75. package/demo/models/users.js +12 -8
  76. package/demo/models/webhook_logs.js +22 -0
  77. package/demo/models/webhooks.js +19 -0
  78. package/demo/models/wishlists.js +4 -2
  79. package/demo/openapi.json +1744 -616
  80. package/demo/package-lock.json +24 -24
  81. package/demo/package.json +9 -0
  82. package/demo/routes/{addresses.js → addresses/index.js} +1 -1
  83. package/demo/routes/auth/index.js +55 -0
  84. package/demo/routes/carts/{cart_items.js → cart_items/index.js} +1 -1
  85. package/demo/routes/{carts.js → carts/index.js} +1 -1
  86. package/demo/routes/{categories.js → categories/index.js} +1 -1
  87. package/demo/routes/{coupons.js → coupons/index.js} +1 -1
  88. package/demo/routes/index.js +39 -24
  89. package/demo/routes/{orders.js → orders/index.js} +1 -1
  90. package/demo/routes/orders/{order_items.js → order_items/index.js} +1 -1
  91. package/demo/routes/orders/{payments.js → payments/index.js} +1 -1
  92. package/demo/routes/orders/{shipments.js → shipments/index.js} +1 -1
  93. package/demo/routes/{products.js → products/index.js} +1 -1
  94. package/demo/routes/products/{product_images.js → product_images/index.js} +1 -1
  95. package/demo/routes/products/{product_reviews.js → product_reviews/index.js} +1 -1
  96. package/demo/routes/products/{product_variants.js → product_variants/index.js} +1 -1
  97. package/demo/routes/roles/index.js +75 -0
  98. package/demo/routes/roles/permissions/index.js +47 -0
  99. package/demo/routes/tenants/index.js +45 -0
  100. package/demo/routes/users/index.js +45 -0
  101. package/demo/routes/{wishlists.js → wishlists/index.js} +1 -1
  102. package/demo/seeds/saas-seed.js +329 -0
  103. package/docker-compose.yml +61 -0
  104. package/package.json +120 -113
  105. package/scripts/demo-create.js +1 -1
  106. package/skill/SKILL.md +119 -3
  107. package/src/cli/commands/db-manager.js +134 -0
  108. package/src/cli/commands/generate.js +112 -43
  109. package/src/cli/commands/help.js +0 -1
  110. package/src/cli/diff-engine.js +2 -1
  111. package/src/cli/generate-model.js +9 -4
  112. package/src/cli/generate-openapi.js +40 -13
  113. package/src/cli/generate-route.js +61 -22
  114. package/src/cli/generate-saas-structure.js +122 -0
  115. package/src/cli/init/generators.js +42 -30
  116. package/src/cli/init.js +8 -0
  117. package/src/cli/main.js +8 -1
  118. package/src/cli/saas/generate-saas-middleware.js +108 -0
  119. package/src/cli/saas/generate-saas-migrations.js +480 -0
  120. package/src/cli/saas/generate-saas-models.js +211 -0
  121. package/src/cli/saas/generate-saas-openapi.js +419 -0
  122. package/src/cli/saas/generate-saas-routes.js +435 -0
  123. package/src/cli/saas/generate-saas-seeds.js +243 -0
  124. package/src/cli/saas/generate-saas-utils.js +176 -0
  125. package/src/commons/kafka.js +139 -0
  126. package/src/commons/model.js +29 -9
  127. package/src/index.js +2 -0
  128. package/src/mssql/db.js +41 -3
  129. package/src/mysql/db.js +3 -0
  130. package/src/postgres/db.js +6 -0
  131. package/src/sqlite3/db.js +11 -0
  132. package/demo/docs/llm.md +0 -197
  133. package/demo/llms.txt +0 -70
  134. package/demo/routes/users.js +0 -6
  135. package/src/cli/commands/generate-llm-docs.js +0 -418
  136. /package/demo/migrations/{20260430155808_create_migrations_table.sql → 20260509170349_create_migrations_table.sql} +0 -0
package/demo/docs/llm.md DELETED
@@ -1,197 +0,0 @@
1
- # db-model-router — LLM Reference
2
-
3
- ## Installation
4
-
5
- ```bash
6
- npm install db-model-router
7
- ```
8
-
9
- ## Canonical Schema-Driven Workflow
10
-
11
- 1. Create or inspect a `dbmr.schema.json` schema file
12
- 2. Run `db-model-router generate --from dbmr.schema.json` to generate all artifacts
13
- 3. Run `db-model-router doctor` to validate schema and check sync
14
- 4. Run `db-model-router diff` to preview changes before regenerating
15
- 5. Edit the schema file and repeat from step 2
16
-
17
- ## Schema Definition
18
-
19
- The `dbmr.schema.json` file is the single source of truth for your project.
20
-
21
- ```json
22
- {
23
- "adapter": "<adapter>",
24
- "framework": "<framework>",
25
- "tables": {
26
- "<table_name>": {
27
- "columns": {
28
- "<column_name>": "<column_rule>"
29
- },
30
- "pk": "<primary_key_column>",
31
- "unique": [
32
- "<column_name>"
33
- ],
34
- "softDelete": "<column_name>",
35
- "timestamps": {
36
- "created_at": "<column_name>",
37
- "modified_at": "<column_name>"
38
- }
39
- }
40
- },
41
- "relationships": [
42
- {
43
- "parent": "<table>",
44
- "child": "<table>",
45
- "foreignKey": "<column>"
46
- }
47
- ],
48
- "options": {}
49
- }
50
- ```
51
-
52
- ### Adapters
53
-
54
- | Adapter | Description |
55
- |---------|-------------|
56
- | mysql | mysql database adapter |
57
- | postgres | postgres database adapter |
58
- | sqlite3 | sqlite3 database adapter |
59
- | mongodb | mongodb database adapter |
60
- | mssql | mssql database adapter |
61
- | cockroachdb | cockroachdb database adapter |
62
- | oracle | oracle database adapter |
63
- | redis | redis database adapter |
64
- | dynamodb | dynamodb database adapter |
65
-
66
- ### Frameworks
67
-
68
- | Framework | Description |
69
- |-----------|-------------|
70
- | express | express HTTP framework |
71
- | ultimate-express | ultimate-express HTTP framework |
72
-
73
- ### Column Rules
74
-
75
- Column rules use pipe-delimited tokens: `(required|)?(string|integer|numeric|boolean|object)`
76
-
77
- Examples: `"required|string"`, `"integer"`, `"required|boolean"`, `"object"`
78
-
79
- ## CLI Commands
80
-
81
- All commands support universal flags: `--yes`, `--json`, `--dry-run`, `--no-install`, `--help`
82
-
83
- ### init
84
-
85
- Scaffold a new project from a schema file or interactively.
86
-
87
- ```bash
88
- # From schema file, non-interactive
89
- db-model-router init --from dbmr.schema.json --yes --no-install
90
-
91
- # Interactive with defaults
92
- db-model-router init --framework express --database sqlite3 --yes
93
-
94
- # Dry run to preview
95
- db-model-router init --from dbmr.schema.json --dry-run
96
- ```
97
-
98
- | Flag | Description |
99
- |------|-------------|
100
- | `--from <path>` | Read config from schema file |
101
- | `--framework <fw>` | Framework: express, ultimate-express |
102
- | `--database <db>` | Adapter name |
103
-
104
- ### inspect
105
-
106
- Introspect a live database and produce a schema file.
107
-
108
- ```bash
109
- db-model-router inspect --type postgres --env .env --out dbmr.schema.json
110
- db-model-router inspect --type sqlite3 --tables users,posts --json
111
- ```
112
-
113
- | Flag | Description |
114
- |------|-------------|
115
- | `--type <adapter>` | Database adapter to use |
116
- | `--env <path>` | Path to .env file |
117
- | `--out <path>` | Output file (default: dbmr.schema.json) |
118
- | `--tables <list>` | Comma-separated table filter |
119
-
120
- ### generate
121
-
122
- Generate code artifacts from the schema file.
123
-
124
- ```bash
125
- # Generate all artifacts
126
- db-model-router generate --from dbmr.schema.json
127
-
128
- # Generate only models
129
- db-model-router generate --models
130
-
131
- # Generate only routes
132
- db-model-router generate --routes
133
-
134
- # Preview with dry-run
135
- db-model-router generate --dry-run --json
136
-
137
- # Generate LLM docs only
138
- db-model-router generate --llm-docs
139
- ```
140
-
141
- | Flag | Description |
142
- |------|-------------|
143
- | `--from <path>` | Schema file (default: dbmr.schema.json) |
144
- | `--models` | Generate model files only |
145
- | `--routes` | Generate route files only |
146
- | `--openapi` | Generate OpenAPI spec only |
147
- | `--tests` | Generate test files only |
148
- | `--llm-docs` | Generate LLM documentation only |
149
-
150
- ### doctor
151
-
152
- Validate schema, check dependencies, and verify generated files are in sync.
153
-
154
- ```bash
155
- db-model-router doctor --from dbmr.schema.json
156
- db-model-router doctor --json
157
- ```
158
-
159
- ### diff
160
-
161
- Preview changes between the schema and currently generated files.
162
-
163
- ```bash
164
- db-model-router diff --from dbmr.schema.json
165
- db-model-router diff --json
166
- ```
167
-
168
- ## Route Contract
169
-
170
- Each table generates 9 endpoints:
171
-
172
- | Method | Path | Description |
173
- |--------|------|-------------|
174
- | GET | `/api/<table>/` | List with pagination (page, size, sort, select_columns) |
175
- | POST | `/api/<table>/` | Bulk insert |
176
- | PUT | `/api/<table>/` | Bulk update |
177
- | DELETE | `/api/<table>/` | Bulk delete |
178
- | GET | `/api/<table>/:id` | Get single record by primary key |
179
- | POST | `/api/<table>/:id` | Insert single record |
180
- | PUT | `/api/<table>/:id` | Update single record |
181
- | PATCH | `/api/<table>/:id` | Partial update single record |
182
- | DELETE | `/api/<table>/:id` | Delete single record |
183
-
184
- ## Adapter Capability Matrix
185
-
186
- | Adapter | SQL | Transactions | Migrations | Streaming |
187
- |---------|-----|--------------|------------|-----------|
188
- | mysql | Yes | Yes | Yes | No |
189
- | postgres | Yes | Yes | Yes | No |
190
- | sqlite3 | Yes | Yes | Yes | No |
191
- | mongodb | No | No | No | No |
192
- | mssql | Yes | Yes | Yes | No |
193
- | cockroachdb | Yes | Yes | Yes | No |
194
- | oracle | Yes | Yes | Yes | No |
195
- | redis | No | No | No | No |
196
- | dynamodb | No | No | No | No |
197
-
package/demo/llms.txt DELETED
@@ -1,70 +0,0 @@
1
- # db-model-router — LLM Quick Reference
2
-
3
- ## Schema File: dbmr.schema.json
4
- { "adapter": "<adapter>", "framework": "<framework>", "tables": { "<name>": { "columns": { "<col>": "<rule>" }, "pk": "<col>", "unique": ["<col>"], "softDelete": "<col>" } }, "relationships": [{ "parent": "<t>", "child": "<t>", "foreignKey": "<col>" }], "options": {} }
5
-
6
- Adapters: mysql, postgres, sqlite3, mongodb, mssql, cockroachdb, oracle, redis, dynamodb
7
- Frameworks: express, ultimate-express
8
- Column rules: (required|)?(string|integer|numeric|boolean|object) e.g. "required|string"
9
-
10
- ## Universal Flags
11
- --yes --json --dry-run --no-install --help
12
-
13
- ## Commands
14
-
15
- ### init
16
- Scaffold a new project.
17
- --from <schema> Read config from schema file
18
- --framework <fw> Framework (express|ultimate-express)
19
- --database <db> Adapter name
20
- Example: db-model-router init --from dbmr.schema.json --yes --no-install
21
-
22
- ### inspect
23
- Introspect a live database and produce a schema file.
24
- --type <adapter> Database adapter
25
- --env <path> Path to .env file
26
- --out <path> Output path (default: dbmr.schema.json)
27
- --tables <list> Comma-separated table filter
28
- Example: db-model-router inspect --type sqlite3 --env .env --out schema.json
29
-
30
- ### generate
31
- Generate code artifacts from schema.
32
- --from <schema> Schema file (default: dbmr.schema.json)
33
- --models Generate model files only
34
- --routes Generate route files only
35
- --openapi Generate OpenAPI spec only
36
- --tests Generate test files only
37
- --llm-docs Generate LLM documentation only
38
- Example: db-model-router generate --from dbmr.schema.json
39
- Example: db-model-router generate --models --dry-run
40
-
41
- ### doctor
42
- Validate schema, check dependencies, verify file sync.
43
- --from <schema> Schema file (default: dbmr.schema.json)
44
- Example: db-model-router doctor --from dbmr.schema.json --json
45
-
46
- ### diff
47
- Preview changes between schema and generated files.
48
- --from <schema> Schema file (default: dbmr.schema.json)
49
- Example: db-model-router diff --from dbmr.schema.json
50
-
51
- ## Route Contract (per table)
52
- GET /api/<table>/ List (page, size, sort, select_columns)
53
- POST /api/<table>/ Bulk insert
54
- PUT /api/<table>/ Bulk update
55
- DELETE /api/<table>/ Bulk delete
56
- GET /api/<table>/:id Get by ID
57
- POST /api/<table>/:id Insert one
58
- PUT /api/<table>/:id Update one
59
- PATCH /api/<table>/:id Partial update one
60
- DELETE /api/<table>/:id Delete one
61
-
62
- ## Generated Files
63
- models/<table>.js Model with CRUD operations
64
- routes/<table>.js Express route handlers
65
- routes/<child>_child_of_<parent>.js Child route
66
- routes/index.js Route mounting index
67
- test/<table>.test.js CRUD endpoint tests
68
- openapi.json OpenAPI 3.0 spec
69
- llms.txt This file
70
- docs/llm.md Full LLM reference
@@ -1,6 +0,0 @@
1
- import dbModelRouter from "db-model-router";
2
- import users from "../models/users.js";
3
-
4
- const { route } = dbModelRouter;
5
-
6
- export default route(users);
@@ -1,418 +0,0 @@
1
- "use strict";
2
-
3
- /**
4
- * LLM Docs Generator
5
- *
6
- * Generates two documentation files optimized for LLM consumption:
7
- * - llms.txt: ultra-compact CLI reference (≤200 lines)
8
- * - docs/llm.md: full reference with examples, schema definition, route contract, etc.
9
- */
10
-
11
- const ADAPTERS = [
12
- "mysql",
13
- "postgres",
14
- "sqlite3",
15
- "mongodb",
16
- "mssql",
17
- "cockroachdb",
18
- "oracle",
19
- "redis",
20
- "dynamodb",
21
- ];
22
-
23
- const FRAMEWORKS = ["express", "ultimate-express"];
24
-
25
- const SUBCOMMANDS = ["init", "inspect", "generate", "doctor", "diff"];
26
-
27
- const UNIVERSAL_FLAGS = [
28
- "--yes",
29
- "--json",
30
- "--dry-run",
31
- "--no-install",
32
- "--help",
33
- ];
34
-
35
- const ADAPTER_CAPABILITIES = {
36
- mysql: { sql: true, transactions: true, migrations: true, streaming: false },
37
- postgres: {
38
- sql: true,
39
- transactions: true,
40
- migrations: true,
41
- streaming: false,
42
- },
43
- sqlite3: {
44
- sql: true,
45
- transactions: true,
46
- migrations: true,
47
- streaming: false,
48
- },
49
- mongodb: {
50
- sql: false,
51
- transactions: false,
52
- migrations: false,
53
- streaming: false,
54
- },
55
- mssql: { sql: true, transactions: true, migrations: true, streaming: false },
56
- cockroachdb: {
57
- sql: true,
58
- transactions: true,
59
- migrations: true,
60
- streaming: false,
61
- },
62
- oracle: { sql: true, transactions: true, migrations: true, streaming: false },
63
- redis: {
64
- sql: false,
65
- transactions: false,
66
- migrations: false,
67
- streaming: false,
68
- },
69
- dynamodb: {
70
- sql: false,
71
- transactions: false,
72
- migrations: false,
73
- streaming: false,
74
- },
75
- };
76
-
77
- /**
78
- * Generate the content for llms.txt — ultra-compact CLI reference (≤200 lines).
79
- * @returns {string}
80
- */
81
- function generateLlmsTxt() {
82
- const lines = [
83
- "# db-model-router — LLM Quick Reference",
84
- "",
85
- "## Schema File: dbmr.schema.json",
86
- '{ "adapter": "<adapter>", "framework": "<framework>", "tables": { "<name>": { "columns": { "<col>": "<rule>" }, "pk": "<col>", "unique": ["<col>"], "softDelete": "<col>" } }, "relationships": [{ "parent": "<t>", "child": "<t>", "foreignKey": "<col>" }], "options": {} }',
87
- "",
88
- "Adapters: " + ADAPTERS.join(", "),
89
- "Frameworks: " + FRAMEWORKS.join(", "),
90
- 'Column rules: (required|)?(string|integer|numeric|boolean|object) e.g. "required|string"',
91
- "",
92
- "## Universal Flags",
93
- UNIVERSAL_FLAGS.join(" "),
94
- "",
95
- "## Commands",
96
- "",
97
- "### init",
98
- "Scaffold a new project.",
99
- " --from <schema> Read config from schema file",
100
- " --framework <fw> Framework (express|ultimate-express)",
101
- " --database <db> Adapter name",
102
- "Example: db-model-router init --from dbmr.schema.json --yes --no-install",
103
- "",
104
- "### inspect",
105
- "Introspect a live database and produce a schema file.",
106
- " --type <adapter> Database adapter",
107
- " --env <path> Path to .env file",
108
- " --out <path> Output path (default: dbmr.schema.json)",
109
- " --tables <list> Comma-separated table filter",
110
- "Example: db-model-router inspect --type sqlite3 --env .env --out schema.json",
111
- "",
112
- "### generate",
113
- "Generate code artifacts from schema.",
114
- " --from <schema> Schema file (default: dbmr.schema.json)",
115
- " --models Generate model files only",
116
- " --routes Generate route files only",
117
- " --openapi Generate OpenAPI spec only",
118
- " --tests Generate test files only",
119
- " --llm-docs Generate LLM documentation only",
120
- "Example: db-model-router generate --from dbmr.schema.json",
121
- "Example: db-model-router generate --models --dry-run",
122
- "",
123
- "### doctor",
124
- "Validate schema, check dependencies, verify file sync.",
125
- " --from <schema> Schema file (default: dbmr.schema.json)",
126
- "Example: db-model-router doctor --from dbmr.schema.json --json",
127
- "",
128
- "### diff",
129
- "Preview changes between schema and generated files.",
130
- " --from <schema> Schema file (default: dbmr.schema.json)",
131
- "Example: db-model-router diff --from dbmr.schema.json",
132
- "",
133
- "## Route Contract (per table)",
134
- "GET /api/<table>/ List (page, size, sort, select_columns)",
135
- "POST /api/<table>/ Bulk insert",
136
- "PUT /api/<table>/ Bulk update",
137
- "DELETE /api/<table>/ Bulk delete",
138
- "GET /api/<table>/:id Get by ID",
139
- "POST /api/<table>/:id Insert one",
140
- "PUT /api/<table>/:id Update one",
141
- "PATCH /api/<table>/:id Partial update one",
142
- "DELETE /api/<table>/:id Delete one",
143
- "",
144
- "## Generated Files",
145
- "models/<table>.js Model with CRUD operations",
146
- "routes/<table>.js Express route handlers",
147
- "routes/<child>_child_of_<parent>.js Child route",
148
- "routes/index.js Route mounting index",
149
- "test/<table>.test.js CRUD endpoint tests",
150
- "openapi.json OpenAPI 3.0 spec",
151
- "llms.txt This file",
152
- "docs/llm.md Full LLM reference",
153
- ];
154
-
155
- return lines.join("\n") + "\n";
156
- }
157
-
158
- /**
159
- * Generate the content for docs/llm.md — full LLM reference.
160
- * @returns {string}
161
- */
162
- function generateLlmMd() {
163
- const sections = [];
164
-
165
- // Title
166
- sections.push("# db-model-router — LLM Reference");
167
- sections.push("");
168
-
169
- // Installation
170
- sections.push("## Installation");
171
- sections.push("");
172
- sections.push("```bash");
173
- sections.push("npm install db-model-router");
174
- sections.push("```");
175
- sections.push("");
176
-
177
- // Workflow
178
- sections.push("## Canonical Schema-Driven Workflow");
179
- sections.push("");
180
- sections.push("1. Create or inspect a `dbmr.schema.json` schema file");
181
- sections.push(
182
- "2. Run `db-model-router generate --from dbmr.schema.json` to generate all artifacts",
183
- );
184
- sections.push(
185
- "3. Run `db-model-router doctor` to validate schema and check sync",
186
- );
187
- sections.push(
188
- "4. Run `db-model-router diff` to preview changes before regenerating",
189
- );
190
- sections.push("5. Edit the schema file and repeat from step 2");
191
- sections.push("");
192
-
193
- // Schema Definition
194
- sections.push("## Schema Definition");
195
- sections.push("");
196
- sections.push(
197
- "The `dbmr.schema.json` file is the single source of truth for your project.",
198
- );
199
- sections.push("");
200
- sections.push("```json");
201
- sections.push(
202
- JSON.stringify(
203
- {
204
- adapter: "<adapter>",
205
- framework: "<framework>",
206
- tables: {
207
- "<table_name>": {
208
- columns: {
209
- "<column_name>": "<column_rule>",
210
- },
211
- pk: "<primary_key_column>",
212
- unique: ["<column_name>"],
213
- softDelete: "<column_name>",
214
- timestamps: {
215
- created_at: "<column_name>",
216
- modified_at: "<column_name>",
217
- },
218
- },
219
- },
220
- relationships: [
221
- { parent: "<table>", child: "<table>", foreignKey: "<column>" },
222
- ],
223
- options: {},
224
- },
225
- null,
226
- 2,
227
- ),
228
- );
229
- sections.push("```");
230
- sections.push("");
231
- sections.push("### Adapters");
232
- sections.push("");
233
- sections.push("| Adapter | Description |");
234
- sections.push("|---------|-------------|");
235
- for (const a of ADAPTERS) {
236
- sections.push(`| ${a} | ${a} database adapter |`);
237
- }
238
- sections.push("");
239
- sections.push("### Frameworks");
240
- sections.push("");
241
- sections.push("| Framework | Description |");
242
- sections.push("|-----------|-------------|");
243
- for (const f of FRAMEWORKS) {
244
- sections.push(`| ${f} | ${f} HTTP framework |`);
245
- }
246
- sections.push("");
247
- sections.push("### Column Rules");
248
- sections.push("");
249
- sections.push(
250
- "Column rules use pipe-delimited tokens: `(required|)?(string|integer|numeric|boolean|object)`",
251
- );
252
- sections.push("");
253
- sections.push(
254
- 'Examples: `"required|string"`, `"integer"`, `"required|boolean"`, `"object"`',
255
- );
256
- sections.push("");
257
-
258
- // CLI Commands
259
- sections.push("## CLI Commands");
260
- sections.push("");
261
- sections.push(
262
- "All commands support universal flags: " +
263
- UNIVERSAL_FLAGS.map((f) => "`" + f + "`").join(", "),
264
- );
265
- sections.push("");
266
-
267
- // init
268
- sections.push("### init");
269
- sections.push("");
270
- sections.push("Scaffold a new project from a schema file or interactively.");
271
- sections.push("");
272
- sections.push("```bash");
273
- sections.push("# From schema file, non-interactive");
274
- sections.push(
275
- "db-model-router init --from dbmr.schema.json --yes --no-install",
276
- );
277
- sections.push("");
278
- sections.push("# Interactive with defaults");
279
- sections.push(
280
- "db-model-router init --framework express --database sqlite3 --yes",
281
- );
282
- sections.push("");
283
- sections.push("# Dry run to preview");
284
- sections.push("db-model-router init --from dbmr.schema.json --dry-run");
285
- sections.push("```");
286
- sections.push("");
287
- sections.push("| Flag | Description |");
288
- sections.push("|------|-------------|");
289
- sections.push("| `--from <path>` | Read config from schema file |");
290
- sections.push(
291
- "| `--framework <fw>` | Framework: express, ultimate-express |",
292
- );
293
- sections.push("| `--database <db>` | Adapter name |");
294
- sections.push("");
295
-
296
- // inspect
297
- sections.push("### inspect");
298
- sections.push("");
299
- sections.push("Introspect a live database and produce a schema file.");
300
- sections.push("");
301
- sections.push("```bash");
302
- sections.push(
303
- "db-model-router inspect --type postgres --env .env --out dbmr.schema.json",
304
- );
305
- sections.push(
306
- "db-model-router inspect --type sqlite3 --tables users,posts --json",
307
- );
308
- sections.push("```");
309
- sections.push("");
310
- sections.push("| Flag | Description |");
311
- sections.push("|------|-------------|");
312
- sections.push("| `--type <adapter>` | Database adapter to use |");
313
- sections.push("| `--env <path>` | Path to .env file |");
314
- sections.push("| `--out <path>` | Output file (default: dbmr.schema.json) |");
315
- sections.push("| `--tables <list>` | Comma-separated table filter |");
316
- sections.push("");
317
-
318
- // generate
319
- sections.push("### generate");
320
- sections.push("");
321
- sections.push("Generate code artifacts from the schema file.");
322
- sections.push("");
323
- sections.push("```bash");
324
- sections.push("# Generate all artifacts");
325
- sections.push("db-model-router generate --from dbmr.schema.json");
326
- sections.push("");
327
- sections.push("# Generate only models");
328
- sections.push("db-model-router generate --models");
329
- sections.push("");
330
- sections.push("# Generate only routes");
331
- sections.push("db-model-router generate --routes");
332
- sections.push("");
333
- sections.push("# Preview with dry-run");
334
- sections.push("db-model-router generate --dry-run --json");
335
- sections.push("");
336
- sections.push("# Generate LLM docs only");
337
- sections.push("db-model-router generate --llm-docs");
338
- sections.push("```");
339
- sections.push("");
340
- sections.push("| Flag | Description |");
341
- sections.push("|------|-------------|");
342
- sections.push(
343
- "| `--from <path>` | Schema file (default: dbmr.schema.json) |",
344
- );
345
- sections.push("| `--models` | Generate model files only |");
346
- sections.push("| `--routes` | Generate route files only |");
347
- sections.push("| `--openapi` | Generate OpenAPI spec only |");
348
- sections.push("| `--tests` | Generate test files only |");
349
- sections.push("| `--llm-docs` | Generate LLM documentation only |");
350
- sections.push("");
351
-
352
- // doctor
353
- sections.push("### doctor");
354
- sections.push("");
355
- sections.push(
356
- "Validate schema, check dependencies, and verify generated files are in sync.",
357
- );
358
- sections.push("");
359
- sections.push("```bash");
360
- sections.push("db-model-router doctor --from dbmr.schema.json");
361
- sections.push("db-model-router doctor --json");
362
- sections.push("```");
363
- sections.push("");
364
-
365
- // diff
366
- sections.push("### diff");
367
- sections.push("");
368
- sections.push(
369
- "Preview changes between the schema and currently generated files.",
370
- );
371
- sections.push("");
372
- sections.push("```bash");
373
- sections.push("db-model-router diff --from dbmr.schema.json");
374
- sections.push("db-model-router diff --json");
375
- sections.push("```");
376
- sections.push("");
377
-
378
- // Route Contract
379
- sections.push("## Route Contract");
380
- sections.push("");
381
- sections.push("Each table generates 9 endpoints:");
382
- sections.push("");
383
- sections.push("| Method | Path | Description |");
384
- sections.push("|--------|------|-------------|");
385
- sections.push(
386
- "| GET | `/api/<table>/` | List with pagination (page, size, sort, select_columns) |",
387
- );
388
- sections.push("| POST | `/api/<table>/` | Bulk insert |");
389
- sections.push("| PUT | `/api/<table>/` | Bulk update |");
390
- sections.push("| DELETE | `/api/<table>/` | Bulk delete |");
391
- sections.push(
392
- "| GET | `/api/<table>/:id` | Get single record by primary key |",
393
- );
394
- sections.push("| POST | `/api/<table>/:id` | Insert single record |");
395
- sections.push("| PUT | `/api/<table>/:id` | Update single record |");
396
- sections.push(
397
- "| PATCH | `/api/<table>/:id` | Partial update single record |",
398
- );
399
- sections.push("| DELETE | `/api/<table>/:id` | Delete single record |");
400
- sections.push("");
401
-
402
- // Adapter Capability Matrix
403
- sections.push("## Adapter Capability Matrix");
404
- sections.push("");
405
- sections.push("| Adapter | SQL | Transactions | Migrations | Streaming |");
406
- sections.push("|---------|-----|--------------|------------|-----------|");
407
- for (const [adapter, caps] of Object.entries(ADAPTER_CAPABILITIES)) {
408
- const yn = (v) => (v ? "Yes" : "No");
409
- sections.push(
410
- `| ${adapter} | ${yn(caps.sql)} | ${yn(caps.transactions)} | ${yn(caps.migrations)} | ${yn(caps.streaming)} |`,
411
- );
412
- }
413
- sections.push("");
414
-
415
- return sections.join("\n") + "\n";
416
- }
417
-
418
- module.exports = { generateLlmsTxt, generateLlmMd };