Skip to content

Commit b71ad7b

Browse files
author
piexlMax(奇淼
committed
docs(mcp): 完善模块关联配置的文档和示例
(cherry picked from commit e042d0464c47ab8441f363df1a1242e26a9c13f7)
1 parent 2139e38 commit b71ad7b

File tree

3 files changed

+183
-6
lines changed

3 files changed

+183
-6
lines changed

server/mcp/execution_plan_schema.md

Lines changed: 139 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,25 @@ type AutoCodeField struct {
8585
Clearable bool `json:"clearable"` // 是否可清空
8686
Sort bool `json:"sort"` // 是否支持排序
8787
PrimaryKey bool `json:"primaryKey"` // 是否主键
88-
DataSource *DataSource `json:"dataSource"` // 数据源
88+
DataSource *DataSource `json:"dataSource"` // 数据源配置(用于关联其他表)
8989
CheckDataSource bool `json:"checkDataSource"` // 是否检查数据源
9090
FieldIndexType string `json:"fieldIndexType"` // 索引类型
9191
}
9292
```
9393

94+
### 4. DataSource 结构体(关联表配置)
95+
96+
```go
97+
type DataSource struct {
98+
DBName string `json:"dbName"` // 关联的数据库名称
99+
Table string `json:"table"` // 关联的表名
100+
Label string `json:"label"` // 用于显示的字段名(如name、title等)
101+
Value string `json:"value"` // 用于存储的值字段名(通常是id)
102+
Association int `json:"association"` // 关联关系:1=一对一,2=一对多
103+
HasDeletedAt bool `json:"hasDeletedAt"` // 关联表是否有软删除字段
104+
}
105+
```
106+
94107
## 使用示例
95108

96109
### 示例1:创建新包和批量创建多个模块
@@ -151,8 +164,15 @@ type AutoCodeField struct {
151164
"clearable": true,
152165
"sort": false,
153166
"primaryKey": false,
154-
"dataSource": null,
155-
"checkDataSource": false,
167+
"dataSource": {
168+
"dbName": "gva",
169+
"table": "sys_users",
170+
"label": "username",
171+
"value": "id",
172+
"association": 2,
173+
"hasDeletedAt": true
174+
},
175+
"checkDataSource": true,
156176
"fieldIndexType": ""
157177
},
158178
{
@@ -360,6 +380,121 @@ type AutoCodeField struct {
360380
}
361381
```
362382

383+
### 示例3:模块关联关系配置详解
384+
385+
以下示例展示了如何配置不同类型的关联关系:
386+
387+
```json
388+
{
389+
"packageName": "order",
390+
"packageType": "package",
391+
"needCreatedPackage": true,
392+
"needCreatedModules": true,
393+
"packageInfo": {
394+
"desc": "订单管理模块",
395+
"label": "订单管理",
396+
"template": "package",
397+
"packageName": "order"
398+
},
399+
"modulesInfo": [
400+
{
401+
"package": "order",
402+
"tableName": "orders",
403+
"structName": "Order",
404+
"packageName": "order",
405+
"description": "订单",
406+
"abbreviation": "order",
407+
"humpPackageName": "order",
408+
"gvaModel": true,
409+
"autoMigrate": true,
410+
"autoCreateResource": true,
411+
"autoCreateApiToSql": true,
412+
"autoCreateMenuToSql": true,
413+
"autoCreateBtnAuth": true,
414+
"generateWeb": true,
415+
"generateServer": true,
416+
"fields": [
417+
{
418+
"fieldName": "UserID",
419+
"fieldDesc": "下单用户",
420+
"fieldType": "uint",
421+
"fieldJson": "userId",
422+
"columnName": "user_id",
423+
"fieldSearchType": "EQ",
424+
"form": true,
425+
"table": true,
426+
"desc": true,
427+
"require": true,
428+
"dataSource": {
429+
"dbName": "gva",
430+
"table": "sys_users",
431+
"label": "username",
432+
"value": "id",
433+
"association": 2,
434+
"hasDeletedAt": true
435+
},
436+
"checkDataSource": true
437+
},
438+
{
439+
"fieldName": "ProductID",
440+
"fieldDesc": "商品",
441+
"fieldType": "uint",
442+
"fieldJson": "productId",
443+
"columnName": "product_id",
444+
"fieldSearchType": "EQ",
445+
"form": true,
446+
"table": true,
447+
"desc": true,
448+
"require": true,
449+
"dataSource": {
450+
"dbName": "gva",
451+
"table": "products",
452+
"label": "name",
453+
"value": "id",
454+
"association": 2,
455+
"hasDeletedAt": false
456+
},
457+
"checkDataSource": true
458+
},
459+
{
460+
"fieldName": "Status",
461+
"fieldDesc": "订单状态",
462+
"fieldType": "int",
463+
"fieldJson": "status",
464+
"columnName": "status",
465+
"fieldSearchType": "EQ",
466+
"form": true,
467+
"table": true,
468+
"desc": true,
469+
"require": true,
470+
"dictType": "order_status"
471+
}
472+
]
473+
}
474+
]
475+
}
476+
```
477+
478+
## DataSource 配置说明
479+
480+
### 关联关系类型
481+
- **association: 1** - 一对一关联(如用户与用户档案)
482+
- **association: 2** - 一对多关联(如用户与订单)
483+
484+
### 配置要点
485+
1. **dbName**: 通常为 "gva"(默认数据库)
486+
2. **table**: 关联表的实际表名
487+
3. **label**: 用于前端显示的字段(如用户名、商品名称)
488+
4. **value**: 用于存储关联ID的字段(通常是 "id")
489+
5. **hasDeletedAt**: 关联表是否支持软删除
490+
6. **checkDataSource**: 建议设为true,会验证关联表是否存在
491+
492+
### 常见关联场景
493+
- 用户关联:`{"table": "sys_users", "label": "username", "value": "id"}`
494+
- 角色关联:`{"table": "sys_authorities", "label": "authorityName", "value": "authorityId"}`
495+
- 部门关联:`{"table": "sys_departments", "label": "name", "value": "id"}`
496+
- 分类关联:`{"table": "categories", "label": "name", "value": "id"}`
497+
363498
## 重要注意事项
364499

365500
1. **PackageType**: 只能是 "plugin" 或 "package"
@@ -369,6 +504,7 @@ type AutoCodeField struct {
369504
5. **搜索类型**: FieldSearchType支持:EQ, NE, GT, GE, LT, LE, LIKE, BETWEEN等
370505
6. **索引类型**: FieldIndexType支持:index, unique等
371506
7. **GvaModel**: 设置为true时会自动包含ID、CreatedAt、UpdatedAt、DeletedAt字段
507+
8. **关联配置**: 使用dataSource时,确保关联表已存在,建议开启checkDataSource验证
372508

373509
## 常见错误避免
374510

server/mcp/gag_usage_example.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,38 @@
7979
"dataSource": {},
8080
"checkDataSource": false,
8181
"fieldIndexType": ""
82+
},
83+
{
84+
"fieldName": "AuthorID",
85+
"fieldDesc": "作者",
86+
"fieldType": "uint",
87+
"fieldJson": "authorId",
88+
"dataTypeLong": "",
89+
"comment": "作者ID",
90+
"columnName": "author_id",
91+
"fieldSearchType": "EQ",
92+
"fieldSearchHide": false,
93+
"dictType": "",
94+
"form": true,
95+
"table": true,
96+
"desc": true,
97+
"excel": true,
98+
"require": true,
99+
"defaultValue": "",
100+
"errorText": "请选择作者",
101+
"clearable": true,
102+
"sort": false,
103+
"primaryKey": false,
104+
"dataSource": {
105+
"dbName": "gva",
106+
"table": "library_authors",
107+
"label": "name",
108+
"value": "id",
109+
"association": 2,
110+
"hasDeletedAt": true
111+
},
112+
"checkDataSource": true,
113+
"fieldIndexType": ""
82114
}
83115
]
84116
},

server/mcp/gva_auto_generate.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ func (t *AutomationModuleAnalyzer) New() mcp.Tool {
189189
"clearable": "是否可清空(bool)",
190190
"sort": "是否排序(bool)",
191191
"primaryKey": "是否主键(bool)",
192-
"dataSource": "数据源(object)",
193-
"checkDataSource": "检查数据源(bool)",
192+
"dataSource": "数据源配置(object) - 用于配置字段的关联表信息,结构:{\"dbName\":\"数据库名\",\"table\":\"关联表名\",\"label\":\"显示字段\",\"value\":\"值字段\",\"association\":1或2(1=一对一,2=一对多),\"hasDeletedAt\":true/false}。\n\n**获取表名提示:**\n- 可在 server/model 和 plugin/xxx/model 目录下查看对应模块的 TableName() 接口实现获取实际表名\n- 例如:SysUser 的表名为 \"sys_users\",ExaFileUploadAndDownload 的表名为 \"exa_file_upload_and_downloads\"\n- 插件模块示例:Info 的表名为 \"gva_announcements_info\"\n\n**获取数据库名提示:**\n- 主数据库:通常使用 \"gva\"(默认数据库标识)\n- 多数据库:可在 config.yaml 的 db-list 配置中查看可用数据库的 alias-name 字段\n- 如果用户未提及关联多数据库信息 则使用默认数据库 默认数据库的情况下 dbName此处填写为空",
193+
"checkDataSource": "是否检查数据源(bool) - 启用后会验证关联表的存在性",
194194
"fieldIndexType": "索引类型(string)"
195195
}]
196196
}, {
@@ -214,7 +214,16 @@ func (t *AutomationModuleAnalyzer) New() mcp.Tool {
214214
9. 智能字典创建功能:当字段使用字典类型(DictType)时,系统会:
215215
- 自动检查字典是否存在,如果不存在则创建字典
216216
- 根据字典类型和字段描述智能生成默认选项,支持状态、性别、类型、等级、优先级、审批、角色、布尔值、订单、颜色、尺寸等常见场景
217-
- 为无法识别的字典类型提供通用默认选项`),
217+
- 为无法识别的字典类型提供通用默认选项
218+
10. **模块关联配置**:当需要配置模块间的关联关系时,使用dataSource字段:
219+
- **dbName**: 关联的数据库名称
220+
- **table**: 关联的表名
221+
- **label**: 用于显示的字段名(如name、title等)
222+
- **value**: 用于存储的值字段名(通常是id)
223+
- **association**: 关联关系类型(1=一对一关联,2=一对多关联)
224+
- **hasDeletedAt**: 关联表是否有软删除字段
225+
- **checkDataSource**: 设为true时会验证关联表的存在性
226+
- 示例:{"dbName":"gva","table":"sys_users","label":"username","value":"id","association":2,"hasDeletedAt":true}`),
218227
mcp.WithString("action",
219228
mcp.Required(),
220229
mcp.Description("执行操作:'analyze' 分析现有模块信息,'confirm' 请求用户确认创建,'execute' 执行创建操作(支持批量创建多个模块)"),

0 commit comments

Comments
 (0)