2017-11-26 23:02:26 +02:00
|
|
|
import deepClone from 'lodash.clonedeep';
|
2017-11-28 17:38:51 +02:00
|
|
|
import {check} from 'meteor/check';
|
2016-10-14 10:57:26 +03:00
|
|
|
|
2017-02-17 15:29:54 +02:00
|
|
|
export default class QueryBase {
|
2017-11-30 22:11:25 +02:00
|
|
|
isGlobalQuery = true;
|
|
|
|
|
2017-11-28 17:38:51 +02:00
|
|
|
constructor(collection, body, options = {}) {
|
2016-10-14 10:57:26 +03:00
|
|
|
this.collection = collection;
|
2016-10-19 15:22:50 +03:00
|
|
|
|
|
|
|
this.body = deepClone(body);
|
|
|
|
|
2017-11-28 17:38:51 +02:00
|
|
|
this.params = options.params || {};
|
|
|
|
this.options = options;
|
2016-10-14 10:57:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
clone(newParams) {
|
2017-11-28 17:38:51 +02:00
|
|
|
const params = _.extend({}, deepClone(this.params), newParams);
|
|
|
|
|
2016-10-14 10:57:26 +03:00
|
|
|
return new this.constructor(
|
|
|
|
this.collection,
|
|
|
|
deepClone(this.body),
|
2017-11-28 17:38:51 +02:00
|
|
|
{
|
|
|
|
params,
|
|
|
|
...this.options
|
|
|
|
}
|
2016-10-14 10:57:26 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
get name() {
|
|
|
|
return `exposure_${this.collection._name}`;
|
|
|
|
}
|
|
|
|
|
2017-11-28 17:38:51 +02:00
|
|
|
/**
|
|
|
|
* Validates the parameters
|
|
|
|
*/
|
|
|
|
doValidateParams() {
|
|
|
|
const {validateParams} = this.options;
|
|
|
|
if (!validateParams) return;
|
|
|
|
|
|
|
|
if (_.isFunction(validateParams)) {
|
|
|
|
validateParams.call(null, this.params)
|
|
|
|
} else {
|
|
|
|
check(this.params)
|
|
|
|
}
|
2016-10-14 10:57:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Merges the params with previous params.
|
|
|
|
*
|
2017-11-28 17:38:51 +02:00
|
|
|
* @param params
|
2016-10-14 10:57:26 +03:00
|
|
|
* @returns {Query}
|
|
|
|
*/
|
2017-11-28 17:38:51 +02:00
|
|
|
setParams(params) {
|
|
|
|
this.params = _.extend({}, this.params, params);
|
2016-10-14 10:57:26 +03:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
2017-03-02 10:43:47 +02:00
|
|
|
}
|