|
|
@@ -3,7 +3,7 @@ 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
|
|
|
@@ -14,9 +14,6 @@ export abstract class GenericCrudService<T extends ObjectLiteral> {
|
|
|
/**
|
|
|
* 获取分页列表
|
|
|
*/
|
|
|
- /**
|
|
|
- * 获取分页列表,支持高级查询
|
|
|
- */
|
|
|
async getList(
|
|
|
page: number = 1,
|
|
|
pageSize: number = 10,
|
|
|
@@ -28,21 +25,21 @@ export abstract class GenericCrudService<T extends ObjectLiteral> {
|
|
|
): 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]) => {
|
|
|
@@ -51,27 +48,23 @@ export abstract class GenericCrudService<T extends ObjectLiteral> {
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 排序
|
|
|
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);
|
|
|
+ return query.skip(skip).take(pageSize).getManyAndCount();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据ID获取单个实体
|
|
|
*/
|
|
|
- async getById(id: number): Promise<T | null> {
|
|
|
- return this.repository.findOneBy({ id } as any);
|
|
|
+ async getById(id: number, relations: string[] = []): Promise<T | null> {
|
|
|
+ return this.repository.findOne({
|
|
|
+ where: { id } as any,
|
|
|
+ relations
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -97,6 +90,13 @@ export abstract class GenericCrudService<T extends ObjectLiteral> {
|
|
|
const result = await this.repository.delete(id);
|
|
|
return result.affected === 1;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 高级查询方法
|
|
|
+ */
|
|
|
+ createQueryBuilder(alias: string = 'entity') {
|
|
|
+ return this.repository.createQueryBuilder(alias);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
export type CrudOptions<
|