@carllee1983/dbcli 1.5.2 → 1.7.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,540 +1,135 @@
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).
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.
107
17
 
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
113
-
114
- # MongoDB: JSON filter (find)
115
- dbcli query '{"status": "active"}' --collection users
116
- dbcli query '{"age": {"$gt": 18}}' --collection users --format json
117
-
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
18
+ Prefer `--format json` for agent-friendly output.
129
19
 
130
- ### insert
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).
131
21
 
132
- Insert data into a table.
22
+ ## Quick start
133
23
 
134
24
  ```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
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)
138
28
  ```
139
29
 
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
169
-
170
- Export query results to file or stdout.
171
-
172
- ```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 '.[]'
176
- ```
177
-
178
- **Options:** `--format <json|csv>` (required), `--output <path>`, `--force`
179
- **Permission:** query-only+
180
-
181
- ### blacklist
182
-
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
- ```
30
+ ## Command overview
221
31
 
222
- **Options:** `--snapshot <path>`, `--against <path>`, `--format <json|table>`
223
- **Permission:** query-only+
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`. |
224
51
 
225
- ### status
52
+ `--use <name>` on any subcommand targets a v2 connection without changing the default.
226
53
 
227
- Show current configuration status (safe for AI agents, no credentials exposed).
54
+ ## Permission levels
228
55
 
229
- ```bash
230
- dbcli status # JSON output (default)
231
- dbcli status --format text # Human-readable text output
232
- ```
233
-
234
- **Output:** `permission`, `system`, `blacklist` summary, `version`
235
- **Permission:** query-only+
236
-
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
- > **MongoDB SRV diagnostics:** When the active connection uses `mongodb+srv://`, `doctor` reports whether the current runtime can resolve SRV records directly or only through the DNS-over-HTTPS fallback used by dbcli. This helps spot execution-environment DNS restrictions even when Compass can connect.
252
-
253
- **Exit code:** 0 if all pass or warnings only, 1 if any error
254
- **Options:** `--format <text|json>`
255
-
256
- ### completion
257
-
258
- Generate shell completion scripts for tab auto-complete.
259
-
260
- ```bash
261
- dbcli completion bash # Output bash completion script
262
- dbcli completion zsh # Output zsh completion script
263
- dbcli completion fish # Output fish completion script
264
- dbcli completion --install # Auto-detect shell and install
265
- dbcli completion --install zsh # Install for specific shell
266
- ```
267
-
268
- **Supported shells:** bash, zsh, fish
269
-
270
- ### upgrade
271
-
272
- Check for updates and self-upgrade dbcli to the latest version from npm.
273
-
274
- ```bash
275
- dbcli upgrade # Check and upgrade if newer version available
276
- dbcli upgrade --check # Only check, do not upgrade
277
- ```
278
-
279
- **Options:** `--check`
280
-
281
- **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.
282
-
283
- ### `dbcli shell`
284
-
285
- Start an interactive database shell.
286
-
287
- ```bash
288
- dbcli shell # Interactive mode with SQL + dbcli commands
289
- dbcli shell --sql # SQL-only mode
290
- ```
291
-
292
- Inside the shell:
293
- - Type SQL statements ending with `;` to execute
294
- - Type dbcli commands without the `dbcli` prefix (e.g., `schema users`)
295
- - Use Tab for auto-completion (SQL keywords, table names, column names)
296
- - Type `.help` for meta commands (.quit, .clear, .format, .history, .timing)
297
- - Multi-line SQL: keeps accumulating until `;` is found
298
- - History persists across sessions (~/.dbcli_history)
299
-
300
- ### migrate
301
-
302
- Schema DDL operations. **All commands default to dry-run** — use `--execute` to actually run the SQL. Destructive operations (DROP) also require `--force`.
303
-
304
- ```bash
305
- # Create table
306
- dbcli migrate create posts \
307
- --column "id:serial:pk" \
308
- --column "title:varchar(200):not-null" \
309
- --column "body:text" \
310
- --column "created_at:timestamp:default=now()"
311
-
312
- # Drop table (dry-run by default)
313
- dbcli migrate drop posts
314
- dbcli migrate drop posts --execute --force # Actually drop
315
-
316
- # Add/drop/alter column
317
- dbcli migrate add-column users bio text --nullable
318
- dbcli migrate drop-column users temp_field --execute --force
319
- dbcli migrate alter-column users name --type "varchar(200)"
320
- dbcli migrate alter-column users email --rename user_email
321
- dbcli migrate alter-column users status --set-default "'active'"
322
- dbcli migrate alter-column users bio --drop-default
323
- dbcli migrate alter-column users bio --set-nullable
324
- dbcli migrate alter-column users email --drop-nullable
325
-
326
- # Index management
327
- dbcli migrate add-index users --columns email --unique
328
- dbcli migrate add-index users --columns "last_name,first_name" --name idx_fullname
329
- dbcli migrate drop-index idx_fullname --execute --force
330
-
331
- # Constraint management
332
- dbcli migrate add-constraint orders --fk user_id --references users.id --on-delete cascade
333
- dbcli migrate add-constraint users --unique email
334
- dbcli migrate add-constraint users --check "age >= 0"
335
- dbcli migrate drop-constraint orders fk_orders_user_id --execute --force
336
-
337
- # Enum (PostgreSQL only — MySQL uses inline ENUM in column type)
338
- dbcli migrate add-enum status active inactive suspended
339
- dbcli migrate alter-enum status --add-value archived
340
- dbcli migrate drop-enum status --execute --force
341
- ```
342
-
343
- **Column spec format:** `name:type[:modifier[:modifier...]]`
344
- - Modifiers: `pk`, `not-null`, `unique`, `auto-increment`, `default=<value>`, `references=<table>.<column>`
345
- - Serial types: `serial`, `bigserial`, `smallserial` (auto-expand per DB dialect)
346
-
347
- **Options (all subcommands):** `--execute`, `--force`, `--config <path>`
348
- **Permission:** admin
349
-
350
- **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.
351
-
352
- ## MongoDB Support
353
-
354
- MongoDB connections use a JSON-based query model instead of SQL.
355
-
356
- Atlas-style `mongodb+srv://` URIs are supported. `list` and `query` run against the database configured for the connection, and `query` always requires `--collection <name>`.
357
-
358
- **Supported commands:** `init`, `list`, `query`, `status`, `use`, `shell`, `doctor`, `upgrade`, `completion`
359
-
360
- **Not supported (exit with error):** `schema`, `insert`, `update`, `delete`, `export`, `diff`, `migrate`, `check`
361
-
362
- ### MongoDB-specific workflow
363
-
364
- ```bash
365
- # 1. Initialize (URI or individual params)
366
- dbcli init --system mongodb --uri "mongodb+srv://user:pass@cluster.example.mongodb.net/mydb"
367
-
368
- # 2. List collections
369
- dbcli list --format json
370
-
371
- # 3. Query with JSON filter (find) or pipeline (aggregate)
372
- dbcli query '{}' --collection orders --format json # All documents
373
- dbcli query '{"status": "paid"}' --collection orders # Filter
374
- dbcli query '[{"$match": {"status":"paid"}}, {"$count":"total"}]' --collection orders # Pipeline
375
- ```
376
-
377
- ### Query syntax
378
-
379
- | Intent | Syntax |
380
- |--------|--------|
381
- | All documents | `'{}'` |
382
- | Field filter | `'{"field": "value"}'` |
383
- | Comparison | `'{"age": {"$gt": 18}}'` |
384
- | Aggregation | `'[{"$match": {...}}, {"$group": {...}}]'` |
385
-
386
- ## Permission Levels
387
-
388
- | Level | Allowed Operations |
389
- |-------|-------------------|
56
+ | Level | Allowed |
57
+ |-------|---------|
390
58
  | query-only | SELECT, list, schema, export |
391
- | read-write | query-only + INSERT, UPDATE |
392
- | data-admin | read-write + DELETE (full DML, no DDL) |
393
- | admin | data-admin + DDL (migrate create/drop/alter, DROP, ALTER, CREATE, TRUNCATE) |
394
-
395
- Set via `dbcli init --permission <level>` or in `.dbcli` config.
396
-
397
- ## Global Options
398
-
399
- | Flag | Description |
400
- |------|-------------|
401
- | `--config <path>` | Path to .dbcli config file (default: `.dbcli`) |
402
- | `--use <connection>` | Use a specific named connection (v2 config) |
403
- | `-v, --verbose` | Increase verbosity (`-v` verbose, `-vv` debug) |
404
- | `-q, --quiet` | Suppress non-essential output |
405
- | `--no-color` | Disable colored output (also respects `NO_COLOR` env var) |
59
+ | read-write | + INSERT, UPDATE |
60
+ | data-admin | + DELETE (DML, no DDL) |
61
+ | admin | + DDL via `migrate` and destructive ops |
406
62
 
407
- ## AI Agent Workflow
63
+ ## Multi-connection (v2)
408
64
 
409
- **Before any database operation, follow this sequence:**
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.
410
68
 
411
- 1. `dbcli status` — Check current permission level and system info (safe — no credentials exposed)
412
- 2. `dbcli blacklist list` — Confirm sensitive data is protected
413
- 3. `dbcli schema <table> --format json` — Verify actual column names
414
- 4. Then execute `query` / `insert` / `update` / `export` / `delete` according to your permission level
69
+ ## MongoDB
415
70
 
416
- **Never guess column names.** Naming conventions vary across projects (e.g. `frozen_balance` vs `freeze`, `amount` vs `balance_variable`). Always confirm with `schema` first.
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.
417
76
 
418
- ## Debugging Workflow
77
+ ## Saved queries
419
78
 
420
- When investigating a bug related to database state:
79
+ Run reusable parameterised SELECT snippets stored in your repo.
421
80
 
422
- 1. `dbcli schema <table> --format json` — Confirm actual columns and types
423
- 2. `dbcli check <table> --format json` — Quick health scan (nulls, orphans, duplicates)
424
- 3. `dbcli query "SELECT * FROM <table> WHERE <condition>" --format json` — Inspect the specific record
425
- 4. Follow foreign keys from schema to trace related tables
426
- 5. Repeat step 3 for each related table to verify referential integrity
81
+ | Step | Command |
82
+ |------|---------|
83
+ | 1. Discover | `dbcli queries list` |
84
+ | 2. Inspect | `dbcli queries show @<name>` |
85
+ | 3. Run | `dbcli q @<name> --param k=v` |
427
86
 
428
- **Key principle:** Let the data tell the story. Don't hypothesize before seeing actual state.
87
+ Snippets resolve from three layers, **local > shared > builtin** (local wins):
88
+ - `builtin` — bundled with dbcli (e.g. `@diag/*`); read-only at runtime
89
+ - `.dbcli-shared/queries/` — committed, team-shared
90
+ - `.dbcli/queries/` — gitignored, personal override
429
91
 
430
- ## Write Verification Workflow
92
+ Manage local snippets with `queries new | edit | delete | rename | copy | import | export`
93
+ (see reference.md). Use `copy` / `import` to fork a builtin or shared snippet into the
94
+ local layer for editing.
431
95
 
432
- After any INSERT or UPDATE:
96
+ Each `.sql` file may declare YAML frontmatter inside `-- ---` blocks
97
+ (name, description, engine, params, tags). See `dbcli queries show @<name> --format json`
98
+ for the machine-readable contract.
433
99
 
434
- 1. `dbcli insert <table> --data '...' --dry-run` Preview SQL first
435
- 2. Execute the actual insert/update (remove --dry-run)
436
- 3. `dbcli query "SELECT * FROM <table> WHERE <condition>" --format json` — Read back the written record
437
- 4. Compare the returned data against the intended values
438
- 5. If mismatch, check for triggers, default values, or blacklisted columns that may alter the result
100
+ ### Built-in diagnostic snippets
439
101
 
440
- ## Migration Safety Workflow
102
+ dbcli ships ready-made diagnostic queries. Run with `dbcli q @diag/<topic>`:
441
103
 
442
- Before and after running database migrations:
104
+ | key | purpose |
105
+ |-------------------------|------------------------------------------|
106
+ | `@diag/connections` | active sessions |
107
+ | `@diag/long-running` | queries above `min_seconds` (default 30) |
108
+ | `@diag/table-sizes` | table data/index size with row counts |
109
+ | `@diag/index-usage` | indexes by scan count |
110
+ | `@diag/missing-indexes` | tables dominated by sequential scans |
111
+ | `@diag/locks` | lock-wait chains |
112
+ | `@diag/db-size` | database size summary |
113
+ | `@diag/cache-hit` | buffer cache hit ratios |
443
114
 
444
- 1. `dbcli diff --snapshot before.json` Capture current schema
445
- 2. Run the migration
446
- 3. `dbcli diff --against before.json --format json` — Compare changes
447
- 4. Verify: added/removed/modified columns match the migration intent
448
- 5. `dbcli check <affected-tables> --format json` — Ensure no orphaned data from column drops or FK changes
115
+ Engine variants are picked automatically based on the active connection.
116
+ Override any of them by placing a same-named file under `.dbcli-shared/queries/`
117
+ or `.dbcli/queries/`.
449
118
 
450
- ## Health Check Workflow
119
+ ## Common workflows
451
120
 
452
- Periodic or on-demand database health scan:
453
-
454
- 1. `dbcli check --all --format json` Scan all tables (huge tables auto-skipped)
455
- 2. Review the summary: focus on orphans (broken FKs) and unexpected nulls
456
- 3. For any flagged issues, drill down with `dbcli query` to inspect specific records
457
- 4. Use `estimatedRowCount` and `sizeCategory` from schema to gauge table growth
458
-
459
- ## Code Generation from Schema
460
-
461
- When setting up a new project or migrating frameworks (e.g., Laravel to Bun + Drizzle):
462
-
463
- 1. `dbcli schema --format json` — Export full database schema with FK, indexes, defaults, enums
464
- 2. Use the JSON output to generate ORM schema definitions (Drizzle, Prisma, TypeORM, etc.)
465
- 3. For each table, map:
466
- - `primaryKey` + `autoIncrement` to ORM primary key decorator
467
- - `foreignKey` to relation/reference definitions
468
- - `indexes` to index declarations
469
- - `enumValues` to TypeScript enums or union types
470
- - `nullable` + `defaultValue` to column options
471
- - `comment` to JSDoc or schema comments
472
- 4. `dbcli check --all --format json` — Verify data health before trusting existing data
473
- 5. After ORM setup, run a test query through the new ORM and compare results with `dbcli query` to validate correctness
474
-
475
- ## Logic Verification Workflow
476
-
477
- Validate that application logic produces correct database state:
478
-
479
- 1. `dbcli query "SELECT * FROM <table> WHERE <condition>" --format json` — Capture state BEFORE
480
- 2. Execute the application logic (API call, script, etc.)
481
- 3. `dbcli query "SELECT * FROM <table> WHERE <condition>" --format json` — Capture state AFTER
482
- 4. Compare before/after:
483
- - Were the expected rows created/updated/deleted?
484
- - Are computed values correct (totals, balances, counters)?
485
- - Did related tables update consistently?
486
- 5. For complex transactions, check ALL affected tables
487
- 6. `dbcli check <affected-tables> --format json` — Ensure no orphaned data post-operation
488
-
489
- **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.
490
-
491
- ## Natural Language Operations Workflow
492
-
493
- When the user describes a database operation in plain language:
494
-
495
- 1. **Parse intent** — Identify the operation type:
496
- - "查今天的訂單" → query (SELECT)
497
- - "幫我新增一筆記事" → insert (INSERT)
498
- - "把這筆訂單改成已出貨" → update (UPDATE)
499
-
500
- 2. **Resolve context** — Use schema to map natural language to actual columns:
501
- - `dbcli schema <table> --format json` — Get real column names
502
- - "今天的訂單" → `WHERE created_at >= CURDATE()` (verify column name from schema)
503
- - "已出貨" → check status column's enum values or existing data patterns
504
-
505
- 3. **Infer missing fields** — Use schema defaults and context:
506
- - `defaultValue` from schema → skip fields with sensible defaults
507
- - `autoIncrement` → don't include primary key in INSERT
508
- - `nullable: false` without default → MUST ask user for this value
509
-
510
- 4. **Safety gate**:
511
- - `dbcli blacklist list` — Ensure no blacklisted columns in the operation
512
- - Check `sizeCategory` — if querying a huge table without filter, warn and suggest conditions
513
- - For writes: ALWAYS use `--dry-run` first, show the SQL, then confirm
514
-
515
- 5. **Execute and verify**:
516
- - Run the operation
517
- - For INSERT/UPDATE: read back with `dbcli query` to confirm
518
- - Report result in natural language back to user
519
-
520
- **Key principle:** Never guess column names or values. Always schema-first, dry-run-first.
121
+ - **Debug odd state:** `schema` → `check` → `query` with tight `WHERE` → follow FKs from schema JSON. Evidence over theory.
122
+ - **After INSERT/UPDATE:** `--dry-run` → run → `query` read-back; explain mismatches via triggers, defaults, or blacklist.
123
+ - **Migrations:** `diff --snapshot` `migrate` (dry-run `--execute`) `diff --against` → `check` affected tables. DROP requires `--force`.
124
+ - **Health / growth:** `check --all` (huge tables skipped unless `--include-large`); consult schema `sizeCategory` before ad-hoc queries.
125
+ - **Codegen from live DB:** `schema --format json` to drive an ORM; cross-check once with `dbcli query`.
126
+ - **Integration truth:** `query` before → run app → `query` after. Unit-test mocks are not a substitute.
127
+ - **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**.
521
128
 
522
129
  ## Notes
523
130
 
524
- - **Use `--format json`**: More reliable for AI parsing than table format
525
- - **Use `--dry-run` before writes**: Preview generated SQL before executing
526
- - **auto-limit**: Query-only mode appends `LIMIT 1000` automatically. Use `--no-limit` for `information_schema` queries or statements incompatible with LIMIT
527
- - **Blacklist scope**: Blacklisted tables/columns are automatically filtered from query results
528
-
529
- ## Data Volume Protection
530
-
531
- Schema output includes `estimatedRowCount` and `sizeCategory` for each table:
532
-
533
- | Category | Rows | Behavior |
534
- |----------|------|----------|
535
- | small | < 10K | No restrictions |
536
- | medium | 10K - 100K | Suggest adding LIMIT/WHERE |
537
- | large | 100K - 1M | Warning displayed |
538
- | huge | > 1M | Full-table SELECT blocked without WHERE/LIMIT — use `--no-limit` to override |
539
-
540
- **Always check `sizeCategory` before querying.** For `large`/`huge` tables, add WHERE conditions or reasonable LIMIT.
131
+ - Query-only mode auto-appends `LIMIT 1000`; add `--no-limit` for `information_schema` or statements that break with `LIMIT`.
132
+ - Blacklisted tables and columns are redacted from query output.
133
+ - `schema` reports `estimatedRowCount` and `sizeCategory` (small / medium / large / huge). For large/huge tables add `WHERE` or `LIMIT` bands in reference.md.
134
+ - `doctor` on `mongodb+srv://` reports whether SRV resolves natively or through the DoH fallback — useful when the runtime restricts DNS.
135
+ - **Global flags:** `--config <path>`, `--use <name>`, `-v` / `-vv` / `-q`, `--no-color` (also honours `NO_COLOR`).