(root)
| 46 | * and generates MongoDB `find` query object |
| 47 | */ |
| 48 | function parseWhere(root) { |
| 49 | const operator = Object.keys(root)[0]; |
| 50 | |
| 51 | // extract leaf binary expressions |
| 52 | if (operator === 'AND') { |
| 53 | const e1 = parseWhere(root[operator][0]); |
| 54 | const e2 = parseWhere(root[operator][1]); |
| 55 | |
| 56 | return { '$and' : [ |
| 57 | e1, |
| 58 | e2, |
| 59 | ] }; |
| 60 | } else if (operator === 'OR') { |
| 61 | const e1 = parseWhere(root[operator][0]); |
| 62 | const e2 = parseWhere(root[operator][1]); |
| 63 | |
| 64 | return { '$or' : [ |
| 65 | e1, |
| 66 | e2, |
| 67 | ] }; |
| 68 | } |
| 69 | const field = root[operator][0]; |
| 70 | const value = root[operator][1]; |
| 71 | const expr = exprMapper[operator]; |
| 72 | const obj = {}; |
| 73 | |
| 74 | if (operator === 'LIKE') { |
| 75 | obj[`value.${field}`] = parseLikeExpression(value); |
| 76 | } else { |
| 77 | obj[`value.${field}`] = { [expr]: value }; |
| 78 | } |
| 79 | |
| 80 | return obj; |
| 81 | } |
| 82 | |
| 83 | module.exports = parseWhere; |
no test coverage detected