dbgraph 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.
Files changed (116) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +347 -0
  3. package/dist/bin/dbgraph.d.ts +8 -0
  4. package/dist/bin/dbgraph.d.ts.map +1 -0
  5. package/dist/bin/dbgraph.js +382 -0
  6. package/dist/bin/dbgraph.js.map +1 -0
  7. package/dist/config.d.ts +25 -0
  8. package/dist/config.d.ts.map +1 -0
  9. package/dist/config.js +158 -0
  10. package/dist/config.js.map +1 -0
  11. package/dist/context/formatter.d.ts +94 -0
  12. package/dist/context/formatter.d.ts.map +1 -0
  13. package/dist/context/formatter.js +288 -0
  14. package/dist/context/formatter.js.map +1 -0
  15. package/dist/context/index.d.ts +77 -0
  16. package/dist/context/index.d.ts.map +1 -0
  17. package/dist/context/index.js +458 -0
  18. package/dist/context/index.js.map +1 -0
  19. package/dist/db/index.d.ts +26 -0
  20. package/dist/db/index.d.ts.map +1 -0
  21. package/dist/db/index.js +127 -0
  22. package/dist/db/index.js.map +1 -0
  23. package/dist/db/migrations.d.ts +8 -0
  24. package/dist/db/migrations.d.ts.map +1 -0
  25. package/dist/db/migrations.js +39 -0
  26. package/dist/db/migrations.js.map +1 -0
  27. package/dist/db/queries.d.ts +46 -0
  28. package/dist/db/queries.d.ts.map +1 -0
  29. package/dist/db/queries.js +436 -0
  30. package/dist/db/queries.js.map +1 -0
  31. package/dist/db/schema.sql +113 -0
  32. package/dist/db/sqlite-adapter.d.ts +30 -0
  33. package/dist/db/sqlite-adapter.d.ts.map +1 -0
  34. package/dist/db/sqlite-adapter.js +78 -0
  35. package/dist/db/sqlite-adapter.js.map +1 -0
  36. package/dist/directory.d.ts +37 -0
  37. package/dist/directory.d.ts.map +1 -0
  38. package/dist/directory.js +160 -0
  39. package/dist/directory.js.map +1 -0
  40. package/dist/errors.d.ts +46 -0
  41. package/dist/errors.d.ts.map +1 -0
  42. package/dist/errors.js +90 -0
  43. package/dist/errors.js.map +1 -0
  44. package/dist/graph/traversal.d.ts +157 -0
  45. package/dist/graph/traversal.d.ts.map +1 -0
  46. package/dist/graph/traversal.js +531 -0
  47. package/dist/graph/traversal.js.map +1 -0
  48. package/dist/index.d.ts +183 -0
  49. package/dist/index.d.ts.map +1 -0
  50. package/dist/index.js +435 -0
  51. package/dist/index.js.map +1 -0
  52. package/dist/introspect/base.d.ts +62 -0
  53. package/dist/introspect/base.d.ts.map +1 -0
  54. package/dist/introspect/base.js +107 -0
  55. package/dist/introspect/base.js.map +1 -0
  56. package/dist/introspect/connection.d.ts +30 -0
  57. package/dist/introspect/connection.d.ts.map +1 -0
  58. package/dist/introspect/connection.js +232 -0
  59. package/dist/introspect/connection.js.map +1 -0
  60. package/dist/introspect/index.d.ts +23 -0
  61. package/dist/introspect/index.d.ts.map +1 -0
  62. package/dist/introspect/index.js +46 -0
  63. package/dist/introspect/index.js.map +1 -0
  64. package/dist/introspect/mysql.d.ts +64 -0
  65. package/dist/introspect/mysql.d.ts.map +1 -0
  66. package/dist/introspect/mysql.js +360 -0
  67. package/dist/introspect/mysql.js.map +1 -0
  68. package/dist/introspect/postgres.d.ts +55 -0
  69. package/dist/introspect/postgres.d.ts.map +1 -0
  70. package/dist/introspect/postgres.js +372 -0
  71. package/dist/introspect/postgres.js.map +1 -0
  72. package/dist/introspect/sqlite.d.ts +33 -0
  73. package/dist/introspect/sqlite.d.ts.map +1 -0
  74. package/dist/introspect/sqlite.js +207 -0
  75. package/dist/introspect/sqlite.js.map +1 -0
  76. package/dist/mcp/engine.d.ts +92 -0
  77. package/dist/mcp/engine.d.ts.map +1 -0
  78. package/dist/mcp/engine.js +261 -0
  79. package/dist/mcp/engine.js.map +1 -0
  80. package/dist/mcp/index.d.ts +33 -0
  81. package/dist/mcp/index.d.ts.map +1 -0
  82. package/dist/mcp/index.js +119 -0
  83. package/dist/mcp/index.js.map +1 -0
  84. package/dist/mcp/server-instructions.d.ts +9 -0
  85. package/dist/mcp/server-instructions.d.ts.map +1 -0
  86. package/dist/mcp/server-instructions.js +71 -0
  87. package/dist/mcp/server-instructions.js.map +1 -0
  88. package/dist/mcp/session.d.ts +35 -0
  89. package/dist/mcp/session.d.ts.map +1 -0
  90. package/dist/mcp/session.js +140 -0
  91. package/dist/mcp/session.js.map +1 -0
  92. package/dist/mcp/tools.d.ts +99 -0
  93. package/dist/mcp/tools.d.ts.map +1 -0
  94. package/dist/mcp/tools.js +499 -0
  95. package/dist/mcp/tools.js.map +1 -0
  96. package/dist/mcp/transport.d.ts +78 -0
  97. package/dist/mcp/transport.d.ts.map +1 -0
  98. package/dist/mcp/transport.js +182 -0
  99. package/dist/mcp/transport.js.map +1 -0
  100. package/dist/search/query-parser.d.ts +66 -0
  101. package/dist/search/query-parser.d.ts.map +1 -0
  102. package/dist/search/query-parser.js +163 -0
  103. package/dist/search/query-parser.js.map +1 -0
  104. package/dist/search/query-utils.d.ts +78 -0
  105. package/dist/search/query-utils.d.ts.map +1 -0
  106. package/dist/search/query-utils.js +203 -0
  107. package/dist/search/query-utils.js.map +1 -0
  108. package/dist/types.d.ts +279 -0
  109. package/dist/types.d.ts.map +1 -0
  110. package/dist/types.js +47 -0
  111. package/dist/types.js.map +1 -0
  112. package/dist/utils.d.ts +40 -0
  113. package/dist/utils.d.ts.map +1 -0
  114. package/dist/utils.js +190 -0
  115. package/dist/utils.js.map +1 -0
  116. package/package.json +54 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 kierankih
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,347 @@
1
+ # DBGraph
2
+
3
+ [![npm version](https://img.shields.io/npm/v/dbgraph)](https://www.npmjs.com/package/dbgraph)
4
+ [![License](https://img.shields.io/npm/l/dbgraph)](LICENSE)
5
+ [![Node](https://img.shields.io/node/v/dbgraph)](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
+ > **快捷方式**: 所有命令也可用 `npm run cli -- <命令>` 替代 `node dist/bin/dbgraph.js <命令>`,会自动先执行构建。
39
+
40
+ ### 1. 初始化项目
41
+
42
+ ```bash
43
+ # 初始化一个项目
44
+ dbgraph init ./demo-project
45
+ ```
46
+
47
+ 这会创建:
48
+ - `demo-project/.dbgraph/` —— 知识图谱数据目录
49
+ - `demo-project/dbgraph-db.json` —— 数据库连接配置(默认模板)
50
+
51
+ ### 2. 配置数据库连接
52
+
53
+ 编辑 `demo-project/dbgraph-db.json`,填入你的数据库信息:
54
+
55
+ ```json
56
+ {
57
+ "databases": [
58
+ {
59
+ "alias": "prod",
60
+ "engine": "postgresql",
61
+ "host": "localhost",
62
+ "port": 5432,
63
+ "database": "ecommerce",
64
+ "schemas": ["public"],
65
+ "auth": "env:DB_PASSWORD"
66
+ },
67
+ {
68
+ "alias": "local",
69
+ "engine": "sqlite",
70
+ "path": "./dev.db"
71
+ }
72
+ ]
73
+ }
74
+ ```
75
+
76
+ ### 3. 提取 Schema
77
+
78
+ ```bash
79
+ dbgraph index ./demo-project
80
+ ```
81
+
82
+ 把数据库 schema 提取到知识图谱中。首次运行会连接所有配置的数据库,提取表/列/外键/索引/视图等信息。
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
+ ## 配置说明
102
+
103
+ ### `dbgraph-db.json`
104
+
105
+ | 字段 | 类型 | 必填 | 说明 |
106
+ |------|------|------|------|
107
+ | `alias` | string | 是 | 数据库别名,在知识图谱中用 `db://@alias` 标识 |
108
+ | `engine` | string | 是 | 数据库引擎:`postgresql` / `mysql` / `mariadb` / `sqlite` |
109
+ | `host` | string | 否 | 主机地址(SQLite 不需要)|
110
+ | `port` | number | 否 | 端口(默认根据引擎判断)|
111
+ | `database` | string | 是(非SQLite) | 数据库名 |
112
+ | `schemas` | string[] | 否 | 要提取的 schema 列表(默认全部)|
113
+ | `path` | string | 是(SQLite) | SQLite 文件路径 |
114
+ | `auth` | string | 否 | 认证方式,如 `env:DB_PASSWORD`(环境变量)、`~/.pgpass` |
115
+ | `ssl` | boolean | 否 | 是否启用 SSL 连接 |
116
+
117
+ ## CLI 命令参考
118
+
119
+ 所有命令格式:
120
+ ```
121
+ node dist/bin/dbgraph.js <命令> [选项] [目录参数]
122
+ ```
123
+
124
+ | 命令 | 说明 |
125
+ |------|------|
126
+ | `init` | 初始化 .dbgraph 项目 + 配置 |
127
+ | `index` | 运行数据库内省,提取 schema |
128
+ | `serve` | 启动 MCP 服务器(供 AI Agent 使用)|
129
+ | `query` | 搜索表/列/视图 |
130
+ | `context` | 查看完整表结构 |
131
+ | `status` | 知识图谱状态统计 |
132
+ | `sources` | 列出数据源 |
133
+ | `test` | 测试数据库连接 |
134
+ | `config` | 查看/创建配置 |
135
+
136
+ ### `init`
137
+
138
+ ```bash
139
+ # 初始化项目
140
+ node dist/bin/dbgraph.js init ./my-project
141
+
142
+ # 初始化后立即索引
143
+ node dist/bin/dbgraph.js init ./my-project --index
144
+
145
+ # 指定配置文件路径
146
+ node dist/bin/dbgraph.js init ./my-project -c ./my-project/custom-config.json
147
+ ```
148
+
149
+ ### `index`
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_*` 工具。
167
+
168
+ ### `query`
169
+
170
+ ```bash
171
+ # 搜索
172
+ node dist/bin/dbgraph.js query orders ./my-project
173
+
174
+ # 按类型过滤
175
+ node dist/bin/dbgraph.js query orders --kind table ./my-project
176
+ node dist/bin/dbgraph.js query amount --kind column ./my-project
177
+
178
+ # JSON 格式输出
179
+ node dist/bin/dbgraph.js query orders --json ./my-project
180
+ ```
181
+
182
+ ### `context`
183
+
184
+ ```bash
185
+ # 查看表结构
186
+ node dist/bin/dbgraph.js context orders ./my-project
187
+ node dist/bin/dbgraph.js context public.orders ./my-project
188
+ ```
189
+
190
+ 输出示例:
191
+ ```
192
+ ## Table: public.orders
193
+
194
+ Database: prod (postgresql)
195
+
196
+ | Column | Type | Nullable | Default | PK | FK |
197
+ |-------------|-------------|----------|----------|----|-----------|
198
+ | id | bigint | NO | | PK | |
199
+ | user_id | integer | NO | | | users.id |
200
+ | status | varchar(20) | NO | pending | | |
201
+ | total_amount| numeric(10,2)| YES | 0.00 | | |
202
+ | created_at | timestamp | NO | now() | | |
203
+
204
+ Indexes:
205
+ - idx_orders_status on (status)
206
+ - pk_orders PRIMARY KEY using btree (id)
207
+
208
+ Foreign Keys:
209
+ - fk_orders_user → users(id) ON DELETE CASCADE
210
+ ```
211
+
212
+ ## MCP 工具
213
+
214
+ 启动 `dbgraph serve` 后,AI Agent 可以调用以下工具:
215
+
216
+ | 工具 | 用途 | 推荐时机 |
217
+ |------|------|---------|
218
+ | `dbgraph_search` | 搜索表/列/视图/索引 | 不确定名字时 |
219
+ | `dbgraph_context` | **主要入口** —— 返回完整表结构 + 列 + FK + 索引 | 写 SQL 前 |
220
+ | `dbgraph_trace` | 追踪 FK 关联路径(orders→users) | 需要写 JOIN 时 |
221
+ | `dbgraph_explore` | 一次性探索多张相关表 | 复杂查询涉及多表时 |
222
+ | `dbgraph_sources` | 列出所有已配置的数据库源 | 了解有哪些库可用 |
223
+ | `dbgraph_status` | 知识图谱统计 | 检查是否已索引 |
224
+
225
+ ### 推荐工具调用策略
226
+
227
+ ```
228
+ 写 SQL 前的标准流程:
229
+ 1. dbgraph_search("order") → 找到 order 相关表
230
+ 2. dbgraph_context("public.orders") → 获取完整 schema
231
+ 3. dbgraph_context("public.users") → 获取关联表 schema
232
+ 4. dbgraph_trace("orders", "users") → 验证 FK 关联
233
+ 5. LLM 写出精确 SQL
234
+ ```
235
+
236
+ ## 支持引擎
237
+
238
+ | 引擎 | 状态 | 说明 |
239
+ |------|------|------|
240
+ | PostgreSQL | ✅ 完整支持 | `information_schema` + `pg_catalog` |
241
+ | MySQL / MariaDB | ✅ 完整支持 | `information_schema` |
242
+ | SQLite | ✅ 完整支持 | `pragma table_info` / `foreign_key_list` |
243
+ | SQL Server | 🔜 计划中 | |
244
+ | Oracle | 🔜 计划中 | |
245
+ | MongoDB | 🔜 计划中 | |
246
+
247
+ ## 项目结构
248
+
249
+ ```
250
+ dbgraph/
251
+ ├── src/
252
+ │ ├── index.ts # DBGraph 主类
253
+ │ ├── types.ts # 所有类型 (Node, Edge, TableSchema...)
254
+ │ ├── config.ts # dbgraph-db.json 配置管理
255
+ │ ├── directory.ts # .dbgraph 目录管理
256
+ │ ├── errors.ts # 错误类型
257
+ │ ├── utils.ts # 工具函数
258
+ │ │
259
+ │ ├── db/ # SQLite 存储层
260
+ │ │ ├── schema.sql # 表结构 (nodes/edges FTS5)
261
+ │ │ ├── sqlite-adapter.ts # node:sqlite 适配
262
+ │ │ ├── migrations.ts # 版本迁移
263
+ │ │ ├── queries.ts # CRUD + FTS5 搜索 + 评分 (LRU缓存)
264
+ │ │ └── index.ts # 连接管理
265
+ │ │
266
+ │ ├── graph/
267
+ │ │ └── traversal.ts # BFS/DFS/寻路/影响半径
268
+ │ │
269
+ │ ├── context/
270
+ │ │ ├── index.ts # 表结构组装
271
+ │ │ └── formatter.ts # Markdown 输出
272
+ │ │
273
+ │ ├── introspect/ # 数据库内省
274
+ │ │ ├── base.ts # 基类 + Node/Edge 工厂
275
+ │ │ ├── index.ts # 工厂方法
276
+ │ │ ├── connection.ts # 连接管理
277
+ │ │ ├── postgres.ts # PostgreSQL
278
+ │ │ ├── mysql.ts # MySQL
279
+ │ │ └── sqlite.ts # SQLite
280
+ │ │
281
+ │ ├── mcp/ # MCP 服务器
282
+ │ │ ├── transport.ts # JSON-RPC 传输
283
+ │ │ ├── session.ts # 会话管理
284
+ │ │ ├── engine.ts # 引擎 + 生命周期
285
+ │ │ ├── tools.ts # 6 个 dbgraph_* 工具
286
+ │ │ ├── server-instructions.ts # LLM 指引
287
+ │ │ └── index.ts # MCPServer
288
+ │ │
289
+ │ └── bin/
290
+ │ └── dbgraph.ts # CLI
291
+ ```
292
+
293
+ ## 开发
294
+
295
+ ```bash
296
+ # 安装依赖
297
+ npm install
298
+
299
+ # 构建
300
+ npm run build
301
+
302
+ # 开发模式(watch)
303
+ npm run dev
304
+
305
+ # 运行帮助
306
+ node dist/bin/dbgraph.js --help
307
+
308
+ # 快速构建 + 运行(等价于 build 后执行 node dist/bin/dbgraph.js)
309
+ npm run cli -- status ./my-project
310
+ ```
311
+
312
+ ### 添加新数据库引擎
313
+
314
+ 在 `src/introspect/` 下创建新文件(如 `mssql.ts`),实现 `BaseIntrospector` 抽象类:
315
+
316
+ ```typescript
317
+ import { BaseIntrospector } from './base';
318
+
319
+ export class MSSQLIntrospector extends BaseIntrospector {
320
+ async extractAll(): Promise<IntrospectResult> {
321
+ // 1. 连接数据库
322
+ // 2. 查询 information_schema
323
+ // 3. 调用 this.makeNode() / this.makeEdge()
324
+ // 4. 返回 IntrospectResult
325
+ }
326
+ }
327
+ ```
328
+
329
+ 然后在 `src/introspect/index.ts` 中注册:
330
+
331
+ ```typescript
332
+ case 'mssql':
333
+ return new MSSQLIntrospector(config);
334
+ ```
335
+
336
+ ## 参考
337
+
338
+ - **[AGENTS.md](AGENTS.md)** — OpenCode AI Agent 的项目指引,包含开发命令速查、架构要点和 CodeGraph 使用说明。
339
+ - **CodeGraph** — 本项目已初始化 CodeGraph 索引(`.codegraph/`),Agent 可优先使用 `codegraph_*` 工具进行结构查询,速度远超 grep。
340
+
341
+ ## 鸣谢
342
+
343
+ DBGraph 的架构和 MCP 设计受到了 [CodeGraph](https://github.com/colbymchenry/codegraph) 的启发。CodeGraph 是一个优秀的代码知识图谱工具,将代码库结构提取为可查询的知识图谱,本项目借鉴了其思路并将其应用于数据库 schema 领域。
344
+
345
+ ## License
346
+
347
+ MIT
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * DBGraph CLI
4
+ *
5
+ * Command-line interface for the database knowledge graph.
6
+ */
7
+ export {};
8
+ //# sourceMappingURL=dbgraph.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dbgraph.d.ts","sourceRoot":"","sources":["../../src/bin/dbgraph.ts"],"names":[],"mappings":";AAEA;;;;GAIG"}