@provis/provis-common-be-module 2.0.3 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -8,6 +8,7 @@ export declare class MainRepository<T> extends Repository<T> {
|
|
|
8
8
|
selectFieldOnFindAllAndCount(): string[];
|
|
9
9
|
modelName(): string;
|
|
10
10
|
orderBy(): string;
|
|
11
|
+
aliasTable(): string;
|
|
11
12
|
getOrderData(sortBy: string): {
|
|
12
13
|
orderBy: string;
|
|
13
14
|
orderDirection: 'ASC' | 'DESC';
|
|
@@ -16,4 +17,9 @@ export declare class MainRepository<T> extends Repository<T> {
|
|
|
16
17
|
summaryField(search: ISearchQuery[], fieldToSum: string[], additionalSearchOr?: ISearchQuery[][]): Promise<any>;
|
|
17
18
|
findAllAndCount(search: ISearchQuery[], maxCount?: number, offset?: number, sortBy?: string, additionalSearchOr?: ISearchQuery[][]): Promise<[number, T[]]>;
|
|
18
19
|
private applySearchConditions;
|
|
20
|
+
private generateRawCount;
|
|
21
|
+
private generateRawOrder;
|
|
22
|
+
private convertRawFilter;
|
|
23
|
+
generateRawFilter(search: ISearchQuery[], additionalSearchOr?: ISearchQuery[][] | undefined, aliasTable?: string): string;
|
|
24
|
+
getData(rawQuery: string, page: number, limit: number, orderBy?: string): Promise<[number, any[]]>;
|
|
19
25
|
}
|
|
@@ -46,6 +46,9 @@ class MainRepository extends typeorm_1.Repository {
|
|
|
46
46
|
orderBy() {
|
|
47
47
|
return 'createdDate';
|
|
48
48
|
}
|
|
49
|
+
aliasTable() {
|
|
50
|
+
return this.modelName();
|
|
51
|
+
}
|
|
49
52
|
getOrderData(sortBy) {
|
|
50
53
|
const [field, direction] = (sortBy || `${this.orderBy()}.desc`).split('.');
|
|
51
54
|
return {
|
|
@@ -176,5 +179,59 @@ class MainRepository extends typeorm_1.Repository {
|
|
|
176
179
|
}
|
|
177
180
|
return query;
|
|
178
181
|
}
|
|
182
|
+
generateRawCount(query) {
|
|
183
|
+
return `SELECT COUNT(*) as total FROM (${query}) as subQry`;
|
|
184
|
+
}
|
|
185
|
+
generateRawOrder(sortBy, aliasTable = this.aliasTable()) {
|
|
186
|
+
const [field, direction] = (sortBy || `${this.orderBy()}.desc`).split('.');
|
|
187
|
+
return `ORDER BY ${aliasTable}."${field}" ${direction}`;
|
|
188
|
+
}
|
|
189
|
+
convertRawFilter(data, aliasTable = this.aliasTable()) {
|
|
190
|
+
const columnName = data.query.split(' ')[0];
|
|
191
|
+
let condition = data.query.replace(`:${data.key}`, `'${data.value}'`);
|
|
192
|
+
if (Array.isArray(data.value)) {
|
|
193
|
+
const value = data.value;
|
|
194
|
+
const statusValues = value.map((value) => `'${value.trim()}'`).join(',');
|
|
195
|
+
condition = data.query.replace(`:...${data.key}`, statusValues);
|
|
196
|
+
}
|
|
197
|
+
condition = condition.replace(columnName, `${aliasTable}."${columnName}"`);
|
|
198
|
+
}
|
|
199
|
+
generateRawFilter(search, additionalSearchOr, aliasTable = this.aliasTable()) {
|
|
200
|
+
let rawQueryFilter = 'WHERE 1=1';
|
|
201
|
+
search.filter(Boolean).forEach((data) => {
|
|
202
|
+
if (data) {
|
|
203
|
+
const condition = this.convertRawFilter(data, aliasTable);
|
|
204
|
+
rawQueryFilter += ` AND ${condition}`;
|
|
205
|
+
}
|
|
206
|
+
}); // -> WHERE 1=1 AND source = 'KALOG'
|
|
207
|
+
let rawQueryFilteOr = '1=1';
|
|
208
|
+
if (additionalSearchOr) {
|
|
209
|
+
additionalSearchOr.forEach((searchOr) => {
|
|
210
|
+
searchOr.filter(Boolean).forEach((data) => {
|
|
211
|
+
if (data) {
|
|
212
|
+
const condition = this.convertRawFilter(data, aliasTable);
|
|
213
|
+
rawQueryFilter += ` OR ${condition}`;
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
rawQueryFilter += `AND (${rawQueryFilteOr})`;
|
|
217
|
+
rawQueryFilteOr = '1=1';
|
|
218
|
+
}); // -> WHERE 1=1 AND source = 'KALOG' AND (1=1) AND (1=1 OR policyNumber='1233567890')
|
|
219
|
+
}
|
|
220
|
+
return rawQueryFilter;
|
|
221
|
+
}
|
|
222
|
+
getData(rawQuery, page, limit, orderBy) {
|
|
223
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
224
|
+
const manager = (0, typeorm_1.getManager)();
|
|
225
|
+
const offset = (page - 1) * limit;
|
|
226
|
+
const rawQueryWithFilter = rawQuery;
|
|
227
|
+
const rawCount = this.generateRawCount(rawQueryWithFilter);
|
|
228
|
+
const rawOrder = this.generateRawOrder(orderBy);
|
|
229
|
+
const [countResult, dataResult] = yield Promise.all([
|
|
230
|
+
manager.query(rawCount),
|
|
231
|
+
manager.query(`${rawQueryWithFilter} ${rawOrder} LIMIT $1 OFFSET $2`, [limit, offset]),
|
|
232
|
+
]);
|
|
233
|
+
return [countResult.length, dataResult];
|
|
234
|
+
});
|
|
235
|
+
}
|
|
179
236
|
}
|
|
180
237
|
exports.MainRepository = MainRepository;
|