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) {
|
2017-11-24 16:32:42 +02:00
|
|
|
let containsDottedFields = false;
|
2016-09-22 17:06:05 +03:00
|
|
|
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});
|
|
|
|
|
2017-01-04 11:29:03 +02:00
|
|
|
if (options.sort && _.keys(options.sort).length > 0) {
|
2016-09-22 17:06:05 +03:00
|
|
|
pipeline.push({$sort: options.sort})
|
|
|
|
}
|
|
|
|
|
|
|
|
let _id = linkStorageField;
|
|
|
|
if (linker.isMeta()) {
|
|
|
|
_id += '._id';
|
|
|
|
}
|
|
|
|
|
2017-11-24 16:32:42 +02:00
|
|
|
let dataPush = {
|
|
|
|
_id: '$_id'
|
|
|
|
};
|
|
|
|
|
2016-09-22 17:06:05 +03:00
|
|
|
_.each(options.fields, (value, field) => {
|
2017-11-24 16:32:42 +02:00
|
|
|
if (field.indexOf('.') >= 0) {
|
|
|
|
containsDottedFields = true;
|
|
|
|
}
|
|
|
|
const safeField = field.replace('.', '___');
|
|
|
|
dataPush[safeField] = '$' + field
|
2016-09-22 17:06:05 +03:00
|
|
|
});
|
|
|
|
|
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}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-11-24 16:32:42 +02:00
|
|
|
return {pipeline, containsDottedFields};
|
2016-09-22 17:06:05 +03:00
|
|
|
}
|