dbgraph 0.1.0 → 0.1.1
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.ZH-cn.md +357 -0
- package/README.md +230 -224
- package/package.json +1 -1
package/README.ZH-cn.md
ADDED
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
# DBGraph
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/dbgraph)
|
|
4
|
+
[](LICENSE)
|
|
5
|
+
[](https://nodejs.org)
|
|
6
|
+
|
|
7
|
+
数据库知识图谱 —— 将数据库 schema 提取为本地知识图谱,通过 MCP 供 LLM 理解库表结构,从而减少 SQL 生成错误。
|
|
8
|
+
|
|
9
|
+
## 原理
|
|
10
|
+
|
|
11
|
+
LLM 写 SQL 犯错的最大原因是**不知道你的库有什么**——表名靠猜、列名靠蒙、JOIN 条件靠碰运气。DBGraph 把数据库的 schema 信息(表、列、类型、外键、约束、索引)全部提取成一个**可搜索的知识图谱**存在 `.dbgraph/` 目录下,LLM 通过 MCP 工具直接查询,不直接连数据库。
|
|
12
|
+
|
|
13
|
+
```
|
|
14
|
+
传统流程:
|
|
15
|
+
LLM → 猜表名列名 → 写 SQL → 执行 → 报错 → 再猜 → 循环
|
|
16
|
+
|
|
17
|
+
DBGraph 流程:
|
|
18
|
+
LLM → dbgraph_context("orders") → 拿到精确 schema
|
|
19
|
+
→ 写 SQL → dbgraph_execute → 成功
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## 快速安装
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
# 全局安装
|
|
26
|
+
npm install -g dbgraph
|
|
27
|
+
|
|
28
|
+
# 或直接用 npx(无需安装)
|
|
29
|
+
npx dbgraph --help
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## 前置要求
|
|
33
|
+
|
|
34
|
+
- **Node.js >= 22.5.0**(需要内置 `node:sqlite` 支持 FTS5 + WAL)
|
|
35
|
+
|
|
36
|
+
## 快速开始
|
|
37
|
+
|
|
38
|
+
> 安装后直接使用 `dbgraph` 命令。如需本地开发,可用 `npm run cli -- <命令>` 替代(自动先构建)。
|
|
39
|
+
|
|
40
|
+
### 1. 初始化项目
|
|
41
|
+
|
|
42
|
+
所有命令默认使用当前目录,也可指定目录路径。
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
# 初始化当前目录
|
|
46
|
+
dbgraph init
|
|
47
|
+
|
|
48
|
+
# 初始化并立即索引(一步完成)
|
|
49
|
+
dbgraph init -i
|
|
50
|
+
|
|
51
|
+
# 初始化指定目录
|
|
52
|
+
dbgraph init ./demo-project
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
这会创建:
|
|
56
|
+
- `.dbgraph/` —— 知识图谱数据目录
|
|
57
|
+
- `dbgraph-db.json` —— 数据库连接配置(默认模板)
|
|
58
|
+
|
|
59
|
+
### 2. 配置数据库连接
|
|
60
|
+
|
|
61
|
+
编辑 `demo-project/dbgraph-db.json`,填入你的数据库信息:
|
|
62
|
+
|
|
63
|
+
```json
|
|
64
|
+
{
|
|
65
|
+
"databases": [
|
|
66
|
+
{
|
|
67
|
+
"alias": "prod",
|
|
68
|
+
"engine": "postgresql",
|
|
69
|
+
"host": "localhost",
|
|
70
|
+
"port": 5432,
|
|
71
|
+
"database": "ecommerce",
|
|
72
|
+
"schemas": ["public"],
|
|
73
|
+
"auth": "env:DB_PASSWORD"
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
"alias": "local",
|
|
77
|
+
"engine": "sqlite",
|
|
78
|
+
"path": "./dev.db"
|
|
79
|
+
}
|
|
80
|
+
]
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### 3. 提取 Schema
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
dbgraph index
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
把数据库 schema 提取到知识图谱中。首次运行会连接所有配置的数据库,提取表/列/外键/索引/视图等信息。
|
|
91
|
+
|
|
92
|
+
### 4. 查询知识图谱
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
# 搜索表/列
|
|
96
|
+
dbgraph query orders
|
|
97
|
+
dbgraph query users --kind table
|
|
98
|
+
|
|
99
|
+
# 查看完整表结构
|
|
100
|
+
dbgraph context public.orders
|
|
101
|
+
|
|
102
|
+
# 查看状态
|
|
103
|
+
dbgraph status
|
|
104
|
+
|
|
105
|
+
# 列出数据源
|
|
106
|
+
dbgraph sources
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## 配置说明
|
|
110
|
+
|
|
111
|
+
### `dbgraph-db.json`
|
|
112
|
+
|
|
113
|
+
| 字段 | 类型 | 必填 | 说明 |
|
|
114
|
+
|------|------|------|------|
|
|
115
|
+
| `alias` | string | 是 | 数据库别名,在知识图谱中用 `db://@alias` 标识 |
|
|
116
|
+
| `engine` | string | 是 | 数据库引擎:`postgresql` / `mysql` / `mariadb` / `sqlite` |
|
|
117
|
+
| `host` | string | 否 | 主机地址(SQLite 不需要)|
|
|
118
|
+
| `port` | number | 否 | 端口(默认根据引擎判断)|
|
|
119
|
+
| `database` | string | 是(非SQLite) | 数据库名 |
|
|
120
|
+
| `schemas` | string[] | 否 | 要提取的 schema 列表(默认全部)|
|
|
121
|
+
| `path` | string | 是(SQLite) | SQLite 文件路径 |
|
|
122
|
+
| `auth` | string | 否 | 认证方式,如 `env:DB_PASSWORD`(环境变量)、`~/.pgpass` |
|
|
123
|
+
| `ssl` | boolean | 否 | 是否启用 SSL 连接 |
|
|
124
|
+
|
|
125
|
+
## CLI 命令参考
|
|
126
|
+
|
|
127
|
+
所有命令格式:`dbgraph <命令> [选项] [目录]`
|
|
128
|
+
|
|
129
|
+
不填目录时默认为当前目录。
|
|
130
|
+
|
|
131
|
+
| 命令 | 说明 |
|
|
132
|
+
|------|------|
|
|
133
|
+
| `init` | 初始化 .dbgraph 项目 + 配置 |
|
|
134
|
+
| `index` | 运行数据库内省,提取 schema |
|
|
135
|
+
| `serve` | 启动 MCP 服务器(供 AI Agent 使用)|
|
|
136
|
+
| `query` | 搜索表/列/视图 |
|
|
137
|
+
| `context` | 查看完整表结构 |
|
|
138
|
+
| `status` | 知识图谱状态统计 |
|
|
139
|
+
| `sources` | 列出数据源 |
|
|
140
|
+
| `test` | 测试数据库连接 |
|
|
141
|
+
| `config` | 查看/创建配置 |
|
|
142
|
+
|
|
143
|
+
### `init`
|
|
144
|
+
|
|
145
|
+
```bash
|
|
146
|
+
# 初始化当前目录
|
|
147
|
+
dbgraph init
|
|
148
|
+
|
|
149
|
+
# 初始化并立即索引
|
|
150
|
+
dbgraph init -i
|
|
151
|
+
|
|
152
|
+
# 初始化指定目录
|
|
153
|
+
dbgraph init ./my-project
|
|
154
|
+
|
|
155
|
+
# 指定配置文件路径
|
|
156
|
+
dbgraph init ./my-project -c ./my-project/custom-config.json
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### `index`
|
|
160
|
+
|
|
161
|
+
```bash
|
|
162
|
+
# 索引当前目录配置的数据库
|
|
163
|
+
dbgraph index
|
|
164
|
+
|
|
165
|
+
# 指定配置文件
|
|
166
|
+
dbgraph index ./my-project -c ./my-project/custom-config.json
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### `serve`(MCP 模式)
|
|
170
|
+
|
|
171
|
+
```bash
|
|
172
|
+
# 启动 MCP stdio 服务器(当前目录)
|
|
173
|
+
dbgraph serve
|
|
174
|
+
|
|
175
|
+
# 指定项目目录
|
|
176
|
+
dbgraph serve ./my-project
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
AI Agent 连接到 MCP 后自动发现 `dbgraph_*` 工具。
|
|
180
|
+
|
|
181
|
+
### `query`
|
|
182
|
+
|
|
183
|
+
```bash
|
|
184
|
+
# 搜索
|
|
185
|
+
dbgraph query orders
|
|
186
|
+
|
|
187
|
+
# 按类型过滤
|
|
188
|
+
dbgraph query orders --kind table
|
|
189
|
+
dbgraph query amount --kind column
|
|
190
|
+
|
|
191
|
+
# JSON 格式输出
|
|
192
|
+
dbgraph query orders --json
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
### `context`
|
|
196
|
+
|
|
197
|
+
```bash
|
|
198
|
+
# 查看表结构
|
|
199
|
+
dbgraph context orders
|
|
200
|
+
dbgraph context public.orders
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
输出示例:
|
|
204
|
+
```
|
|
205
|
+
## Table: public.orders
|
|
206
|
+
|
|
207
|
+
Database: prod (postgresql)
|
|
208
|
+
|
|
209
|
+
| Column | Type | Nullable | Default | PK | FK |
|
|
210
|
+
|-------------|-------------|----------|----------|----|-----------|
|
|
211
|
+
| id | bigint | NO | | PK | |
|
|
212
|
+
| user_id | integer | NO | | | users.id |
|
|
213
|
+
| status | varchar(20) | NO | pending | | |
|
|
214
|
+
| total_amount| numeric(10,2)| YES | 0.00 | | |
|
|
215
|
+
| created_at | timestamp | NO | now() | | |
|
|
216
|
+
|
|
217
|
+
Indexes:
|
|
218
|
+
- idx_orders_status on (status)
|
|
219
|
+
- pk_orders PRIMARY KEY using btree (id)
|
|
220
|
+
|
|
221
|
+
Foreign Keys:
|
|
222
|
+
- fk_orders_user → users(id) ON DELETE CASCADE
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
## MCP 工具
|
|
226
|
+
|
|
227
|
+
启动 `dbgraph serve` 后,AI Agent 可以调用以下工具:
|
|
228
|
+
|
|
229
|
+
| 工具 | 用途 | 推荐时机 |
|
|
230
|
+
|------|------|---------|
|
|
231
|
+
| `dbgraph_search` | 搜索表/列/视图/索引 | 不确定名字时 |
|
|
232
|
+
| `dbgraph_context` | **主要入口** —— 返回完整表结构 + 列 + FK + 索引 | 写 SQL 前 |
|
|
233
|
+
| `dbgraph_trace` | 追踪 FK 关联路径(orders→users) | 需要写 JOIN 时 |
|
|
234
|
+
| `dbgraph_explore` | 一次性探索多张相关表 | 复杂查询涉及多表时 |
|
|
235
|
+
| `dbgraph_sources` | 列出所有已配置的数据库源 | 了解有哪些库可用 |
|
|
236
|
+
| `dbgraph_status` | 知识图谱统计 | 检查是否已索引 |
|
|
237
|
+
|
|
238
|
+
### 推荐工具调用策略
|
|
239
|
+
|
|
240
|
+
```
|
|
241
|
+
写 SQL 前的标准流程:
|
|
242
|
+
1. dbgraph_search("order") → 找到 order 相关表
|
|
243
|
+
2. dbgraph_context("public.orders") → 获取完整 schema
|
|
244
|
+
3. dbgraph_context("public.users") → 获取关联表 schema
|
|
245
|
+
4. dbgraph_trace("orders", "users") → 验证 FK 关联
|
|
246
|
+
5. LLM 写出精确 SQL
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
## 支持引擎
|
|
250
|
+
|
|
251
|
+
| 引擎 | 状态 | 说明 |
|
|
252
|
+
|------|------|------|
|
|
253
|
+
| PostgreSQL | ✅ 完整支持 | `information_schema` + `pg_catalog` |
|
|
254
|
+
| MySQL / MariaDB | ✅ 完整支持 | `information_schema` |
|
|
255
|
+
| SQLite | ✅ 完整支持 | `pragma table_info` / `foreign_key_list` |
|
|
256
|
+
| SQL Server | 🔜 计划中 | |
|
|
257
|
+
| Oracle | 🔜 计划中 | |
|
|
258
|
+
| MongoDB | 🔜 计划中 | |
|
|
259
|
+
|
|
260
|
+
## 项目结构
|
|
261
|
+
|
|
262
|
+
```
|
|
263
|
+
dbgraph/
|
|
264
|
+
├── src/
|
|
265
|
+
│ ├── index.ts # DBGraph 主类
|
|
266
|
+
│ ├── types.ts # 所有类型 (Node, Edge, TableSchema...)
|
|
267
|
+
│ ├── config.ts # dbgraph-db.json 配置管理
|
|
268
|
+
│ ├── directory.ts # .dbgraph 目录管理
|
|
269
|
+
│ ├── errors.ts # 错误类型
|
|
270
|
+
│ ├── utils.ts # 工具函数
|
|
271
|
+
│ │
|
|
272
|
+
│ ├── db/ # SQLite 存储层
|
|
273
|
+
│ │ ├── schema.sql # 表结构 (nodes/edges FTS5)
|
|
274
|
+
│ │ ├── sqlite-adapter.ts # node:sqlite 适配
|
|
275
|
+
│ │ ├── migrations.ts # 版本迁移
|
|
276
|
+
│ │ ├── queries.ts # CRUD + FTS5 搜索 + 评分 (LRU缓存)
|
|
277
|
+
│ │ └── index.ts # 连接管理
|
|
278
|
+
│ │
|
|
279
|
+
│ ├── graph/
|
|
280
|
+
│ │ └── traversal.ts # BFS/DFS/寻路/影响半径
|
|
281
|
+
│ │
|
|
282
|
+
│ ├── context/
|
|
283
|
+
│ │ ├── index.ts # 表结构组装
|
|
284
|
+
│ │ └── formatter.ts # Markdown 输出
|
|
285
|
+
│ │
|
|
286
|
+
│ ├── introspect/ # 数据库内省
|
|
287
|
+
│ │ ├── base.ts # 基类 + Node/Edge 工厂
|
|
288
|
+
│ │ ├── index.ts # 工厂方法
|
|
289
|
+
│ │ ├── connection.ts # 连接管理
|
|
290
|
+
│ │ ├── postgres.ts # PostgreSQL
|
|
291
|
+
│ │ ├── mysql.ts # MySQL
|
|
292
|
+
│ │ └── sqlite.ts # SQLite
|
|
293
|
+
│ │
|
|
294
|
+
│ ├── mcp/ # MCP 服务器
|
|
295
|
+
│ │ ├── transport.ts # JSON-RPC 传输
|
|
296
|
+
│ │ ├── session.ts # 会话管理
|
|
297
|
+
│ │ ├── engine.ts # 引擎 + 生命周期
|
|
298
|
+
│ │ ├── tools.ts # 6 个 dbgraph_* 工具
|
|
299
|
+
│ │ ├── server-instructions.ts # LLM 指引
|
|
300
|
+
│ │ └── index.ts # MCPServer
|
|
301
|
+
│ │
|
|
302
|
+
│ └── bin/
|
|
303
|
+
│ └── dbgraph.ts # CLI
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
## 开发
|
|
307
|
+
|
|
308
|
+
```bash
|
|
309
|
+
# 安装依赖
|
|
310
|
+
npm install
|
|
311
|
+
|
|
312
|
+
# 构建
|
|
313
|
+
npm run build
|
|
314
|
+
|
|
315
|
+
# 开发模式(watch)
|
|
316
|
+
npm run dev
|
|
317
|
+
|
|
318
|
+
# 运行帮助
|
|
319
|
+
npm run cli -- --help
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
### 添加新数据库引擎
|
|
323
|
+
|
|
324
|
+
在 `src/introspect/` 下创建新文件(如 `mssql.ts`),实现 `BaseIntrospector` 抽象类:
|
|
325
|
+
|
|
326
|
+
```typescript
|
|
327
|
+
import { BaseIntrospector } from './base';
|
|
328
|
+
|
|
329
|
+
export class MSSQLIntrospector extends BaseIntrospector {
|
|
330
|
+
async extractAll(): Promise<IntrospectResult> {
|
|
331
|
+
// 1. 连接数据库
|
|
332
|
+
// 2. 查询 information_schema
|
|
333
|
+
// 3. 调用 this.makeNode() / this.makeEdge()
|
|
334
|
+
// 4. 返回 IntrospectResult
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
然后在 `src/introspect/index.ts` 中注册:
|
|
340
|
+
|
|
341
|
+
```typescript
|
|
342
|
+
case 'mssql':
|
|
343
|
+
return new MSSQLIntrospector(config);
|
|
344
|
+
```
|
|
345
|
+
|
|
346
|
+
## 参考
|
|
347
|
+
|
|
348
|
+
- **[AGENTS.md](AGENTS.md)** — OpenCode AI Agent 的项目指引,包含开发命令速查、架构要点和 CodeGraph 使用说明。
|
|
349
|
+
- **CodeGraph** — 本项目已初始化 CodeGraph 索引(`.codegraph/`),Agent 可优先使用 `codegraph_*` 工具进行结构查询,速度远超 grep。
|
|
350
|
+
|
|
351
|
+
## 鸣谢
|
|
352
|
+
|
|
353
|
+
DBGraph 的架构和 MCP 设计受到了 [CodeGraph](https://github.com/colbymchenry/codegraph) 的启发。CodeGraph 是一个优秀的代码知识图谱工具,将代码库结构提取为可查询的知识图谱,本项目借鉴了其思路并将其应用于数据库 schema 领域。
|
|
354
|
+
|
|
355
|
+
## License
|
|
356
|
+
|
|
357
|
+
MIT
|
package/README.md
CHANGED
|
@@ -1,193 +1,202 @@
|
|
|
1
|
-
# DBGraph
|
|
1
|
+
# DBGraph
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/dbgraph)
|
|
4
4
|
[](LICENSE)
|
|
5
5
|
[](https://nodejs.org)
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
Database knowledge graph — Introspect database schemas into a local-first knowledge graph, exposed over MCP for LLM-powered SQL generation.
|
|
8
8
|
|
|
9
|
-
##
|
|
9
|
+
## The Problem
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
LLMs make SQL mistakes because they **don't know your schema** — guessing table names, column names, and JOIN conditions. DBGraph extracts your complete database schema (tables, columns, types, foreign keys, constraints, indexes) into a **searchable knowledge graph** stored in `.dbgraph/`. LLMs query it via MCP tools directly — no live database connection needed.
|
|
12
12
|
|
|
13
13
|
```
|
|
14
|
-
|
|
15
|
-
LLM →
|
|
14
|
+
Without DBGraph:
|
|
15
|
+
LLM → guess names → write SQL → run → error → guess again → loop
|
|
16
16
|
|
|
17
|
-
DBGraph
|
|
18
|
-
LLM → dbgraph_context("orders") →
|
|
19
|
-
→
|
|
17
|
+
With DBGraph:
|
|
18
|
+
LLM → dbgraph_context("orders") → get exact schema
|
|
19
|
+
→ write SQL → dbgraph_execute → success
|
|
20
20
|
```
|
|
21
21
|
|
|
22
|
-
##
|
|
22
|
+
## Quick Install
|
|
23
23
|
|
|
24
24
|
```bash
|
|
25
|
-
#
|
|
25
|
+
# Global install
|
|
26
26
|
npm install -g dbgraph
|
|
27
27
|
|
|
28
|
-
#
|
|
28
|
+
# Or use directly with npx
|
|
29
29
|
npx dbgraph --help
|
|
30
30
|
```
|
|
31
31
|
|
|
32
|
-
##
|
|
33
|
-
|
|
34
|
-
- **Node.js >= 22.5.0
|
|
35
|
-
|
|
36
|
-
##
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
### 1.
|
|
41
|
-
|
|
42
|
-
```bash
|
|
43
|
-
#
|
|
44
|
-
dbgraph init
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
"
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
"
|
|
69
|
-
"
|
|
70
|
-
"
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
32
|
+
## Requirements
|
|
33
|
+
|
|
34
|
+
- **Node.js >= 22.5.0** (requires built-in `node:sqlite` for FTS5 + WAL)
|
|
35
|
+
|
|
36
|
+
## Quick Start
|
|
37
|
+
|
|
38
|
+
All commands default to the current directory. Pass a directory path to target another project.
|
|
39
|
+
|
|
40
|
+
### 1. Initialize a project
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
# Initialize current directory
|
|
44
|
+
dbgraph init
|
|
45
|
+
|
|
46
|
+
# Or initialize + index in one step
|
|
47
|
+
dbgraph init -i
|
|
48
|
+
|
|
49
|
+
# Or target a specific directory
|
|
50
|
+
dbgraph init ./demo-project
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
This creates:
|
|
54
|
+
- `.dbgraph/` — knowledge graph data directory
|
|
55
|
+
- `dbgraph-db.json` — database connection config (default template)
|
|
56
|
+
|
|
57
|
+
### 2. Configure database connections
|
|
58
|
+
|
|
59
|
+
Edit `dbgraph-db.json` with your database info:
|
|
60
|
+
|
|
61
|
+
```json
|
|
62
|
+
{
|
|
63
|
+
"databases": [
|
|
64
|
+
{
|
|
65
|
+
"alias": "prod",
|
|
66
|
+
"engine": "postgresql",
|
|
67
|
+
"host": "localhost",
|
|
68
|
+
"port": 5432,
|
|
69
|
+
"database": "ecommerce",
|
|
70
|
+
"schemas": ["public"],
|
|
71
|
+
"auth": "env:DB_PASSWORD"
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
"alias": "local",
|
|
75
|
+
"engine": "sqlite",
|
|
76
|
+
"path": "./dev.db"
|
|
77
|
+
}
|
|
78
|
+
]
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### 3. Extract schema
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
dbgraph index
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Introspects all configured databases and stores tables, columns, foreign keys, indexes, and views into the knowledge graph.
|
|
89
|
+
|
|
90
|
+
### 4. Query the knowledge graph
|
|
91
|
+
|
|
92
|
+
```bash
|
|
93
|
+
# Search tables/columns
|
|
94
|
+
dbgraph query orders
|
|
95
|
+
dbgraph query users --kind table
|
|
96
|
+
|
|
97
|
+
# View full table structure
|
|
98
|
+
dbgraph context public.orders
|
|
99
|
+
|
|
100
|
+
# Check status
|
|
101
|
+
dbgraph status
|
|
102
|
+
|
|
103
|
+
# List data sources
|
|
104
|
+
dbgraph sources
|
|
80
105
|
```
|
|
81
106
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
### 4. 查询知识图谱
|
|
85
|
-
|
|
86
|
-
```bash
|
|
87
|
-
# 搜索表/列
|
|
88
|
-
node dist/bin/dbgraph.js query orders ./demo-project
|
|
89
|
-
node dist/bin/dbgraph.js query users --kind table ./demo-project
|
|
90
|
-
|
|
91
|
-
# 查看完整表结构
|
|
92
|
-
node dist/bin/dbgraph.js context public.orders ./demo-project
|
|
93
|
-
|
|
94
|
-
# 查看状态
|
|
95
|
-
node dist/bin/dbgraph.js status ./demo-project
|
|
96
|
-
|
|
97
|
-
# 列出数据源
|
|
98
|
-
node dist/bin/dbgraph.js sources ./demo-project
|
|
99
|
-
```
|
|
100
|
-
|
|
101
|
-
## 配置说明
|
|
107
|
+
## Configuration
|
|
102
108
|
|
|
103
109
|
### `dbgraph-db.json`
|
|
104
110
|
|
|
105
|
-
|
|
|
106
|
-
|
|
107
|
-
| `alias` | string |
|
|
108
|
-
| `engine` | string |
|
|
109
|
-
| `host` | string |
|
|
110
|
-
| `port` | number |
|
|
111
|
-
| `database` | string |
|
|
112
|
-
| `schemas` | string[] |
|
|
113
|
-
| `path` | string |
|
|
114
|
-
| `auth` | string |
|
|
115
|
-
| `ssl` | boolean |
|
|
116
|
-
|
|
117
|
-
## CLI
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
|
125
|
-
|
|
126
|
-
| `
|
|
127
|
-
| `
|
|
128
|
-
| `
|
|
129
|
-
| `
|
|
130
|
-
| `
|
|
131
|
-
| `
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
#
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
#
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
#
|
|
146
|
-
|
|
111
|
+
| Field | Type | Required | Description |
|
|
112
|
+
|-------|------|----------|-------------|
|
|
113
|
+
| `alias` | string | yes | Database alias, identified as `db://@alias` in the graph |
|
|
114
|
+
| `engine` | string | yes | Database engine: `postgresql` / `mysql` / `mariadb` / `sqlite` |
|
|
115
|
+
| `host` | string | no | Host address (not needed for SQLite) |
|
|
116
|
+
| `port` | number | no | Port (default depends on engine) |
|
|
117
|
+
| `database` | string | yes (non-SQLite) | Database name |
|
|
118
|
+
| `schemas` | string[] | no | Schemas to introspect (default: all) |
|
|
119
|
+
| `path` | string | yes (SQLite) | SQLite file path |
|
|
120
|
+
| `auth` | string | no | Authentication, e.g. `env:DB_PASSWORD` or `~/.pgpass` |
|
|
121
|
+
| `ssl` | boolean | no | Enable SSL connection |
|
|
122
|
+
|
|
123
|
+
## CLI Reference
|
|
124
|
+
|
|
125
|
+
All commands follow: `dbgraph <command> [options] [directory]`
|
|
126
|
+
|
|
127
|
+
| Command | Description |
|
|
128
|
+
|---------|-------------|
|
|
129
|
+
| `init` | Initialize .dbgraph project + config |
|
|
130
|
+
| `index` | Run database introspection |
|
|
131
|
+
| `serve` | Start MCP server (for AI agents) |
|
|
132
|
+
| `query` | Search tables/columns/views |
|
|
133
|
+
| `context` | View full table structure |
|
|
134
|
+
| `status` | Knowledge graph statistics |
|
|
135
|
+
| `sources` | List data sources |
|
|
136
|
+
| `test` | Test database connections |
|
|
137
|
+
| `config` | View/create configuration |
|
|
138
|
+
|
|
139
|
+
### `init`
|
|
140
|
+
|
|
141
|
+
```bash
|
|
142
|
+
# Initialize current directory
|
|
143
|
+
dbgraph init
|
|
144
|
+
|
|
145
|
+
# Init and index immediately
|
|
146
|
+
dbgraph init -i
|
|
147
|
+
|
|
148
|
+
# Initialize a specific directory
|
|
149
|
+
dbgraph init ./my-project
|
|
150
|
+
|
|
151
|
+
# Specify config file path
|
|
152
|
+
dbgraph init ./my-project -c ./my-project/custom-config.json
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### `index`
|
|
156
|
+
|
|
157
|
+
```bash
|
|
158
|
+
# Index configured databases (current dir)
|
|
159
|
+
dbgraph index
|
|
160
|
+
|
|
161
|
+
# Use a specific config file
|
|
162
|
+
dbgraph index ./my-project -c ./my-project/custom-config.json
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### `serve` (MCP mode)
|
|
166
|
+
|
|
167
|
+
```bash
|
|
168
|
+
# Start MCP stdio server (current dir)
|
|
169
|
+
dbgraph serve
|
|
170
|
+
|
|
171
|
+
# Start for a specific project directory
|
|
172
|
+
dbgraph serve ./my-project
|
|
147
173
|
```
|
|
148
174
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
```bash
|
|
152
|
-
# 索引指定项目配置的数据库
|
|
153
|
-
node dist/bin/dbgraph.js index ./my-project
|
|
154
|
-
|
|
155
|
-
# 指定配置文件
|
|
156
|
-
node dist/bin/dbgraph.js index ./my-project -c ./my-project/custom-config.json
|
|
157
|
-
```
|
|
158
|
-
|
|
159
|
-
### `serve`(MCP 模式)
|
|
160
|
-
|
|
161
|
-
```bash
|
|
162
|
-
# 启动 MCP stdio 服务器
|
|
163
|
-
node dist/bin/dbgraph.js serve ./my-project
|
|
164
|
-
```
|
|
165
|
-
|
|
166
|
-
AI Agent 连接到 MCP 后自动发现 `dbgraph_*` 工具。
|
|
175
|
+
AI agents automatically discover `dbgraph_*` tools upon connecting to MCP.
|
|
167
176
|
|
|
168
177
|
### `query`
|
|
169
178
|
|
|
170
179
|
```bash
|
|
171
|
-
#
|
|
172
|
-
|
|
180
|
+
# Search
|
|
181
|
+
dbgraph query orders ./my-project
|
|
173
182
|
|
|
174
|
-
#
|
|
175
|
-
|
|
176
|
-
|
|
183
|
+
# Filter by kind
|
|
184
|
+
dbgraph query orders --kind table ./my-project
|
|
185
|
+
dbgraph query amount --kind column ./my-project
|
|
177
186
|
|
|
178
|
-
# JSON
|
|
179
|
-
|
|
187
|
+
# JSON output
|
|
188
|
+
dbgraph query orders --json ./my-project
|
|
180
189
|
```
|
|
181
190
|
|
|
182
191
|
### `context`
|
|
183
192
|
|
|
184
193
|
```bash
|
|
185
|
-
#
|
|
186
|
-
|
|
187
|
-
|
|
194
|
+
# View table structure
|
|
195
|
+
dbgraph context orders ./my-project
|
|
196
|
+
dbgraph context public.orders ./my-project
|
|
188
197
|
```
|
|
189
198
|
|
|
190
|
-
|
|
199
|
+
Example output:
|
|
191
200
|
```
|
|
192
201
|
## Table: public.orders
|
|
193
202
|
|
|
@@ -209,139 +218,136 @@ Foreign Keys:
|
|
|
209
218
|
- fk_orders_user → users(id) ON DELETE CASCADE
|
|
210
219
|
```
|
|
211
220
|
|
|
212
|
-
## MCP
|
|
221
|
+
## MCP Tools
|
|
213
222
|
|
|
214
|
-
|
|
223
|
+
After starting `dbgraph serve`, AI agents can call these tools:
|
|
215
224
|
|
|
216
|
-
|
|
|
217
|
-
|
|
218
|
-
| `dbgraph_search` |
|
|
219
|
-
| `dbgraph_context` |
|
|
220
|
-
| `dbgraph_trace` |
|
|
221
|
-
| `dbgraph_explore` |
|
|
222
|
-
| `dbgraph_sources` |
|
|
223
|
-
| `dbgraph_status` |
|
|
225
|
+
| Tool | Purpose | When to use |
|
|
226
|
+
|------|---------|-------------|
|
|
227
|
+
| `dbgraph_search` | Search tables/columns/views/indexes | When unsure about names |
|
|
228
|
+
| `dbgraph_context` | **Primary entry** — full table schema + columns + FKs + indexes | Before writing SQL |
|
|
229
|
+
| `dbgraph_trace` | Trace FK join paths (orders→users) | Before writing JOINs |
|
|
230
|
+
| `dbgraph_explore` | Explore multiple related tables at once | Complex multi-table queries |
|
|
231
|
+
| `dbgraph_sources` | List all configured database sources | Learn what databases are available |
|
|
232
|
+
| `dbgraph_status` | Knowledge graph statistics | Verify the graph is healthy |
|
|
224
233
|
|
|
225
|
-
###
|
|
234
|
+
### Recommended workflow
|
|
226
235
|
|
|
227
236
|
```
|
|
228
|
-
|
|
229
|
-
1. dbgraph_search("order") →
|
|
230
|
-
2. dbgraph_context("public.orders") →
|
|
231
|
-
3. dbgraph_context("public.users") →
|
|
232
|
-
4. dbgraph_trace("orders", "users") →
|
|
233
|
-
5. LLM
|
|
237
|
+
Standard pre-SQL flow:
|
|
238
|
+
1. dbgraph_search("order") → find relevant tables
|
|
239
|
+
2. dbgraph_context("public.orders") → get full schema
|
|
240
|
+
3. dbgraph_context("public.users") → get related table schema
|
|
241
|
+
4. dbgraph_trace("orders", "users") → verify FK paths
|
|
242
|
+
5. LLM writes precise SQL
|
|
234
243
|
```
|
|
235
244
|
|
|
236
|
-
##
|
|
245
|
+
## Supported Engines
|
|
237
246
|
|
|
238
|
-
|
|
|
239
|
-
|
|
240
|
-
| PostgreSQL | ✅
|
|
241
|
-
| MySQL / MariaDB | ✅
|
|
242
|
-
| SQLite | ✅
|
|
243
|
-
| SQL Server | 🔜
|
|
244
|
-
| Oracle | 🔜
|
|
245
|
-
| MongoDB | 🔜
|
|
247
|
+
| Engine | Status | Details |
|
|
248
|
+
|--------|--------|---------|
|
|
249
|
+
| PostgreSQL | ✅ Full | `information_schema` + `pg_catalog` |
|
|
250
|
+
| MySQL / MariaDB | ✅ Full | `information_schema` |
|
|
251
|
+
| SQLite | ✅ Full | `pragma table_info` / `foreign_key_list` |
|
|
252
|
+
| SQL Server | 🔜 Planned | |
|
|
253
|
+
| Oracle | 🔜 Planned | |
|
|
254
|
+
| MongoDB | 🔜 Planned | |
|
|
246
255
|
|
|
247
|
-
##
|
|
256
|
+
## Project Structure
|
|
248
257
|
|
|
249
258
|
```
|
|
250
259
|
dbgraph/
|
|
251
260
|
├── src/
|
|
252
|
-
│ ├── index.ts # DBGraph
|
|
253
|
-
│ ├── types.ts #
|
|
254
|
-
│ ├── config.ts # dbgraph-db.json
|
|
255
|
-
│ ├── directory.ts # .dbgraph
|
|
256
|
-
│ ├── errors.ts #
|
|
257
|
-
│ ├── utils.ts #
|
|
261
|
+
│ ├── index.ts # DBGraph main class
|
|
262
|
+
│ ├── types.ts # All types (Node, Edge, TableSchema...)
|
|
263
|
+
│ ├── config.ts # dbgraph-db.json config management
|
|
264
|
+
│ ├── directory.ts # .dbgraph directory management
|
|
265
|
+
│ ├── errors.ts # Error types
|
|
266
|
+
│ ├── utils.ts # Utility functions
|
|
258
267
|
│ │
|
|
259
|
-
│ ├── db/ # SQLite
|
|
260
|
-
│ │ ├── schema.sql #
|
|
261
|
-
│ │ ├── sqlite-adapter.ts # node:sqlite
|
|
262
|
-
│ │ ├── migrations.ts #
|
|
263
|
-
│ │ ├── queries.ts # CRUD + FTS5
|
|
264
|
-
│ │ └── index.ts #
|
|
268
|
+
│ ├── db/ # SQLite storage layer
|
|
269
|
+
│ │ ├── schema.sql # Table schema (nodes/edges FTS5)
|
|
270
|
+
│ │ ├── sqlite-adapter.ts # node:sqlite adapter
|
|
271
|
+
│ │ ├── migrations.ts # Version migrations
|
|
272
|
+
│ │ ├── queries.ts # CRUD + FTS5 search + scoring (LRU cache)
|
|
273
|
+
│ │ └── index.ts # Connection management
|
|
265
274
|
│ │
|
|
266
275
|
│ ├── graph/
|
|
267
|
-
│ │ └── traversal.ts # BFS/DFS
|
|
276
|
+
│ │ └── traversal.ts # BFS/DFS/pathfinding/impact radius
|
|
268
277
|
│ │
|
|
269
278
|
│ ├── context/
|
|
270
|
-
│ │ ├── index.ts #
|
|
271
|
-
│ │ └── formatter.ts # Markdown
|
|
279
|
+
│ │ ├── index.ts # Table context assembly
|
|
280
|
+
│ │ └── formatter.ts # Markdown output
|
|
272
281
|
│ │
|
|
273
|
-
│ ├── introspect/ #
|
|
274
|
-
│ │ ├── base.ts #
|
|
275
|
-
│ │ ├── index.ts #
|
|
276
|
-
│ │ ├── connection.ts #
|
|
282
|
+
│ ├── introspect/ # Database introspection
|
|
283
|
+
│ │ ├── base.ts # Base class + Node/Edge factory
|
|
284
|
+
│ │ ├── index.ts # Factory method
|
|
285
|
+
│ │ ├── connection.ts # Connection management
|
|
277
286
|
│ │ ├── postgres.ts # PostgreSQL
|
|
278
287
|
│ │ ├── mysql.ts # MySQL
|
|
279
288
|
│ │ └── sqlite.ts # SQLite
|
|
280
289
|
│ │
|
|
281
|
-
│ ├── mcp/ # MCP
|
|
282
|
-
│ │ ├── transport.ts # JSON-RPC
|
|
283
|
-
│ │ ├── session.ts #
|
|
284
|
-
│ │ ├── engine.ts #
|
|
285
|
-
│ │ ├── tools.ts # 6
|
|
286
|
-
│ │ ├── server-instructions.ts # LLM
|
|
290
|
+
│ ├── mcp/ # MCP server
|
|
291
|
+
│ │ ├── transport.ts # JSON-RPC transport
|
|
292
|
+
│ │ ├── session.ts # Session management
|
|
293
|
+
│ │ ├── engine.ts # Engine + lifecycle
|
|
294
|
+
│ │ ├── tools.ts # 6 dbgraph_* tools
|
|
295
|
+
│ │ ├── server-instructions.ts # LLM instructions
|
|
287
296
|
│ │ └── index.ts # MCPServer
|
|
288
297
|
│ │
|
|
289
298
|
│ └── bin/
|
|
290
299
|
│ └── dbgraph.ts # CLI
|
|
291
300
|
```
|
|
292
301
|
|
|
293
|
-
##
|
|
302
|
+
## Development
|
|
294
303
|
|
|
295
304
|
```bash
|
|
296
|
-
#
|
|
305
|
+
# Install dependencies
|
|
297
306
|
npm install
|
|
298
307
|
|
|
299
|
-
#
|
|
308
|
+
# Build
|
|
300
309
|
npm run build
|
|
301
310
|
|
|
302
|
-
#
|
|
311
|
+
# Development mode (watch)
|
|
303
312
|
npm run dev
|
|
304
313
|
|
|
305
|
-
#
|
|
306
|
-
node dist/bin/dbgraph.js --help
|
|
307
|
-
|
|
308
|
-
# 快速构建 + 运行(等价于 build 后执行 node dist/bin/dbgraph.js)
|
|
314
|
+
# Quick build + run
|
|
309
315
|
npm run cli -- status ./my-project
|
|
310
316
|
```
|
|
311
317
|
|
|
312
|
-
###
|
|
318
|
+
### Adding a new database engine
|
|
313
319
|
|
|
314
|
-
|
|
320
|
+
Create a new file in `src/introspect/` (e.g. `mssql.ts`) implementing `BaseIntrospector`:
|
|
315
321
|
|
|
316
322
|
```typescript
|
|
317
323
|
import { BaseIntrospector } from './base';
|
|
318
324
|
|
|
319
325
|
export class MSSQLIntrospector extends BaseIntrospector {
|
|
320
326
|
async extractAll(): Promise<IntrospectResult> {
|
|
321
|
-
// 1.
|
|
322
|
-
// 2.
|
|
323
|
-
// 3.
|
|
324
|
-
// 4.
|
|
327
|
+
// 1. Connect to database
|
|
328
|
+
// 2. Query information_schema
|
|
329
|
+
// 3. Call this.makeNode() / this.makeEdge()
|
|
330
|
+
// 4. Return IntrospectResult
|
|
325
331
|
}
|
|
326
332
|
}
|
|
327
333
|
```
|
|
328
334
|
|
|
329
|
-
|
|
335
|
+
Then register it in `src/introspect/index.ts`:
|
|
330
336
|
|
|
331
337
|
```typescript
|
|
332
338
|
case 'mssql':
|
|
333
339
|
return new MSSQLIntrospector(config);
|
|
334
340
|
```
|
|
335
341
|
|
|
336
|
-
##
|
|
342
|
+
## References
|
|
337
343
|
|
|
338
|
-
- **[AGENTS.md](AGENTS.md)** — OpenCode AI Agent
|
|
339
|
-
- **CodeGraph** —
|
|
344
|
+
- **[AGENTS.md](AGENTS.md)** — OpenCode AI Agent project guide with dev commands and architecture overview
|
|
345
|
+
- **CodeGraph** — This project uses CodeGraph indexing (`.codegraph/`) for fast structural queries
|
|
340
346
|
|
|
341
|
-
##
|
|
347
|
+
## Acknowledgments
|
|
342
348
|
|
|
343
|
-
DBGraph
|
|
349
|
+
DBGraph\'s architecture and MCP design were inspired by [CodeGraph](https://github.com/colbymchenry/codegraph), an excellent code knowledge graph tool that extracts codebase structure into a queryable graph. This project adapts those ideas to the database schema domain.
|
|
344
350
|
|
|
345
351
|
## License
|
|
346
352
|
|
|
347
|
-
MIT
|
|
353
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dbgraph",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Database knowledge graph for LLM-powered SQL generation. Introspect database schemas into a local-first knowledge graph, exposed over MCP.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|