grapher/lib/query/hypernova/assembler.js

45 lines
1.4 KiB
JavaScript
Raw Normal View History

2016-09-22 13:46:27 +03:00
import createSearchFilters from '../../links/lib/createSearchFilters';
2016-09-22 12:01:47 +03:00
import sift from 'sift';
export default (childCollectionNode, {limit, skip}) => {
2016-09-22 12:01:47 +03:00
const parent = childCollectionNode.parent;
2016-09-22 13:46:27 +03:00
const linker = childCollectionNode.linker;
2016-09-22 12:01:47 +03:00
2016-09-22 13:46:27 +03:00
const strategy = linker.strategy;
const isVirtual = linker.isVirtual();
const isSingle = linker.isSingle();
const removeStorageField = !childCollectionNode.parentHasMyLinkStorageFieldSpecified();
const oneResult = linker.isOneResult();
2016-09-22 13:46:27 +03:00
const fieldStorage = linker.linkStorageField;
2016-09-22 12:01:47 +03:00
_.each(parent.results, result => {
const data = assembleData(childCollectionNode, result, {
fieldStorage, strategy, isVirtual, isSingle
2016-09-22 13:46:27 +03:00
});
result[childCollectionNode.linkName] = filterAssembledData(data, {limit, skip, oneResult})
2016-09-22 13:46:27 +03:00
});
2016-09-22 12:01:47 +03:00
if (removeStorageField) {
_.each(parent.results, result => delete result[fieldStorage]);
}
}
2016-09-22 13:46:27 +03:00
function filterAssembledData(data, {limit, skip, oneResult}) {
2016-09-22 13:46:27 +03:00
if (limit) {
return data.slice(skip, limit);
2016-09-22 13:46:27 +03:00
}
if (oneResult) {
return _.first(data);
}
2016-09-22 12:01:47 +03:00
2016-09-22 13:46:27 +03:00
return data;
}
function assembleData(childCollectionNode, result, {fieldStorage, strategy, isVirtual}) {
const filters = createSearchFilters(result, fieldStorage, strategy, isVirtual);
return sift(filters, childCollectionNode.results);
2016-09-22 12:01:47 +03:00
}