语义模型(Semantic Model)
网关前缀:${API_BASE}/metadata/models/...
语义模型接口用于根据「自然语言 + 简化字段配置」生成元数据实体(Entity + Field),并自动在指定数据源下创建对应表结构。
DTO 结构(GeneratedSchema)
控制器:DataModelController.saveModel
后端 DTO 定义(简化版):
GeneratedSchemaentity:字符串,必填,实体名(同时作为表名)fields:GeneratedField[],可选,字段列表
GeneratedFieldname:字符串,必填,字段名(同时作为列名)type:字符串,必填,字段类型(见下方映射)label:字符串,可选,字段显示名choices:Choice[],可选,枚举/选项(当前仅在 UI 层使用)
Choiceid:任意类型,可选name:字符串,可选
注意:服务端会根据租户的默认数据源(名称
primary)自动填充schemaName、dataSourceId等信息。
字段类型映射(type -> FieldDto.DataType)
后端使用 mapDataType 进行类型映射:
"string"→STRING"number"→INTEGER(默认整数,后续可按上下文细分)"date"→DATE"enum"→STRING"file"→STRING- 其他/未知 →
STRING
保存生成的模型
- 方法:
POST /models - 权限:
admin/tenant:admin - 请求头:
Authorization: Bearer <token>X-Tenant-Id: <tenantId>Content-Type: application/json
- 请求体:
GeneratedSchema
示例:
curl -X POST \
"${API_BASE}/metadata/models" \
-H "Authorization: Bearer <token>" \
-H "X-Tenant-Id: tenant-abc123" \
-H "Content-Type: application/json" \
-d '{
"entity": "crm_leads",
"fields": [
{ "name": "name", "type": "string", "label": "线索名称" },
{ "name": "email", "type": "string", "label": "邮箱" },
{ "name": "status", "type": "enum", "label": "状态",
"choices": [
{ "id": "new", "name": "新建" },
{ "id": "contacted", "name": "已联系" }
]
}
]
}'
响应(成功):
{
"success": true,
"message": "模型保存成功",
"data": {
"id": "en_xxx",
"name": "crm_leads",
"tableName": "crm_leads",
"schemaName": "public",
"dataSourceId": "ds_primary",
"fields": [
{ "name": "name", "dataType": "STRING", "columnName": "name" },
{ "name": "email", "dataType": "STRING", "columnName": "email" },
{ "name": "status", "dataType": "STRING", "columnName": "status" }
]
}
}
说明:
- 控制器会调用
EntityService.createEntity(entityDto, true),并启用 DDL 创建(创建实际物理表)。 - 若需要更精细的类型(如
DECIMAL、DOUBLE),可在后续通过实体管理接口更新字段。