mirror of
https://github.com/memohai/Memoh.git
synced 2026-04-27 07:16:19 +09:00
191 lines
5.1 KiB
Markdown
191 lines
5.1 KiB
Markdown
# 数据库和API修复总结
|
|
|
|
本次修复解决了数据库表设计和后端API的6个主要问题。
|
|
|
|
## ✅ 已完成的修复
|
|
|
|
### 1. 修复 isActive 数据类型 ✓
|
|
|
|
**问题**: `users.isActive` 字段使用 `text` 类型而不是 `boolean`
|
|
|
|
**修复**:
|
|
- 文件: `packages/db/src/users.ts`
|
|
- 将 `isActive: text('is_active').notNull().default('true')` 改为 `isActive: boolean('is_active').notNull().default(true)`
|
|
|
|
### 2. 添加外键约束 ✓
|
|
|
|
**问题**: 缺少重要的外键约束
|
|
|
|
**修复**:
|
|
- 文件: `packages/db/src/settings.ts`
|
|
- `userId` 字段从 `text` 改为 `uuid`,并添加外键引用 `users.id`
|
|
- 文件: `packages/db/src/history.ts`
|
|
- `user` 字段从 `text` 改为 `uuid`,并添加外键引用 `users.id`
|
|
|
|
### 3. 重构 JWT 中间件消除重复代码 ✓
|
|
|
|
**问题**: JWT 配置在多个模块中重复定义
|
|
|
|
**修复**:
|
|
- 文件: `packages/api/src/middlewares/auth.ts`
|
|
- 创建共享的 `jwtPlugin` 包含 JWT 和 Bearer token 配置
|
|
- 所有中间件复用这个插件,消除重复代码
|
|
- 更新的模块:
|
|
- `packages/api/src/modules/auth/index.ts`
|
|
- `packages/api/src/modules/memory/index.ts`
|
|
- `packages/api/src/modules/settings/index.ts`
|
|
- `packages/api/src/modules/agent/index.ts`
|
|
|
|
### 4. 实现统一错误处理中间件 ✓
|
|
|
|
**问题**: 缺少统一的错误处理机制
|
|
|
|
**修复**:
|
|
- 文件: `packages/api/src/middlewares/error.ts` (新建)
|
|
- 创建统一的错误处理中间件
|
|
- 定义标准的错误响应格式 `ErrorResponse`
|
|
- 定义标准的成功响应格式 `SuccessResponse`
|
|
- 自动根据错误类型设置合适的 HTTP 状态码
|
|
- 支持的错误类型:
|
|
- `VALIDATION` (400)
|
|
- `NOT_FOUND` (404)
|
|
- `PARSE` (400)
|
|
- `UNAUTHORIZED` (401)
|
|
- `FORBIDDEN` (403)
|
|
- `CONFLICT` (409)
|
|
- `INTERNAL_SERVER_ERROR` (500)
|
|
- 文件: `packages/api/src/index.ts`
|
|
- 在主应用中启用错误处理中间件
|
|
|
|
### 5. 为 model 模块添加权限控制 ✓
|
|
|
|
**问题**: model 模块的创建、更新、删除操作没有权限检查
|
|
|
|
**修复**:
|
|
- 文件: `packages/api/src/modules/model/index.ts`
|
|
- 读取操作 (GET) 使用 `optionalAuthMiddleware`(公开或可选认证)
|
|
- 写入操作 (POST, PUT, DELETE) 使用 `adminMiddleware`(仅管理员)
|
|
- 使用 `guard` 分离不同权限级别的路由
|
|
|
|
### 6. 添加分页功能到列表接口 ✓
|
|
|
|
**问题**: 列表接口缺少分页、排序功能
|
|
|
|
**修复**:
|
|
- 文件: `packages/api/src/utils/pagination.ts` (新建)
|
|
- 创建通用的分页工具函数
|
|
- `parsePaginationParams()` - 解析分页参数
|
|
- `createPaginatedResult()` - 创建分页结果
|
|
- `calculateOffset()` - 计算偏移量
|
|
- 标准分页响应格式:
|
|
```typescript
|
|
{
|
|
items: T[],
|
|
pagination: {
|
|
page: number,
|
|
limit: number,
|
|
total: number,
|
|
totalPages: number,
|
|
hasNext: boolean,
|
|
hasPrev: boolean
|
|
}
|
|
}
|
|
```
|
|
|
|
- 文件: `packages/api/src/modules/user/service.ts`
|
|
- 更新 `getUsers()` 支持分页和排序
|
|
- 支持参数: `page`, `limit`, `sortBy`, `sortOrder`
|
|
|
|
- 文件: `packages/api/src/modules/user/index.ts`
|
|
- GET `/user` 接口支持分页查询参数
|
|
|
|
- 文件: `packages/api/src/modules/model/service.ts`
|
|
- 更新 `getModels()` 支持分页
|
|
- 支持参数: `page`, `limit`, `sortOrder`
|
|
|
|
- 文件: `packages/api/src/modules/model/index.ts`
|
|
- GET `/model` 接口支持分页查询参数
|
|
|
|
## 📋 API 使用示例
|
|
|
|
### 分页查询用户
|
|
```bash
|
|
GET /user?page=1&limit=10&sortBy=createdAt&sortOrder=desc
|
|
```
|
|
|
|
响应:
|
|
```json
|
|
{
|
|
"success": true,
|
|
"items": [...],
|
|
"pagination": {
|
|
"page": 1,
|
|
"limit": 10,
|
|
"total": 50,
|
|
"totalPages": 5,
|
|
"hasNext": true,
|
|
"hasPrev": false
|
|
}
|
|
}
|
|
```
|
|
|
|
### 分页查询模型
|
|
```bash
|
|
GET /model?page=1&limit=10&sortOrder=desc
|
|
```
|
|
|
|
### 错误响应格式
|
|
```json
|
|
{
|
|
"success": false,
|
|
"error": "Error message",
|
|
"code": "ERROR_CODE",
|
|
"details": { ... }
|
|
}
|
|
```
|
|
|
|
## 🔄 数据库迁移
|
|
|
|
修改了数据库 schema 后,需要运行迁移:
|
|
|
|
```bash
|
|
cd packages/db
|
|
pnpm run generate # 生成迁移文件
|
|
pnpm run push # 执行迁移
|
|
```
|
|
|
|
## ⚠️ 注意事项
|
|
|
|
1. **数据库迁移**: 修改了 `users.isActive`, `settings.userId`, `history.user` 字段,需要迁移现有数据
|
|
2. **API 响应格式变化**: 列表接口现在返回分页格式,前端需要适配
|
|
3. **权限控制**: model 的写入操作现在需要管理员权限
|
|
|
|
## 📚 相关文件
|
|
|
|
### 数据库 Schema
|
|
- `packages/db/src/users.ts`
|
|
- `packages/db/src/settings.ts`
|
|
- `packages/db/src/history.ts`
|
|
|
|
### 中间件
|
|
- `packages/api/src/middlewares/auth.ts`
|
|
- `packages/api/src/middlewares/error.ts`
|
|
- `packages/api/src/middlewares/index.ts`
|
|
|
|
### API 模块
|
|
- `packages/api/src/modules/user/index.ts`
|
|
- `packages/api/src/modules/user/service.ts`
|
|
- `packages/api/src/modules/model/index.ts`
|
|
- `packages/api/src/modules/model/service.ts`
|
|
- `packages/api/src/modules/auth/index.ts`
|
|
- `packages/api/src/modules/memory/index.ts`
|
|
- `packages/api/src/modules/settings/index.ts`
|
|
- `packages/api/src/modules/agent/index.ts`
|
|
|
|
### 工具函数
|
|
- `packages/api/src/utils/pagination.ts`
|
|
|
|
### 主应用
|
|
- `packages/api/src/index.ts`
|
|
|