| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- import { DataSource, Repository, ObjectLiteral, DeepPartial } from 'typeorm';
- import { z } from '@hono/zod-openapi';
- export abstract class GenericCrudService<T extends ObjectLiteral> {
- protected repository: Repository<T>;
-
- constructor(
- protected dataSource: DataSource,
- protected entity: new () => T
- ) {
- this.repository = this.dataSource.getRepository(entity);
- }
- /**
- * 获取分页列表
- */
- /**
- * 获取分页列表,支持高级查询
- */
- async getList(
- page: number = 1,
- pageSize: number = 10,
- keyword?: string,
- searchFields?: string[],
- where?: Partial<T>,
- relations: string[] = [],
- order: { [P in keyof T]?: 'ASC' | 'DESC' } = {}
- ): Promise<[T[], number]> {
- const skip = (page - 1) * pageSize;
- const query = this.repository.createQueryBuilder('entity');
-
- // 关联查询
- if (relations.length > 0) {
- relations.forEach(relation => {
- query.leftJoinAndSelect(`entity.${relation}`, relation);
- });
- }
-
- // 关键词搜索
- if (keyword && searchFields && searchFields.length > 0) {
- query.andWhere(searchFields.map(field => `entity.${field} LIKE :keyword`).join(' OR '), {
- keyword: `%${keyword}%`
- });
- }
-
- // 条件查询
- if (where) {
- Object.entries(where).forEach(([key, value]) => {
- if (value !== undefined && value !== null) {
- query.andWhere(`entity.${key} = :${key}`, { [key]: value });
- }
- });
- }
-
- // 排序
- Object.entries(order).forEach(([key, direction]) => {
- query.orderBy(`entity.${key}`, direction);
- });
-
- return query.skip(skip).take(pageSize).getManyAndCount();
- }
- /**
- * 高级查询方法
- */
- createQueryBuilder(alias: string = 'entity') {
- return this.repository.createQueryBuilder(alias);
- }
- /**
- * 根据ID获取单个实体
- */
- async getById(id: number): Promise<T | null> {
- return this.repository.findOneBy({ id } as any);
- }
- /**
- * 创建实体
- */
- async create(data: DeepPartial<T>): Promise<T> {
- const entity = this.repository.create(data as DeepPartial<T>);
- return this.repository.save(entity);
- }
- /**
- * 更新实体
- */
- async update(id: number, data: Partial<T>): Promise<T | null> {
- await this.repository.update(id, data);
- return this.getById(id);
- }
- /**
- * 删除实体
- */
- async delete(id: number): Promise<boolean> {
- const result = await this.repository.delete(id);
- return result.affected === 1;
- }
- }
- export type CrudOptions<
- T extends ObjectLiteral,
- CreateSchema extends z.ZodSchema = z.ZodSchema,
- UpdateSchema extends z.ZodSchema = z.ZodSchema,
- GetSchema extends z.ZodSchema = z.ZodSchema,
- ListSchema extends z.ZodSchema = z.ZodSchema
- > = {
- entity: new () => T;
- createSchema: CreateSchema;
- updateSchema: UpdateSchema;
- getSchema: GetSchema;
- listSchema: ListSchema;
- searchFields?: string[];
- middleware?: any[];
- };
|