Files
bw-mini-app-server/src/templates/n8n-dynamic-template.ts
imeepos de2858012d feat: 实现模板执行记录系统和完整的CRUD API接口
- 新增TemplateExecutionEntity实体替代原有的GenerationTask方式
- 实现模板执行记录的完整生命周期管理,包含状态跟踪和性能指标
- 添加模板管理的完整CRUD操作API(创建、更新、删除、查询)
- 更新数据库迁移文件,包含n8n_templates和template_executions表
- 完善模板工厂服务,支持执行记录管理和统计分析
- 重构DynamicN8nImageTemplate和DynamicN8nVideoTemplate类名
- 添加用户相关实体:User、PlatformUser、ExtensionData、UserCredit等
- 实现完整的数据库表结构和索引优化
2025-09-04 16:29:24 +08:00

176 lines
4.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { Repository } from 'typeorm';
import { N8nTemplateEntity, TemplateType } from '../entities/n8n-template.entity';
import { N8nImageGenerateTemplate, N8nVideoGenerateTemplate } from './n8nTemplate';
/**
* 动态图片模板实现
* 从数据库加载配置实现N8nImageGenerateTemplate抽象类
*/
export class DynamicN8nImageTemplate extends N8nImageGenerateTemplate {
private config: N8nTemplateEntity | null = null;
constructor(
private templateId: number,
private templateRepository: Repository<N8nTemplateEntity>
) {
super();
}
/**
* 初始化模板配置,从数据库加载数据
*/
async onInit(): Promise<DynamicN8nImageTemplate> {
this.config = await this.templateRepository.findOne({
where: {
id: this.templateId,
templateType: TemplateType.IMAGE,
isActive: true
}
});
if (!this.config) {
throw new Error(`Image template with id ${this.templateId} not found or inactive`);
}
return this;
}
// 从数据库动态获取配置 - 实现Template抽象类的属性
get code(): string {
return this.config?.code || '';
}
get name(): string {
return this.config?.name || '';
}
get description(): string {
return this.config?.description || '';
}
get creditCost(): number {
return this.config?.creditCost || 0;
}
get version(): string {
return this.config?.version || '';
}
get input(): string {
return this.config?.inputExampleUrl || '';
}
get output(): string {
return this.config?.outputExampleUrl || '';
}
get tags(): string[] {
return this.config?.tags || [];
}
// 实现N8nImageGenerateTemplate抽象类的属性
get imageModel(): string {
return this.config?.imageModel || '';
}
get imagePrompt(): string {
return this.config?.imagePrompt || '';
}
// execute方法自动继承自N8nImageGenerateTemplate无需重写
}
/**
* 动态视频模板实现
* 从数据库加载配置实现N8nVideoGenerateTemplate抽象类
*/
export class DynamicN8nVideoTemplate extends N8nVideoGenerateTemplate {
private config: N8nTemplateEntity | null = null;
constructor(
private templateId: number,
private templateRepository: Repository<N8nTemplateEntity>
) {
super();
}
/**
* 初始化模板配置,从数据库加载数据
*/
async onInit(): Promise<DynamicN8nVideoTemplate> {
this.config = await this.templateRepository.findOne({
where: {
id: this.templateId,
templateType: TemplateType.VIDEO,
isActive: true
}
});
if (!this.config) {
throw new Error(`Video template with id ${this.templateId} not found or inactive`);
}
return this;
}
// 从数据库动态获取配置 - 实现Template抽象类的属性
get code(): string {
return this.config?.code || '';
}
get name(): string {
return this.config?.name || '';
}
get description(): string {
return this.config?.description || '';
}
get creditCost(): number {
return this.config?.creditCost || 0;
}
get version(): string {
return this.config?.version || '';
}
get input(): string {
return this.config?.inputExampleUrl || '';
}
get output(): string {
return this.config?.outputExampleUrl || '';
}
get tags(): string[] {
return this.config?.tags || [];
}
// 实现N8nImageGenerateTemplate抽象类的属性视频模板也需要图片生成
get imageModel(): string {
return this.config?.imageModel || '';
}
get imagePrompt(): string {
return this.config?.imagePrompt || '';
}
// 实现N8nVideoGenerateTemplate抽象类的属性
get videoModel(): string {
return this.config?.videoModel || '';
}
get videoPrompt(): string {
return this.config?.videoPrompt || '';
}
get duration(): number {
return this.config?.duration || 6;
}
get aspectRatio(): string {
return this.config?.aspectRatio || '9:16';
}
// execute方法自动继承自N8nVideoGenerateTemplate无需重写
}