db-model-router 1.0.0 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,7 +1,21 @@
1
- # rest-router
1
+ # db-model-router
2
2
 
3
3
  A database-agnostic REST API generator for Node.js. Works with Express or ultimate-express (a high-performance drop-in replacement). Define a model, get a full CRUD API with filtering, pagination, and bulk operations — backed by any of 9 supported databases.
4
4
 
5
+ ## Build a REST API with AI
6
+
7
+ This library is designed to be driven by an AI assistant. Give it a prompt like this:
8
+
9
+ ```
10
+ Use db-model-router to build a REST API for a task management app with postgres.
11
+ I need: users (name, email, password_hash), projects (name, description, owner_id → users),
12
+ and tasks (title, status, priority, project_id → projects, assignee_id → users).
13
+ Scaffold the project, write the migrations, generate models and routes with parent-child
14
+ relationships for projects.tasks, and make sure everything runs.
15
+ ```
16
+
17
+ For the LLM skill reference, see [SKILL.md](./docs/SKILL.md).
18
+
5
19
  ## Supported Adapters
6
20
 
7
21
  | Adapter | Module Key | Driver | Install |
@@ -55,6 +69,143 @@ npm install db-model-router @aws-sdk/client-dynamodb @aws-sdk/lib-dynamodb
55
69
 
56
70
  Both `express` and `ultimate-express` are optional peer dependencies. The library auto-detects which one is installed (preferring `ultimate-express` when both are present). All database drivers are also optional peer dependencies, so your `node_modules` stays lean.
57
71
 
72
+ ## Quick Start
73
+
74
+ The fastest way to start a new project:
75
+
76
+ ```bash
77
+ # Scaffold a project interactively
78
+ npx db-model-router init
79
+
80
+ # Or fully non-interactive from a schema file
81
+ db-model-router init --from dbmr.schema.json --yes --no-install
82
+ db-model-router generate --from dbmr.schema.json
83
+ ```
84
+
85
+ After scaffolding:
86
+
87
+ ```bash
88
+ # 1. Edit .env with your database credentials
89
+ # 2. Start developing
90
+ npm run dev
91
+ ```
92
+
93
+ ## Schema-Driven Workflow
94
+
95
+ Instead of running multiple CLI commands manually, you can define your entire project in a single `dbmr.schema.json` file and let the CLI generate everything from it.
96
+
97
+ ### The Schema File
98
+
99
+ `dbmr.schema.json` is a declarative JSON file that describes your adapter, framework, tables, columns, relationships, and options — all in one place:
100
+
101
+ ```json
102
+ {
103
+ "adapter": "postgres",
104
+ "framework": "express",
105
+ "options": {
106
+ "session": "redis",
107
+ "rateLimiting": true,
108
+ "helmet": true,
109
+ "logger": true
110
+ },
111
+ "tables": {
112
+ "users": {
113
+ "columns": {
114
+ "name": "required|string",
115
+ "email": "required|string",
116
+ "age": "integer",
117
+ "is_deleted": "boolean"
118
+ },
119
+ "pk": "id",
120
+ "unique": ["email"],
121
+ "softDelete": "is_deleted",
122
+ "timestamps": {
123
+ "created_at": "created_at",
124
+ "modified_at": "updated_at"
125
+ }
126
+ },
127
+ "posts": {
128
+ "columns": {
129
+ "title": "required|string",
130
+ "body": "string",
131
+ "user_id": "required|integer"
132
+ },
133
+ "pk": "id",
134
+ "unique": ["id"]
135
+ }
136
+ },
137
+ "relationships": [
138
+ { "parent": "users", "child": "posts", "foreignKey": "user_id" }
139
+ ]
140
+ }
141
+ ```
142
+
143
+ Table entries support these fields:
144
+
145
+ | Field | Required | Description |
146
+ | ------------ | -------- | -------------------------------------------------------------- |
147
+ | `columns` | Yes | Object mapping column names to Column_Rule strings |
148
+ | `pk` | No | Primary key column name (defaults to `"id"`) |
149
+ | `unique` | No | Array of unique constraint columns (defaults to `[pk]`) |
150
+ | `softDelete` | No | Column name used for soft-delete |
151
+ | `timestamps` | No | Object with `created_at` and `modified_at` column name mapping |
152
+
153
+ Column rules use the format `(required|)?(string|integer|numeric|boolean|object)` — e.g. `"required|string"`, `"integer"`, `"object"`.
154
+
155
+ ### Unified CLI: `db-model-router`
156
+
157
+ The `db-model-router` command is the unified entry point with five subcommands:
158
+
159
+ ```bash
160
+ db-model-router <subcommand> [flags]
161
+ ```
162
+
163
+ | Subcommand | Description |
164
+ | ---------- | ------------------------------------------------------------------- |
165
+ | `init` | Scaffold a new project (optionally from a schema file) |
166
+ | `inspect` | Introspect a live database and produce a `dbmr.schema.json` |
167
+ | `generate` | Generate models, routes, tests, and OpenAPI spec from the schema |
168
+ | `doctor` | Validate schema, check dependencies, verify generated files in sync |
169
+ | `diff` | Preview what changes regeneration would make (read-only) |
170
+
171
+ #### Universal Flags
172
+
173
+ All subcommands accept these flags:
174
+
175
+ | Flag | Description |
176
+ | -------------- | ----------------------------------------------------------- |
177
+ | `--yes` | Accept all defaults, suppress interactive prompts |
178
+ | `--json` | Output machine-readable JSON instead of human-readable text |
179
+ | `--dry-run` | Preview actions without writing files or running commands |
180
+ | `--no-install` | Skip `npm install` (applies to commands that would run it) |
181
+ | `--help` | Show usage information for the subcommand |
182
+
183
+ #### Quick Workflow Example
184
+
185
+ ```bash
186
+ # 1. Introspect an existing database into a schema file
187
+ db-model-router inspect --type postgres --env .env
188
+
189
+ # 2. (Optional) Edit dbmr.schema.json to add relationships, tweak columns, etc.
190
+
191
+ # 3. Generate all artifacts from the schema
192
+ db-model-router generate --from dbmr.schema.json
193
+
194
+ # 4. Check everything is in sync
195
+ db-model-router doctor --from dbmr.schema.json
196
+
197
+ # 5. Preview what a regeneration would change
198
+ db-model-router diff --from dbmr.schema.json
199
+ ```
200
+
201
+ Or start a brand-new project from a schema file:
202
+
203
+ ```bash
204
+ # Scaffold project + generate everything in one go
205
+ db-model-router init --from dbmr.schema.json --yes --no-install
206
+ db-model-router generate --from dbmr.schema.json
207
+ ```
208
+
58
209
  ## MySQL Example
59
210
 
60
211
  ### 1. Connect
@@ -109,13 +260,14 @@ app.use("/users", route(users));
109
260
  app.listen(3000);
110
261
  ```
111
262
 
112
- This creates 8 endpoints:
263
+ This creates 9 endpoints:
113
264
 
114
265
  | Method | Path | Description |
115
266
  | ------ | ------------ | ------------------------------- |
116
267
  | GET | `/users/:id` | Get one record by PK |
117
268
  | POST | `/users/add` | Insert a single record |
118
269
  | PUT | `/users/:id` | Update a single record |
270
+ | PATCH | `/users/:id` | Partial update a single record |
119
271
  | DELETE | `/users/:id` | Delete a single record |
120
272
  | GET | `/users/` | List with pagination |
121
273
  | POST | `/users/` | Bulk insert (`{ data: [...] }`) |
@@ -274,207 +426,6 @@ The model and route APIs remain identical across all adapters. See the individua
274
426
  - [Redis](./docs/adapters/redis.md)
275
427
  - [DynamoDB](./docs/adapters/dynamodb.md)
276
428
 
277
- ## CLI Tools
278
-
279
- Three CLI commands are included to scaffold models, routes, and full apps from an existing database.
280
-
281
- ### generate-app
282
-
283
- The fastest way to go from database to running API. Scaffolds a complete Express REST API project in a single command — introspects your database, generates models and routes (including parent-child relationships), and creates the app entry point, middleware, environment config, and project structure.
284
-
285
- ```bash
286
- # Full app from MySQL
287
- rest-router-generate-app --type mysql --env .env
288
-
289
- # SQLite3 into a specific directory
290
- rest-router-generate-app --type sqlite3 --database ./myapp.db --output ./my-api
291
-
292
- # Postgres with specific tables and relationships
293
- rest-router-generate-app --type postgres --env .env --tables users,posts,posts.comments
294
- ```
295
-
296
- Options:
297
-
298
- | Option | Description |
299
- | ------------ | ------------------------------------------------------------------------------- |
300
- | `--type` | Database type (mysql, postgres, sqlite3, mssql, oracle, cockroachdb) [required] |
301
- | `--output` | Output directory (default: current directory) |
302
- | `--host` | Database host |
303
- | `--port` | Database port |
304
- | `--database` | Database name or file path |
305
- | `--user` | Database user |
306
- | `--password` | Database password |
307
- | `--schema` | Schema name (postgres only) |
308
- | `--tables` | Comma-separated tables, supports `parent.child` notation for relationships |
309
- | `--env` | Path to .env file for DB connection |
310
-
311
- Generated project structure:
312
-
313
- ```
314
- my-api/
315
- app.js # Express app with init(), db.connect(), middleware, error handler, health check
316
- .env.example # Pre-filled environment template for your DB type
317
- .gitignore # node_modules, .env, *.db
318
- middleware/
319
- logger.js # Request logger (method, URL, status, response time)
320
- models/
321
- users.js # Auto-generated from DB introspection
322
- posts.js
323
- index.js
324
- routes/
325
- users.js # Auto-generated route files
326
- posts.js
327
- index.js
328
- openapi.json # OpenAPI 3.0 spec
329
- migrations/
330
- README.md # Placeholder for migration scripts
331
- sessions/
332
- README.md # Placeholder for session config
333
- ```
334
-
335
- When `--tables` includes parent-child notation (e.g., `posts.comments`), the routes directory also includes scoped child route files and nested route mounting — see [Parent-Child Relationships](#parent-child-relationships-foreign-keys) below.
336
-
337
- To start the generated app:
338
-
339
- ```bash
340
- cp .env.example .env # edit with your DB credentials
341
- npm install
342
- node app.js
343
- ```
344
-
345
- ### generate-model
346
-
347
- Connects to your database, introspects all tables, and generates model files with validation rules, primary keys, unique constraints, and auto-detected options (safeDelete, timestamps). This is called automatically by `generate-app`, but can be used standalone.
348
-
349
- ```bash
350
- # Basic usage
351
- rest-router-generate-model --type mysql --host localhost --database mydb --user root --password secret
352
-
353
- # Using an .env file
354
- rest-router-generate-model --type postgres --env .env --output ./src/models
355
-
356
- # SQLite3
357
- rest-router-generate-model --type sqlite3 --database ./myapp.db --output ./models
358
-
359
- # Only specific tables
360
- rest-router-generate-model --type mysql --env .env --tables users,posts,comments
361
- ```
362
-
363
- Options:
364
-
365
- | Option | Description |
366
- | ------------ | ----------------------------------------------------------------------------- |
367
- | `--type` | Database type (mysql, postgres, sqlite3, mssql, oracle, cockroachdb) |
368
- | `--host` | Database host (default: localhost) |
369
- | `--port` | Database port |
370
- | `--database` | Database name (or file path for sqlite3) |
371
- | `--user` | Database user |
372
- | `--password` | Database password |
373
- | `--schema` | Schema name (postgres only, default: public) |
374
- | `--output` | Output directory (default: ./models) |
375
- | `--tables` | Comma-separated list of tables to generate (supports `parent.child` notation) |
376
- | `--env` | Path to .env file to load |
377
-
378
- Auto-detection:
379
-
380
- - Columns with `DEFAULT` values are marked as optional (not `required`)
381
- - Timestamp columns (`created_at`, `updated_at`, `modified_at`, `createdAt`, etc.) are excluded from the model structure and added to the option object
382
- - Soft-delete columns (`is_deleted`, `deleted`, `is_active`, `archived`, etc.) are excluded from the structure and set as `safeDelete` in the option
383
- - Multi-column unique indexes are correctly grouped for the unique constraint parameter
384
-
385
- Generated output:
386
-
387
- ```
388
- models/
389
- users.js # model(db, "users", {...}, "user_id", ["email"], { safeDelete: "is_deleted", ... })
390
- posts.js # model(db, "posts", {...}, "post_id", ["post_id"])
391
- index.js # exports { users, posts }
392
- ```
393
-
394
- ### generate-route
395
-
396
- Generates Express route files for each model. If models don't exist yet, it auto-generates them first. Also generates an OpenAPI 3.0 spec (`openapi.json`) from the model metadata. This is called automatically by `generate-app`, but can be used standalone.
397
-
398
- ```bash
399
- # From existing models
400
- rest-router-generate-route --models ./models --output ./routes
401
-
402
- # Auto-generate models + routes in one step
403
- rest-router-generate-route --type mysql --env .env --models ./models --output ./routes
404
-
405
- # SQLite3 one-liner
406
- rest-router-generate-route --type sqlite3 --database ./myapp.db
407
- ```
408
-
409
- Options:
410
-
411
- | Option | Description |
412
- | ------------ | -------------------------------------------------------------------------- |
413
- | `--models` | Path to models directory (default: ./models) |
414
- | `--output` | Output directory for routes (default: ./routes) |
415
- | `--type` | Database type — triggers model generation if models are missing |
416
- | `--host` | Database host (passed to model generation) |
417
- | `--port` | Database port (passed to model generation) |
418
- | `--database` | Database name or file path (passed to model generation) |
419
- | `--user` | Database user (passed to model generation) |
420
- | `--password` | Database password (passed to model generation) |
421
- | `--schema` | Schema name (passed to model generation) |
422
- | `--tables` | Comma-separated tables, supports `parent.child` notation for relationships |
423
- | `--env` | Path to .env file (passed to model generation) |
424
-
425
- #### Parent-Child Relationships (Foreign Keys)
426
-
427
- Use dot notation in `--tables` to declare parent-child relationships. This works in `generate-route`, `generate-app`, and `generate-model`. The generator creates nested routes that automatically scope child queries by the parent's foreign key.
428
-
429
- ```bash
430
- # Declare that comments belong to posts
431
- rest-router-generate-route --type mysql --env .env --tables users,posts,posts.comments
432
-
433
- # Same via generate-app
434
- rest-router-generate-app --type mysql --env .env --tables users,posts,posts.comments
435
- ```
436
-
437
- The FK column is derived by convention: `<parent_singular>_id` (e.g., `posts.comments` → `post_id`).
438
-
439
- This generates:
440
-
441
- ```
442
- routes/
443
- users.js # route(users)
444
- posts.js # route(posts)
445
- comments.js # route(comments) — direct access
446
- comments_child_of_posts.js # route(comments, { post_id: "params.post_id" }) — scoped
447
- index.js # mounts all routes
448
- openapi.json # OpenAPI 3.0 spec
449
- ```
450
-
451
- The generated `index.js` mounts both nested and top-level routes:
452
-
453
- ```js
454
- // Nested: GET /api/posts/:post_id/comments — returns only comments for that post
455
- router.use("/posts/:post_id/comments", commentsChildRoute);
456
-
457
- // Direct: GET /api/comments — returns all comments
458
- router.use("/comments", commentsRoute);
459
- ```
460
-
461
- The child route file uses payload override to scope every query:
462
-
463
- ```js
464
- // comments_child_of_posts.js
465
- module.exports = route(comments, { post_id: "params.post_id" });
466
- ```
467
-
468
- #### Generated output (without relationships)
469
-
470
- ```
471
- routes/
472
- users.js # route(users)
473
- posts.js # route(posts)
474
- index.js # express.Router() mounting all routes at /users, /posts, etc.
475
- openapi.json # OpenAPI 3.0 spec
476
- ```
477
-
478
429
  ## Environment Setup (Docker)
479
430
 
480
431
  A `docker-compose.yml` is included for running all supported databases locally:
@@ -5,7 +5,7 @@ networks:
5
5
  services:
6
6
  mysql:
7
7
  image: mysql:8.0
8
- container_name: rest-router-mysql
8
+ container_name: db-model-router-mysql
9
9
  restart: unless-stopped
10
10
  ports:
11
11
  - "3306:3306"
@@ -25,7 +25,7 @@ services:
25
25
 
26
26
  postgres:
27
27
  image: postgres:16
28
- container_name: rest-router-postgres
28
+ container_name: db-model-router-postgres
29
29
  restart: unless-stopped
30
30
  ports:
31
31
  - "5432:5432"
@@ -44,7 +44,7 @@ services:
44
44
 
45
45
  mongodb:
46
46
  image: mongo:7
47
- container_name: rest-router-mongodb
47
+ container_name: db-model-router-mongodb
48
48
  restart: unless-stopped
49
49
  ports:
50
50
  - "27017:27017"
@@ -59,7 +59,7 @@ services:
59
59
 
60
60
  redis:
61
61
  image: redis:7
62
- container_name: rest-router-redis
62
+ container_name: db-model-router-redis
63
63
  restart: unless-stopped
64
64
  ports:
65
65
  - "6379:6379"
@@ -74,7 +74,7 @@ services:
74
74
 
75
75
  cockroachdb:
76
76
  image: cockroachdb/cockroach:latest
77
- container_name: rest-router-cockroachdb
77
+ container_name: db-model-router-cockroachdb
78
78
  restart: unless-stopped
79
79
  ports:
80
80
  - "26257:26257"
@@ -91,7 +91,7 @@ services:
91
91
 
92
92
  mssql:
93
93
  image: mcr.microsoft.com/mssql/server
94
- container_name: rest-router-mssql
94
+ container_name: db-model-router-mssql
95
95
  restart: unless-stopped
96
96
  ports:
97
97
  - "1433:1433"
@@ -99,7 +99,11 @@ services:
99
99
  ACCEPT_EULA: "Y"
100
100
  MSSQL_SA_PASSWORD: "Password123!"
101
101
  healthcheck:
102
- test: ["CMD-SHELL", "/opt/mssql-tools*/bin/sqlcmd -S localhost -U sa -P 'Password123!' -Q 'SELECT 1' || exit 1"]
102
+ test:
103
+ [
104
+ "CMD-SHELL",
105
+ "/opt/mssql-tools*/bin/sqlcmd -S localhost -U sa -P 'Password123!' -Q 'SELECT 1' || exit 1",
106
+ ]
103
107
  interval: 10s
104
108
  timeout: 5s
105
109
  retries: 10
@@ -109,7 +113,7 @@ services:
109
113
 
110
114
  dynamodb:
111
115
  image: amazon/dynamodb-local
112
- container_name: rest-router-dynamodb
116
+ container_name: db-model-router-dynamodb
113
117
  restart: unless-stopped
114
118
  ports:
115
119
  - "8000:8000"
@@ -125,7 +129,7 @@ services:
125
129
 
126
130
  oracle:
127
131
  image: gvenzl/oracle-xe:21-slim
128
- container_name: rest-router-oracle
132
+ container_name: db-model-router-oracle
129
133
  restart: unless-stopped
130
134
  ports:
131
135
  - "1521:1521"