Performance fixes for assembler

This commit is contained in:
Theodor Diaconu 2018-07-07 09:10:31 +03:00
parent eef3f4df0d
commit 3d26c74b62

View file

@ -20,13 +20,51 @@ export default (childCollectionNode, {limit, skip, metaFilters}) => {
})
}
_.each(parent.results, result => {
let data = assembleData(childCollectionNode, result, {
fieldStorage, strategy, isSingle
});
const resultsByKeyId = _.groupBy(childCollectionNode.results, '_id');
result[childCollectionNode.linkName] = filterAssembledData(data, {limit, skip})
});
if (strategy === 'one') {
parent.results.forEach(parentResult => {
parentResult[childCollectionNode.linkName] =
filterAssembledData(
resultsByKeyId[parentResult[fieldStorage]],
{limit, skip}
)
})
}
if (strategy === 'many') {
parent.results.forEach(parentResult => {
let data = [];
parentResult[fieldStorage].forEach(_id => {
data.push(resultsByKeyId[_id]);
})
parentResult[childCollectionNode.linkName] = filterAssembledData(data, {limit, skip});
})
}
if (strategy === 'one-meta') {
parent.results.forEach(parentResult => {
parentResult[childCollectionNode.linkName] =
filterAssembledData(
resultsByKeyId[parentResult[fieldStorage]._id],
{limit, skip}
)
})
}
if (strategy === 'many-meta') {
parent.results.forEach(parentResult => {
const _ids = _.pluck(parentResult[fieldStorage], '_id');
let data = [];
_ids.forEach(_id => {
data.push(resultsByKeyId[_id]);
})
parentResult[childCollectionNode.linkName] = filterAssembledData(data, {limit, skip});
})
}
}
function filterAssembledData(data, {limit, skip}) {
@ -36,9 +74,3 @@ function filterAssembledData(data, {limit, skip}) {
return data;
}
function assembleData(childCollectionNode, result, {fieldStorage, strategy}) {
const filters = createSearchFilters(result, fieldStorage, strategy, false);
return sift(filters, childCollectionNode.results);
}