@carllee1983/dbcli 0.1.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/README.md ADDED
@@ -0,0 +1,725 @@
1
+ # dbcli — Database CLI for AI Agents
2
+
3
+ A unified database CLI tool that enables AI agents (Claude Code, Gemini, Copilot, Cursor) to safely query, discover, and operate on databases.
4
+
5
+ **Core Value:** AI agents can safely and intelligently access project databases through a single, permission-controlled CLI tool.
6
+
7
+ ## Quick Start
8
+
9
+ ### Installation
10
+
11
+ #### Global Installation (Recommended)
12
+
13
+ ```bash
14
+ npm install -g dbcli
15
+ ```
16
+
17
+ #### Zero-Install (No Installation Needed)
18
+
19
+ ```bash
20
+ npx dbcli init
21
+ npx dbcli query "SELECT * FROM users"
22
+ ```
23
+
24
+ #### Development Installation
25
+
26
+ ```bash
27
+ git clone <repository>
28
+ cd dbcli
29
+ bun install
30
+ bun run dev -- --help
31
+ ```
32
+
33
+ ### First Steps
34
+
35
+ ```bash
36
+ # Initialize project with database connection
37
+ dbcli init
38
+
39
+ # List available tables
40
+ dbcli list
41
+
42
+ # View table structure
43
+ dbcli schema users
44
+
45
+ # Query data
46
+ dbcli query "SELECT * FROM users"
47
+
48
+ # Generate AI agent skill
49
+ dbcli skill --install claude
50
+ ```
51
+
52
+ ---
53
+
54
+ ## API Reference
55
+
56
+ ### Commands
57
+
58
+ #### `dbcli init`
59
+
60
+ Initialize a new dbcli project with database connection configuration.
61
+
62
+ **Usage:**
63
+ ```bash
64
+ dbcli init
65
+ ```
66
+
67
+ **Behavior:**
68
+ - Reads `.env` file if present (auto-fills DATABASE_URL, DB_* variables)
69
+ - Prompts for missing values (host, port, user, password, database name, permission level)
70
+ - Creates `.dbcli` JSON config file in project root
71
+ - Tests database connection before saving
72
+
73
+ **Examples:**
74
+ ```bash
75
+ # Interactive initialization
76
+ dbcli init
77
+
78
+ # With environment variables pre-set
79
+ export DATABASE_URL="postgresql://user:pass@localhost/mydb"
80
+ dbcli init
81
+
82
+ # Specify permission level
83
+ echo "PERMISSION_LEVEL=admin" >> .env && dbcli init
84
+ ```
85
+
86
+ ---
87
+
88
+ #### `dbcli list`
89
+
90
+ List all tables in the connected database.
91
+
92
+ **Usage:**
93
+ ```bash
94
+ dbcli list [OPTIONS]
95
+ ```
96
+
97
+ **Options:**
98
+ - `--format json` — Output as JSON instead of ASCII table
99
+
100
+ **Examples:**
101
+ ```bash
102
+ # Table format (human-readable)
103
+ dbcli list
104
+
105
+ # JSON format (for AI parsing)
106
+ dbcli list --format json
107
+
108
+ # Pipe to tools
109
+ dbcli list --format json | jq '.data[].name'
110
+ ```
111
+
112
+ ---
113
+
114
+ #### `dbcli schema [table]`
115
+
116
+ Show table structure (columns, types, constraints, foreign keys).
117
+
118
+ **Usage:**
119
+ ```bash
120
+ dbcli schema [table]
121
+ dbcli schema # Scan entire database and update .dbcli
122
+ dbcli schema users # Show structure of 'users' table
123
+ ```
124
+
125
+ **Options:**
126
+ - `--format json` — Output as JSON
127
+ - `--refresh` — Detect and update schema changes (requires --force for approval)
128
+ - `--force` — Skip confirmation for schema refresh/overwrite
129
+
130
+ **Examples:**
131
+ ```bash
132
+ # Show users table structure
133
+ dbcli schema users
134
+
135
+ # JSON output with full metadata
136
+ dbcli schema users --format json
137
+
138
+ # Update schema with new tables
139
+ dbcli schema --refresh
140
+
141
+ # Auto-approve schema refresh
142
+ dbcli schema --refresh --force
143
+
144
+ # Scan entire database
145
+ dbcli schema
146
+ ```
147
+
148
+ ---
149
+
150
+ #### `dbcli query "SQL"`
151
+
152
+ Execute SQL query and return results.
153
+
154
+ **Usage:**
155
+ ```bash
156
+ dbcli query "SELECT * FROM users"
157
+ ```
158
+
159
+ **Options:**
160
+ - `--format json|table|csv` — Output format (default: table)
161
+ - `--output file` — Write to file instead of stdout
162
+
163
+ **Behavior:**
164
+ - Enforces permission-based restrictions (Query-only mode blocks INSERT/UPDATE/DELETE)
165
+ - Auto-limits results to 1000 rows in Query-only mode (notification shown)
166
+ - Returns structured results with metadata (row count, execution time)
167
+
168
+ **Examples:**
169
+ ```bash
170
+ # Table output (human-readable)
171
+ dbcli query "SELECT * FROM users"
172
+
173
+ # JSON (for AI/programmatic parsing)
174
+ dbcli query "SELECT * FROM users" --format json
175
+
176
+ # CSV export
177
+ dbcli query "SELECT * FROM users" --format csv --output users.csv
178
+
179
+ # Pipe to other tools
180
+ dbcli query "SELECT * FROM products" --format json | jq '.data[] | .name'
181
+
182
+ # Large result sets (paginate with LIMIT/OFFSET)
183
+ dbcli query "SELECT * FROM users LIMIT 100 OFFSET 0"
184
+ ```
185
+
186
+ ---
187
+
188
+ #### `dbcli insert [table]` (Requires Read-Write or Admin permission)
189
+
190
+ Insert data into table.
191
+
192
+ **Usage:**
193
+ ```bash
194
+ dbcli insert users --data '{"name": "Alice", "email": "alice@example.com"}'
195
+ ```
196
+
197
+ **Options:**
198
+ - `--data JSON` — Row data as JSON object (REQUIRED)
199
+ - `--dry-run` — Show SQL without executing
200
+ - `--force` — Skip confirmation
201
+
202
+ **Behavior:**
203
+ - Validates JSON format
204
+ - Generates parameterized SQL (prevents SQL injection)
205
+ - Shows confirmation prompt before inserting (unless --force used)
206
+
207
+ **Examples:**
208
+ ```bash
209
+ # Insert single row
210
+ dbcli insert users --data '{"name": "Bob", "email": "bob@example.com"}'
211
+
212
+ # Preview SQL without executing
213
+ dbcli insert users --data '{"name": "Charlie"}' --dry-run
214
+
215
+ # Skip confirmation
216
+ dbcli insert users --data '{"name": "Diana"}' --force
217
+ ```
218
+
219
+ ---
220
+
221
+ #### `dbcli update [table]` (Requires Read-Write or Admin permission)
222
+
223
+ Update existing rows.
224
+
225
+ **Usage:**
226
+ ```bash
227
+ dbcli update users --where "id=1" --set '{"name": "Alice Updated"}'
228
+ ```
229
+
230
+ **Options:**
231
+ - `--where condition` — WHERE clause (REQUIRED, e.g., "id=1 AND status='active'")
232
+ - `--set JSON` — Updated columns as JSON object (REQUIRED)
233
+ - `--dry-run` — Show SQL without executing
234
+ - `--force` — Skip confirmation
235
+
236
+ **Examples:**
237
+ ```bash
238
+ # Update single row
239
+ dbcli update users --where "id=1" --set '{"name": "Alice"}'
240
+
241
+ # Update multiple rows
242
+ dbcli update users --where "status='inactive'" --set '{"status":"active"}'
243
+
244
+ # Preview SQL
245
+ dbcli update users --where "id=1" --set '{"name": "Bob"}' --dry-run
246
+
247
+ # Skip confirmation
248
+ dbcli update users --where "id=2" --set '{"email": "new@example.com"}' --force
249
+ ```
250
+
251
+ ---
252
+
253
+ #### `dbcli delete [table]` (Requires Admin permission only)
254
+
255
+ Delete rows (admin-only for safety).
256
+
257
+ **Usage:**
258
+ ```bash
259
+ dbcli delete users --where "id=1" --force
260
+ ```
261
+
262
+ **Options:**
263
+ - `--where condition` — WHERE clause (REQUIRED)
264
+ - `--dry-run` — Show SQL without executing
265
+ - `--force` — Required to actually delete (safety guard)
266
+
267
+ **Examples:**
268
+ ```bash
269
+ # Delete single row (requires --force)
270
+ dbcli delete users --where "id=1" --force
271
+
272
+ # Preview deletion
273
+ dbcli delete products --where "status='deprecated'" --dry-run
274
+
275
+ # Delete multiple rows
276
+ dbcli delete orders --where "created_at < '2020-01-01'" --force
277
+ ```
278
+
279
+ ---
280
+
281
+ #### `dbcli export "SQL"`
282
+
283
+ Export query results to file.
284
+
285
+ **Usage:**
286
+ ```bash
287
+ dbcli export "SELECT * FROM users" --format json --output users.json
288
+ ```
289
+
290
+ **Options:**
291
+ - `--format json|csv` — Output format
292
+ - `--output file` — Write to file (default: stdout for piping)
293
+
294
+ **Behavior:**
295
+ - Query-only permission limited to 1000 rows per export
296
+ - Generates RFC 4180 compliant CSV
297
+ - Creates well-formed JSON arrays
298
+
299
+ **Examples:**
300
+ ```bash
301
+ # Export to JSON
302
+ dbcli export "SELECT * FROM users" --format json --output users.json
303
+
304
+ # Export to CSV
305
+ dbcli export "SELECT * FROM orders" --format csv --output orders.csv
306
+
307
+ # Pipe compressed export
308
+ dbcli export "SELECT * FROM products" --format csv | gzip > products.csv.gz
309
+
310
+ # Combine with query tools
311
+ dbcli export "SELECT * FROM users WHERE active=true" --format json | jq '.data | length'
312
+ ```
313
+
314
+ ---
315
+
316
+ #### `dbcli skill`
317
+
318
+ Generate or install AI agent skill documentation.
319
+
320
+ **Usage:**
321
+ ```bash
322
+ dbcli skill # Output skill to stdout
323
+ dbcli skill --output SKILL.md # Write to file
324
+ dbcli skill --install claude # Install to Claude Code config
325
+ dbcli skill --install gemini # Install to Gemini CLI
326
+ dbcli skill --install copilot # Install to GitHub Copilot
327
+ dbcli skill --install cursor # Install to Cursor IDE
328
+ ```
329
+
330
+ **Behavior:**
331
+ - Dynamically generates SKILL.md from CLI introspection
332
+ - Filters commands by permission level (Query-only hides write commands)
333
+ - Supports multiple output modes: stdout, file, platform installation
334
+
335
+ **Examples:**
336
+ ```bash
337
+ # Generate skill for Claude Code
338
+ dbcli skill --install claude
339
+
340
+ # Generate skill manually for documentation
341
+ dbcli skill > ./docs/SKILL.md
342
+
343
+ # View generated skill (stdout)
344
+ dbcli skill
345
+
346
+ # Install for all platforms
347
+ dbcli skill --install claude && \
348
+ dbcli skill --install gemini && \
349
+ dbcli skill --install copilot && \
350
+ dbcli skill --install cursor
351
+ ```
352
+
353
+ ---
354
+
355
+ ## Permission Model
356
+
357
+ dbcli implements a coarse-grained permission system with three levels. Permission level is set during `dbcli init` and stored in `.dbcli` config file.
358
+
359
+ ### Permission Levels
360
+
361
+ | Level | Allowed Commands | Blocked Commands | Use Case |
362
+ |-------|------------------|------------------|----------|
363
+ | **Query-only** | `init`, `list`, `schema`, `query`, `export` (limited to 1000 rows) | `insert`, `update`, `delete` | Read-only AI agents, data analysts, reporting |
364
+ | **Read-Write** | + `insert`, `update` | `delete` | Application developers, content managers |
365
+ | **Admin** | All commands | — | Database administrators, schema modifications |
366
+
367
+ ### Configuration
368
+
369
+ Permission level is set during initialization:
370
+
371
+ ```bash
372
+ dbcli init
373
+ # Prompts: "Permission level? (query-only / read-write / admin)"
374
+ # Stored in ~/.dbcli as: "permissionLevel": "query-only"
375
+ ```
376
+
377
+ ### Permission-Based Examples
378
+
379
+ #### Query-Only Mode (AI Agent)
380
+ ```bash
381
+ # Allowed: Read operations
382
+ dbcli query "SELECT * FROM users"
383
+ dbcli schema users
384
+ dbcli export "SELECT * FROM orders" --format json
385
+
386
+ # Blocked: Write operations
387
+ dbcli insert users --data '{...}' # ERROR: Permission denied
388
+ dbcli delete users --where "id=1" # ERROR: Permission denied
389
+ ```
390
+
391
+ #### Read-Write Mode (Application Developer)
392
+ ```bash
393
+ # Allowed: Read + write
394
+ dbcli query "SELECT * FROM users"
395
+ dbcli insert users --data '{"name": "Alice"}'
396
+ dbcli update users --where "id=1" --set '{"name": "Bob"}'
397
+
398
+ # Blocked: Delete (safety feature)
399
+ dbcli delete users --where "id=1" # ERROR: Admin only
400
+ ```
401
+
402
+ #### Admin Mode (Database Administrator)
403
+ ```bash
404
+ # Allowed: Everything
405
+ dbcli query "SELECT * FROM users"
406
+ dbcli insert users --data '{"name": "Eve"}'
407
+ dbcli update users --where "id=1" --set '{"status": "active"}'
408
+ dbcli delete users --where "id=1" --force # Only Admin can delete
409
+ ```
410
+
411
+ ### Best Practices
412
+
413
+ - **AI Agents:** Use Query-only for read-only scenarios; prevents accidental data loss
414
+ - **Applications:** Use Read-Write for normal CRUD operations; prevents DROP TABLE accidents
415
+ - **Maintenance:** Use Admin only for schema changes, bulk deletes, or emergency operations
416
+ - **Principle of Least Privilege:** Assign minimum permission level needed for each use case
417
+
418
+ ---
419
+
420
+ ## AI Integration Guide
421
+
422
+ dbcli generates AI-consumable skill documentation and can be integrated into your favorite AI development tools.
423
+
424
+ ### Quick Start
425
+
426
+ Generate skill for your preferred platform:
427
+
428
+ ```bash
429
+ # Claude Code (Anthropic's VS Code extension)
430
+ dbcli skill --install claude
431
+
432
+ # Gemini CLI (Google's command-line AI)
433
+ dbcli skill --install gemini
434
+
435
+ # GitHub Copilot CLI
436
+ dbcli skill --install copilot
437
+
438
+ # Cursor IDE (AI-native editor)
439
+ dbcli skill --install cursor
440
+ ```
441
+
442
+ After installation, the AI agent will have access to dbcli commands and can use them to query, insert, update, or export data based on your permission level.
443
+
444
+ ### Platform-Specific Setup
445
+
446
+ #### Claude Code (Anthropic)
447
+
448
+ 1. Install dbcli globally: `npm install -g dbcli`
449
+ 2. Initialize: `dbcli init` (choose permission level)
450
+ 3. Install skill: `dbcli skill --install claude`
451
+ 4. Restart Claude Code extension
452
+ 5. In Claude Code chat, ask: "Show me the database schema" or "Query active users"
453
+
454
+ **Skill location:** `~/.claude/skills/SKILL.md`
455
+
456
+ ---
457
+
458
+ #### Gemini CLI (Google)
459
+
460
+ 1. Install dbcli globally: `npm install -g dbcli`
461
+ 2. Initialize: `dbcli init`
462
+ 3. Install skill: `dbcli skill --install gemini`
463
+ 4. Start Gemini: `gemini start`
464
+ 5. In chat, request: "Query the users table" or "Show database tables"
465
+
466
+ **Skill location:** `~/.local/share/gemini/skills/` (Linux) or platform equivalent
467
+
468
+ ---
469
+
470
+ #### GitHub Copilot CLI
471
+
472
+ 1. Install dbcli globally: `npm install -g dbcli`
473
+ 2. Initialize: `dbcli init`
474
+ 3. Install skill: `dbcli skill --install copilot`
475
+ 4. Install Copilot CLI: `npm install -g @github-next/github-copilot-cli`
476
+ 5. Use copilot preview: `copilot --help` and explore dbcli integration
477
+
478
+ **Skill location:** Per Copilot configuration
479
+
480
+ ---
481
+
482
+ #### Cursor IDE
483
+
484
+ 1. Install dbcli globally: `npm install -g dbcli`
485
+ 2. Initialize: `dbcli init`
486
+ 3. Install skill: `dbcli skill --install cursor`
487
+ 4. Open Cursor editor
488
+ 5. Use Cursor's Composer: "Insert a new user" or "Export user data"
489
+
490
+ **Skill location:** `~/.cursor/skills/`
491
+
492
+ ---
493
+
494
+ ### Example: AI Agent Workflow
495
+
496
+ **Scenario:** You want an AI agent to analyze user engagement data.
497
+
498
+ ```bash
499
+ # 1. Install and initialize
500
+ npm install -g dbcli
501
+ dbcli init # Choose "query-only" for safety
502
+
503
+ # 2. Install skill to Claude Code
504
+ dbcli skill --install claude
505
+
506
+ # 3. In Claude Code chat, ask:
507
+ # "Analyze the last 7 days of user activity and summarize insights"
508
+
509
+ # Claude Code will:
510
+ # - Use: dbcli schema users, dbcli query "SELECT ..."
511
+ # - Parse JSON output
512
+ # - Provide analysis
513
+ ```
514
+
515
+ ### Skill Refresh
516
+
517
+ dbcli dynamically generates skills based on your current configuration:
518
+
519
+ ```bash
520
+ # When permission level changes, skill updates automatically
521
+ # Edit ~/.dbcli and change "permissionLevel" to "admin"
522
+ dbcli skill # Now shows delete and admin commands
523
+
524
+ # Re-install to push changes to AI platform
525
+ dbcli skill --install claude
526
+ ```
527
+
528
+ ---
529
+
530
+ ## Troubleshooting
531
+
532
+ ### Connection Issues
533
+
534
+ #### "ECONNREFUSED: Connection refused"
535
+
536
+ Database is not running or host/port is incorrect.
537
+
538
+ **Solutions:**
539
+
540
+ ```bash
541
+ # Verify database is running
542
+ psql --version # PostgreSQL installed?
543
+ mysql --version # MySQL installed?
544
+
545
+ # Check connection string
546
+ dbcli init # Re-run initialization to verify credentials
547
+
548
+ # Verify host/port from command line
549
+ psql -h localhost -U postgres # PostgreSQL test
550
+ mysql -h 127.0.0.1 -u root # MySQL test
551
+ ```
552
+
553
+ #### "ENOTFOUND: getaddrinfo ENOTFOUND hostname"
554
+
555
+ Hostname resolution failed (typo or DNS issue).
556
+
557
+ **Solutions:**
558
+
559
+ ```bash
560
+ # Verify hostname in .dbcli
561
+ cat ~/.dbcli | grep host
562
+
563
+ # Test DNS resolution
564
+ ping your-hostname.com
565
+
566
+ # Use 127.0.0.1 instead of localhost if issues persist
567
+ dbcli init # Re-initialize with correct host
568
+ ```
569
+
570
+ ---
571
+
572
+ ### Permission Errors
573
+
574
+ #### "Permission denied: INSERT requires Read-Write or Admin"
575
+
576
+ Trying to write with Query-only permission level.
577
+
578
+ **Solution:** Re-initialize with higher permission level:
579
+
580
+ ```bash
581
+ rm ~/.dbcli # Remove old config
582
+ dbcli init # Re-run, choose "read-write" or "admin"
583
+ ```
584
+
585
+ #### "Permission denied: DELETE requires Admin"
586
+
587
+ Only Admin can delete rows (safety feature).
588
+
589
+ **Solution:** Re-initialize with Admin permission, or ask administrator.
590
+
591
+ ```bash
592
+ dbcli init # Choose "admin"
593
+ dbcli delete users --where "id=1" --force
594
+ ```
595
+
596
+ ---
597
+
598
+ ### Query Errors
599
+
600
+ #### "Table not found: users"
601
+
602
+ Table doesn't exist or name is misspelled.
603
+
604
+ **Solution:**
605
+
606
+ ```bash
607
+ # Show all available tables
608
+ dbcli list
609
+
610
+ # Check spelling and retry
611
+ dbcli query "SELECT * FROM user" --format json
612
+ ```
613
+
614
+ #### "Syntax error near SELECT"
615
+
616
+ SQL syntax error in query.
617
+
618
+ **Solution:**
619
+
620
+ ```bash
621
+ # Test query in native database client first
622
+ psql # Or mysql
623
+ # SELECT * FROM users; <- Test here first
624
+
625
+ # Then use in dbcli
626
+ dbcli query "SELECT * FROM users"
627
+ ```
628
+
629
+ ---
630
+
631
+ ### Performance Issues
632
+
633
+ #### "Query returns 1000 rows instead of full result set"
634
+
635
+ Query-only permission auto-limits results for safety.
636
+
637
+ **Solution:** Increase permission level or fetch data in chunks:
638
+
639
+ ```bash
640
+ # Re-initialize with higher permission
641
+ dbcli init # Choose "read-write" or "admin"
642
+
643
+ # OR fetch data in chunks
644
+ dbcli query "SELECT * FROM users LIMIT 100 OFFSET 0"
645
+ dbcli query "SELECT * FROM users LIMIT 100 OFFSET 100"
646
+ ```
647
+
648
+ #### "CLI startup takes 30+ seconds (first run)"
649
+
650
+ npx is downloading and caching package.
651
+
652
+ **Solution:** This is normal on first run. Subsequent runs are instant:
653
+
654
+ ```bash
655
+ npx dbcli init # First run: 30s (downloads)
656
+ npx dbcli init # Second run: <1s (cached)
657
+
658
+ # Or install globally for faster startup
659
+ npm install -g dbcli
660
+ dbcli init # All future runs: <1s
661
+ ```
662
+
663
+ ---
664
+
665
+ ### Cross-Platform Issues
666
+
667
+ #### Windows: "Command not found: dbcli"
668
+
669
+ npm .cmd wrapper not created or PATH not updated.
670
+
671
+ **Solutions:**
672
+
673
+ ```bash
674
+ # Restart terminal to refresh PATH
675
+ # OR reinstall globally
676
+ npm uninstall -g dbcli
677
+ npm install -g dbcli
678
+
679
+ # Verify installation
680
+ where dbcli # Windows command to find executable
681
+ ```
682
+
683
+ #### macOS/Linux: "Permission denied: ./dist/cli.mjs"
684
+
685
+ Executable bit not set.
686
+
687
+ **Solution:**
688
+
689
+ ```bash
690
+ chmod +x dist/cli.mjs
691
+ ./dist/cli.mjs --help
692
+ ```
693
+
694
+ ---
695
+
696
+ ## System Requirements
697
+
698
+ ### Database Support
699
+
700
+ - **PostgreSQL:** 12.0+
701
+ - **MySQL:** 8.0+
702
+ - **MariaDB:** 10.5+
703
+
704
+ ### Runtime
705
+
706
+ - **Node.js:** 18.0.0+
707
+ - **Bun:** 1.3.3+
708
+
709
+ ### Platforms
710
+
711
+ - **macOS:** Intel and Apple Silicon
712
+ - **Linux:** x86_64 (Ubuntu, Debian, CentOS, etc.)
713
+ - **Windows:** 10+ (via npm .cmd wrapper)
714
+
715
+ ---
716
+
717
+ ## Development
718
+
719
+ See [CONTRIBUTING.md](./CONTRIBUTING.md) for development setup, testing, and release process.
720
+
721
+ ---
722
+
723
+ ## License
724
+
725
+ See LICENSE file for details.