Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
createQuery (originalQuery) {
const { filters, query } = filterQuery(originalQuery || {});
let r = this.options.r;
let rq = this.table.filter(this.createFilter(query));
// Handle $select
if (filters.$select) {
rq = rq.pluck(filters.$select);
}
// Handle $sort
if (filters.$sort) {
_.each(filters.$sort, (order, fieldName) => {
if (parseInt(order) === 1) {
rq = rq.orderBy(fieldName);
} else {
rq = rq.orderBy(r.desc(fieldName));
}
});
}
return rq;
}
if (Array.isArray(or)) {
matcher = createFilter(or[0], r)(doc);
for (let i = 0; i < or.length; i++) {
matcher = matcher.or(createFilter(or[i], r)(doc));
}
// Handle $and
} else if (Array.isArray(and)) {
matcher = createFilter(and[0], r)(doc);
for (let i = 0; i < and.length; i++) {
matcher = matcher.and(createFilter(and[i], r)(doc));
}
}
_.each(query, (value, field) => {
if (typeof value !== 'object') {
// Match value directly
matcher = matcher.and(buildNestedQueryPredicate(field, doc).eq(value));
} else {
// Handle special parameters
_.each(value, (selector, type) => {
if (type === '$in') {
matcher = matcher.and(r.expr(selector).contains(buildNestedQueryPredicate(field, doc)));
} else if (type === '$nin') {
matcher = matcher.and(
r.expr(selector).contains(buildNestedQueryPredicate(field, doc)).not()
);
} else if (mappings[type]) {
const selectorArray = Array.isArray(selector) ? selector : [ selector ];
const method = mappings[type];
function cleanQuery (query: any, operators: any, filters: any): any {
if (Array.isArray(query)) {
return query.map(value => cleanQuery(value, operators, filters));
} else if (_.isObject(query) && query.constructor === {}.constructor) {
const result: { [key: string]: any } = {};
_.each(query, (value, key) => {
if (key[0] === '$') {
if (filters[key] !== undefined) {
return;
}
if (!operators.includes(key)) {
throw new BadRequest(`Invalid query parameter ${key}`, query);
}
}
result[key] = cleanQuery(value, operators, filters);
});
Object.getOwnPropertySymbols(query).forEach(symbol => {
// @ts-ignore
result[symbol] = query[symbol];
_.each(query, (value, field) => {
if (typeof value !== 'object') {
// Match value directly
matcher = matcher.and(buildNestedQueryPredicate(field, doc).eq(value));
} else {
// Handle special parameters
_.each(value, (selector, type) => {
if (type === '$in') {
matcher = matcher.and(r.expr(selector).contains(buildNestedQueryPredicate(field, doc)));
} else if (type === '$nin') {
matcher = matcher.and(
r.expr(selector).contains(buildNestedQueryPredicate(field, doc)).not()
);
} else if (mappings[type]) {
const selectorArray = Array.isArray(selector) ? selector : [ selector ];
const method = mappings[type];
matcher = matcher.and(buildNestedQueryPredicate(field, doc)[method](...selectorArray));
}
});
}
});
function assignFilters (object: any, query: any, filters: any, options: any) {
if (Array.isArray(filters)) {
_.each(filters, (key) => {
if (query[key] !== undefined) {
object[key] = query[key];
}
});
} else {
_.each(filters, (converter, key) => {
const converted = converter(query[key], options);
if (converted !== undefined) {
object[key] = converted;
}
});
}
return object;
}