grapher/lib/query/nodes/collectionNode.js

112 lines
2.9 KiB
JavaScript
Raw Normal View History

2016-09-14 16:04:08 +03:00
import FieldNode from './fieldNode.js';
import deepClone from '../lib/deepClone';
2016-09-14 16:04:08 +03:00
export default class CollectionNode {
constructor(collection, body = {}, linkName = null) {
if (collection && !_.isObject(body)) {
throw new Meteor.Error('invalid-body', 'Every collection link should have its body defined as an object.');
}
this.body = deepClone(body);
2016-09-14 16:04:08 +03:00
this.linkName = linkName;
2016-09-22 12:01:47 +03:00
this.collection = collection;
2016-09-28 18:30:12 +03:00
this.nodes = [];
2016-09-14 16:04:08 +03:00
this.props = {};
this.parent = null;
2016-09-21 18:33:50 +03:00
this.linker = null;
this.linkStorageField = null;
2016-09-14 16:04:08 +03:00
}
get collectionNodes() {
return _.filter(this.nodes, n => n instanceof CollectionNode)
}
get fieldNodes() {
return _.filter(this.nodes, n => n instanceof FieldNode);
}
/**
* @param node
2016-09-21 18:33:50 +03:00
* @param linker
2016-09-14 16:04:08 +03:00
*/
2016-09-21 18:33:50 +03:00
add(node, linker) {
2016-09-14 16:04:08 +03:00
node.parent = this;
2016-09-21 18:33:50 +03:00
node.linker = linker;
if (linker) {
node.linkStorageField = linker.linkStorageField;
node.isMeta = linker.isMeta();
node.isVirtual = linker.isVirtual();
node.isOneResult = linker.isOneResult();
node.shouldCleanStorage = (node.isVirtual)
? !this.hasField(this.linkStorageField)
: !node.parent.hasField(this.linkStorageField)
;
}
2016-09-14 16:04:08 +03:00
this.nodes.push(node);
}
2016-09-28 18:30:12 +03:00
/**
* @param _node
*/
remove(_node) {
this.nodes = _.filter(this.nodes, node => _node !== node);
}
2016-09-14 16:04:08 +03:00
/**
2016-09-16 19:22:12 +03:00
* @param filters
2016-09-14 16:04:08 +03:00
* @param options
*/
2016-09-16 19:22:12 +03:00
applyFields(filters, options) {
let hasAddedAnyField = false;
2016-09-14 16:04:08 +03:00
2016-09-16 19:22:12 +03:00
_.each(this.fieldNodes, n => {
hasAddedAnyField = true;
n.applyFields(options.fields)
});
// it will only get here if it has collectionNodes children
_.each(this.collectionNodes, (collectionNode) => {
let linker = collectionNode.linker;
2016-09-21 18:33:50 +03:00
if (linker && !linker.isVirtual()) {
options.fields[linker.linkStorageField] = 1;
2016-09-21 18:33:50 +03:00
hasAddedAnyField = true;
}
2016-09-16 19:22:12 +03:00
});
// if he selected filters, we should automatically add those fields
_.each(filters, (value, field) => {
// special handling for the $meta filter
if (field === '$meta') {
return;
}
2016-09-16 19:22:12 +03:00
hasAddedAnyField = true;
options.fields[field] = 1;
});
if (!hasAddedAnyField) {
options.fields = {_id: 1};
}
}
/**
* @param fieldName
* @returns {boolean}
*/
hasField(fieldName) {
return !!_.find(this.fieldNodes, fieldNode => {
return fieldNode.name == fieldName
})
}
2016-09-28 18:30:12 +03:00
getName() {
return this.linkName
? this.linkName
: (this.collection ? this.collection._name : 'N/A');
}
2016-09-14 16:04:08 +03:00
}