-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchCriteria.ts
37 lines (34 loc) · 1.06 KB
/
SearchCriteria.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
export class SearchCriteria {
page: number;
limit: number;
filterBy: any;
orderBy: any;
/*
* buildOrderByCriteria parses data coming in form ["createdAt", "-updatedAt"], convert into
* TypeORM query format
*/
static buildOrderByCriteria(orderByArray: Array<string>): any {
const orderBy = {};
if (orderByArray && orderByArray.length > 0) {
for (const orderKey of orderByArray) {
if (orderKey.startsWith('-')) {
const orderKeyWithoutPrefix = orderKey.substring(1);
orderBy[orderKeyWithoutPrefix] = 'DESC';
} else {
orderBy[orderKey] = 'ASC';
}
}
}
return orderBy;
}
buildFilterByCriteria() {
// TODO: Will be used in Generic Search API
}
static getSearchCriteriaFromPagination({ page, limit, orderBy }): SearchCriteria {
const searchCriteria = new SearchCriteria();
searchCriteria.page = page;
searchCriteria.limit = limit;
searchCriteria.orderBy = this.buildOrderByCriteria(orderBy);
return searchCriteria;
}
}