@youtyan/code-viewer 0.5.4 → 0.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/README.md +389 -13
- package/dist/code-viewer.js +15568 -9512
- package/package.json +1 -1
- package/skills/code-viewer-query/SKILL.md +159 -14
- package/skills/code-viewer-snapshot/SKILL.md +59 -2
- package/web/app.js +830 -169
- package/web/index.html +1 -1
- package/web/style.css +118 -26
package/package.json
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: code-viewer-query
|
|
3
|
-
description: Use when investigating database contents, checking schema, running SQL queries, or answering questions about data in SQLite/MySQL/PostgreSQL databases visible to code-viewer. Triggers on "query", "SQL", "database", "テーブル", "データベース", "クエリ", "スキーマ", "DB".
|
|
3
|
+
description: Use when investigating database contents, checking schema, running SQL queries, locating a value across many tables, or answering questions about data in SQLite/MySQL/PostgreSQL databases visible to code-viewer. Triggers on "query", "SQL", "database", "search", "find value", "locate column", "テーブル", "データベース", "クエリ", "スキーマ", "DB", "全文検索", "横断検索", "どのテーブルに".
|
|
4
4
|
---
|
|
5
5
|
|
|
6
6
|
# code-viewer query
|
|
7
7
|
|
|
8
|
-
Execute read-only SQL queries
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
Execute read-only SQL queries — and run global table-wide searches —
|
|
9
|
+
against databases discovered by code-viewer. Query results are saved to
|
|
10
|
+
query history; search results are returned by the CLI and mirror the
|
|
11
|
+
browser's Database > Search tab, so the human can review the same workflow.
|
|
11
12
|
|
|
12
13
|
## When to use
|
|
13
14
|
|
|
@@ -15,6 +16,15 @@ view, so the human can review what you queried.
|
|
|
15
16
|
- Checking schema, row counts, sample data
|
|
16
17
|
- Investigating data quality or anomalies
|
|
17
18
|
- Exploring table relationships
|
|
19
|
+
- Locating where a specific value (an email, an id, a free-text fragment)
|
|
20
|
+
appears when you do not yet know the table or column — use the global
|
|
21
|
+
search workflow below.
|
|
22
|
+
|
|
23
|
+
## When NOT to use this skill
|
|
24
|
+
|
|
25
|
+
- Capturing a database snapshot or diffing two snapshots before/after an
|
|
26
|
+
operation → use the `code-viewer-snapshot` skill instead. That skill owns
|
|
27
|
+
`snapshot create / list / note / delete` and `diff tables / diff rows`.
|
|
18
28
|
|
|
19
29
|
## Requirements
|
|
20
30
|
|
|
@@ -24,31 +34,166 @@ view, so the human can review what you queried.
|
|
|
24
34
|
- If `code-viewer` is not on PATH, prefix every command with
|
|
25
35
|
`npx -y @youtyan/code-viewer`.
|
|
26
36
|
|
|
27
|
-
## Workflow
|
|
37
|
+
## Workflow: Run a query
|
|
38
|
+
|
|
39
|
+
1. Discover the datastore ids the running server has detected. This is the
|
|
40
|
+
AI-friendly equivalent of opening the browser's Database tab and reading
|
|
41
|
+
the sidebar — it lists every SQLite file plus PostgreSQL / MySQL / Redis
|
|
42
|
+
/ Elasticsearch / S3 service that any nearby `docker-compose` exposes.
|
|
43
|
+
Use the printed `id` as `--db` on every other command. Credentials and
|
|
44
|
+
internal config are stripped server-side.
|
|
45
|
+
|
|
46
|
+
```sh
|
|
47
|
+
code-viewer query sources --json
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Default (no `--json`) output is one source per line as
|
|
51
|
+
`<id>\t<kind>\t<name>`, with any `truncated` or `dockerError` notice
|
|
52
|
+
appended as `# ...` comment lines so the AI can copy ids without
|
|
53
|
+
stripping JSON.
|
|
54
|
+
|
|
55
|
+
To skip the per-SQL-source "what command should I run next?" step, use
|
|
56
|
+
`--commands`. For SQLite / PostgreSQL / MySQL sources it prints a small
|
|
57
|
+
block of shell-pasteable next-step commands (`schema --with-columns
|
|
58
|
+
--json`, an `exec` example with `SELECT 1 --no-save`, plus `schemas` for
|
|
59
|
+
PostgreSQL), followed by paste-safe `list --db <id> --json` and
|
|
60
|
+
`snapshot list --db <id> --json` so you can step into the existing query
|
|
61
|
+
history and snapshot store without rebuilding those commands. For Redis
|
|
62
|
+
sources it prints `redis databases / redis keys` lines, for Elasticsearch
|
|
63
|
+
sources it prints `elasticsearch indices / elasticsearch docs` lines,
|
|
64
|
+
and for S3 sources it prints `s3 buckets / s3 objects` lines with
|
|
65
|
+
`--bucket <bucket-name>` as a placeholder so you can step into a bucket
|
|
66
|
+
discovered via `buckets`. Every emitted SQL command line pins
|
|
67
|
+
`--server '<url>'` to the same server URL this invocation resolved, so
|
|
68
|
+
pasting them into a different shell never silently falls back to
|
|
69
|
+
auto-discovery. Every `--db` value and the server URL are wrapped in
|
|
70
|
+
POSIX single-quotes so paths or URLs with spaces or `'` paste safely.
|
|
71
|
+
Notice/comment metadata is collapsed to one line so copied command blocks
|
|
72
|
+
stay intact.
|
|
73
|
+
`--json` and `--commands` are mutually exclusive.
|
|
74
|
+
|
|
75
|
+
```sh
|
|
76
|
+
code-viewer query sources --commands
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
2. Inspect tables, columns, and DDL without writing dialect-specific SQL.
|
|
80
|
+
These commands wrap the same schema endpoints as the browser, so use them
|
|
81
|
+
before guessing table names or engine-specific catalog queries.
|
|
82
|
+
|
|
83
|
+
```sh
|
|
84
|
+
code-viewer query schemas --db docker:pg-svc --json
|
|
85
|
+
code-viewer query schema --db app.db --json
|
|
86
|
+
code-viewer query schema --db docker:pg-svc --schema analytics --with-columns --json
|
|
87
|
+
code-viewer query columns --db app.db --table users --json
|
|
88
|
+
code-viewer query ddl --db app.db --table users
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Default output is tab-separated and human-readable. Pass `--json` when the
|
|
92
|
+
next agent step should parse the full endpoint response. `query schema
|
|
93
|
+
--json` adds paste-safe `columnsCommand` and `ddlCommand` fields to every
|
|
94
|
+
`tables[]` element — each pins `--server '<url>'` and single-quotes the
|
|
95
|
+
`--db` / `--schema` / `--table` arguments — so you can step into a single
|
|
96
|
+
table without rebuilding the call. Default (non-JSON) mode prints only
|
|
97
|
+
`<table>\t<type>\t<rowCount>` lines, unchanged.
|
|
98
|
+
|
|
99
|
+
3. To inspect saved query history (separate from source discovery), run:
|
|
100
|
+
|
|
101
|
+
```sh
|
|
102
|
+
code-viewer query list --json
|
|
103
|
+
```
|
|
28
104
|
|
|
29
|
-
|
|
105
|
+
`list --json` enriches each `entries[]` element with a paste-safe
|
|
106
|
+
`replayCommand` field — `code-viewer query --server '<url>' exec --db
|
|
107
|
+
'<dbId>' [--schema '<schema>'] --sql '<sql>' [--title '<title>']
|
|
108
|
+
--no-save` — so you can re-run any past query without rebuilding the
|
|
109
|
+
call. `server URL / dbId / schema / sql / title` are POSIX single-quoted;
|
|
110
|
+
`--title` is included only when the entry has one. `body` and
|
|
111
|
+
`--max-rows` are intentionally omitted (re-author `body` per replay,
|
|
112
|
+
and override `--max-rows` based on the entry's `truncated` flag).
|
|
113
|
+
`--no-save` is fixed so replay does not re-pollute history; drop it
|
|
114
|
+
manually if you want the replay saved. Default (non-JSON) mode prints
|
|
115
|
+
only the existing summary lines.
|
|
116
|
+
|
|
117
|
+
For PostgreSQL multi-schema query history, pass `--schema <name>` with
|
|
118
|
+
`--db` so list/clear stays scoped to that schema:
|
|
30
119
|
|
|
31
120
|
```sh
|
|
32
|
-
code-viewer query list
|
|
121
|
+
code-viewer query list --db docker:pg-svc --schema analytics --json
|
|
122
|
+
code-viewer query clear --db docker:pg-svc --schema analytics
|
|
33
123
|
```
|
|
34
124
|
|
|
35
|
-
|
|
125
|
+
4. Execute a query against an id picked from step 1:
|
|
36
126
|
|
|
37
127
|
```sh
|
|
38
128
|
code-viewer query exec --db data.db --sql "SELECT * FROM users LIMIT 10" \
|
|
39
129
|
--title "Sample user data" --body "Checking what user records look like."
|
|
130
|
+
code-viewer query exec --db docker:pg-svc --schema analytics \
|
|
131
|
+
--sql "SELECT * FROM events LIMIT 10"
|
|
40
132
|
```
|
|
41
133
|
|
|
42
|
-
|
|
134
|
+
`exec` prints pretty JSON with `dbId`, `columns`, `columnTypes`, `rows`,
|
|
135
|
+
`rowCount`, `truncated`, `elapsedMs`, and optional `schema` /
|
|
136
|
+
`executedSql`. Check `truncated` before treating the result as complete.
|
|
137
|
+
|
|
138
|
+
5. The human sees results in the browser's Database > Query History panel.
|
|
139
|
+
|
|
140
|
+
## Workflow: Find a value across all tables (global search)
|
|
141
|
+
|
|
142
|
+
Use this when you have a value (an email, a foreign key, a free-text
|
|
143
|
+
fragment) and do not yet know which table or column holds it. The CLI
|
|
144
|
+
mirrors the browser's Database > Search tab and blocks until the scan
|
|
145
|
+
finishes or `--timeout` expires.
|
|
146
|
+
|
|
147
|
+
```sh
|
|
148
|
+
code-viewer query search --db app.db --term "needle@example.com" --json
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
Narrow the scan when you already know the tables to look at, and bound the
|
|
152
|
+
hit set with `--max-hits` (per table):
|
|
153
|
+
|
|
154
|
+
```sh
|
|
155
|
+
code-viewer query search --db app.db --term "needle@example.com" \
|
|
156
|
+
--tables users,orders --max-hits 20 --json
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
By default only text-like columns (TEXT/VARCHAR/CLOB/JSON/UUID/...) are
|
|
160
|
+
scanned. Add `--include-non-text` to also scan non-BLOB columns such as
|
|
161
|
+
numeric ids. BLOB/BYTEA columns are always skipped. Leave it off unless
|
|
162
|
+
the value might live outside text-like columns — the default is faster and
|
|
163
|
+
avoids noisy hits.
|
|
164
|
+
|
|
165
|
+
Each hit names the `table`, `column`, a `valuePreview`, and (for SQL stores
|
|
166
|
+
with a primary key) a JSON-serialized `rowKeyJson` you can feed into a
|
|
167
|
+
follow-up `query exec` with a matching `WHERE` clause.
|
|
168
|
+
|
|
169
|
+
The default output prints hit lines plus a summary; pass `--json` to get
|
|
170
|
+
the full status payload (hits + scan counts) for programmatic use. An
|
|
171
|
+
empty result is a clean exit 0 — `no hits (scanned N tables)` on stdout.
|
|
172
|
+
|
|
173
|
+
Tune `--timeout <sec>` (default 60) when scanning a large database.
|
|
174
|
+
On timeout the running job is cancelled and the CLI exits 1 with a
|
|
175
|
+
"search timed out after Ns" message.
|
|
176
|
+
|
|
177
|
+
PostgreSQL: pass `--schema <name>` to pin the search to one schema; without
|
|
178
|
+
it the server uses `public` (or the first available schema).
|
|
43
179
|
|
|
44
180
|
## Guidelines
|
|
45
181
|
|
|
46
|
-
- Always use LIMIT
|
|
47
|
-
-
|
|
48
|
-
|
|
182
|
+
- Always use LIMIT in `--sql`. The server caps rows but be explicit.
|
|
183
|
+
- Before writing schema-discovery SQL, prefer `query schema`, `query columns`,
|
|
184
|
+
or `query ddl`; they work across SQLite, PostgreSQL, and MySQL through the
|
|
185
|
+
server's existing introspection endpoints.
|
|
186
|
+
- When using PostgreSQL schemas, pass the same `--schema` to `query exec` that
|
|
187
|
+
you used for introspection.
|
|
188
|
+
- Write `--title` for the human, not for yourself.
|
|
189
|
+
- Use `--body` to explain why the query matters.
|
|
49
190
|
- Do not query broad PII or secrets unless explicitly asked.
|
|
50
|
-
- Use
|
|
51
|
-
- Prefer specific columns over SELECT
|
|
191
|
+
- Use `--no-save` for exploratory queries that should not remain in history.
|
|
192
|
+
- Prefer specific columns over `SELECT *`.
|
|
193
|
+
- For `search`, prefer `--tables` when you already know which tables to scan,
|
|
194
|
+
and `--max-hits` to keep large hit sets bounded.
|
|
195
|
+
- For `search`, leave `--include-non-text` off unless the value can live in
|
|
196
|
+
a non-text column such as a numeric id. BLOB/BYTEA are always skipped.
|
|
52
197
|
|
|
53
198
|
## Full reference
|
|
54
199
|
|
|
@@ -32,12 +32,24 @@ operation that should modify the database in a predictable way.
|
|
|
32
32
|
Specify which database and tables to capture. Always use `--tables` to
|
|
33
33
|
avoid scanning unnecessary tables.
|
|
34
34
|
|
|
35
|
+
Add `--wait --json` so the CLI blocks until the snapshot finishes and
|
|
36
|
+
returns the final metadata as JSON. This lets an AI agent grab the
|
|
37
|
+
snapshot id from the `id` field without a separate
|
|
38
|
+
`snapshot list` poll. Default `--timeout` is 120 seconds; on timeout the
|
|
39
|
+
running snapshot is cancelled and the CLI exits 1.
|
|
40
|
+
|
|
35
41
|
```sh
|
|
36
42
|
code-viewer query snapshot create --db app.db \
|
|
37
43
|
--tables users,orders \
|
|
38
|
-
--note "Before running user registration test"
|
|
44
|
+
--note "Before running user registration test" \
|
|
45
|
+
--wait --json
|
|
39
46
|
```
|
|
40
47
|
|
|
48
|
+
The ack-only form (no `--wait`) returns immediately with
|
|
49
|
+
`{ ok, message, snapshotId, pollCommand }` and the scan runs in the
|
|
50
|
+
background; use `snapshot list` (or paste `pollCommand` verbatim) to
|
|
51
|
+
confirm completion before diffing.
|
|
52
|
+
|
|
41
53
|
### 2. Perform the operation
|
|
42
54
|
|
|
43
55
|
The human (or a test runner, migration script, etc.) modifies the database.
|
|
@@ -47,15 +59,30 @@ The human (or a test runner, migration script, etc.) modifies the database.
|
|
|
47
59
|
```sh
|
|
48
60
|
code-viewer query snapshot create --db app.db \
|
|
49
61
|
--tables users,orders \
|
|
50
|
-
--note "After running user registration test"
|
|
62
|
+
--note "After running user registration test" \
|
|
63
|
+
--wait --json
|
|
51
64
|
```
|
|
52
65
|
|
|
53
66
|
### 4. Get snapshot IDs
|
|
54
67
|
|
|
68
|
+
`--wait --json` already prints the `id` for each created snapshot. If you
|
|
69
|
+
took the snapshots without `--wait`, list them to find the IDs:
|
|
70
|
+
|
|
55
71
|
```sh
|
|
56
72
|
code-viewer query snapshot list --db app.db
|
|
57
73
|
```
|
|
58
74
|
|
|
75
|
+
`snapshot create` (no-wait) also prints a paste-safe poll command that
|
|
76
|
+
pins `--server '<url>'` and single-quotes db/schema, so copying it never
|
|
77
|
+
silently falls back to auto-discovery. The same string is available as
|
|
78
|
+
the `pollCommand` field in `--json` ack output.
|
|
79
|
+
|
|
80
|
+
`snapshot list --json` enriches each `snapshots[]` element with paste-safe
|
|
81
|
+
`deleteCommand` and `noteCommand` fields. Each pins `--server '<url>'` and
|
|
82
|
+
single-quotes the snapshot id; `noteCommand` also quotes the current note
|
|
83
|
+
as-is so you can paste the line and edit the value to update. Use these
|
|
84
|
+
when you want to drop or re-label snapshots without rebuilding the call.
|
|
85
|
+
|
|
59
86
|
### 5. View the diff
|
|
60
87
|
|
|
61
88
|
Compare table-level summary (which tables changed and how many rows):
|
|
@@ -64,6 +91,12 @@ Compare table-level summary (which tables changed and how many rows):
|
|
|
64
91
|
code-viewer query diff tables --before snap-abc123 --after snap-def456
|
|
65
92
|
```
|
|
66
93
|
|
|
94
|
+
Each table summary line is followed by a paste-safe
|
|
95
|
+
`# diff rows: code-viewer query --server '<url>' diff rows --before '<id>'
|
|
96
|
+
--after '<id>' --table '<table>' --json` comment line, so you can copy the
|
|
97
|
+
exact next command per table without rebuilding it. With `--json`, the same
|
|
98
|
+
literal is exposed as a `diffRowsCommand` field on each `tables[]` element.
|
|
99
|
+
|
|
67
100
|
Drill into row-level changes for a specific table:
|
|
68
101
|
|
|
69
102
|
```sh
|
|
@@ -74,6 +107,27 @@ code-viewer query diff rows --before snap-abc123 --after snap-def456 \
|
|
|
74
107
|
The human can also view all snapshots and diffs in the browser's
|
|
75
108
|
Database > Snapshot tab.
|
|
76
109
|
|
|
110
|
+
## PostgreSQL (multi-schema)
|
|
111
|
+
|
|
112
|
+
For PostgreSQL with multiple schemas, pass `--schema <name>` to BOTH
|
|
113
|
+
`snapshot create` and the matching `snapshot list`. Snapshot create binds
|
|
114
|
+
the snapshot to that schema, the polling lookup performed during `--wait`
|
|
115
|
+
re-uses it, and `snapshot list --schema` filters to the same scope so the
|
|
116
|
+
before/after pair stays consistent. Without `--schema`, snapshot create
|
|
117
|
+
infers the schema (`public` or the first available) and `snapshot list`
|
|
118
|
+
returns every snapshot for the database id.
|
|
119
|
+
|
|
120
|
+
```sh
|
|
121
|
+
code-viewer query snapshot create --db docker:pg-svc --schema analytics \
|
|
122
|
+
--tables events \
|
|
123
|
+
--note "Before backfill" \
|
|
124
|
+
--wait --json
|
|
125
|
+
|
|
126
|
+
code-viewer query snapshot list --db docker:pg-svc --schema analytics --json
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
SQLite / MySQL / Redis / S3 do not need `--schema` (single namespace).
|
|
130
|
+
|
|
77
131
|
## Managing snapshots
|
|
78
132
|
|
|
79
133
|
```sh
|
|
@@ -94,6 +148,9 @@ code-viewer query snapshot delete --id snap-abc123
|
|
|
94
148
|
database size.
|
|
95
149
|
- The diff is computed on demand, not stored — you can diff any two
|
|
96
150
|
snapshots of the same database.
|
|
151
|
+
- Prefer `--wait --json` when you want to chain the next command on the
|
|
152
|
+
resulting snapshot id. Tune `--timeout <sec>` (default 120) for very
|
|
153
|
+
large tables.
|
|
97
154
|
|
|
98
155
|
## Full reference
|
|
99
156
|
|