db-model-router 1.0.4 → 1.0.6

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 (46) hide show
  1. package/README.md +110 -16
  2. package/TODO.md +15 -0
  3. package/dbmr.schema.json +333 -0
  4. package/docker-compose.yml +1 -1
  5. package/package.json +8 -7
  6. package/scripts/demo-create.js +47 -0
  7. package/skill/SKILL.md +464 -0
  8. package/skill/references/cockroachdb.md +49 -0
  9. package/skill/references/dynamodb.md +53 -0
  10. package/skill/references/mongodb.md +56 -0
  11. package/skill/references/mssql.md +55 -0
  12. package/skill/references/oracle.md +52 -0
  13. package/skill/references/postgres.md +50 -0
  14. package/skill/references/redis.md +53 -0
  15. package/skill/references/sqlite3.md +43 -0
  16. package/src/cli/commands/generate.js +95 -31
  17. package/src/cli/commands/help.js +12 -7
  18. package/src/cli/commands/init.js +2 -2
  19. package/src/cli/commands/inspect.js +1 -0
  20. package/src/cli/diff-engine.js +54 -23
  21. package/src/cli/generate-db-manager.js +1573 -0
  22. package/src/cli/generate-docs-route.js +31 -0
  23. package/src/cli/generate-migration.js +356 -0
  24. package/src/cli/generate-model.js +9 -4
  25. package/src/cli/generate-openapi.js +40 -13
  26. package/src/cli/generate-route.js +55 -27
  27. package/src/cli/init/dependencies.js +3 -0
  28. package/src/cli/init/generators.js +37 -31
  29. package/src/cli/init.js +8 -8
  30. package/src/cli/main.js +2 -2
  31. package/src/cockroachdb/db.js +90 -59
  32. package/src/commons/route.js +20 -20
  33. package/src/commons/validator.js +58 -1
  34. package/src/dynamodb/db.js +50 -27
  35. package/src/mongodb/db.js +1 -0
  36. package/src/mssql/db.js +89 -61
  37. package/src/mysql/db.js +1 -0
  38. package/src/oracle/db.js +1 -0
  39. package/src/postgres/db.js +61 -41
  40. package/src/redis/db.js +1 -0
  41. package/src/schema/schema-parser.js +43 -1
  42. package/src/schema/schema-printer.js +7 -0
  43. package/src/schema/schema-validator.js +17 -0
  44. package/src/sqlite3/db.js +12 -0
  45. package/docs/SKILL.md +0 -419
  46. package/src/cli/commands/generate-llm-docs.js +0 -418
@@ -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 };