2016-09-14 16:04:08 +03:00
|
|
|
import createGraph from './lib/createGraph.js';
|
|
|
|
import recursiveFetch from './lib/recursiveFetch.js';
|
|
|
|
import applyFilterFunction from './lib/applyFilterFunction.js';
|
2016-09-21 18:33:50 +03:00
|
|
|
import hypernova from './hypernova/hypernova.js';
|
2016-09-14 16:04:08 +03:00
|
|
|
|
|
|
|
export default class Query {
|
|
|
|
constructor(collection, body, params = {}) {
|
|
|
|
this.collection = collection;
|
|
|
|
this.body = body;
|
|
|
|
this.subscriptionHandle = null;
|
|
|
|
this._params = params;
|
|
|
|
}
|
|
|
|
|
|
|
|
get name() {
|
|
|
|
return `exposure_${this.collection._name}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
get params() {
|
|
|
|
return this._params;
|
|
|
|
}
|
|
|
|
|
|
|
|
get props() {
|
|
|
|
const body = applyFilterFunction(this.body, this.params);
|
|
|
|
return {
|
|
|
|
filters: body.$filters,
|
|
|
|
options: body.$options
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
subscribe() {
|
|
|
|
if (!Meteor.isClient) {
|
|
|
|
throw new Meteor.Error('not-allowed', 'You cannot subscribe from server');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.subscriptionHandle = Meteor.subscribe(
|
|
|
|
this.name,
|
|
|
|
applyFilterFunction(this.body, this.params)
|
|
|
|
);
|
|
|
|
|
|
|
|
return this.subscriptionHandle;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsubscribe() {
|
|
|
|
if (!Meteor.isClient) {
|
|
|
|
throw new Meteor.Error('not-allowed', 'You cannot subscribe from server');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.subscriptionHandle) {
|
|
|
|
this.subscriptionHandle.stop();
|
|
|
|
}
|
2016-09-16 19:22:12 +03:00
|
|
|
|
|
|
|
this.subscriptionHandle = null;
|
2016-09-14 16:04:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fetch(callback) {
|
|
|
|
if (Meteor.isClient && !callback && !this.subscriptionHandle) {
|
|
|
|
throw new Meteor.Error('not-allowed', 'You are on client so you must either provide a callback to get the data or subscribe first.');
|
|
|
|
}
|
|
|
|
|
2016-09-19 12:22:20 +03:00
|
|
|
if (Meteor.isServer && callback && _.isFunction(callback)) {
|
2016-09-14 16:04:08 +03:00
|
|
|
throw new Meteor.Error('not-allowed', 'You are on server, fetching is done directly so no need for callback.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Meteor.isClient) {
|
|
|
|
// do method call with callback
|
|
|
|
if (!this.subscriptionHandle) {
|
|
|
|
Meteor.call(this.name, applyFilterFunction(this.body, this.params), callback);
|
|
|
|
} else {
|
|
|
|
const node = createGraph(
|
|
|
|
this.collection,
|
|
|
|
applyFilterFunction(this.body, this.params)
|
|
|
|
);
|
|
|
|
|
|
|
|
return recursiveFetch(node);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// fetch directly, he is from server
|
2016-09-19 12:22:20 +03:00
|
|
|
const options = _.isObject(callback) ? callback : {};
|
2016-09-14 16:04:08 +03:00
|
|
|
const node = createGraph(
|
|
|
|
this.collection,
|
|
|
|
applyFilterFunction(this.body, this.params)
|
|
|
|
);
|
|
|
|
|
2016-09-22 13:46:27 +03:00
|
|
|
return hypernova(node, options.userId);
|
2016-09-14 16:04:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchOne(...args) {
|
|
|
|
return _.first(this.fetch(...args));
|
|
|
|
}
|
|
|
|
|
|
|
|
setParams(data) {
|
|
|
|
_.extend(this._params, data);
|
2016-09-19 14:27:38 +03:00
|
|
|
|
|
|
|
return this;
|
2016-09-14 16:04:08 +03:00
|
|
|
}
|
|
|
|
}
|