-
Notifications
You must be signed in to change notification settings - Fork 6
/
convertOperators.ts
30 lines (24 loc) · 948 Bytes
/
convertOperators.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
import { operatorResolver } from './operatorResolver';
import { _ } from '@feathersjs/commons/lib';
export const convertOperators = (query: any, escapeFn: any, root = ''): any => {
if (Array.isArray(query)) {
return query.map(q => convertOperators(q, escapeFn, root));
}
return Object.keys(query).map((prop) => {
if (prop === '$or') {
return operatorResolver.$or(convertOperators(query[prop], escapeFn));
}
if (prop === '$and') {
return operatorResolver.$and(convertOperators(query[prop], escapeFn, prop));
}
if (prop in operatorResolver) {
const values = Object.values(escapeFn(root, query[prop]));
return operatorResolver[prop](...values);
}
if (_.isObject(query[prop])) {
return operatorResolver.$and(convertOperators(query[prop], escapeFn, prop));
}
const values = Object.values(escapeFn(prop, query[prop]));
return operatorResolver.$eq(...values);
});
};