2016-10-07 10:31:58 +03:00
|
|
|
import NamedQuery from '../namedQuery.js';
|
|
|
|
import ExposeSchema from './schema.js';
|
|
|
|
import mergeDeep from './lib/mergeDeep.js';
|
|
|
|
import createGraph from '../../query/lib/createGraph.js';
|
|
|
|
import recursiveCompose from '../../query/lib/recursiveCompose.js';
|
|
|
|
import applyFilterFunction from '../../query/lib/applyFilterFunction.js';
|
|
|
|
|
|
|
|
_.extend(NamedQuery.prototype, {
|
|
|
|
expose(config = {}) {
|
|
|
|
if (!Meteor.isServer) {
|
|
|
|
throw new Meteor.Error('invalid-environment', `You must run this in server-side code`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.isExposed) {
|
|
|
|
throw new Meteor.Error('query-already-exposed', `You have already exposed: "${this.name}" named query`);
|
|
|
|
}
|
|
|
|
|
|
|
|
ExposeSchema.clean(config);
|
|
|
|
this.exposeConfig = config;
|
|
|
|
|
|
|
|
if (config.method) {
|
|
|
|
this._initMethod();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config.publication) {
|
|
|
|
this._initPublication();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!config.method && !config.publication) {
|
2016-10-07 13:08:36 +03:00
|
|
|
throw new Meteor.Error('weird', 'If you want to expose your named query you need to specify at least one of ["method", "publication"] options to true')
|
2016-10-07 10:31:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
this._initCountMethod();
|
|
|
|
|
|
|
|
if (config.embody) {
|
|
|
|
this.body = mergeDeep(this.body, config.embody);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.isExposed = true;
|
|
|
|
},
|
|
|
|
|
|
|
|
_initMethod() {
|
|
|
|
const self = this;
|
|
|
|
Meteor.methods({
|
|
|
|
[this.name](newParams) {
|
|
|
|
this.unblock();
|
|
|
|
|
2016-10-07 11:59:18 +03:00
|
|
|
self._validateParams(newParams);
|
|
|
|
|
2016-10-07 10:31:58 +03:00
|
|
|
if (self.exposeConfig.firewall) {
|
|
|
|
self.exposeConfig.firewall.call(this, this.userId, newParams);
|
|
|
|
}
|
|
|
|
|
|
|
|
return self.clone(newParams).fetch();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
_initCountMethod() {
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
Meteor.methods({
|
|
|
|
[this.name + '.count'](newParams) {
|
|
|
|
this.unblock();
|
2016-10-07 11:59:18 +03:00
|
|
|
self._validateParams(newParams);
|
2016-10-07 10:31:58 +03:00
|
|
|
|
|
|
|
return self.clone(newParams).getCount();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
_initPublication() {
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
Meteor.publishComposite(this.name, function (newParams) {
|
2016-10-07 11:59:18 +03:00
|
|
|
self._validateParams(newParams);
|
|
|
|
|
2016-10-07 10:31:58 +03:00
|
|
|
if (self.exposeConfig.firewall) {
|
|
|
|
self.exposeConfig.firewall.call(this, this.userId, newParams);
|
|
|
|
}
|
|
|
|
|
|
|
|
let params = _.extend({}, self.params, newParams);
|
|
|
|
let body = applyFilterFunction(self.body, params);
|
|
|
|
|
|
|
|
const rootNode = createGraph(self.collection, body);
|
|
|
|
|
|
|
|
return recursiveCompose(rootNode);
|
|
|
|
});
|
2016-10-07 11:59:18 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_validateParams(params) {
|
2016-10-07 12:49:02 +03:00
|
|
|
if (params && this.exposeConfig.schema) {
|
2016-10-07 11:59:18 +03:00
|
|
|
(new SimpleSchema(this.exposeConfig.schema)).validate(params);
|
|
|
|
}
|
2016-10-07 10:31:58 +03:00
|
|
|
}
|
|
|
|
});
|