feat: 添加Neo4j图数据库支持及前端代码重构
- 新增 Neo4j 图数据库 handler、service、model - 后端添加 SaveGraph API 接口 - 前端 Database.vue 重构,拆分为独立组件 - 新增 web/src/views/database/ 组件目录 - 删除临时文件 (temp_*.go) - 添加 Neo4j 相关 API 需求文档 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
110
team-require/web/neo4j-api-requirement.md
Normal file
110
team-require/web/neo4j-api-requirement.md
Normal file
@@ -0,0 +1,110 @@
|
||||
# Neo4j 接口后端需求
|
||||
|
||||
## 需求说明
|
||||
|
||||
前端 Neo4j 图谱功能已完成,后端接口需要匹配前端调用。
|
||||
|
||||
---
|
||||
|
||||
## 1. 新增 `/neo4j/graphs` 接口
|
||||
|
||||
### 接口地址
|
||||
```
|
||||
POST /neo4j/graphs
|
||||
```
|
||||
|
||||
### 请求参数
|
||||
```json
|
||||
{
|
||||
"uri": "bolt://localhost:7687",
|
||||
"username": "neo4j",
|
||||
"password": "password",
|
||||
"database": "neo4j"
|
||||
}
|
||||
```
|
||||
|
||||
| 参数 | 类型 | 必填 | 说明 |
|
||||
|------|------|------|------|
|
||||
| uri | string | 是 | Neo4j 连接地址,如 bolt://localhost:7687 |
|
||||
| username | string | 是 | 用户名 |
|
||||
| password | string | 是 | 密码 |
|
||||
| database | string | 否 | 数据库名(默认 neo4j) |
|
||||
|
||||
### 返回参数
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"graphs": {
|
||||
"labels": [
|
||||
{"name": "User", "count": 1523},
|
||||
{"name": "Order", "count": 856}
|
||||
],
|
||||
"relationshipTypes": [
|
||||
{"name": "KNOWS", "count": 2341},
|
||||
{"name": "BOUGHT", "count": 5678}
|
||||
]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 2. 修改路由路径
|
||||
|
||||
### 当前状态
|
||||
- `/database/neo4j/nodes` → 需要改为 → `/neo4j/nodes`
|
||||
- `/database/neo4j/relationships` → 需要改为 → `/neo4j/relationships`
|
||||
|
||||
---
|
||||
|
||||
## 总结
|
||||
|
||||
后端需要修改以下内容:
|
||||
|
||||
1. **新增** `/neo4j/graphs` 接口
|
||||
2. **修改** `/database/neo4j/nodes` → `/neo4j/nodes`
|
||||
3. **修改** `/database/neo4j/relationships` → `/neo4j/relationships`
|
||||
|
||||
---
|
||||
|
||||
## 附:前端 API 调用示例
|
||||
|
||||
```javascript
|
||||
// 获取图谱概览
|
||||
fetch('/neo4j/graphs', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
uri: 'bolt://10.10.10.189:7687',
|
||||
username: 'neo4j',
|
||||
password: 'neo4j',
|
||||
database: 'neo4j'
|
||||
})
|
||||
})
|
||||
|
||||
// 获取节点详情
|
||||
fetch('/neo4j/nodes', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
uri: 'bolt://10.10.10.189:7687',
|
||||
username: 'neo4j',
|
||||
password: 'neo4j',
|
||||
label: 'User',
|
||||
limit: 10
|
||||
})
|
||||
})
|
||||
|
||||
// 获取关系详情
|
||||
fetch('/neo4j/relationships', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
uri: 'bolt://10.10.10.189:7687',
|
||||
username: 'neo4j',
|
||||
password: 'neo4j',
|
||||
relationship_type: 'KNOWS',
|
||||
limit: 10
|
||||
})
|
||||
})
|
||||
```
|
||||
99
team-require/web/neo4j-check-return-id.md
Normal file
99
team-require/web/neo4j-check-return-id.md
Normal file
@@ -0,0 +1,99 @@
|
||||
# Neo4j 连接成功后返回数据库 ID
|
||||
|
||||
## 需求说明
|
||||
|
||||
当前端 Connect 测试 Neo4j 连接成功后,后端需要返回数据库的 ID,以便前端保存图谱配置。
|
||||
|
||||
---
|
||||
|
||||
## 问题
|
||||
|
||||
当前 `/neo4j/check` 接口返回:
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"message": "connection successful",
|
||||
"version": "5.14.0",
|
||||
"databases": ["neo4j", "system"]
|
||||
}
|
||||
```
|
||||
|
||||
**没有返回 `databaseId`**,导致后续保存图谱时缺少 `databaseId`。
|
||||
|
||||
---
|
||||
|
||||
## 需求内容
|
||||
|
||||
修改 `/neo4j/check` 接口,在连接成功时:
|
||||
|
||||
1. **检查数据库是否已存在** - 根据 URI(bolt://host:port)、username、database 查询
|
||||
2. **如果存在** - 返回已有的 `databaseId`
|
||||
3. **如果不存在** - 自动创建一条数据库记录,并返回新的 `databaseId`
|
||||
|
||||
### 返回格式
|
||||
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"message": "connection successful",
|
||||
"version": "5.14.0",
|
||||
"databases": ["neo4j", "system"],
|
||||
"databaseId": "xxx-xxx-xxx",
|
||||
"name": "Neo4j-neo4j",
|
||||
"description": "Neo4j neo4j@10.10.10.189:7687"
|
||||
}
|
||||
```
|
||||
|
||||
| 字段 | 类型 | 说明 |
|
||||
|------|------|------|
|
||||
| success | bool | 是否成功 |
|
||||
| message | string | 消息 |
|
||||
| version | string | Neo4j 版本 |
|
||||
| databases | array | 数据库列表 |
|
||||
| databaseId | string | 数据库记录 ID |
|
||||
| name | string | 数据库名称 |
|
||||
| description | string | 数据库描述 |
|
||||
|
||||
### 请求参数
|
||||
|
||||
当前 `/neo4j/check` 请求:
|
||||
```json
|
||||
{
|
||||
"db_type": "Neo4j",
|
||||
"host": "10.10.10.189",
|
||||
"port": 7687,
|
||||
"username": "neo4j",
|
||||
"password": "neo4j",
|
||||
"database": "neo4j"
|
||||
}
|
||||
```
|
||||
|
||||
后端需要增加 `name` 字段用于数据库名称(可选):
|
||||
```json
|
||||
{
|
||||
"name": "My Neo4j Database"
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 涉及文件
|
||||
|
||||
- `server/internal/service/neo4j_service.go`
|
||||
- 函数:`Check()` - 第 81-128 行
|
||||
- 函数:`ensureNeo4jDatabase()` - 第 131-175 行(已有代码但可能有问题)
|
||||
|
||||
- `server/internal/model/neo4j_info.go`
|
||||
- 结构体:`Neo4jCheckResponse` - 需要确保 `databaseId` 字段正确返回
|
||||
|
||||
---
|
||||
|
||||
## 前端使用
|
||||
|
||||
前端代码已实现兼容处理:
|
||||
```javascript
|
||||
const dbId = result.databaseId || result.id || result.database_id || ''
|
||||
```
|
||||
|
||||
所以后端返回 `databaseId`、`id` 或 `database_id` 都可以被正确识别。
|
||||
|
||||
196
team-require/web/neo4j-graphs.md
Normal file
196
team-require/web/neo4j-graphs.md
Normal file
@@ -0,0 +1,196 @@
|
||||
# 后端需求 - Neo4j 图谱数据获取(完善版)
|
||||
|
||||
## 需求描述
|
||||
|
||||
Neo4j 连接成功后,需要获取图谱数据供前端可视化展示。前端使用 ECharts 力导向图谱展示科幻风格效果。
|
||||
|
||||
## Neo4j 图谱核心概念
|
||||
|
||||
Neo4j 是图数据库,与关系型数据库概念不同:
|
||||
- **Node(节点)** - 类似于表,但不需要固定结构
|
||||
- **Label(标签)** - 类似于表的类型名(如 User, Order)
|
||||
- **Relationship(关系)** - 节点之间的边
|
||||
- **Relationship Type(关系类型)** - 关系的类型(如 KNOWS, OWNS)
|
||||
|
||||
## 后端需要提供的接口
|
||||
|
||||
### 1. 获取图谱概览数据(核心接口)
|
||||
|
||||
返回所有 Label(标签)和 Relationship Type(关系类型)的统计信息。这是前端图谱可视化的核心数据来源。
|
||||
|
||||
**接口地址:** `POST /database/check` (复用现有接口,在 db_type 为 Neo4j 时返回图谱数据)
|
||||
|
||||
**请求参数:**
|
||||
```json
|
||||
{
|
||||
"db_type": "Neo4j",
|
||||
"uri": "bolt://localhost:7687",
|
||||
"username": "neo4j",
|
||||
"password": "password",
|
||||
"database": "neo4j"
|
||||
}
|
||||
```
|
||||
|
||||
**返回参数:**
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"graphs": {
|
||||
"labels": [
|
||||
{"name": "User", "count": 1523},
|
||||
{"name": "Order", "count": 856},
|
||||
{"name": "Product", "count": 2341},
|
||||
{"name": "Category", "count": 45},
|
||||
{"name": "Review", "count": 5678}
|
||||
],
|
||||
"relationshipTypes": [
|
||||
{"name": "KNOWS", "count": 2341},
|
||||
{"name": "BOUGHT", "count": 5678},
|
||||
{"name": "BELONGS_TO", "count": 2341},
|
||||
{"name": "HAS_REVIEW", "count": 5678},
|
||||
{"name": "LOCATED_IN", "count": 1523}
|
||||
]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 2. 获取节点详情(可选,用于点击显示)
|
||||
|
||||
点击某个 Label 节点时,获取该类型节点的样本数据用于详情展示。
|
||||
|
||||
**接口地址:** `POST /database/neo4j/nodes`
|
||||
|
||||
**请求参数:**
|
||||
```json
|
||||
{
|
||||
"uri": "bolt://localhost:7687",
|
||||
"username": "neo4j",
|
||||
"password": "password",
|
||||
"database": "neo4j",
|
||||
"label": "User",
|
||||
"limit": 5
|
||||
}
|
||||
```
|
||||
|
||||
**返回参数:**
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"nodes": [
|
||||
{"id": "1", "name": "张三", "email": "zhangsan@example.com", "created_at": "2024-01-01"},
|
||||
{"id": "2", "name": "李四", "email": "lisi@example.com", "created_at": "2024-01-02"}
|
||||
],
|
||||
"properties": [
|
||||
{"name": "id", "type": "string"},
|
||||
{"name": "name", "type": "string"},
|
||||
{"name": "email", "type": "string"},
|
||||
{"name": "created_at", "type": "datetime"}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
### 3. 获取关系详情(可选)
|
||||
|
||||
获取两个节点之间的关系数据。
|
||||
|
||||
**接口地址:** `POST /database/neo4j/relationships`
|
||||
|
||||
**请求参数:**
|
||||
```json
|
||||
{
|
||||
"uri": "bolt://localhost:7687",
|
||||
"username": "neo4j",
|
||||
"password": "password",
|
||||
"database": "neo4j",
|
||||
"relationshipType": "KNOWS",
|
||||
"limit": 10
|
||||
}
|
||||
```
|
||||
|
||||
**返回参数:**
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"relationships": [
|
||||
{
|
||||
"id": "rel-1",
|
||||
"source": "1",
|
||||
"target": "2",
|
||||
"properties": {"since": "2020-01-01"}
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
## 数据结构说明
|
||||
|
||||
### graphs.labels[] - 标签列表(前端图谱节点)
|
||||
| 字段 | 类型 | 说明 | 用途 |
|
||||
|------|------|------|------|
|
||||
| name | string | 标签名称(如 User, Order) | 作为图谱节点显示 |
|
||||
| count | int | 该标签的节点数量 | 计算节点大小 symbolSize |
|
||||
|
||||
### graphs.relationshipTypes[] - 关系类型列表(前端图谱边)
|
||||
| 字段 | 类型 | 说明 | 用途 |
|
||||
|------|------|------|------|
|
||||
| name | string | 关系类型(如 KNOWS, OWNS) | 作为图谱边的标签 |
|
||||
| count | int | 该关系的数量 | 可能影响边的粗细 |
|
||||
|
||||
### nodes[] - 节点详情
|
||||
| 字段 | 类型 | 说明 |
|
||||
|------|------|------|
|
||||
| id | string | 节点唯一标识 |
|
||||
| (其他) | any | 节点的其他属性 |
|
||||
|
||||
### relationships[] - 关系详情
|
||||
| 字段 | 类型 | 说明 |
|
||||
|------|------|------|
|
||||
| id | string | 关系唯一标识 |
|
||||
| source | string | 起始节点ID |
|
||||
| target | string | 目标节点ID |
|
||||
| properties | object | 关系属性 |
|
||||
|
||||
## 前端图谱展示逻辑
|
||||
|
||||
前端使用 ECharts 力导向图谱(force-directed graph),展示方式如下:
|
||||
|
||||
1. **节点生成**:
|
||||
- 根据 `graphs.labels` 数组生成节点
|
||||
- `name` 作为节点显示名称
|
||||
- `count` 决定节点大小(symbolSize = log2(count+1) * 12)
|
||||
- 节点颜色按索引分配科幻配色(紫、蓝、绿、橙、粉、青)
|
||||
- 节点带发光效果(shadowBlur: 20)
|
||||
|
||||
2. **边生成**:
|
||||
- 根据 `graphs.relationshipTypes` 生成边
|
||||
- 边 label 显示关系类型名称
|
||||
- 曲线连接(curveness: 0.2)
|
||||
- 带箭头
|
||||
|
||||
3. **交互效果**:
|
||||
- 弹簧物理效果(force layout)
|
||||
- 节点可拖拽
|
||||
- 滚轮缩放
|
||||
- hover 相邻节点高亮
|
||||
|
||||
## 优先级
|
||||
|
||||
高 - Neo4j 可视化的核心数据
|
||||
|
||||
## 注意事项
|
||||
|
||||
1. Neo4j 连接使用官方 Go 驱动:`github.com/neo4j/neo4j-go-driver`
|
||||
2. 注意处理连接超时和认证失败的情况
|
||||
3. 大数据量时需要限制返回数量(limit 参数)
|
||||
4. **建议返回足够多的关系类型**(建议至少 5-10 个),以便前端生成丰富的图谱连接
|
||||
5. 如果关系类型少于节点数,可以创建额外连接让图谱更美观
|
||||
|
||||
## 前端缓存策略
|
||||
|
||||
### 方案设计
|
||||
- **首次加载**:获取数据并缓存
|
||||
- **第二次展示**:直接使用缓存,秒开
|
||||
- **刷新按钮**:用户手动点击刷新获取最新数据
|
||||
|
||||
### 实现说明
|
||||
前端会缓存图谱数据,第二次进入时直接展示缓存数据,提升用户体验。同时提供"刷新"按钮供用户手动刷新。
|
||||
42
team-require/web/neo4j-support.md
Normal file
42
team-require/web/neo4j-support.md
Normal file
@@ -0,0 +1,42 @@
|
||||
# 后端需求 - 支持 Neo4j 图数据库
|
||||
|
||||
## 需求描述
|
||||
|
||||
添加 Neo4j 图数据库类型支持。
|
||||
|
||||
## Neo4j 连接参数
|
||||
|
||||
Neo4j 连接需要以下参数:
|
||||
|
||||
| 参数 | 类型 | 必填 | 说明 | 默认值 |
|
||||
|------|------|------|------|--------|
|
||||
| uri | string | 是 | 连接地址 | bolt://localhost:7687 |
|
||||
| username | string | 是 | 用户名 | neo4j |
|
||||
| password | string | 是 | 密码 | - |
|
||||
| database | string | 否 | 数据库名 | neo4j(默认数据库) |
|
||||
|
||||
### 连接示例
|
||||
- `bolt://localhost:7687`
|
||||
- `neo4j://localhost:7687`
|
||||
- `bolt://192.168.1.100:7687`
|
||||
|
||||
## 需要修改的地方
|
||||
|
||||
### 1. 数据库类型列表
|
||||
在前端和后端添加 "Neo4j" 选项
|
||||
|
||||
### 2. 连接表单
|
||||
Neo4j 只需要 3-4 个字段:
|
||||
- URI(连接地址)
|
||||
- Username(用户名)
|
||||
- Password(密码)
|
||||
- Database(数据库名,可选)
|
||||
|
||||
### 3. 数据库服务
|
||||
- `server/internal/service/database_service.go`
|
||||
- 新增 `connectNeo4j` 方法
|
||||
- 新增 `getNeo4jTables` 方法
|
||||
|
||||
## 优先级
|
||||
|
||||
中 - 扩展数据库类型支持
|
||||
@@ -22,6 +22,31 @@
|
||||
- 相关文件:`server/internal/service/database_service.go`, `server/internal/model/`
|
||||
- 详细需求:[mapping-state.md](./mapping-state.md)
|
||||
|
||||
- [x] **Neo4j 图谱数据获取** - 前端已完成 ECharts 科幻风格图谱,后端需提供图谱数据接口 ✔
|
||||
- 前端:使用 ECharts force-directed graph,力导向弹簧效果,可拖拽,hover 高亮
|
||||
- 详细需求:[neo4j-graphs.md](./neo4j-graphs.md), [neo4j-support.md](./neo4j-support.md)
|
||||
|
||||
---
|
||||
|
||||
- [x] **Neo4j 接口路由修改** - 后端已完成 ✔
|
||||
- 新增 `/neo4j/graphs` 接口
|
||||
- 修改 `/database/neo4j/nodes` → `/neo4j/nodes`
|
||||
- 修改 `/database/neo4j/relationships` → `/neo4j/relationships`
|
||||
- 详细需求:[neo4j-api-requirement.md](./neo4j-api-requirement.md)
|
||||
|
||||
---
|
||||
|
||||
### 2026-03-07
|
||||
|
||||
- [x] **Neo4j 图谱保存接口** - 后端已完成 ✔
|
||||
- 接口地址:`POST /database/graph/save`
|
||||
- 详细需求:[neo4j-graph-save.md](./neo4j-graph-save.md)
|
||||
|
||||
- [x] **Neo4j 连接成功后返回 databaseId** - 后端已完成 ✔
|
||||
- 问题:Connect 测试连接成功后没有保存数据库记录,导致后续保存图谱时缺少 databaseId
|
||||
- 解决方案:/neo4j/check 成功时检查数据库是否已存在,不存在则自动创建并返回 databaseId
|
||||
- 详细需求:[neo4j-check-return-id.md](./neo4j-check-return-id.md)
|
||||
|
||||
---
|
||||
|
||||
> 需求完成后请完成者打 ✔
|
||||
Reference in New Issue
Block a user