-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcustomExtended.ts
116 lines (102 loc) · 2.98 KB
/
customExtended.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { BadRequestException } from '@nestjs/common';
import {
FindManyOptions,
FindOneOptions,
FindOptionsOrder,
Repository,
} from 'typeorm';
import {
IDriection,
IGetData,
ISort,
OneRepoQuery,
RepoQuery,
checkObject,
directionObj,
valueObj,
} from './types';
import { processWhere } from './utils/processWhere';
const isObject = (value: unknown): boolean => {
return typeof value === 'object' && !Array.isArray(value) && value !== null;
};
type EmptyObject<T> = { [K in keyof T]?: never };
type EmptyObjectOf<T> = EmptyObject<T> extends T ? EmptyObject<T> : never;
const isEmptyObject = <T extends object>(
value: T,
): value is EmptyObjectOf<T> => {
return Object.keys(value).length === 0;
};
export function filterOrder<T>(
this: Repository<T>,
order: FindOptionsOrder<T>,
) {
Object.entries(order).forEach(([key, value]: [string, ISort]) => {
if (!(key in this.metadata.propertiesMap)) {
throw new BadRequestException(
`Order key ${key} is not in ${this.metadata.name}`,
);
}
if (isObject(value)) {
Object.entries(value).forEach(([_key, _value]) => {
if (!directionObj[_key]) {
throw new BadRequestException(
`Order must be ${Object.keys(directionObj).join(' or ')}`,
);
}
if (!checkObject[_key].includes(_value as unknown)) {
throw new BadRequestException(
`Order ${_key} must be ${checkObject[_key].join(' or ')}`,
);
}
});
} else {
if (!valueObj[value as IDriection]) {
throw new BadRequestException(
`Order must be ${Object.keys(valueObj).join(' or ')}`,
);
}
}
});
}
export class ExtendedRepository<T = unknown> extends Repository<T> {
async getMany<T>(
this: Repository<T>,
option: RepoQuery<T> = {},
dataType?: 'count' | 'data',
): Promise<IGetData<T>> {
const { pagination, where, order, relations, select } = option;
// You can remark these lines(if order {}) if you don't want to use strict order roles
if (order) {
filterOrder.call(this, order);
}
const condition: FindManyOptions<T> = {
relations,
...(select && { select }),
...(where && !isEmptyObject(where) && { where: processWhere(where) }),
...(order && { order }),
...(pagination && {
skip: pagination.page * pagination.size,
take: pagination.size,
}),
};
if (dataType === 'count') {
return { count: await this.count(condition) };
}
if (dataType === 'data') {
return { data: await this.find(condition) };
}
const [data, count] = await this.findAndCount(condition);
return { data, count };
}
async getOne<T>(
this: Repository<T>,
{ where, relations, select }: OneRepoQuery<T>,
): Promise<T> {
const condition: FindOneOptions<T> = {
relations,
...(select && { select }),
...(where && { where: processWhere(where) }),
};
return await this.findOne(condition);
}
}