初始化项目结构和基础模块
完成内容: - 创建前后端项目目录结构 - 初始化 Node.js + TypeScript 后端项目 (Fastify) - 初始化 Vue 3 + TypeScript 前端项目 (Vite) - 配置 Prisma ORM 和 MySQL 数据库 - 创建数据库 Schema 和种子数据 - 实现后端基础模块 (配置、日志、错误处理、验证) - 创建基础服务类 (BaseService) - 配置 Git 仓库和忽略文件 技术栈: - 后端:Fastify + Prisma + MySQL + Winston - 前端:Vue 3 + TypeScript + Element Plus + Pinia Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
commit
830a17bf46
53 files changed
+16893
No files matched your search
@@ -0,0 +1,112 @@
|
||||
import prisma from '../config/database';
|
||||
import { NotFoundError, ConflictError } from '../utils/errors';
|
||||
|
||||
export abstract class BaseService<T, CreateInput, UpdateInput> {
|
||||
protected model: any;
|
||||
|
||||
constructor(model: any) {
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
// 获取所有记录
|
||||
async findAll(options: {
|
||||
skip?: number;
|
||||
take?: number;
|
||||
where?: any;
|
||||
orderBy?: any;
|
||||
include?: any;
|
||||
} = {}): Promise<T[]> {
|
||||
return this.model.findMany({
|
||||
skip: options.skip,
|
||||
take: options.take,
|
||||
where: options.where,
|
||||
orderBy: options.orderBy || { createdAt: 'desc' },
|
||||
include: options.include,
|
||||
});
|
||||
}
|
||||
|
||||
// 根据 ID 查找
|
||||
async findById(id: number, include?: any): Promise<T | null> {
|
||||
return this.model.findUnique({
|
||||
where: { id },
|
||||
include,
|
||||
});
|
||||
}
|
||||
|
||||
// 根据 ID 查找(不存在则抛出错误)
|
||||
async findByIdOrThrow(id: number, include?: any): Promise<T> {
|
||||
const record = await this.findById(id, include);
|
||||
if (!record) {
|
||||
throw new NotFoundError(`${this.model.name} ID ${id} 不存在`);
|
||||
}
|
||||
return record;
|
||||
}
|
||||
|
||||
// 创建记录
|
||||
async create(data: CreateInput, uniqueFields?: string[]): Promise<T> {
|
||||
// 检查唯一约束
|
||||
if (uniqueFields && uniqueFields.length > 0) {
|
||||
for (const field of uniqueFields) {
|
||||
const value = (data as any)[field];
|
||||
if (value !== undefined) {
|
||||
const existing = await this.model.findFirst({
|
||||
where: { [field]: value },
|
||||
});
|
||||
if (existing) {
|
||||
throw new ConflictError(`${field} 已存在: ${value}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return this.model.create({ data });
|
||||
}
|
||||
|
||||
// 更新记录
|
||||
async update(id: number, data: UpdateInput): Promise<T> {
|
||||
await this.findByIdOrThrow(id);
|
||||
return this.model.update({
|
||||
where: { id },
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
||||
// 删除记录
|
||||
async delete(id: number): Promise<T> {
|
||||
await this.findByIdOrThrow(id);
|
||||
return this.model.delete({ where: { id } });
|
||||
}
|
||||
|
||||
// 计数
|
||||
async count(where?: any): Promise<number> {
|
||||
return this.model.count({ where });
|
||||
}
|
||||
|
||||
// 分页查询
|
||||
async paginate(
|
||||
page: number = 1,
|
||||
limit: number = 20,
|
||||
where?: any,
|
||||
include?: any,
|
||||
orderBy?: any
|
||||
): Promise<{ data: T[]; total: number; totalPages: number }> {
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
const [data, total] = await Promise.all([
|
||||
this.model.findMany({
|
||||
skip,
|
||||
take: limit,
|
||||
where,
|
||||
include,
|
||||
orderBy: orderBy || { createdAt: 'desc' },
|
||||
}),
|
||||
this.model.count({ where }),
|
||||
]);
|
||||
|
||||
const totalPages = Math.ceil(total / limit);
|
||||
|
||||
return { data, total, totalPages };
|
||||
}
|
||||
}
|
||||
|
||||
export default BaseService;
|
||||
Reference in new issue
Block a user