db-model-router 1.0.12 → 1.0.14
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 +8 -9
- package/dbmr.postgres.schema.json +269 -0
- package/dbmr.postgres.test.schema.json +705 -0
- package/docs/dbmr-schema-spec.md +26 -38
- package/package.json +3 -2
- package/scripts/demo-create.js +63 -23
- package/skill/SKILL.md +5 -5
- package/src/cli/commands/diff.js +17 -2
- package/src/cli/commands/doctor.js +45 -4
- package/src/cli/commands/generate.js +98 -53
- package/src/cli/commands/init.js +13 -0
- package/src/cli/diff-engine.js +79 -50
- package/src/cli/generate-migration.js +21 -9
- package/src/cli/generate-route.js +11 -4
- package/src/cli/generate-saas-structure.js +3 -3
- package/src/cli/init/generators.js +0 -125
- package/src/cli/init.js +0 -13
- package/src/cli/main.js +2 -1
- package/src/schema/schema-validator.js +27 -10
package/README.md
CHANGED
|
@@ -114,7 +114,8 @@ When a table has `parent` set, the CLI automatically:
|
|
|
114
114
|
|
|
115
115
|
1. Creates a child route file scoped by the parent's PK as a URL parameter
|
|
116
116
|
2. Mounts the child routes under the parent path
|
|
117
|
-
|
|
117
|
+
|
|
118
|
+
Intermediate tables that are both a child and a parent (e.g. `campaign` under `project`, with `campaign_recipient` under it) get a hybrid route file that scopes their own CRUD by the ancestor parameter and mounts their own children. Children are **only** available under their parent path — there is no duplicate top-level route.
|
|
118
119
|
|
|
119
120
|
#### Best Practice: Don't Use System Tables as Parents
|
|
120
121
|
|
|
@@ -191,8 +192,7 @@ This generates routes:
|
|
|
191
192
|
|
|
192
193
|
- `GET /users/`, `GET /users/:user_id` — top-level
|
|
193
194
|
- `GET /posts/`, `GET /posts/:post_id` — top-level
|
|
194
|
-
- `GET /posts/:post_id/comments/`, `GET /posts/:post_id/comments/:comment_id` — nested under posts
|
|
195
|
-
- `GET /comments/`, `GET /comments/:comment_id` — also available as top-level
|
|
195
|
+
- `GET /posts/:post_id/comments/`, `GET /posts/:post_id/comments/:comment_id` — nested under posts (not available as top-level)
|
|
196
196
|
|
|
197
197
|
Note: `comments` has `user_id` as a foreign key column but `users` is NOT its parent — `posts` is. The `user_id` is just a data reference, not a route hierarchy.
|
|
198
198
|
|
|
@@ -202,7 +202,7 @@ Note: `comments` has `user_id` as a foreign key column but `users` is NOT its pa
|
|
|
202
202
|
| ------------ | -------- | ------------------------------------------------------------------------ |
|
|
203
203
|
| `columns` | Yes | Object mapping column names to Column_Rule strings (include ALL columns) |
|
|
204
204
|
| `pk` | Yes | Primary key column name (convention: `<table>_id`) |
|
|
205
|
-
| `unique` | No |
|
|
205
|
+
| `unique` | No | Unique constraint columns. Flat array = one composite group; array-of-arrays = multiple independent constraints. Defaults to `[[pk]]`. |
|
|
206
206
|
| `softDelete` | No | Column name used for soft-delete |
|
|
207
207
|
| `timestamps` | No | Object with `created_at` and `modified_at` column name mapping |
|
|
208
208
|
| `parent` | No | Parent table name for route nesting, or `null` for top-level |
|
|
@@ -278,7 +278,7 @@ All subcommands accept these flags:
|
|
|
278
278
|
# 1. Introspect an existing database into a schema file
|
|
279
279
|
db-model-router inspect --type postgres --env .env
|
|
280
280
|
|
|
281
|
-
# 2. (Optional) Edit dbmr.schema.json to
|
|
281
|
+
# 2. (Optional) Edit dbmr.schema.json to tweak columns, set parent for nesting, etc.
|
|
282
282
|
|
|
283
283
|
# 3. Generate all artifacts from the schema
|
|
284
284
|
db-model-router generate --from dbmr.schema.json
|
|
@@ -340,9 +340,7 @@ Generated project structure (with `--output backend`):
|
|
|
340
340
|
├── .gitignore
|
|
341
341
|
├── Dockerfile # node:alpine production image
|
|
342
342
|
├── .dockerignore
|
|
343
|
-
├── docker-compose.yml # database +
|
|
344
|
-
├── .cloudbeaver/
|
|
345
|
-
│ └── data-sources.json # auto-connects CloudBeaver to your DB
|
|
343
|
+
├── docker-compose.yml # database + optional Loki/Grafana
|
|
346
344
|
├── .grafana/ # (only when --loki)
|
|
347
345
|
│ └── datasources.yml # auto-connects Grafana to Loki
|
|
348
346
|
└── backend/
|
|
@@ -367,7 +365,6 @@ Docker services included automatically:
|
|
|
367
365
|
| ----------- | ------------------------------------- | ------ | -------------------------------------- |
|
|
368
366
|
| Database | Always (except sqlite3) | Varies | Selected database with random password |
|
|
369
367
|
| Redis | `--session redis` (if DB isn't redis) | 6379 | Session store |
|
|
370
|
-
| CloudBeaver | SQL/MongoDB databases | 8978 | Web-based DB admin, auto-connected |
|
|
371
368
|
| Loki | `--loki` | 3100 | Log aggregation |
|
|
372
369
|
| Grafana | `--loki` | 3001 | Log visualization, Loki pre-configured |
|
|
373
370
|
|
|
@@ -397,6 +394,7 @@ Generate models, routes, tests, OpenAPI spec, and LLM docs from a schema file. A
|
|
|
397
394
|
| Flag / Arg | Description |
|
|
398
395
|
| ------------------------ | ------------------------------------------------- |
|
|
399
396
|
| `--from <path>` | Path to schema file (default: `dbmr.schema.json`) |
|
|
397
|
+
| `--output <dir>` | Directory for generated files (default: cwd) |
|
|
400
398
|
| `--models=false` | Disable model file generation |
|
|
401
399
|
| `--routes=false` | Disable route file generation |
|
|
402
400
|
| `--openapi=false` | Disable OpenAPI spec generation |
|
|
@@ -427,6 +425,7 @@ db-model-router generate --tests=false --dry-run # skip tests
|
|
|
427
425
|
db-model-router generate --saas-structure=false # skip SaaS generation
|
|
428
426
|
db-model-router generate --openapi=false --tests=false # skip OpenAPI and tests
|
|
429
427
|
db-model-router generate --from dbmr.schema.json --json
|
|
428
|
+
db-model-router generate --from dbmr.schema.json --output ./backend
|
|
430
429
|
```
|
|
431
430
|
|
|
432
431
|
#### `doctor`
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
{
|
|
2
|
+
"adapter": "postgres",
|
|
3
|
+
"framework": "express",
|
|
4
|
+
"options": {
|
|
5
|
+
"session": "redis",
|
|
6
|
+
"rateLimiting": true,
|
|
7
|
+
"helmet": true,
|
|
8
|
+
"logger": true,
|
|
9
|
+
"loki": false
|
|
10
|
+
},
|
|
11
|
+
"tables": {
|
|
12
|
+
"projects": {
|
|
13
|
+
"columns": {
|
|
14
|
+
"project_id": "auto_increment",
|
|
15
|
+
"name": "required|string|minLength:3|maxLength:300",
|
|
16
|
+
"slug": "required|string|regex:^[a-z0-9-]+$|maxLength:300",
|
|
17
|
+
"description": "string:text|maxLength:5000",
|
|
18
|
+
"status": "required|string|in:active,archived,paused",
|
|
19
|
+
"priority": "integer|between:1,5",
|
|
20
|
+
"budget": "numeric:decimal(10,2)|min:0",
|
|
21
|
+
"currency": "string|minLength:3|maxLength:3",
|
|
22
|
+
"start_date": "datetime",
|
|
23
|
+
"due_date": "datetime",
|
|
24
|
+
"owner_id": "required|integer",
|
|
25
|
+
"settings": "object",
|
|
26
|
+
"settings.notifications": "boolean",
|
|
27
|
+
"settings.theme": "string|in:light,dark,auto",
|
|
28
|
+
"is_deleted": "boolean",
|
|
29
|
+
"created_at": "datetime",
|
|
30
|
+
"modified_at": "datetime"
|
|
31
|
+
},
|
|
32
|
+
"pk": "project_id",
|
|
33
|
+
"unique": ["slug"],
|
|
34
|
+
"softDelete": "is_deleted",
|
|
35
|
+
"timestamps": {
|
|
36
|
+
"created_at": "created_at",
|
|
37
|
+
"modified_at": "modified_at"
|
|
38
|
+
},
|
|
39
|
+
"parent": null
|
|
40
|
+
},
|
|
41
|
+
"tasks": {
|
|
42
|
+
"columns": {
|
|
43
|
+
"task_id": "auto_increment",
|
|
44
|
+
"project_id": "required|integer",
|
|
45
|
+
"title": "required|string|minLength:3|maxLength:500",
|
|
46
|
+
"description": "string:text|maxLength:10000",
|
|
47
|
+
"status": "required|string|in:todo,in_progress,in_review,done,blocked",
|
|
48
|
+
"priority": "required|string|in:low,medium,high,critical",
|
|
49
|
+
"assignee_id": "integer",
|
|
50
|
+
"estimated_hours": "numeric:decimal(6,2)|min:0",
|
|
51
|
+
"actual_hours": "numeric:decimal(6,2)|min:0",
|
|
52
|
+
"due_date": "datetime",
|
|
53
|
+
"completed_at": "datetime",
|
|
54
|
+
"tags": "object",
|
|
55
|
+
"is_deleted": "boolean",
|
|
56
|
+
"created_at": "datetime",
|
|
57
|
+
"modified_at": "datetime"
|
|
58
|
+
},
|
|
59
|
+
"pk": "task_id",
|
|
60
|
+
"unique": ["task_id"],
|
|
61
|
+
"softDelete": "is_deleted",
|
|
62
|
+
"timestamps": {
|
|
63
|
+
"created_at": "created_at",
|
|
64
|
+
"modified_at": "modified_at"
|
|
65
|
+
},
|
|
66
|
+
"parent": "projects"
|
|
67
|
+
},
|
|
68
|
+
"comments": {
|
|
69
|
+
"columns": {
|
|
70
|
+
"comment_id": "auto_increment",
|
|
71
|
+
"task_id": "required|integer",
|
|
72
|
+
"user_id": "required|integer",
|
|
73
|
+
"body": "required|string:text|maxLength:5000",
|
|
74
|
+
"is_edited": "boolean",
|
|
75
|
+
"created_at": "datetime",
|
|
76
|
+
"modified_at": "datetime"
|
|
77
|
+
},
|
|
78
|
+
"pk": "comment_id",
|
|
79
|
+
"unique": ["comment_id"],
|
|
80
|
+
"timestamps": {
|
|
81
|
+
"created_at": "created_at",
|
|
82
|
+
"modified_at": "modified_at"
|
|
83
|
+
},
|
|
84
|
+
"parent": "tasks"
|
|
85
|
+
},
|
|
86
|
+
"attachments": {
|
|
87
|
+
"columns": {
|
|
88
|
+
"attachment_id": "auto_increment",
|
|
89
|
+
"task_id": "required|integer",
|
|
90
|
+
"file_name": "required|string|maxLength:500",
|
|
91
|
+
"file_url": "required|string|url|maxLength:2048",
|
|
92
|
+
"mime_type": "required|string|maxLength:100",
|
|
93
|
+
"file_size": "integer:bigint|min:0",
|
|
94
|
+
"uploaded_by": "required|integer",
|
|
95
|
+
"created_at": "datetime"
|
|
96
|
+
},
|
|
97
|
+
"pk": "attachment_id",
|
|
98
|
+
"unique": ["attachment_id"],
|
|
99
|
+
"timestamps": {
|
|
100
|
+
"created_at": "created_at"
|
|
101
|
+
},
|
|
102
|
+
"parent": "tasks"
|
|
103
|
+
},
|
|
104
|
+
"customers": {
|
|
105
|
+
"columns": {
|
|
106
|
+
"customer_id": "auto_increment",
|
|
107
|
+
"email": "required|string|email|maxLength:300",
|
|
108
|
+
"phone": "string|phoneNumber|maxLength:20",
|
|
109
|
+
"first_name": "required|string|maxLength:200",
|
|
110
|
+
"last_name": "required|string|maxLength:200",
|
|
111
|
+
"company": "string|maxLength:300",
|
|
112
|
+
"notes": "string:text|maxLength:5000",
|
|
113
|
+
"metadata": "object",
|
|
114
|
+
"metadata.source": "string|in:web,api,import,in_person",
|
|
115
|
+
"metadata.verified": "boolean",
|
|
116
|
+
"is_active": "boolean",
|
|
117
|
+
"created_at": "datetime",
|
|
118
|
+
"modified_at": "datetime"
|
|
119
|
+
},
|
|
120
|
+
"pk": "customer_id",
|
|
121
|
+
"unique": ["email"],
|
|
122
|
+
"timestamps": {
|
|
123
|
+
"created_at": "created_at",
|
|
124
|
+
"modified_at": "modified_at"
|
|
125
|
+
},
|
|
126
|
+
"parent": null
|
|
127
|
+
},
|
|
128
|
+
"invoices": {
|
|
129
|
+
"columns": {
|
|
130
|
+
"invoice_id": "auto_increment",
|
|
131
|
+
"customer_id": "required|integer",
|
|
132
|
+
"invoice_number": "required|string|alphaDash|maxLength:50",
|
|
133
|
+
"status": "required|string|in:draft,sent,paid,overdue,cancelled",
|
|
134
|
+
"subtotal": "required|numeric:money|min:0",
|
|
135
|
+
"tax_rate": "numeric:decimal(5,4)|between:0,1",
|
|
136
|
+
"tax_amount": "required|numeric:money|min:0",
|
|
137
|
+
"total": "required|numeric:money|min:0",
|
|
138
|
+
"currency": "required|string|minLength:3|maxLength:3",
|
|
139
|
+
"due_date": "datetime",
|
|
140
|
+
"paid_at": "datetime",
|
|
141
|
+
"notes": "string:text|maxLength:5000",
|
|
142
|
+
"is_deleted": "boolean",
|
|
143
|
+
"created_at": "datetime",
|
|
144
|
+
"modified_at": "datetime"
|
|
145
|
+
},
|
|
146
|
+
"pk": "invoice_id",
|
|
147
|
+
"unique": ["invoice_number"],
|
|
148
|
+
"softDelete": "is_deleted",
|
|
149
|
+
"timestamps": {
|
|
150
|
+
"created_at": "created_at",
|
|
151
|
+
"modified_at": "modified_at"
|
|
152
|
+
},
|
|
153
|
+
"parent": "customers"
|
|
154
|
+
},
|
|
155
|
+
"invoice_items": {
|
|
156
|
+
"columns": {
|
|
157
|
+
"invoice_item_id": "auto_increment",
|
|
158
|
+
"invoice_id": "required|integer",
|
|
159
|
+
"description": "required|string|maxLength:500",
|
|
160
|
+
"quantity": "required|integer:unsigned|min:1",
|
|
161
|
+
"unit_price": "required|numeric:money|min:0",
|
|
162
|
+
"amount": "required|numeric:money|min:0",
|
|
163
|
+
"created_at": "datetime"
|
|
164
|
+
},
|
|
165
|
+
"pk": "invoice_item_id",
|
|
166
|
+
"unique": ["invoice_item_id"],
|
|
167
|
+
"timestamps": {
|
|
168
|
+
"created_at": "created_at"
|
|
169
|
+
},
|
|
170
|
+
"parent": "invoices"
|
|
171
|
+
},
|
|
172
|
+
"documents": {
|
|
173
|
+
"columns": {
|
|
174
|
+
"document_id": "auto_increment",
|
|
175
|
+
"title": "required|string|minLength:3|maxLength:500",
|
|
176
|
+
"slug": "required|string|regex:^[a-z0-9-]+$|maxLength:500",
|
|
177
|
+
"content": "required|string:text|maxLength:50000",
|
|
178
|
+
"excerpt": "string|maxLength:500",
|
|
179
|
+
"status": "required|string|in:draft,published,archived",
|
|
180
|
+
"author_id": "required|integer",
|
|
181
|
+
"featured": "boolean",
|
|
182
|
+
"tags": "object",
|
|
183
|
+
"is_deleted": "boolean",
|
|
184
|
+
"created_at": "datetime",
|
|
185
|
+
"modified_at": "datetime"
|
|
186
|
+
},
|
|
187
|
+
"pk": "document_id",
|
|
188
|
+
"unique": ["slug"],
|
|
189
|
+
"softDelete": "is_deleted",
|
|
190
|
+
"timestamps": {
|
|
191
|
+
"created_at": "created_at",
|
|
192
|
+
"modified_at": "modified_at"
|
|
193
|
+
},
|
|
194
|
+
"parent": null
|
|
195
|
+
},
|
|
196
|
+
"categories": {
|
|
197
|
+
"columns": {
|
|
198
|
+
"category_id": "auto_increment",
|
|
199
|
+
"name": "required|string|minLength:3|maxLength:200",
|
|
200
|
+
"slug": "required|string|regex:^[a-z0-9-]+$|maxLength:200",
|
|
201
|
+
"description": "string:text|maxLength:2000",
|
|
202
|
+
"sort_order": "integer",
|
|
203
|
+
"is_active": "boolean",
|
|
204
|
+
"created_at": "datetime",
|
|
205
|
+
"modified_at": "datetime"
|
|
206
|
+
},
|
|
207
|
+
"pk": "category_id",
|
|
208
|
+
"unique": ["slug"],
|
|
209
|
+
"timestamps": {
|
|
210
|
+
"created_at": "created_at",
|
|
211
|
+
"modified_at": "modified_at"
|
|
212
|
+
},
|
|
213
|
+
"parent": null
|
|
214
|
+
},
|
|
215
|
+
"events": {
|
|
216
|
+
"columns": {
|
|
217
|
+
"event_id": "auto_increment",
|
|
218
|
+
"name": "required|string|minLength:3|maxLength:300",
|
|
219
|
+
"description": "string:text|maxLength:5000",
|
|
220
|
+
"location": "required|string|maxLength:500",
|
|
221
|
+
"start_time": "required|datetime",
|
|
222
|
+
"end_time": "datetime",
|
|
223
|
+
"capacity": "integer:unsigned|min:1",
|
|
224
|
+
"price": "numeric:money|min:0",
|
|
225
|
+
"currency": "string|minLength:3|maxLength:3",
|
|
226
|
+
"status": "required|string|in:upcoming,ongoing,completed,cancelled",
|
|
227
|
+
"organizer_id": "required|integer",
|
|
228
|
+
"is_deleted": "boolean",
|
|
229
|
+
"created_at": "datetime",
|
|
230
|
+
"modified_at": "datetime"
|
|
231
|
+
},
|
|
232
|
+
"pk": "event_id",
|
|
233
|
+
"unique": ["event_id"],
|
|
234
|
+
"softDelete": "is_deleted",
|
|
235
|
+
"timestamps": {
|
|
236
|
+
"created_at": "created_at",
|
|
237
|
+
"modified_at": "modified_at"
|
|
238
|
+
},
|
|
239
|
+
"parent": null
|
|
240
|
+
}
|
|
241
|
+
},
|
|
242
|
+
"relationships": [
|
|
243
|
+
{
|
|
244
|
+
"parent": "projects",
|
|
245
|
+
"child": "tasks",
|
|
246
|
+
"foreignKey": "project_id"
|
|
247
|
+
},
|
|
248
|
+
{
|
|
249
|
+
"parent": "tasks",
|
|
250
|
+
"child": "comments",
|
|
251
|
+
"foreignKey": "task_id"
|
|
252
|
+
},
|
|
253
|
+
{
|
|
254
|
+
"parent": "tasks",
|
|
255
|
+
"child": "attachments",
|
|
256
|
+
"foreignKey": "task_id"
|
|
257
|
+
},
|
|
258
|
+
{
|
|
259
|
+
"parent": "customers",
|
|
260
|
+
"child": "invoices",
|
|
261
|
+
"foreignKey": "customer_id"
|
|
262
|
+
},
|
|
263
|
+
{
|
|
264
|
+
"parent": "invoices",
|
|
265
|
+
"child": "invoice_items",
|
|
266
|
+
"foreignKey": "invoice_id"
|
|
267
|
+
}
|
|
268
|
+
]
|
|
269
|
+
}
|