(tree, path = [])
| 327 | // processes it recursively, running the `find` method in the |
| 328 | // appropriate models. |
| 329 | const processTree = (tree, path = []) => { |
| 330 | let queue = Promise.resolve({}) |
| 331 | |
| 332 | Object.keys(tree).forEach(key => { |
| 333 | const canonicalKey = key.split('@')[0] |
| 334 | |
| 335 | queue = queue.then(query => { |
| 336 | if ( |
| 337 | tree[key] && |
| 338 | typeof tree[key] === 'object' && |
| 339 | !isOperatorQuery(tree[key]) |
| 340 | ) { |
| 341 | return processTree(tree[key], path.concat(key)).then(result => { |
| 342 | return Object.assign({}, query, { |
| 343 | [canonicalKey]: result |
| 344 | }) |
| 345 | }) |
| 346 | } |
| 347 | |
| 348 | return Object.assign({}, query, { |
| 349 | [canonicalKey]: tree[key] |
| 350 | }) |
| 351 | }) |
| 352 | }) |
| 353 | |
| 354 | const firstKey = Object.keys(tree)[0] |
| 355 | const modelChain = createModelChain(this, path.concat(firstKey)) |
| 356 | const model = modelChain && modelChain[modelChain.length - 1] |
| 357 | |
| 358 | return queue.then(query => { |
| 359 | if (path.length === 0) { |
| 360 | return query |
| 361 | } |
| 362 | |
| 363 | // This is a little optimisation. If the current query didn't yield |
| 364 | // any results, there's no point in processing any nodes to the left |
| 365 | // because the result will always be an empty array. |
| 366 | Object.keys(query).forEach(field => { |
| 367 | if ( |
| 368 | query[field].$containsAny && |
| 369 | query[field].$containsAny.length === 0 |
| 370 | ) { |
| 371 | return { |
| 372 | $containsAny: [] |
| 373 | } |
| 374 | } |
| 375 | }) |
| 376 | |
| 377 | return model |
| 378 | .find({ |
| 379 | query |
| 380 | }) |
| 381 | .then(({results}) => { |
| 382 | return { |
| 383 | $containsAny: results.map(item => item._id.toString()) |
| 384 | } |
| 385 | }) |
| 386 | }) |
no test coverage detected