refactor(prisma): 统一导入prisma客户端方式
- 所有控制器中从libs/prisma.ts导入prisma替代旧的libs/db.ts - 规范了step模块中zod验证架构的格式与换行 - 生成Prisma客户端及类型定义文件,包含browser、client、commonInputTypes、enums和内部类文件 - 优化listStepsQuerySchema默认参数与链式调用格式 - 保持代码风格一致,提升可维护性和类型安全性
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import type { Context } from 'koa';
|
||||
import prisma from '../../libs/db.ts';
|
||||
import { prisma } from '../../libs/prisma.ts';
|
||||
import { log } from '../../libs/logger.ts';
|
||||
import { BusinessError } from '../../middlewares/exception.ts';
|
||||
import { Controller, Get, Post, Put, Delete } from '../../decorators/route.ts';
|
||||
@@ -7,54 +7,99 @@ import { z } from 'zod';
|
||||
|
||||
// 定义验证架构
|
||||
const createStepSchema = z.object({
|
||||
name: z.string({
|
||||
message: '步骤名称必须是字符串',
|
||||
}).min(1, { message: '步骤名称不能为空' }).max(100, { message: '步骤名称不能超过100个字符' }),
|
||||
name: z
|
||||
.string({
|
||||
message: '步骤名称必须是字符串',
|
||||
})
|
||||
.min(1, { message: '步骤名称不能为空' })
|
||||
.max(100, { message: '步骤名称不能超过100个字符' }),
|
||||
|
||||
description: z.string({
|
||||
message: '步骤描述必须是字符串',
|
||||
}).max(500, { message: '步骤描述不能超过500个字符' }).optional(),
|
||||
description: z
|
||||
.string({
|
||||
message: '步骤描述必须是字符串',
|
||||
})
|
||||
.max(500, { message: '步骤描述不能超过500个字符' })
|
||||
.optional(),
|
||||
|
||||
order: z.number({
|
||||
message: '步骤顺序必须是数字',
|
||||
}).int().min(0, { message: '步骤顺序必须是非负整数' }),
|
||||
order: z
|
||||
.number({
|
||||
message: '步骤顺序必须是数字',
|
||||
})
|
||||
.int()
|
||||
.min(0, { message: '步骤顺序必须是非负整数' }),
|
||||
|
||||
script: z.string({
|
||||
message: '脚本命令必须是字符串',
|
||||
}).min(1, { message: '脚本命令不能为空' }),
|
||||
script: z
|
||||
.string({
|
||||
message: '脚本命令必须是字符串',
|
||||
})
|
||||
.min(1, { message: '脚本命令不能为空' }),
|
||||
|
||||
pipelineId: z.number({
|
||||
message: '流水线ID必须是数字',
|
||||
}).int().positive({ message: '流水线ID必须是正整数' }),
|
||||
pipelineId: z
|
||||
.number({
|
||||
message: '流水线ID必须是数字',
|
||||
})
|
||||
.int()
|
||||
.positive({ message: '流水线ID必须是正整数' }),
|
||||
});
|
||||
|
||||
const updateStepSchema = z.object({
|
||||
name: z.string({
|
||||
message: '步骤名称必须是字符串',
|
||||
}).min(1, { message: '步骤名称不能为空' }).max(100, { message: '步骤名称不能超过100个字符' }).optional(),
|
||||
name: z
|
||||
.string({
|
||||
message: '步骤名称必须是字符串',
|
||||
})
|
||||
.min(1, { message: '步骤名称不能为空' })
|
||||
.max(100, { message: '步骤名称不能超过100个字符' })
|
||||
.optional(),
|
||||
|
||||
description: z.string({
|
||||
message: '步骤描述必须是字符串',
|
||||
}).max(500, { message: '步骤描述不能超过500个字符' }).optional(),
|
||||
description: z
|
||||
.string({
|
||||
message: '步骤描述必须是字符串',
|
||||
})
|
||||
.max(500, { message: '步骤描述不能超过500个字符' })
|
||||
.optional(),
|
||||
|
||||
order: z.number({
|
||||
message: '步骤顺序必须是数字',
|
||||
}).int().min(0, { message: '步骤顺序必须是非负整数' }).optional(),
|
||||
order: z
|
||||
.number({
|
||||
message: '步骤顺序必须是数字',
|
||||
})
|
||||
.int()
|
||||
.min(0, { message: '步骤顺序必须是非负整数' })
|
||||
.optional(),
|
||||
|
||||
script: z.string({
|
||||
message: '脚本命令必须是字符串',
|
||||
}).min(1, { message: '脚本命令不能为空' }).optional(),
|
||||
script: z
|
||||
.string({
|
||||
message: '脚本命令必须是字符串',
|
||||
})
|
||||
.min(1, { message: '脚本命令不能为空' })
|
||||
.optional(),
|
||||
});
|
||||
|
||||
const stepIdSchema = z.object({
|
||||
id: z.coerce.number().int().positive({ message: '步骤 ID 必须是正整数' }),
|
||||
});
|
||||
|
||||
const listStepsQuerySchema = z.object({
|
||||
pipelineId: z.coerce.number().int().positive({ message: '流水线ID必须是正整数' }).optional(),
|
||||
page: z.coerce.number().int().min(1, { message: '页码必须大于0' }).optional().default(1),
|
||||
limit: z.coerce.number().int().min(1, { message: '每页数量必须大于0' }).max(100, { message: '每页数量不能超过100' }).optional().default(10),
|
||||
}).optional();
|
||||
const listStepsQuerySchema = z
|
||||
.object({
|
||||
pipelineId: z.coerce
|
||||
.number()
|
||||
.int()
|
||||
.positive({ message: '流水线ID必须是正整数' })
|
||||
.optional(),
|
||||
page: z.coerce
|
||||
.number()
|
||||
.int()
|
||||
.min(1, { message: '页码必须大于0' })
|
||||
.optional()
|
||||
.default(1),
|
||||
limit: z.coerce
|
||||
.number()
|
||||
.int()
|
||||
.min(1, { message: '每页数量必须大于0' })
|
||||
.max(100, { message: '每页数量不能超过100' })
|
||||
.optional()
|
||||
.default(10),
|
||||
})
|
||||
.optional();
|
||||
|
||||
// TypeScript 类型
|
||||
type CreateStepInput = z.infer<typeof createStepSchema>;
|
||||
@@ -87,7 +132,7 @@ export class StepController {
|
||||
orderBy: {
|
||||
order: 'asc',
|
||||
},
|
||||
})
|
||||
}),
|
||||
]);
|
||||
|
||||
return {
|
||||
@@ -97,7 +142,7 @@ export class StepController {
|
||||
limit: query?.limit || 10,
|
||||
total,
|
||||
totalPages: Math.ceil(total / (query?.limit || 10)),
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user