2016-09-21 18:33:50 +03:00
|
|
|
import applyProps from '../lib/applyProps.js';
|
2016-09-22 12:01:47 +03:00
|
|
|
import AggregateFilters from './aggregateSearchFilters.js';
|
|
|
|
import assemble from './assembler.js';
|
2016-09-22 17:06:05 +03:00
|
|
|
import assembleAggregateResults from './assembleAggregateResults.js';
|
|
|
|
import buildAggregatePipeline from './buildAggregatePipeline.js';
|
2017-11-24 16:32:42 +02:00
|
|
|
import snapBackDottedFields from './lib/snapBackDottedFields';
|
2016-09-21 18:33:50 +03:00
|
|
|
|
2016-10-19 15:22:50 +03:00
|
|
|
export default function storeHypernovaResults(childCollectionNode, userId) {
|
2016-09-22 12:01:47 +03:00
|
|
|
if (childCollectionNode.parent.results.length === 0) {
|
|
|
|
return childCollectionNode.results = [];
|
2016-09-21 18:33:50 +03:00
|
|
|
}
|
|
|
|
|
2016-09-22 12:01:47 +03:00
|
|
|
let {filters, options} = applyProps(childCollectionNode);
|
2016-09-21 18:33:50 +03:00
|
|
|
|
2016-10-23 18:39:11 +03:00
|
|
|
const metaFilters = filters.$meta;
|
|
|
|
const aggregateFilters = new AggregateFilters(childCollectionNode, metaFilters);
|
|
|
|
delete filters.$meta;
|
|
|
|
|
2016-09-22 12:01:47 +03:00
|
|
|
const linker = childCollectionNode.linker;
|
2016-09-21 18:33:50 +03:00
|
|
|
const isVirtual = linker.isVirtual();
|
2016-09-22 12:01:47 +03:00
|
|
|
const collection = childCollectionNode.collection;
|
2016-09-21 18:33:50 +03:00
|
|
|
|
|
|
|
_.extend(filters, aggregateFilters.create());
|
|
|
|
|
2016-09-22 13:46:27 +03:00
|
|
|
// if it's not virtual then we retrieve them and assemble them here.
|
2016-09-22 12:01:47 +03:00
|
|
|
if (!isVirtual) {
|
|
|
|
const filteredOptions = _.omit(options, 'limit');
|
|
|
|
|
2016-09-24 08:05:26 +03:00
|
|
|
childCollectionNode.results = collection.find(filters, filteredOptions, userId).fetch();
|
2016-09-22 12:01:47 +03:00
|
|
|
|
2016-10-23 18:39:11 +03:00
|
|
|
assemble(childCollectionNode, {
|
|
|
|
...options,
|
|
|
|
metaFilters
|
|
|
|
});
|
2016-09-22 15:25:08 +03:00
|
|
|
} else {
|
2016-09-22 17:06:05 +03:00
|
|
|
// virtuals arrive here
|
2017-11-24 16:32:42 +02:00
|
|
|
let {pipeline, containsDottedFields} = buildAggregatePipeline(childCollectionNode, filters, options, userId);
|
2016-09-22 15:25:08 +03:00
|
|
|
|
2017-11-24 16:32:42 +02:00
|
|
|
let aggregateResults = collection.aggregate(pipeline, {explains: true});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If in aggregation it contains '.', we replace it with a custom string '___'
|
|
|
|
* And then after aggregation is complete we need to snap-it back to how it was.
|
|
|
|
*/
|
|
|
|
if (containsDottedFields) {
|
|
|
|
snapBackDottedFields(aggregateResults);
|
|
|
|
}
|
2016-09-22 12:01:47 +03:00
|
|
|
|
2016-10-23 18:39:11 +03:00
|
|
|
assembleAggregateResults(childCollectionNode, aggregateResults, metaFilters);
|
2016-09-22 15:25:08 +03:00
|
|
|
}
|
2017-03-02 10:03:24 +02:00
|
|
|
}
|