grapher/lib/query/nodes/collectionNode.js

175 lines
4.5 KiB
JavaScript
Raw Normal View History

2016-09-14 16:04:08 +03:00
import FieldNode from './fieldNode.js';
import ReducerNode from './reducerNode.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-11-18 18:33:21 +02:00
this.scheduledForDeletion = false;
this.reducers = [];
this.results = [];
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);
}
get reducerNodes() {
return _.filter(this.nodes, n => n instanceof ReducerNode);
}
2016-09-14 16:04:08 +03:00
/**
* @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
if (linker) {
node.linker = linker;
node.linkStorageField = linker.linkStorageField;
node.isMeta = linker.isMeta();
node.isVirtual = linker.isVirtual();
node.isOneResult = linker.isOneResult();
2016-11-30 13:00:27 +02:00
// we clean the storage in 2 scenarios, if it is virtual then we need t o
node.shouldCleanStorage = (node.isVirtual)
2016-11-30 13:00:27 +02:00
? !node.hasField(node.linkStorageField)
: !this.hasField(node.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) {
2016-11-18 18:33:21 +02:00
this.nodes = _.filter(this.nodes, node => _node !== node);
2016-09-28 18:30:12 +03:00
}
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) => {
2017-01-04 11:29:03 +02:00
// special handling for the $meta filter and conditional operators
if (!_.contains(['$or', '$nor', '$not', '$and', '$meta'], field)) {
// if the field or the parent of the field already exists, don't add it
2017-06-08 11:16:19 +02:00
if(!_.has(options.fields,field.split('.')[0])){
hasAddedAnyField = true;
options.fields[field] = 1;
}
}
2016-09-16 19:22:12 +03:00
});
if (!hasAddedAnyField) {
options.fields = {_id: 1};
}
}
/**
* @param fieldName
* @returns {boolean}
*/
hasField(fieldName) {
return !!_.find(this.fieldNodes, fieldNode => {
return fieldNode.name == fieldName
})
}
2016-11-18 18:33:21 +02:00
/**
* @param fieldName
* @returns {FieldNode}
*/
getField(fieldName) {
return _.find(this.fieldNodes, fieldNode => {
return fieldNode.name == fieldName
})
}
/**
* @param name
* @returns {boolean}
*/
hasCollectionNode(name) {
return !!_.find(this.collectionNodes, node => {
return node.linkName == name
})
}
/**
* @param name
* @returns {boolean}
*/
hasReducerNode(name) {
return !!_.find(this.reducerNodes, node => {
return node.name == name
})
}
/**
* @param name
* @returns {ReducerNode}
*/
getReducerNode(name) {
return _.find(this.reducerNodes, node => {
return node.name == name
})
}
2016-11-18 18:33:21 +02:00
/**
* @param name
* @returns {CollectionNode}
*/
getCollectionNode(name) {
return _.find(this.collectionNodes, node => {
return node.linkName == name
})
}
/**
* @returns {*}
*/
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
}