@carllee1983/dbcli 1.6.0 → 1.8.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/CHANGELOG.md CHANGED
@@ -5,6 +5,36 @@ All notable changes to dbcli are documented here.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [1.8.0] - 2026-05-06
9
+
10
+ ### Added
11
+
12
+ - **Redis 與 Elasticsearch 支援**:`init`、`list`、`schema`、`query`、`status`、`use`、`doctor`、`upgrade`、`completion` 在兩個系統皆真實可用。
13
+ - Redis:`list` 透過 SCAN 取 keys;`schema <key>` 顯示 type/TTL/size/sample;`query` 執行白名單 Redis 指令並走原本權限與黑名單檢查。
14
+ - Elasticsearch:`list` 顯示 indices 與文件數;`schema [index]` 攤平 mapping、揭露 `.fields` multi-fields;`query` 接受 DSL JSON 或 Lucene 字串。
15
+ - 文件:`assets/SKILL.md` 與 `assets/reference.md` 同步加入 Redis / Elasticsearch 章節。
16
+
17
+ ### Fixed
18
+
19
+ - **`insert` / `update` / `delete` / `export` / `diff` 對 Redis / Elasticsearch 的早期錯誤訊息**:先前會落入 SQL DataExecutor 出現「Column ... not found in table」之類誤導訊息,現在直接回傳明確的「不支援」JSON,並指引正確替代路徑(Redis 改用 `query`、Elasticsearch 改用外部工具或 `query --index`)。
20
+ - **TypeScript 嚴格度**:`bun run typecheck` 從 43 個錯誤降為 0。
21
+ - `ConnectionConfig` union 加入 `ElasticsearchConnectionConfig`。
22
+ - `ResolvedConnection.connection.system`、`ReplContext.system` 涵蓋 `'elasticsearch'`。
23
+ - `ExecutionResult` 補上 optional `rowCount` / `columnNames`。
24
+ - `getDefaultsForSystem` 涵蓋 redis (6379) / elasticsearch (9200) 預設值。
25
+
26
+ ## [1.7.0] - 2026-05-04
27
+
28
+ ### Added
29
+
30
+ - `dbcli q @<name>` 執行已保存的參數化 SELECT 片段
31
+ - `dbcli queries list/show/new/edit/check` 管理片段
32
+ - 兩層片段儲存:`.dbcli-shared/queries/`(共享)+ `.dbcli/queries/`(個人覆蓋)
33
+ - 完整安全 invariants:拒絕非 SELECT/WITH、多語句、`${...}` / `{{...}}` 模板語法
34
+ - 子查詢式 size guard 包裹 (`SELECT * FROM (...) AS _dbcli_guard LIMIT 1000`)
35
+ - 內建 YAML 子集 frontmatter parser(無新增 npm 依賴)
36
+ - `queries list/show --format json` 為未來 MCP server 預留契約
37
+
8
38
  ## [1.6.0] - 2026-04-23
9
39
 
10
40
  ### Added
package/README.md CHANGED
@@ -114,6 +114,8 @@ dbcli query '{"status":"active"}' --collection users --use atlas
114
114
 
115
115
  For MongoDB, `list` and `query` operate on the database configured for the connection, and `query` requires `--collection <name>`.
116
116
 
117
+ For a command-by-command support matrix across PostgreSQL, MySQL, MariaDB, and MongoDB, see [docs/feature-matrix.md](./docs/feature-matrix.md).
118
+
117
119
  ---
118
120
 
119
121
  ## Multi-connection Support (v2)
@@ -769,6 +771,43 @@ dbcli migrate drop-enum status --execute --force
769
771
 
770
772
  ---
771
773
 
774
+ ## Query Risk Planning
775
+
776
+ Use `plan` to inspect SQL safety before execution. It reads local dbcli config, permissions, blacklist rules, and cached schema metadata only; it does not connect to the database.
777
+
778
+ ```bash
779
+ dbcli plan "UPDATE users SET status='inactive'" --format json
780
+ ```
781
+
782
+ Decisions are:
783
+
784
+ - `ALLOW` — no obvious risk was detected.
785
+ - `WARN` — inspect warnings before executing.
786
+ - `BLOCK` — unsafe, unsupported, or violates configured safety constraints.
787
+
788
+ Text output is concise for humans:
789
+
790
+ ```text
791
+ Decision: BLOCK
792
+ Operation: UPDATE
793
+ Target tables: users
794
+
795
+ Risk factors:
796
+ - UPDATE statement has no WHERE clause.
797
+
798
+ Recommendations:
799
+ - Add a WHERE clause.
800
+ - Use --dry-run on the actual write command.
801
+ ```
802
+
803
+ JSON output includes `suggestedCommands` for agents:
804
+
805
+ ```bash
806
+ dbcli plan "SELECT id FROM users WHERE id = 1 LIMIT 1" --format json
807
+ ```
808
+
809
+ ---
810
+
772
811
  ## Global Options
773
812
 
774
813
  All commands support these global options:
@@ -957,6 +996,22 @@ A Query-only agent cannot write to any table, and also cannot read blacklisted t
957
996
 
958
997
  ---
959
998
 
999
+ ## Saved queries
1000
+
1001
+ Save parameterised SELECT snippets and re-run them by name:
1002
+
1003
+ ```bash
1004
+ dbcli queries list
1005
+ dbcli queries show @dau
1006
+ dbcli q @dau --param days=30 --format json
1007
+ ```
1008
+
1009
+ Snippets live in `.dbcli-shared/queries/` (committed) or `.dbcli/queries/`
1010
+ (gitignored, personal override). Each `.sql` file declares its frontmatter
1011
+ in a `-- ---` block. Read `assets/reference.md` for the full schema.
1012
+
1013
+ ---
1014
+
960
1015
  ## AI Integration Guide
961
1016
 
962
1017
  dbcli ships AI-consumable skill files (`assets/SKILL.md` and `assets/reference.md`) and can copy them into your favorite AI tool directories.
@@ -1258,12 +1313,15 @@ chmod +x dist/cli.mjs
1258
1313
 
1259
1314
  ```bash
1260
1315
  bun test # full test suite (Bun test runner)
1316
+ bun run typecheck # TypeScript compile-time validation
1261
1317
  bun run test:unit # unit + core tests only
1262
1318
  bun run test:integration # integration tests
1263
1319
  bun run test:docker # integration tests with docker-compose.test.yml (MySQL + PostgreSQL)
1264
1320
  bun run build # bundle CLI to dist/ (used before publish)
1265
1321
  ```
1266
1322
 
1323
+ CI treats `bun run typecheck` and `bun test` as the required pass/fail validation gate on every push and pull request. Lint, build, smoke checks, and benchmarks run in addition to that gate.
1324
+
1267
1325
  Live database integration tests use `.dbcli/config.json` by default. If your live
1268
1326
  config lives elsewhere, set `LIVE_DB_CONFIG_PATH=/path/to/.dbcli` before running:
1269
1327
 
package/README.zh-TW.md CHANGED
@@ -779,6 +779,43 @@ dbcli migrate drop-enum status --execute --force
779
779
 
780
780
  ---
781
781
 
782
+ ## 查詢風險規劃
783
+
784
+ 使用 `plan` 在執行前檢查 SQL 安全性。它只讀取本機 dbcli 設定、權限、黑名單規則與已快取的 schema metadata;不會連線到資料庫。
785
+
786
+ ```bash
787
+ dbcli plan "UPDATE users SET status='inactive'" --format json
788
+ ```
789
+
790
+ 決策結果:
791
+
792
+ - `ALLOW` — 未偵測到明顯風險。
793
+ - `WARN` — 執行前應檢查警告。
794
+ - `BLOCK` — 不安全、不支援,或違反安全設定。
795
+
796
+ 文字輸出保持精簡,適合人工閱讀:
797
+
798
+ ```text
799
+ Decision: BLOCK
800
+ Operation: UPDATE
801
+ Target tables: users
802
+
803
+ Risk factors:
804
+ - UPDATE statement has no WHERE clause.
805
+
806
+ Recommendations:
807
+ - Add a WHERE clause.
808
+ - Use --dry-run on the actual write command.
809
+ ```
810
+
811
+ JSON 輸出會包含給 AI agent 使用的 `suggestedCommands`:
812
+
813
+ ```bash
814
+ dbcli plan "SELECT id FROM users WHERE id = 1 LIMIT 1" --format json
815
+ ```
816
+
817
+ ---
818
+
782
819
  ## 全域選項
783
820
 
784
821
  所有指令皆支援下列全域選項:
@@ -963,6 +1000,23 @@ Query-only 代理無法寫入任何表,也無法讀取黑名單表或欄位
963
1000
 
964
1001
  ---
965
1002
 
1003
+ ## 已保存查詢片段(Saved queries)
1004
+
1005
+ 將參數化的 SELECT 片段保存於版控,並依名稱重複執行:
1006
+
1007
+ ```bash
1008
+ dbcli queries list
1009
+ dbcli queries show @dau
1010
+ dbcli q @dau --param days=30 --format json
1011
+ ```
1012
+
1013
+ 片段位於兩層儲存:`.dbcli-shared/queries/`(commit 進版控、團隊共享)與
1014
+ `.dbcli/queries/`(gitignore、個人覆蓋)。每個 `.sql` 檔以 `-- ---` 區塊
1015
+ 宣告 frontmatter(name、description、engine、params、tags),完整 schema 見
1016
+ `assets/reference.md`。
1017
+
1018
+ ---
1019
+
966
1020
  ## AI 整合指南
967
1021
 
968
1022
  dbcli 內建供 AI 使用的 skill 文件(`assets/SKILL.md` 與 `assets/reference.md`),並可複製到常見 AI 開發工具的目錄。
package/assets/SKILL.md CHANGED
@@ -1,6 +1,6 @@
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, 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`.
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, MongoDB, Redis, and Elasticsearch with multiple named connections per project and custom env files. Trigger when working with databases, running SQL / MongoDB JSON / Redis commands / Elasticsearch DSL, exploring table/collection/key/index 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
@@ -33,21 +33,21 @@ dbcli query "SELECT * FROM users" # Execute SQL (auto LIMIT 1000)
33
33
  |---------|-----------------|---------|
34
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
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. |
36
+ | `list` | query-only+ | Tables (SQL), collections (MongoDB), keys (Redis), or indices (Elasticsearch). |
37
+ | `schema` | query-only+ | SQL: per-table or full scan into `.dbcli/schemas/` (use `--use` for the right cache). MongoDB: sampled. ES: flattened mapping. Redis: per-key only (type/TTL/size). |
38
+ | `query` | query-only+ | SQL, Mongo JSON (`--collection`), Redis command, or ES DSL/Lucene (`--collection`/`--index`). |
39
+ | `insert` / `update` | read-write+ | SQL or MongoDB only. JSON `--data` / `--set`; `--where` required on `update`; `--dry-run` first. Redis writes go through `query`. |
40
+ | `delete` | data-admin+ | SQL or MongoDB only. `--where` required; `--dry-run` first. |
41
+ | `export` | query-only+ | SQL or MongoDB only. Query → CSV/JSON(L) file or stdout. |
42
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. |
43
+ | `check` | query-only+ | SQL only (best on MySQL/MariaDB). |
44
+ | `diff` | query-only+ | SQL only. Save/compare schema snapshots. |
45
45
  | `status` | query-only+ | Safe JSON/text summary (no credentials). |
46
- | `doctor` | n/a | Environment, config, connection, SRV diagnostics (Mongo), schema cache age. |
46
+ | `doctor` | n/a | Environment, config, connection, SRV diagnostics (Mongo), schema cache age; ES has a dedicated path. |
47
47
  | `completion` | n/a | bash / zsh / fish scripts. |
48
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`. |
49
+ | `shell` | (same as query+) | Interactive REPL. SQL engines + MongoDB shell only; Redis/ES not currently exposed in REPL. |
50
+ | `migrate` | admin | SQL only. **DDL; dry-run by default** — needs `--execute`; DROP also needs `--force`. Mongo/Redis exit with error. |
51
51
 
52
52
  `--use <name>` on any subcommand targets a v2 connection without changing the default.
53
53
 
@@ -69,11 +69,75 @@ dbcli query "SELECT * FROM users" # Execute SQL (auto LIMIT 1000)
69
69
  ## MongoDB
70
70
 
71
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.
72
+ - **Supported:** `init`, `list`, `schema` (sampled), `query`, `insert`, `update`, `delete`, `export`, `status`, `use`, `shell`, `doctor`, `upgrade`, `completion`.
73
+ - **Not supported:** `q` (saved queries), `diff`, `migrate`, `check`.
74
+ - Schema is **sampled** (default 50 docs, `--sample-size`); types are JS `typeof` strings no PK/FK/index info.
75
+ - `--limit` applies on `find`/aggregate; query-only mode caps at 1000 unless `--no-limit`.
75
76
  - See reference.md MongoDB section for full syntax and examples.
76
77
 
78
+ ## Redis
79
+
80
+ - Command-style execution; `query` runs a whitelisted Redis command (e.g. `GET`, `HSET`, `DEL`).
81
+ - **Supported:** `init`, `list` (keys via SCAN), `schema <key>` (type / TTL / size / sample), `query`, `status`, `use`, `doctor`, `upgrade`, `completion`.
82
+ - **Not supported:** `schema` full scan / `--refresh` / `--reset`, `insert`, `update`, `delete`, `export`, `check`, `diff`, `migrate`, `q`.
83
+ Use `query "DEL <key>"` etc. for writes — they go through the same permission gate.
84
+ - Permission tiers map to commands: read commands → `query-only`; mutators (`SET`, `HSET`, `EXPIRE`, …) → `read-write`; `DEL` / `HDEL` / `UNLINK` → `data-admin`; `KEYS`, `FLUSHDB`, `CONFIG`, `INFO`, … → `admin`. Unlisted commands are denied.
85
+ - `database` field is the logical DB index (default `0`); `list` returns ≤ 100 000 keys via SCAN.
86
+ - See reference.md Redis section.
87
+
88
+ ## Elasticsearch
89
+
90
+ - DSL (JSON body) or Lucene query string; `--collection <index>` (or `--index <index>`) is required on `query`.
91
+ - **Supported:** `init`, `list` (indices with doc count), `schema [index]` (flattened mapping), `query`, `status`, `use`, `doctor`, `upgrade`, `completion`.
92
+ - **Not supported:** `insert`, `update`, `delete`, `export`, `check`, `diff`, `migrate`, `q`.
93
+ Writes are not exposed via dedicated subcommands yet — use external tooling for indexing.
94
+ - Auth: API key (`apiKey`), Basic (`user`/`password`), or Cloud ID (`cloudId`); supports HTTPS, custom CA (`caPath`), and multi-node arrays (`nodes`).
95
+ - Query-only mode caps at 1000 hits; `--no-limit` is bounded at 10 000 (use saved searches with `search_after` beyond that).
96
+ - Schema flattens nested fields (`a.b.c`) and surfaces `.fields` multi-fields (e.g. `text.keyword`).
97
+ - See reference.md Elasticsearch section.
98
+
99
+ ## Saved queries
100
+
101
+ Run reusable parameterised SELECT snippets stored in your repo.
102
+
103
+ | Step | Command |
104
+ |------|---------|
105
+ | 1. Discover | `dbcli queries list` |
106
+ | 2. Inspect | `dbcli queries show @<name>` |
107
+ | 3. Run | `dbcli q @<name> --param k=v` |
108
+
109
+ Snippets resolve from three layers, **local > shared > builtin** (local wins):
110
+ - `builtin` — bundled with dbcli (e.g. `@diag/*`); read-only at runtime
111
+ - `.dbcli-shared/queries/` — committed, team-shared
112
+ - `.dbcli/queries/` — gitignored, personal override
113
+
114
+ Manage local snippets with `queries new | edit | delete | rename | copy | import | export`
115
+ (see reference.md). Use `copy` / `import` to fork a builtin or shared snippet into the
116
+ local layer for editing.
117
+
118
+ Each `.sql` file may declare YAML frontmatter inside `-- ---` blocks
119
+ (name, description, engine, params, tags). See `dbcli queries show @<name> --format json`
120
+ for the machine-readable contract.
121
+
122
+ ### Built-in diagnostic snippets
123
+
124
+ dbcli ships ready-made diagnostic queries. Run with `dbcli q @diag/<topic>`:
125
+
126
+ | key | purpose |
127
+ |-------------------------|------------------------------------------|
128
+ | `@diag/connections` | active sessions |
129
+ | `@diag/long-running` | queries above `min_seconds` (default 30) |
130
+ | `@diag/table-sizes` | table data/index size with row counts |
131
+ | `@diag/index-usage` | indexes by scan count |
132
+ | `@diag/missing-indexes` | tables dominated by sequential scans |
133
+ | `@diag/locks` | lock-wait chains |
134
+ | `@diag/db-size` | database size summary |
135
+ | `@diag/cache-hit` | buffer cache hit ratios |
136
+
137
+ Engine variants are picked automatically based on the active connection.
138
+ Override any of them by placing a same-named file under `.dbcli-shared/queries/`
139
+ or `.dbcli/queries/`.
140
+
77
141
  ## Common workflows
78
142
 
79
143
  - **Debug odd state:** `schema` → `check` → `query` with tight `WHERE` → follow FKs from schema JSON. Evidence over theory.