UNPKG

780 BJavaScriptView Raw
1const sift = require('sift').default;
2
3function preprocessQueryObj(queryObj) {
4 if (typeof queryObj === 'object' && queryObj) {
5 if (queryObj.constructor === Object) {
6 const result = {};
7 for (const key of Object.keys(queryObj)) {
8 result[key] = preprocessQueryObj(queryObj[key]);
9 }
10 return result;
11 } else {
12 if (queryObj.id) {
13 // Assume Asset or Relation instance
14 return { id: queryObj.id };
15 } else {
16 // Maybe RegExp
17 return queryObj;
18 }
19 }
20 } else if (Array.isArray(queryObj)) {
21 return queryObj.map(preprocessQueryObj);
22 } else {
23 // Primitive value
24 return queryObj;
25 }
26}
27
28module.exports = function compileQuery(queryObj = {}) {
29 return sift(preprocessQueryObj(queryObj));
30};