@carllee1983/dbcli 1.5.0 → 1.6.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/assets/SKILL.md CHANGED
@@ -1,536 +1,93 @@
1
1
  ---
2
2
  name: dbcli
3
- description: Database CLI for AI agents with permission-based access control. Use to query, inspect schemas, insert/update/delete data, export results, and manage sensitive data blacklists. Supports MySQL, PostgreSQL, MariaDB, and MongoDB with multiple named connections per project and custom env files. Trigger when working with databases, running SQL or MongoDB JSON queries, exploring table/collection structures, switching between database environments, or protecting sensitive columns/tables from AI access.
3
+ description: Database CLI for AI agents with permission-based access control. Use to query, inspect schemas, insert/update/delete, export results, and blacklist sensitive columns/tables. Supports MySQL, PostgreSQL, MariaDB, and MongoDB with multiple named connections per project and custom env files. Trigger when working with databases, running SQL or MongoDB JSON queries, exploring table/collection structures, switching database environments, or protecting sensitive data from AI access. For exhaustive flags and examples, read the sibling `reference.md`.
4
4
  ---
5
5
 
6
6
  # dbcli
7
7
 
8
8
  Database CLI for AI agents with permission-based access control.
9
9
 
10
- ## Quick Start
10
+ ## AI agent workflow (follow in order)
11
11
 
12
- ```bash
13
- dbcli init # Initialize .dbcli config (parses .env automatically)
14
- dbcli schema # Scan all tables and save to .dbcli
15
- dbcli query "SELECT * FROM users" # Execute SQL
16
- ```
17
-
18
- ## Commands
19
-
20
- ### init
21
-
22
- Initialize `.dbcli` configuration file. Typically run manually by the developer — avoid running on behalf of the user unless explicitly requested.
23
-
24
- ```bash
25
- dbcli init # Single connection (v1 format)
26
- dbcli init --system mysql --host localhost --port 3306 --user root --name mydb
27
- dbcli init --use-env-refs # Store env var references
28
- dbcli init --no-interactive --force # Non-interactive mode
29
-
30
- # MongoDB
31
- dbcli init --system mongodb --uri "mongodb://user:pass@host:27017/mydb?authSource=admin"
32
- dbcli init --system mongodb --host localhost --port 27017 --user admin --password secret --name mydb
33
- dbcli init --system mongodb --host localhost --port 27017 --name mydb # No auth
34
-
35
- # Multi-connection (v2 format)
36
- dbcli init --conn-name staging --env-file .env.staging # Named connection with custom env file
37
- dbcli init --conn-name prod --env-file .env.production --use-env-refs --skip-test
38
- dbcli init --remove staging # Remove a named connection
39
- dbcli init --rename staging:production # Rename a connection
40
- ```
41
-
42
- **Key options:** `--system`, `--permission`, `--use-env-refs`, `--skip-test`, `--no-interactive`, `--force`, `--conn-name <name>`, `--env-file <path>`, `--remove <name>`, `--rename <old:new>`
43
-
44
- **MongoDB-specific options:** `--uri <uri>` (full connection URI), `--auth-source <db>` (auth database, default: `admin` when user/password set)
45
-
46
- **Multi-connection:** Using `--conn-name` or `--env-file` creates a v2 config with named connections. Each connection can have its own env file and permission level. Existing v1 configs are automatically imported as the `default` connection when upgrading.
47
-
48
- > **AI agent note on `--use-env-refs`:** If an existing `.dbcli` config contains `{"$env": "DB_HOST"}` style references, the connection values are read from environment variables at runtime. Do NOT re-run `init` to replace these references with actual values — the env-ref format is intentional for CI/CD and multi-environment setups.
49
-
50
- ### use
51
-
52
- Switch or display the default database connection (v2 multi-connection config).
53
-
54
- ```bash
55
- dbcli use # Show current default connection
56
- dbcli use staging # Switch default to 'staging'
57
- dbcli use --list # List all connections (* marks default)
58
- ```
59
-
60
- Any command can also use `--use <name>` to temporarily select a connection without changing the default:
61
-
62
- ```bash
63
- dbcli query --use staging "SELECT * FROM users LIMIT 10"
64
- dbcli list --use prod
65
- ```
66
-
67
- **Requires v2 config** (created with `dbcli init --conn-name`).
68
-
69
- ### list
70
-
71
- List all tables (SQL) or collections (MongoDB).
72
-
73
- ```bash
74
- dbcli list
75
- dbcli list --format json
76
- ```
77
-
78
- **Permission:** query-only+
79
-
80
- > **MongoDB:** Lists collections with estimated document count instead of tables.
81
-
82
- ### schema
83
-
84
- Display table schema or scan entire database.
85
-
86
- ```bash
87
- dbcli schema # Scan all tables, save to .dbcli/schemas/
88
- dbcli schema users # Show single table schema
89
- dbcli schema users --format json
90
- dbcli schema --refresh # Detect and apply schema changes
91
- dbcli schema --reset # Clear all schema data and re-fetch
92
- dbcli schema --reset --force # Skip confirmation
93
-
94
- # Per-connection schema isolation (v2 multi-connection config)
95
- dbcli schema --use staging # Scan staging DB; saves to .dbcli/schemas/staging/
96
- dbcli schema --use prod # Scan prod DB; saves to .dbcli/schemas/prod/
97
- ```
98
-
99
- **Options:** `--format <table|json>`, `--refresh`, `--reset`, `--force`, `--use <connection>`
100
- **Permission:** query-only+
101
-
102
- **Schema storage (v1.4+):** Schema is persisted as layered files under `.dbcli/schemas/`. With v2 multi-connection config each connection gets its own subdirectory (`.dbcli/schemas/<connection>/`). Run `dbcli schema --use <connection>` once per connection before querying it — otherwise `schema <table>` may return data from the wrong connection's cache.
103
-
104
- ### query
105
-
106
- Execute SQL query (MySQL/PostgreSQL/MariaDB) or JSON filter/pipeline (MongoDB).
107
-
108
- ```bash
109
- # SQL databases
110
- dbcli query "SELECT * FROM users LIMIT 10"
111
- dbcli query "SELECT id, email FROM users" --format json
112
- dbcli query "SELECT * FROM logs" --no-limit
12
+ 1. `dbcli status` — permission level and system summary (no credentials).
13
+ 2. `dbcli blacklist list` sensitive data boundaries.
14
+ 3. `dbcli schema <table> --format json` real column names. **Never guess.**
15
+ 4. Run `query` / `insert` / `update` / `delete` / `export` within permission.
16
+ 5. All writes: `--dry-run` → run → `query` read-back to confirm.
113
17
 
114
- # MongoDB: JSON filter (find)
115
- dbcli query '{"status": "active"}' --collection users
116
- dbcli query '{"age": {"$gt": 18}}' --collection users --format json
18
+ Prefer `--format json` for agent-friendly output.
117
19
 
118
- # MongoDB: aggregation pipeline
119
- dbcli query '[{"$match": {"status": "active"}}, {"$group": {"_id": "$role", "count": {"$sum": 1}}}]' --collection users
120
- ```
121
-
122
- **Options:** `--format <table|json|csv>`, `--limit <number>`, `--no-limit`, `--collection <name>` (MongoDB only)
123
- **Permission:** query-only+
124
-
125
- > **MongoDB notes:**
126
- > - SQL syntax is rejected — use JSON object (filter) or JSON array (pipeline)
127
- > - `--collection <name>` is required
128
- > - Auto-limit does not apply; use `$limit` in your pipeline if needed
129
-
130
- ### insert
131
-
132
- Insert data into a table.
133
-
134
- ```bash
135
- dbcli insert users --data '{"name":"Alice","email":"alice@example.com"}'
136
- dbcli insert users --data '{"name":"Alice"}' --dry-run
137
- dbcli insert users --data '{"name":"Alice"}' --force
138
- ```
139
-
140
- **Options:** `--data <json>`, `--dry-run`, `--force`
141
- **Permission:** read-write+
142
-
143
- ### update
144
-
145
- Update existing data.
146
-
147
- ```bash
148
- dbcli update users --where "id=1" --set '{"name":"Bob"}'
149
- dbcli update users --where "id=1" --set '{"name":"Bob"}' --dry-run
150
- ```
151
-
152
- **Options:** `--where <condition>` (required), `--set <json>` (required), `--dry-run`, `--force`
153
- **Permission:** read-write+
154
-
155
- ### delete
156
-
157
- Delete data from a table.
158
-
159
- ```bash
160
- dbcli delete users --where "id=1"
161
- dbcli delete users --where "id=1" --dry-run
162
- dbcli delete users --where "id=1" --force
163
- ```
164
-
165
- **Options:** `--where <condition>` (required), `--dry-run`, `--force`
166
- **Permission:** data-admin+
167
-
168
- ### export
20
+ Full flags, per-command copy-paste blocks, `migrate` DDL, interactive `shell`, and MongoDB walkthroughs are in [reference.md](reference.md) (installed next to this file).
169
21
 
170
- Export query results to file or stdout.
22
+ ## Quick start
171
23
 
172
24
  ```bash
173
- dbcli export "SELECT * FROM users" --format csv --output users.csv
174
- dbcli export "SELECT * FROM users" --format csv --output users.csv --force # Skip overwrite confirmation
175
- dbcli export "SELECT * FROM users" --format json | jq '.[]'
25
+ dbcli init # Create .dbcli config (parses .env automatically)
26
+ dbcli schema # Scan all tables .dbcli/schemas/
27
+ dbcli query "SELECT * FROM users" # Execute SQL (auto LIMIT 1000)
176
28
  ```
177
29
 
178
- **Options:** `--format <json|csv>` (required), `--output <path>`, `--force`
179
- **Permission:** query-only+
30
+ ## Command overview
180
31
 
181
- ### blacklist
32
+ | Command | Min permission | Summary |
33
+ |---------|-----------------|---------|
34
+ | `init` | n/a | Create `.dbcli` (v1 single or v2 multi via `--conn-name` / `--env-file`). **Usually run by the human** — do NOT re-run to strip `{"$env"}` references; that format is intentional. |
35
+ | `use` | n/a | Show/switch default named connection (v2 only). |
36
+ | `list` | query-only+ | Tables (SQL) or collections (MongoDB). |
37
+ | `schema` | query-only+ | Per-table or full scan into `.dbcli/schemas/`; use `--use` for the correct connection cache. |
38
+ | `query` | query-only+ | SQL, or Mongo JSON filter / pipeline with `--collection`. |
39
+ | `insert` / `update` | read-write+ | JSON `--data` / `--set`; `--where` required on `update`; `--dry-run` first. |
40
+ | `delete` | data-admin+ | `--where` required; `--dry-run` first. |
41
+ | `export` | query-only+ | Query → CSV/JSON file or stdout. |
42
+ | `blacklist` | n/a | `list` / `table` / `column` subcommands redact sensitive data from query results. |
43
+ | `check` | query-only+ | Table health: nulls, duplicates, orphans, rowCount, size. |
44
+ | `diff` | query-only+ | Save/compare schema snapshots. |
45
+ | `status` | query-only+ | Safe JSON/text summary (no credentials). |
46
+ | `doctor` | n/a | Environment, config, connection, SRV diagnostics (Mongo), schema cache age. |
47
+ | `completion` | n/a | bash / zsh / fish scripts. |
48
+ | `upgrade` | n/a | Self-update from npm; 24h-cached version hints on every command. |
49
+ | `shell` | (same as query+) | Interactive REPL. |
50
+ | `migrate` | admin | **DDL; dry-run by default** — needs `--execute`; DROP also needs `--force`. |
182
51
 
183
- Manage sensitive data blacklist to prevent AI access to restricted tables/columns.
184
-
185
- ```bash
186
- dbcli blacklist list # Show current blacklist
187
- dbcli blacklist table add payments # Block entire table
188
- dbcli blacklist table remove payments # Unblock table
189
- dbcli blacklist column add users.password # Block specific column
190
- dbcli blacklist column remove users.password
191
- ```
192
-
193
- **Subcommands:** `list`, `table add <name>`, `table remove <name>`, `column add <table.column>`, `column remove <table.column>`
194
-
195
- ### check
196
-
197
- Run data health checks on tables.
198
-
199
- ```bash
200
- dbcli check users # Check single table
201
- dbcli check users --format json # JSON output (default)
202
- dbcli check --all # Check all tables (huge tables auto-skipped)
203
- dbcli check --all --include-large # Include huge tables
204
- dbcli check orders --checks nulls,orphans # Specific checks only
205
- dbcli check orders --sample 10000 # Sample size for large tables
206
- ```
207
-
208
- **Checks:** `nulls`, `duplicates`, `orphans`, `emptyStrings`, `rowCount`, `size`
209
- **Options:** `--all`, `--include-large`, `--checks <types>`, `--sample <number>`, `--format <json|table>`
210
- **Permission:** query-only+
211
-
212
- ### diff
213
-
214
- Compare schema snapshots to detect changes.
215
-
216
- ```bash
217
- dbcli diff --snapshot before.json # Save current schema snapshot
218
- dbcli diff --against before.json # Compare current vs snapshot
219
- dbcli diff --against before.json --format json
220
- ```
221
-
222
- **Options:** `--snapshot <path>`, `--against <path>`, `--format <json|table>`
223
- **Permission:** query-only+
224
-
225
- ### status
226
-
227
- Show current configuration status (safe for AI agents, no credentials exposed).
228
-
229
- ```bash
230
- dbcli status # JSON output (default)
231
- dbcli status --format text # Human-readable text output
232
- ```
52
+ `--use <name>` on any subcommand targets a v2 connection without changing the default.
233
53
 
234
- **Output:** `permission`, `system`, `blacklist` summary, `version`
235
- **Permission:** query-only+
54
+ ## Permission levels
236
55
 
237
- ### doctor
238
-
239
- Run diagnostic checks on environment, configuration, connection, and data.
240
-
241
- ```bash
242
- dbcli doctor # Colored text output
243
- dbcli doctor --format json # JSON output for AI agents
244
- ```
245
-
246
- **Checks:**
247
- - Environment: Bun version, dbcli version (compares with npm registry)
248
- - Configuration: config file exists/valid, permission level, blacklist completeness (detects unprotected sensitive columns)
249
- - Connection & Data: database connectivity, schema cache freshness (warns if > 7 days), large table warnings (> 1M rows)
250
-
251
- **Exit code:** 0 if all pass or warnings only, 1 if any error
252
- **Options:** `--format <text|json>`
253
-
254
- ### completion
255
-
256
- Generate shell completion scripts for tab auto-complete.
257
-
258
- ```bash
259
- dbcli completion bash # Output bash completion script
260
- dbcli completion zsh # Output zsh completion script
261
- dbcli completion fish # Output fish completion script
262
- dbcli completion --install # Auto-detect shell and install
263
- dbcli completion --install zsh # Install for specific shell
264
- ```
265
-
266
- **Supported shells:** bash, zsh, fish
267
-
268
- ### upgrade
269
-
270
- Check for updates and self-upgrade dbcli to the latest version from npm.
271
-
272
- ```bash
273
- dbcli upgrade # Check and upgrade if newer version available
274
- dbcli upgrade --check # Only check, do not upgrade
275
- ```
276
-
277
- **Options:** `--check`
278
-
279
- **Background check:** Every command silently checks the npm registry for a newer version (at most once per 24 hours, cached in `.dbcli/version-check.json`). If a newer version is found, a one-line hint is printed to stderr after the command completes. Pass `-q` / `--quiet` to suppress the hint.
280
-
281
- ### `dbcli shell`
282
-
283
- Start an interactive database shell.
284
-
285
- ```bash
286
- dbcli shell # Interactive mode with SQL + dbcli commands
287
- dbcli shell --sql # SQL-only mode
288
- ```
289
-
290
- Inside the shell:
291
- - Type SQL statements ending with `;` to execute
292
- - Type dbcli commands without the `dbcli` prefix (e.g., `schema users`)
293
- - Use Tab for auto-completion (SQL keywords, table names, column names)
294
- - Type `.help` for meta commands (.quit, .clear, .format, .history, .timing)
295
- - Multi-line SQL: keeps accumulating until `;` is found
296
- - History persists across sessions (~/.dbcli_history)
297
-
298
- ### migrate
299
-
300
- Schema DDL operations. **All commands default to dry-run** — use `--execute` to actually run the SQL. Destructive operations (DROP) also require `--force`.
301
-
302
- ```bash
303
- # Create table
304
- dbcli migrate create posts \
305
- --column "id:serial:pk" \
306
- --column "title:varchar(200):not-null" \
307
- --column "body:text" \
308
- --column "created_at:timestamp:default=now()"
309
-
310
- # Drop table (dry-run by default)
311
- dbcli migrate drop posts
312
- dbcli migrate drop posts --execute --force # Actually drop
313
-
314
- # Add/drop/alter column
315
- dbcli migrate add-column users bio text --nullable
316
- dbcli migrate drop-column users temp_field --execute --force
317
- dbcli migrate alter-column users name --type "varchar(200)"
318
- dbcli migrate alter-column users email --rename user_email
319
- dbcli migrate alter-column users status --set-default "'active'"
320
- dbcli migrate alter-column users bio --drop-default
321
- dbcli migrate alter-column users bio --set-nullable
322
- dbcli migrate alter-column users email --drop-nullable
323
-
324
- # Index management
325
- dbcli migrate add-index users --columns email --unique
326
- dbcli migrate add-index users --columns "last_name,first_name" --name idx_fullname
327
- dbcli migrate drop-index idx_fullname --execute --force
328
-
329
- # Constraint management
330
- dbcli migrate add-constraint orders --fk user_id --references users.id --on-delete cascade
331
- dbcli migrate add-constraint users --unique email
332
- dbcli migrate add-constraint users --check "age >= 0"
333
- dbcli migrate drop-constraint orders fk_orders_user_id --execute --force
334
-
335
- # Enum (PostgreSQL only — MySQL uses inline ENUM in column type)
336
- dbcli migrate add-enum status active inactive suspended
337
- dbcli migrate alter-enum status --add-value archived
338
- dbcli migrate drop-enum status --execute --force
339
- ```
340
-
341
- **Column spec format:** `name:type[:modifier[:modifier...]]`
342
- - Modifiers: `pk`, `not-null`, `unique`, `auto-increment`, `default=<value>`, `references=<table>.<column>`
343
- - Serial types: `serial`, `bigserial`, `smallserial` (auto-expand per DB dialect)
344
-
345
- **Options (all subcommands):** `--execute`, `--force`, `--config <path>`
346
- **Permission:** admin
347
-
348
- **AI agent note:** Always use dry-run first (no `--execute`) to preview generated SQL. Only add `--execute` after confirming the SQL is correct. For DROP operations, both `--execute` and `--force` are required.
349
-
350
- ## MongoDB Support
351
-
352
- MongoDB connections use a JSON-based query model instead of SQL.
353
-
354
- **Supported commands:** `init`, `list`, `query`, `status`, `use`, `shell`, `doctor`, `upgrade`, `completion`
355
-
356
- **Not supported (exit with error):** `schema`, `insert`, `update`, `delete`, `export`, `diff`, `migrate`, `check`
357
-
358
- ### MongoDB-specific workflow
359
-
360
- ```bash
361
- # 1. Initialize (URI or individual params)
362
- dbcli init --system mongodb --uri "mongodb://localhost:27017/mydb"
363
-
364
- # 2. List collections
365
- dbcli list --format json
366
-
367
- # 3. Query with JSON filter (find) or pipeline (aggregate)
368
- dbcli query '{}' --collection orders --format json # All documents
369
- dbcli query '{"status": "paid"}' --collection orders # Filter
370
- dbcli query '[{"$match": {"status":"paid"}}, {"$count":"total"}]' --collection orders # Pipeline
371
- ```
372
-
373
- ### Query syntax
374
-
375
- | Intent | Syntax |
376
- |--------|--------|
377
- | All documents | `'{}'` |
378
- | Field filter | `'{"field": "value"}'` |
379
- | Comparison | `'{"age": {"$gt": 18}}'` |
380
- | Aggregation | `'[{"$match": {...}}, {"$group": {...}}]'` |
381
-
382
- ## Permission Levels
383
-
384
- | Level | Allowed Operations |
385
- |-------|-------------------|
56
+ | Level | Allowed |
57
+ |-------|---------|
386
58
  | query-only | SELECT, list, schema, export |
387
- | read-write | query-only + INSERT, UPDATE |
388
- | data-admin | read-write + DELETE (full DML, no DDL) |
389
- | admin | data-admin + DDL (migrate create/drop/alter, DROP, ALTER, CREATE, TRUNCATE) |
390
-
391
- Set via `dbcli init --permission <level>` or in `.dbcli` config.
392
-
393
- ## Global Options
394
-
395
- | Flag | Description |
396
- |------|-------------|
397
- | `--config <path>` | Path to .dbcli config file (default: `.dbcli`) |
398
- | `--use <connection>` | Use a specific named connection (v2 config) |
399
- | `-v, --verbose` | Increase verbosity (`-v` verbose, `-vv` debug) |
400
- | `-q, --quiet` | Suppress non-essential output |
401
- | `--no-color` | Disable colored output (also respects `NO_COLOR` env var) |
402
-
403
- ## AI Agent Workflow
404
-
405
- **Before any database operation, follow this sequence:**
406
-
407
- 1. `dbcli status` — Check current permission level and system info (safe — no credentials exposed)
408
- 2. `dbcli blacklist list` — Confirm sensitive data is protected
409
- 3. `dbcli schema <table> --format json` — Verify actual column names
410
- 4. Then execute `query` / `insert` / `update` / `export` / `delete` according to your permission level
411
-
412
- **Never guess column names.** Naming conventions vary across projects (e.g. `frozen_balance` vs `freeze`, `amount` vs `balance_variable`). Always confirm with `schema` first.
413
-
414
- ## Debugging Workflow
415
-
416
- When investigating a bug related to database state:
417
-
418
- 1. `dbcli schema <table> --format json` — Confirm actual columns and types
419
- 2. `dbcli check <table> --format json` — Quick health scan (nulls, orphans, duplicates)
420
- 3. `dbcli query "SELECT * FROM <table> WHERE <condition>" --format json` — Inspect the specific record
421
- 4. Follow foreign keys from schema to trace related tables
422
- 5. Repeat step 3 for each related table to verify referential integrity
423
-
424
- **Key principle:** Let the data tell the story. Don't hypothesize before seeing actual state.
425
-
426
- ## Write Verification Workflow
59
+ | read-write | + INSERT, UPDATE |
60
+ | data-admin | + DELETE (DML, no DDL) |
61
+ | admin | + DDL via `migrate` and destructive ops |
427
62
 
428
- After any INSERT or UPDATE:
63
+ ## Multi-connection (v2)
429
64
 
430
- 1. `dbcli insert <table> --data '...' --dry-run` Preview SQL first
431
- 2. Execute the actual insert/update (remove --dry-run)
432
- 3. `dbcli query "SELECT * FROM <table> WHERE <condition>" --format json` Read back the written record
433
- 4. Compare the returned data against the intended values
434
- 5. If mismatch, check for triggers, default values, or blacklisted columns that may alter the result
65
+ - Each named connection has its own schema dir: `.dbcli/schemas/<connection>/`.
66
+ - Run `dbcli schema --use <name>` once per connection before `schema <table>` — otherwise the cache may return another connection's columns.
67
+ - `schema --refresh` / `--reset` manage the cache; see reference.md.
435
68
 
436
- ## Migration Safety Workflow
69
+ ## MongoDB
437
70
 
438
- Before and after running database migrations:
71
+ - JSON filter object (`find`) or JSON array (`aggregate`); SQL is rejected. `--collection <name>` is required on `query`.
72
+ - **Supported:** `init`, `list`, `query`, `status`, `use`, `shell`, `doctor`, `upgrade`, `completion`.
73
+ - **Not supported:** `schema`, `insert`, `update`, `delete`, `export`, `diff`, `migrate`, `check`.
74
+ - No auto-limit on MongoDB queries — use `$limit` in the pipeline if needed.
75
+ - See reference.md MongoDB section for full syntax and examples.
439
76
 
440
- 1. `dbcli diff --snapshot before.json` — Capture current schema
441
- 2. Run the migration
442
- 3. `dbcli diff --against before.json --format json` — Compare changes
443
- 4. Verify: added/removed/modified columns match the migration intent
444
- 5. `dbcli check <affected-tables> --format json` — Ensure no orphaned data from column drops or FK changes
77
+ ## Common workflows
445
78
 
446
- ## Health Check Workflow
447
-
448
- Periodic or on-demand database health scan:
449
-
450
- 1. `dbcli check --all --format json` Scan all tables (huge tables auto-skipped)
451
- 2. Review the summary: focus on orphans (broken FKs) and unexpected nulls
452
- 3. For any flagged issues, drill down with `dbcli query` to inspect specific records
453
- 4. Use `estimatedRowCount` and `sizeCategory` from schema to gauge table growth
454
-
455
- ## Code Generation from Schema
456
-
457
- When setting up a new project or migrating frameworks (e.g., Laravel to Bun + Drizzle):
458
-
459
- 1. `dbcli schema --format json` — Export full database schema with FK, indexes, defaults, enums
460
- 2. Use the JSON output to generate ORM schema definitions (Drizzle, Prisma, TypeORM, etc.)
461
- 3. For each table, map:
462
- - `primaryKey` + `autoIncrement` to ORM primary key decorator
463
- - `foreignKey` to relation/reference definitions
464
- - `indexes` to index declarations
465
- - `enumValues` to TypeScript enums or union types
466
- - `nullable` + `defaultValue` to column options
467
- - `comment` to JSDoc or schema comments
468
- 4. `dbcli check --all --format json` — Verify data health before trusting existing data
469
- 5. After ORM setup, run a test query through the new ORM and compare results with `dbcli query` to validate correctness
470
-
471
- ## Logic Verification Workflow
472
-
473
- Validate that application logic produces correct database state:
474
-
475
- 1. `dbcli query "SELECT * FROM <table> WHERE <condition>" --format json` — Capture state BEFORE
476
- 2. Execute the application logic (API call, script, etc.)
477
- 3. `dbcli query "SELECT * FROM <table> WHERE <condition>" --format json` — Capture state AFTER
478
- 4. Compare before/after:
479
- - Were the expected rows created/updated/deleted?
480
- - Are computed values correct (totals, balances, counters)?
481
- - Did related tables update consistently?
482
- 5. For complex transactions, check ALL affected tables
483
- 6. `dbcli check <affected-tables> --format json` — Ensure no orphaned data post-operation
484
-
485
- **When to use:** Unit tests mock the DB and may miss real constraint violations, triggers, and default values. dbcli verifies actual DB state — catches what mocks hide. Best practice: unit tests for logic, dbcli for integration truth.
486
-
487
- ## Natural Language Operations Workflow
488
-
489
- When the user describes a database operation in plain language:
490
-
491
- 1. **Parse intent** — Identify the operation type:
492
- - "查今天的訂單" → query (SELECT)
493
- - "幫我新增一筆記事" → insert (INSERT)
494
- - "把這筆訂單改成已出貨" → update (UPDATE)
495
-
496
- 2. **Resolve context** — Use schema to map natural language to actual columns:
497
- - `dbcli schema <table> --format json` — Get real column names
498
- - "今天的訂單" → `WHERE created_at >= CURDATE()` (verify column name from schema)
499
- - "已出貨" → check status column's enum values or existing data patterns
500
-
501
- 3. **Infer missing fields** — Use schema defaults and context:
502
- - `defaultValue` from schema → skip fields with sensible defaults
503
- - `autoIncrement` → don't include primary key in INSERT
504
- - `nullable: false` without default → MUST ask user for this value
505
-
506
- 4. **Safety gate**:
507
- - `dbcli blacklist list` — Ensure no blacklisted columns in the operation
508
- - Check `sizeCategory` — if querying a huge table without filter, warn and suggest conditions
509
- - For writes: ALWAYS use `--dry-run` first, show the SQL, then confirm
510
-
511
- 5. **Execute and verify**:
512
- - Run the operation
513
- - For INSERT/UPDATE: read back with `dbcli query` to confirm
514
- - Report result in natural language back to user
515
-
516
- **Key principle:** Never guess column names or values. Always schema-first, dry-run-first.
79
+ - **Debug odd state:** `schema` → `check` → `query` with tight `WHERE` → follow FKs from schema JSON. Evidence over theory.
80
+ - **After INSERT/UPDATE:** `--dry-run` → run → `query` read-back; explain mismatches via triggers, defaults, or blacklist.
81
+ - **Migrations:** `diff --snapshot` → `migrate` (dry-run `--execute`) → `diff --against` → `check` affected tables. DROP requires `--force`.
82
+ - **Health / growth:** `check --all` (huge tables skipped unless `--include-large`); consult schema `sizeCategory` before ad-hoc queries.
83
+ - **Codegen from live DB:** `schema --format json` to drive an ORM; cross-check once with `dbcli query`.
84
+ - **Integration truth:** `query` before run app `query` after. Unit-test mocks are not a substitute.
85
+ - **Natural language requests** (e.g. "update order to shipped"): pick `query` vs DML, map terms columns via `schema` (and enum values in data), respect blacklist and `sizeCategory`, **always `--dry-run` writes first**.
517
86
 
518
87
  ## Notes
519
88
 
520
- - **Use `--format json`**: More reliable for AI parsing than table format
521
- - **Use `--dry-run` before writes**: Preview generated SQL before executing
522
- - **auto-limit**: Query-only mode appends `LIMIT 1000` automatically. Use `--no-limit` for `information_schema` queries or statements incompatible with LIMIT
523
- - **Blacklist scope**: Blacklisted tables/columns are automatically filtered from query results
524
-
525
- ## Data Volume Protection
526
-
527
- Schema output includes `estimatedRowCount` and `sizeCategory` for each table:
528
-
529
- | Category | Rows | Behavior |
530
- |----------|------|----------|
531
- | small | < 10K | No restrictions |
532
- | medium | 10K - 100K | Suggest adding LIMIT/WHERE |
533
- | large | 100K - 1M | Warning displayed |
534
- | huge | > 1M | Full-table SELECT blocked without WHERE/LIMIT — use `--no-limit` to override |
535
-
536
- **Always check `sizeCategory` before querying.** For `large`/`huge` tables, add WHERE conditions or reasonable LIMIT.
89
+ - Query-only mode auto-appends `LIMIT 1000`; add `--no-limit` for `information_schema` or statements that break with `LIMIT`.
90
+ - Blacklisted tables and columns are redacted from query output.
91
+ - `schema` reports `estimatedRowCount` and `sizeCategory` (small / medium / large / huge). For large/huge tables add `WHERE` or `LIMIT` bands in reference.md.
92
+ - `doctor` on `mongodb+srv://` reports whether SRV resolves natively or through the DoH fallback — useful when the runtime restricts DNS.
93
+ - **Global flags:** `--config <path>`, `--use <name>`, `-v` / `-vv` / `-q`, `--no-color` (also honours `NO_COLOR`).