243 lines
6.0 KiB
Markdown
243 lines
6.0 KiB
Markdown
# 数据库配置说明
|
||
|
||
## 概述
|
||
基于混合式架构设计,实现了**代码定义逻辑 + 数据库存储配置**的AI模板管理系统。
|
||
|
||
## 数据库连接信息
|
||
- **服务器**: mysql-6bc9094abd49-public.rds.volces.com:53305
|
||
- **数据库**: nano_camera_miniapp
|
||
- **用户名**: root
|
||
- **密码**: WsJWXfwFx0bq6DE2kmB6
|
||
|
||
## 安装依赖
|
||
|
||
```bash
|
||
npm install
|
||
```
|
||
|
||
如果安装失败,请手动添加以下依赖到 package.json:
|
||
```json
|
||
{
|
||
"@nestjs/typeorm": "^11.0.0",
|
||
"typeorm": "^0.3.20",
|
||
"mysql2": "^3.9.7"
|
||
}
|
||
```
|
||
|
||
## 数据库迁移
|
||
|
||
### 运行迁移
|
||
```bash
|
||
npm run migration:run
|
||
```
|
||
|
||
### 生成新迁移
|
||
```bash
|
||
npm run migration:generate -- -n MigrationName
|
||
```
|
||
|
||
### 回滚迁移
|
||
```bash
|
||
npm run migration:revert
|
||
```
|
||
|
||
## 混合式架构使用
|
||
|
||
### 1. 传统方式(代码定义)
|
||
```typescript
|
||
import { templateManager } from './templates';
|
||
|
||
// 使用现有的代码定义模板
|
||
const template = templateManager.getTemplate('photo_restore_v1');
|
||
const result = await template.execute(imageUrl);
|
||
```
|
||
|
||
### 2. 数据库驱动方式(推荐)
|
||
```typescript
|
||
import { N8nTemplateFactoryService } from './services/n8n-template-factory.service';
|
||
|
||
// 通过模板ID创建实例
|
||
const template = await templateFactory.createTemplate(1);
|
||
const result = await template.execute(imageUrl);
|
||
|
||
// 通过模板代码创建实例
|
||
const template = await templateFactory.createTemplateByCode('character_figurine_v1');
|
||
const result = await template.execute(imageUrl);
|
||
|
||
// 您期望的使用方式
|
||
const result = await new N8nVideoGenerateTemplateEntity(templateId, repo)
|
||
.onInit()
|
||
.then(e => e.execute(imgUrl));
|
||
```
|
||
|
||
## API 接口
|
||
|
||
### 基础模板操作
|
||
|
||
#### 获取所有模板
|
||
```
|
||
GET /templates
|
||
```
|
||
|
||
#### 获取图片模板
|
||
```
|
||
GET /templates/image
|
||
```
|
||
|
||
#### 获取视频模板
|
||
```
|
||
GET /templates/video
|
||
```
|
||
|
||
#### 获取模板详情
|
||
```
|
||
GET /templates/{templateId}
|
||
```
|
||
|
||
### 模板执行
|
||
|
||
#### 通过模板ID执行
|
||
```
|
||
POST /templates/{templateId}/execute
|
||
{
|
||
"imageUrl": "https://example.com/image.jpg"
|
||
}
|
||
```
|
||
|
||
#### 通过模板代码执行
|
||
```
|
||
POST /templates/code/{templateCode}/execute
|
||
{
|
||
"imageUrl": "https://example.com/image.jpg"
|
||
}
|
||
```
|
||
|
||
#### 批量执行
|
||
```
|
||
POST /templates/{templateId}/batch-execute
|
||
{
|
||
"imageUrls": ["url1", "url2", "url3"]
|
||
}
|
||
```
|
||
|
||
### 管理接口
|
||
|
||
#### 迁移现有模板到数据库
|
||
```
|
||
POST /templates/migrate
|
||
```
|
||
|
||
#### 同步模板配置
|
||
```
|
||
POST /templates/sync
|
||
```
|
||
|
||
#### 获取迁移报告
|
||
```
|
||
GET /templates/admin/migration-report
|
||
```
|
||
|
||
## 数据库表结构
|
||
|
||
### n8n_templates 表
|
||
|
||
| 字段 | 类型 | 说明 |
|
||
|------|------|------|
|
||
| id | bigint | 主键ID |
|
||
| code | varchar(100) | 模板唯一标识码 |
|
||
| name | varchar(200) | 模板名称 |
|
||
| description | text | 模板详细描述 |
|
||
| credit_cost | int | 积分消耗 |
|
||
| version | varchar(50) | 版本号 |
|
||
| input_example_url | text | 输入示例图片 |
|
||
| output_example_url | text | 输出示例图片 |
|
||
| tags | json | 标签数组 |
|
||
| template_type | enum | 模板类型(image/video) |
|
||
| template_class | varchar(100) | 具体模板类名 |
|
||
| image_model | varchar(100) | 图片生成模型 |
|
||
| image_prompt | text | 图片生成提示词 |
|
||
| video_model | varchar(100) | 视频生成模型 |
|
||
| video_prompt | text | 视频生成提示词 |
|
||
| duration | int | 视频时长(秒) |
|
||
| aspect_ratio | varchar(20) | 视频比例 |
|
||
| is_active | boolean | 是否启用 |
|
||
| sort_order | int | 排序权重 |
|
||
| created_at | timestamp | 创建时间 |
|
||
| updated_at | timestamp | 更新时间 |
|
||
|
||
## 架构优势
|
||
|
||
### 🔒 保持现有优势
|
||
- **类型安全**: 继承现有抽象类,保持TypeScript类型检查
|
||
- **执行逻辑不变**: N8nTemplate的execute方法逻辑完全保持不变
|
||
- **抽象层次清晰**: Template → ImageGenerate/VideoGenerate → N8nTemplate → 具体实现
|
||
|
||
### 🆕 新增数据库能力
|
||
- **配置动态化**: 模板配置存储在数据库中,可在线修改
|
||
- **运营友好**: 通过管理后台可以调整模板参数、启用/禁用模板
|
||
- **A/B测试**: 可以创建同一功能的不同版本进行测试
|
||
- **统计分析**: 可以跟踪每个模板的使用情况和效果
|
||
|
||
### 🚀 业务价值增强
|
||
- **个性化推荐**: 根据用户历史偏好推荐模板
|
||
- **实时监控**: 模板性能、成功率、用户满意度监控
|
||
- **成本优化**: 基于使用统计调整积分定价策略
|
||
- **自动化运营**: 根据数据自动调整模板优先级
|
||
|
||
## 开发工作流
|
||
|
||
### 1. 添加新模板(数据库方式)
|
||
```sql
|
||
INSERT INTO n8n_templates (
|
||
code, name, description, credit_cost, version,
|
||
template_type, image_model, image_prompt, ...
|
||
) VALUES (
|
||
'new_template_v1', '新模板', '描述', 20, '1.0.0',
|
||
'image', 'gemini-2.5-flash-image-preview', '提示词', ...
|
||
);
|
||
```
|
||
|
||
### 2. 添加新模板(代码方式)
|
||
```typescript
|
||
export class NewTemplate extends N8nImageGenerateTemplate {
|
||
readonly code = 'new_template_v1';
|
||
readonly name = '新模板';
|
||
// ... 其他属性
|
||
}
|
||
```
|
||
|
||
### 3. 迁移现有模板
|
||
```typescript
|
||
// 运行迁移服务,将代码中的模板配置导入数据库
|
||
await migrationService.migrateAllExistingTemplates();
|
||
```
|
||
|
||
## 常见问题
|
||
|
||
### Q: 如何选择使用代码模板还是数据库模板?
|
||
A: 推荐使用数据库模板方式,它提供了更强的灵活性和运营能力。代码模板主要用于维护现有系统兼容性。
|
||
|
||
### Q: 现有的模板代码需要修改吗?
|
||
A: 不需要。现有的模板代码可以继续使用,同时也可以通过迁移服务导入到数据库中。
|
||
|
||
### Q: 数据库中的模板配置可以实时生效吗?
|
||
A: 是的,每次调用都会从数据库读取最新配置,支持实时更新。
|
||
|
||
### Q: 如何进行A/B测试?
|
||
A: 可以在数据库中创建同一功能的不同配置版本,通过调整 `sort_order` 和 `is_active` 字段进行测试。
|
||
|
||
## 监控与维护
|
||
|
||
### 性能监控
|
||
- 模板执行时间统计
|
||
- 成功率监控
|
||
- 资源使用监控
|
||
|
||
### 数据维护
|
||
- 定期备份数据库
|
||
- 清理过期的使用记录
|
||
- 优化查询性能
|
||
|
||
---
|
||
|
||
**混合式架构完美融合了代码优先的技术优势和数据库驱动的运营能力!** |