add base repository
This commit is contained in:
parent
5452f890ec
commit
823046e94a
25
src/modules/database/base/repository.ts
Normal file
25
src/modules/database/base/repository.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import { isNil } from 'lodash';
|
||||
import { ObjectLiteral, Repository, SelectQueryBuilder } from 'typeorm';
|
||||
|
||||
import { OrderType } from '@/modules/database/constants';
|
||||
import { OrderQueryType } from '@/modules/database/types';
|
||||
import { getOrderByQuery } from '@/modules/database/utils';
|
||||
|
||||
export abstract class BaseRepository<T extends ObjectLiteral> extends Repository<T> {
|
||||
protected abstract _qbName: string;
|
||||
|
||||
protected orderBy?: string | { name: string; order: `${OrderType}` };
|
||||
|
||||
get qbName() {
|
||||
return this._qbName;
|
||||
}
|
||||
|
||||
buildBaseQB() {
|
||||
return this.createQueryBuilder(this.qbName);
|
||||
}
|
||||
|
||||
addOrderByQuery(qb: SelectQueryBuilder<T>, orderBy?: OrderQueryType) {
|
||||
const orderByQuery = orderBy ?? this.orderBy;
|
||||
return isNil(orderByQuery) ? qb : getOrderByQuery(qb, this.qbName, orderByQuery);
|
||||
}
|
||||
}
|
@ -8,3 +8,8 @@ export enum SelectTrashMode {
|
||||
// NONE: 只包含未软删除的数据 (只查询正常数据)
|
||||
NONE = 'none',
|
||||
}
|
||||
|
||||
export enum OrderType {
|
||||
ASC = 'ASC',
|
||||
DESC = 'DESC',
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
import { ObjectLiteral, SelectQueryBuilder } from 'typeorm';
|
||||
|
||||
import { OrderType } from '@/modules/database/constants';
|
||||
|
||||
export type QueryHook<Entity> = (
|
||||
qb: SelectQueryBuilder<Entity>,
|
||||
) => Promise<SelectQueryBuilder<Entity>>;
|
||||
@ -21,3 +23,8 @@ export interface PaginateReturn<E extends ObjectLiteral> {
|
||||
meta: PaginateMeta;
|
||||
items: E[];
|
||||
}
|
||||
|
||||
export type OrderQueryType =
|
||||
| string
|
||||
| { name: string; order: `${OrderType}` }
|
||||
| Array<string | { name: string; order: `${OrderType}` }>;
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { isNil } from 'lodash';
|
||||
import { isArray, isNil } from 'lodash';
|
||||
import { ObjectLiteral, SelectQueryBuilder } from 'typeorm';
|
||||
|
||||
import { PaginateOptions, PaginateReturn } from '@/modules/database/types';
|
||||
@ -58,3 +58,25 @@ export function treePaginate<T extends ObjectLiteral>(
|
||||
items,
|
||||
};
|
||||
}
|
||||
|
||||
export const getOrderByQuery = <T extends ObjectLiteral>(
|
||||
qb: SelectQueryBuilder<T>,
|
||||
alias: string,
|
||||
orderBy?: OrderQueryType,
|
||||
) => {
|
||||
if (isNil(orderBy)) {
|
||||
return qb;
|
||||
}
|
||||
if (typeof orderBy === 'string') {
|
||||
return qb.orderBy(`${alias}.${orderBy}`, 'DESC');
|
||||
}
|
||||
if (isArray(orderBy)) {
|
||||
for (const item of orderBy) {
|
||||
typeof item === 'string'
|
||||
? qb.addOrderBy(`${alias}.${item}`, 'DESC')
|
||||
: qb.addOrderBy(`${alias}.${item.name}`, item.order);
|
||||
}
|
||||
return qb;
|
||||
}
|
||||
return qb.orderBy(`${alias}.${(orderBy as any).name}`, (orderBy as any).order);
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user