2016-09-16 19:22:12 +03:00
|
|
|
import Query from './query.js';
|
2016-10-07 10:31:58 +03:00
|
|
|
import NamedQueryStore from '../namedQuery/store.js';
|
2016-09-16 19:22:12 +03:00
|
|
|
|
2016-09-28 18:30:12 +03:00
|
|
|
export default (data, ...args) => {
|
2016-09-16 19:22:12 +03:00
|
|
|
if (_.keys(data).length > 1) {
|
|
|
|
throw new Meteor.Error('invalid-query', 'When using createQuery you should only have one main root point.')
|
|
|
|
}
|
|
|
|
|
2016-10-07 10:31:58 +03:00
|
|
|
const entryPointName = _.first(_.keys(data));
|
2016-09-16 19:22:12 +03:00
|
|
|
|
2016-10-07 10:31:58 +03:00
|
|
|
const collection = Mongo.Collection.get(entryPointName);
|
2016-09-16 19:22:12 +03:00
|
|
|
|
|
|
|
if (!collection) {
|
2016-10-07 10:31:58 +03:00
|
|
|
const namedQuery = NamedQueryStore.get(entryPointName);
|
|
|
|
|
|
|
|
if (!namedQuery) {
|
|
|
|
throw new Meteor.Error('entry-point-not-found', `We could not find any collection or frozen-query with the name ${entryPointName}`)
|
|
|
|
} else {
|
|
|
|
return namedQuery.clone(data[entryPointName], ...args);
|
|
|
|
}
|
2016-09-16 19:22:12 +03:00
|
|
|
}
|
|
|
|
|
2016-10-07 10:31:58 +03:00
|
|
|
return new Query(collection, data[entryPointName], ...args);
|
2016-09-16 19:22:12 +03:00
|
|
|
}
|