2017-01-04 11:05:26 +02:00
|
|
|
import { _ } from 'meteor/underscore';
|
|
|
|
|
2016-09-22 17:06:05 +03:00
|
|
|
export default function (childCollectionNode, filters, options, userId) {
|
|
|
|
const linker = childCollectionNode.linker;
|
|
|
|
const linkStorageField = linker.linkStorageField;
|
|
|
|
const collection = childCollectionNode.collection;
|
|
|
|
|
|
|
|
let pipeline = [];
|
|
|
|
|
|
|
|
if (collection.firewall) {
|
|
|
|
collection.firewall(filters, options, userId);
|
|
|
|
}
|
|
|
|
|
|
|
|
pipeline.push({$match: filters});
|
|
|
|
|
|
|
|
if (options.sort) {
|
|
|
|
pipeline.push({$sort: options.sort})
|
|
|
|
}
|
|
|
|
|
|
|
|
let _id = linkStorageField;
|
|
|
|
if (linker.isMeta()) {
|
|
|
|
_id += '._id';
|
|
|
|
}
|
|
|
|
|
|
|
|
let dataPush = {};
|
|
|
|
_.each(options.fields, (value, field) => {
|
2017-01-04 11:05:26 +02:00
|
|
|
if (!_.contains(['$or', '$nor', '$not', '$and'], field)) {
|
|
|
|
dataPush[field] = '$' + field
|
|
|
|
}
|
2016-09-22 17:06:05 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!dataPush._id) {
|
|
|
|
dataPush['_id'] = '$_id';
|
|
|
|
}
|
|
|
|
|
2016-10-14 10:57:26 +03:00
|
|
|
if (linker.isMeta()) {
|
|
|
|
dataPush[linkStorageField] = '$' + linkStorageField;
|
|
|
|
}
|
|
|
|
|
2016-09-22 17:06:05 +03:00
|
|
|
pipeline.push({
|
|
|
|
$group: {
|
|
|
|
_id: "$" + _id,
|
|
|
|
data: {
|
|
|
|
$push: dataPush
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (options.limit || options.skip) {
|
|
|
|
let $slice = ["$data"];
|
|
|
|
if (options.skip) $slice.push(options.skip);
|
|
|
|
if (options.limit) $slice.push(options.limit);
|
|
|
|
|
|
|
|
pipeline.push({
|
|
|
|
$project: {
|
|
|
|
_id: 1,
|
|
|
|
data: {$slice}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return pipeline;
|
|
|
|
}
|