@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.
@@ -0,0 +1,371 @@
1
+ # dbcli — full command reference
2
+
3
+ Companion to [SKILL.md](SKILL.md). Exhaustive flags, copy-paste examples, `shell`, `completion`, `upgrade`, `migrate` DDL, and extended MongoDB examples.
4
+
5
+ ## Commands
6
+
7
+ ### init
8
+
9
+ Initialize `.dbcli` configuration file. Typically run manually by the developer — avoid running on behalf of the user unless explicitly requested.
10
+
11
+ ```bash
12
+ dbcli init # Single connection (v1 format)
13
+ dbcli init --system mysql --host localhost --port 3306 --user root --name mydb
14
+ dbcli init --use-env-refs # Store env var references
15
+ dbcli init --no-interactive --force # Non-interactive mode
16
+
17
+ # MongoDB
18
+ dbcli init --system mongodb --uri "mongodb://user:pass@host:27017/mydb?authSource=admin"
19
+ dbcli init --system mongodb --host localhost --port 27017 --user admin --password secret --name mydb
20
+ dbcli init --system mongodb --host localhost --port 27017 --name mydb # No auth
21
+
22
+ # Multi-connection (v2 format)
23
+ dbcli init --conn-name staging --env-file .env.staging # Named connection with custom env file
24
+ dbcli init --conn-name prod --env-file .env.production --use-env-refs --skip-test
25
+ dbcli init --remove staging # Remove a named connection
26
+ dbcli init --rename staging:production # Rename a connection
27
+ ```
28
+
29
+ **Key options:** `--system`, `--permission`, `--use-env-refs`, `--skip-test`, `--no-interactive`, `--force`, `--conn-name <name>`, `--env-file <path>`, `--remove <name>`, `--rename <old:new>`
30
+
31
+ **MongoDB-specific options:** `--uri <uri>` (full connection URI), `--auth-source <db>` (auth database, default: `admin` when user/password set)
32
+
33
+ **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.
34
+
35
+ > **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.
36
+
37
+ ### use
38
+
39
+ Switch or display the default database connection (v2 multi-connection config).
40
+
41
+ ```bash
42
+ dbcli use # Show current default connection
43
+ dbcli use staging # Switch default to 'staging'
44
+ dbcli use --list # List all connections (* marks default)
45
+ ```
46
+
47
+ Any command can also use `--use <name>` to temporarily select a connection without changing the default:
48
+
49
+ ```bash
50
+ dbcli query --use staging "SELECT * FROM users LIMIT 10"
51
+ dbcli list --use prod
52
+ ```
53
+
54
+ **Requires v2 config** (created with `dbcli init --conn-name`).
55
+
56
+ ### list
57
+
58
+ List all tables (SQL) or collections (MongoDB).
59
+
60
+ ```bash
61
+ dbcli list
62
+ dbcli list --format json
63
+ ```
64
+
65
+ **Permission:** query-only+
66
+
67
+ > **MongoDB:** Lists collections with estimated document count instead of tables.
68
+
69
+ ### schema
70
+
71
+ Display table schema or scan entire database.
72
+
73
+ ```bash
74
+ dbcli schema # Scan all tables, save to .dbcli/schemas/
75
+ dbcli schema users # Show single table schema
76
+ dbcli schema users --format json
77
+ dbcli schema --refresh # Detect and apply schema changes
78
+ dbcli schema --reset # Clear all schema data and re-fetch
79
+ dbcli schema --reset --force # Skip confirmation
80
+
81
+ # Per-connection schema isolation (v2 multi-connection config)
82
+ dbcli schema --use staging # Scan staging DB; saves to .dbcli/schemas/staging/
83
+ dbcli schema --use prod # Scan prod DB; saves to .dbcli/schemas/prod/
84
+ ```
85
+
86
+ **Options:** `--format <table|json>`, `--refresh`, `--reset`, `--force`, `--use <connection>`
87
+ **Permission:** query-only+
88
+
89
+ **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.
90
+
91
+ ### query
92
+
93
+ Execute SQL query (MySQL/PostgreSQL/MariaDB) or JSON filter/pipeline (MongoDB).
94
+
95
+ ```bash
96
+ # SQL databases
97
+ dbcli query "SELECT * FROM users LIMIT 10"
98
+ dbcli query "SELECT id, email FROM users" --format json
99
+ dbcli query "SELECT * FROM logs" --no-limit
100
+
101
+ # MongoDB: JSON filter (find)
102
+ dbcli query '{"status": "active"}' --collection users
103
+ dbcli query '{"age": {"$gt": 18}}' --collection users --format json
104
+
105
+ # MongoDB: aggregation pipeline
106
+ dbcli query '[{"$match": {"status": "active"}}, {"$group": {"_id": "$role", "count": {"$sum": 1}}}]' --collection users
107
+ ```
108
+
109
+ **Options:** `--format <table|json|csv>`, `--limit <number>`, `--no-limit`, `--collection <name>` (MongoDB only)
110
+ **Permission:** query-only+
111
+
112
+ > **MongoDB notes:**
113
+ > - SQL syntax is rejected — use JSON object (filter) or JSON array (pipeline)
114
+ > - `--collection <name>` is required
115
+ > - Auto-limit does not apply; use `$limit` in your pipeline if needed
116
+
117
+ ### insert
118
+
119
+ Insert data into a table.
120
+
121
+ ```bash
122
+ dbcli insert users --data '{"name":"Alice","email":"alice@example.com"}'
123
+ dbcli insert users --data '{"name":"Alice"}' --dry-run
124
+ dbcli insert users --data '{"name":"Alice"}' --force
125
+ ```
126
+
127
+ **Options:** `--data <json>`, `--dry-run`, `--force`
128
+ **Permission:** read-write+
129
+
130
+ ### update
131
+
132
+ Update existing data.
133
+
134
+ ```bash
135
+ dbcli update users --where "id=1" --set '{"name":"Bob"}'
136
+ dbcli update users --where "id=1" --set '{"name":"Bob"}' --dry-run
137
+ ```
138
+
139
+ **Options:** `--where <condition>` (required), `--set <json>` (required), `--dry-run`, `--force`
140
+ **Permission:** read-write+
141
+
142
+ ### delete
143
+
144
+ Delete data from a table.
145
+
146
+ ```bash
147
+ dbcli delete users --where "id=1"
148
+ dbcli delete users --where "id=1" --dry-run
149
+ dbcli delete users --where "id=1" --force
150
+ ```
151
+
152
+ **Options:** `--where <condition>` (required), `--dry-run`, `--force`
153
+ **Permission:** data-admin+
154
+
155
+ ### export
156
+
157
+ Export query results to file or stdout.
158
+
159
+ ```bash
160
+ dbcli export "SELECT * FROM users" --format csv --output users.csv
161
+ dbcli export "SELECT * FROM users" --format csv --output users.csv --force # Skip overwrite confirmation
162
+ dbcli export "SELECT * FROM users" --format json | jq '.[]'
163
+ ```
164
+
165
+ **Options:** `--format <json|csv>` (required), `--output <path>`, `--force`
166
+ **Permission:** query-only+
167
+
168
+ ### blacklist
169
+
170
+ Manage sensitive data blacklist to prevent AI access to restricted tables/columns.
171
+
172
+ ```bash
173
+ dbcli blacklist list # Show current blacklist
174
+ dbcli blacklist table add payments # Block entire table
175
+ dbcli blacklist table remove payments # Unblock table
176
+ dbcli blacklist column add users.password # Block specific column
177
+ dbcli blacklist column remove users.password
178
+ ```
179
+
180
+ **Subcommands:** `list`, `table add <name>`, `table remove <name>`, `column add <table.column>`, `column remove <table.column>`
181
+
182
+ ### check
183
+
184
+ Run data health checks on tables.
185
+
186
+ ```bash
187
+ dbcli check users # Check single table
188
+ dbcli check users --format json # JSON output (default)
189
+ dbcli check --all # Check all tables (huge tables auto-skipped)
190
+ dbcli check --all --include-large # Include huge tables
191
+ dbcli check orders --checks nulls,orphans # Specific checks only
192
+ dbcli check orders --sample 10000 # Sample size for large tables
193
+ ```
194
+
195
+ **Checks:** `nulls`, `duplicates`, `orphans`, `emptyStrings`, `rowCount`, `size`
196
+ **Options:** `--all`, `--include-large`, `--checks <types>`, `--sample <number>`, `--format <json|table>`
197
+ **Permission:** query-only+
198
+
199
+ ### diff
200
+
201
+ Compare schema snapshots to detect changes.
202
+
203
+ ```bash
204
+ dbcli diff --snapshot before.json # Save current schema snapshot
205
+ dbcli diff --against before.json # Compare current vs snapshot
206
+ dbcli diff --against before.json --format json
207
+ ```
208
+
209
+ **Options:** `--snapshot <path>`, `--against <path>`, `--format <json|table>`
210
+ **Permission:** query-only+
211
+
212
+ ### status
213
+
214
+ Show current configuration status (safe for AI agents, no credentials exposed).
215
+
216
+ ```bash
217
+ dbcli status # JSON output (default)
218
+ dbcli status --format text # Human-readable text output
219
+ ```
220
+
221
+ **Output:** `permission`, `system`, `blacklist` summary, `version`
222
+ **Permission:** query-only+
223
+
224
+ ### doctor
225
+
226
+ Run diagnostic checks on environment, configuration, connection, and data.
227
+
228
+ ```bash
229
+ dbcli doctor # Colored text output
230
+ dbcli doctor --format json # JSON output for AI agents
231
+ ```
232
+
233
+ **Checks:**
234
+ - Environment: Bun version, dbcli version (compares with npm registry)
235
+ - Configuration: config file exists/valid, permission level, blacklist completeness (detects unprotected sensitive columns)
236
+ - Connection & Data: database connectivity, schema cache freshness (warns if > 7 days), large table warnings (> 1M rows)
237
+
238
+ > **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.
239
+
240
+ **Exit code:** 0 if all pass or warnings only, 1 if any error
241
+ **Options:** `--format <text|json>`
242
+
243
+ ### completion
244
+
245
+ Generate shell completion scripts for tab auto-complete.
246
+
247
+ ```bash
248
+ dbcli completion bash # Output bash completion script
249
+ dbcli completion zsh # Output zsh completion script
250
+ dbcli completion fish # Output fish completion script
251
+ dbcli completion --install # Auto-detect shell and install
252
+ dbcli completion --install zsh # Install for specific shell
253
+ ```
254
+
255
+ **Supported shells:** bash, zsh, fish
256
+
257
+ ### upgrade
258
+
259
+ Check for updates and self-upgrade dbcli to the latest version from npm.
260
+
261
+ ```bash
262
+ dbcli upgrade # Check and upgrade if newer version available
263
+ dbcli upgrade --check # Only check, do not upgrade
264
+ ```
265
+
266
+ **Options:** `--check`
267
+
268
+ **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.
269
+
270
+ ### `dbcli shell`
271
+
272
+ Start an interactive database shell.
273
+
274
+ ```bash
275
+ dbcli shell # Interactive mode with SQL + dbcli commands
276
+ dbcli shell --sql # SQL-only mode
277
+ ```
278
+
279
+ Inside the shell:
280
+ - Type SQL statements ending with `;` to execute
281
+ - Type dbcli commands without the `dbcli` prefix (e.g., `schema users`)
282
+ - Use Tab for auto-completion (SQL keywords, table names, column names)
283
+ - Type `.help` for meta commands (.quit, .clear, .format, .history, .timing)
284
+ - Multi-line SQL: keeps accumulating until `;` is found
285
+ - History persists across sessions (~/.dbcli_history)
286
+
287
+ ### migrate
288
+
289
+ Schema DDL operations. **All commands default to dry-run** — use `--execute` to actually run the SQL. Destructive operations (DROP) also require `--force`.
290
+
291
+ ```bash
292
+ # Create table
293
+ dbcli migrate create posts \
294
+ --column "id:serial:pk" \
295
+ --column "title:varchar(200):not-null" \
296
+ --column "body:text" \
297
+ --column "created_at:timestamp:default=now()"
298
+
299
+ # Drop table (dry-run by default)
300
+ dbcli migrate drop posts
301
+ dbcli migrate drop posts --execute --force # Actually drop
302
+
303
+ # Add/drop/alter column
304
+ dbcli migrate add-column users bio text --nullable
305
+ dbcli migrate drop-column users temp_field --execute --force
306
+ dbcli migrate alter-column users name --type "varchar(200)"
307
+ dbcli migrate alter-column users email --rename user_email
308
+ dbcli migrate alter-column users status --set-default "'active'"
309
+ dbcli migrate alter-column users bio --drop-default
310
+ dbcli migrate alter-column users bio --set-nullable
311
+ dbcli migrate alter-column users email --drop-nullable
312
+
313
+ # Index management
314
+ dbcli migrate add-index users --columns email --unique
315
+ dbcli migrate add-index users --columns "last_name,first_name" --name idx_fullname
316
+ dbcli migrate drop-index idx_fullname --execute --force
317
+
318
+ # Constraint management
319
+ dbcli migrate add-constraint orders --fk user_id --references users.id --on-delete cascade
320
+ dbcli migrate add-constraint users --unique email
321
+ dbcli migrate add-constraint users --check "age >= 0"
322
+ dbcli migrate drop-constraint orders fk_orders_user_id --execute --force
323
+
324
+ # Enum (PostgreSQL only — MySQL uses inline ENUM in column type)
325
+ dbcli migrate add-enum status active inactive suspended
326
+ dbcli migrate alter-enum status --add-value archived
327
+ dbcli migrate drop-enum status --execute --force
328
+ ```
329
+
330
+ **Column spec format:** `name:type[:modifier[:modifier...]]`
331
+ - Modifiers: `pk`, `not-null`, `unique`, `auto-increment`, `default=<value>`, `references=<table>.<column>`
332
+ - Serial types: `serial`, `bigserial`, `smallserial` (auto-expand per DB dialect)
333
+
334
+ **Options (all subcommands):** `--execute`, `--force`, `--config <path>`
335
+ **Permission:** admin
336
+
337
+ **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.
338
+
339
+ ## MongoDB Support
340
+
341
+ MongoDB connections use a JSON-based query model instead of SQL.
342
+
343
+ Atlas-style `mongodb+srv://` URIs are supported. `list` and `query` run against the database configured for the connection, and `query` always requires `--collection <name>`.
344
+
345
+ **Supported commands:** `init`, `list`, `query`, `status`, `use`, `shell`, `doctor`, `upgrade`, `completion`
346
+
347
+ **Not supported (exit with error):** `schema`, `insert`, `update`, `delete`, `export`, `diff`, `migrate`, `check`
348
+
349
+ ### MongoDB-specific workflow
350
+
351
+ ```bash
352
+ # 1. Initialize (URI or individual params)
353
+ dbcli init --system mongodb --uri "mongodb+srv://user:pass@cluster.example.mongodb.net/mydb"
354
+
355
+ # 2. List collections
356
+ dbcli list --format json
357
+
358
+ # 3. Query with JSON filter (find) or pipeline (aggregate)
359
+ dbcli query '{}' --collection orders --format json # All documents
360
+ dbcli query '{"status": "paid"}' --collection orders # Filter
361
+ dbcli query '[{"$match": {"status":"paid"}}, {"$count":"total"}]' --collection orders # Pipeline
362
+ ```
363
+
364
+ ### Query syntax
365
+
366
+ | Intent | Syntax |
367
+ |--------|--------|
368
+ | All documents | `'{}'` |
369
+ | Field filter | `'{"field": "value"}'` |
370
+ | Comparison | `'{"age": {"$gt": 18}}'` |
371
+ | Aggregation | `'[{"$match": {...}}, {"$group": {...}}]'` |