| name | architect/data-api-design |
| description | 数据模型和API设计方法论,包含ERD设计、数据字典、RESTful API规范 |
| version | 1.0.0 |
| agent | architect |
| type | methodology |
| user-invocable | false |
| agent-invocable | true |
| dependencies | [] |
| triggers | ["架构设计完成后","需要设计数据模型时","需要定义API接口时"] |
数据模型与API设计方法论
适用场景
在系统架构确定后,需要设计:
- 数据库表结构和关系
- 数据字典和约束
- API接口规范
- 请求/响应格式
数据模型设计
1. 实体识别
从PRD中识别核心实体(名词):
示例:
- 用户系统:User(用户)、Role(角色)、Permission(权限)
- 博客系统:User(用户)、Post(文章)、Comment(评论)、Tag(标签)
- 电商系统:User(用户)、Product(商品)、Order(订单)、OrderItem(订单项)
2. 关系识别
确定实体之间的关系:
| 关系类型 | 说明 | 示例 |
|---|
| 一对一 (1:1) | 一个A对应一个B | User - Profile |
| 一对多 (1:N) | 一个A对应多个B | User - Post |
| 多对多 (M:N) | 多个A对应多个B | Post - Tag |
关系表示:
||--o{: 一对多
||--||: 一对一
}o--o{: 多对多
3. 实体关系图 (ERD)
使用 Mermaid 绘制 ERD:
erDiagram
User ||--o{ Post : creates
User ||--o{ Comment : writes
Post ||--o{ Comment : has
Post }o--o{ Tag : has
User {
uuid id PK
string email UK
string name
string passwordHash
enum role
datetime createdAt
datetime updatedAt
}
Post {
uuid id PK
uuid authorId FK
string title
text content
enum status
datetime publishedAt
datetime createdAt
datetime updatedAt
}
Comment {
uuid id PK
uuid postId FK
uuid userId FK
text content
datetime createdAt
}
Tag {
uuid id PK
string name UK
}
4. 数据字典
为每个表定义详细的字段信息:
User 表
| 字段 | 类型 | 约束 | 默认值 | 说明 |
|---|
| id | UUID | PK | uuid_generate_v4() | 主键 |
| email | VARCHAR(255) | UNIQUE, NOT NULL | - | 邮箱,用于登录 |
| name | VARCHAR(100) | NOT NULL | - | 用户名 |
| passwordHash |