grapher/lib/query/hypernova/storeHypernovaResults.js

40 lines
1.5 KiB
JavaScript
Raw Normal View History

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';
import assembleAggregateResults from './assembleAggregateResults.js';
import buildAggregatePipeline from './buildAggregatePipeline.js';
2016-09-21 18:33:50 +03:00
2016-09-22 12:01:47 +03:00
export default function storeHypernovaResults(childCollectionNode, userId) {
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-09-22 12:01:47 +03:00
const aggregateFilters = new AggregateFilters(childCollectionNode);
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-21 18:33:50 +03:00
if (collection.findSecure) {
2016-09-22 12:01:47 +03:00
childCollectionNode.results = collection.findSecure(filters, filteredOptions, userId).fetch();
2016-09-21 18:33:50 +03:00
} else {
2016-09-22 12:01:47 +03:00
childCollectionNode.results = collection.find(filters, filteredOptions, userId).fetch();
2016-09-21 18:33:50 +03:00
}
2016-09-22 12:01:47 +03:00
assemble(childCollectionNode, options);
2016-09-22 15:25:08 +03:00
} else {
// virtuals arrive here
let pipeline = buildAggregatePipeline(childCollectionNode, filters, options, userId);
2016-09-22 15:25:08 +03:00
const aggregateResults = collection.aggregate(pipeline, {explains: true});
2016-09-22 12:01:47 +03:00
assembleAggregateResults(childCollectionNode, aggregateResults);
2016-09-22 15:25:08 +03:00
}
2016-09-21 18:33:50 +03:00
}