-
Notifications
You must be signed in to change notification settings - Fork 53
/
createQuery.js
67 lines (55 loc) · 2.34 KB
/
createQuery.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import Query from './query/query.js';
import NamedQuery from './namedQuery/namedQuery.js';
import NamedQueryStore from './namedQuery/store.js';
/**
* This is a polymorphic function, it allows you to create a query as an object
* or it also allows you to re-use an existing query if it's a named one
*
* @param args
* @returns {*}
*/
export default (...args) => {
if (typeof args[0] === 'string') {
let [name, body, options] = args;
options = options || {};
// It's a resolver query
if (_.isFunction(body)) {
return createNamedQuery(name, null, body, options);
}
const entryPointName = _.first(_.keys(body));
const collection = Mongo.Collection.get(entryPointName);
if (!collection) {
throw new Meteor.Error('invalid-name', `We could not find any collection with the name "${entryPointName}". Make sure it is imported prior to using this`)
}
return createNamedQuery(name, collection, body[entryPointName], options);
} else {
// Query Creation, it can have an endpoint as collection or as a NamedQuery
let [body, options] = args;
options = options || {};
const entryPointName = _.first(_.keys(body));
const collection = Mongo.Collection.get(entryPointName);
if (!collection) {
if (Meteor.isDevelopment && !NamedQueryStore.get(entryPointName)) {
console.warn(`You are creating a query with the entry point "${entryPointName}", but there was no collection found for it (maybe you forgot to import it client-side?). It's assumed that it's referencing a NamedQuery.`)
}
return createNamedQuery(entryPointName, null, {}, {params: body[entryPointName]});
} else {
return createNormalQuery(collection, body[entryPointName], options);
}
}
}
function createNamedQuery(name, collection, body, options = {}) {
// if it exists already, we re-use it
const namedQuery = NamedQueryStore.get(name);
let query;
if (!namedQuery) {
query = new NamedQuery(name, collection, body, options);
NamedQueryStore.add(name, query);
} else {
query = namedQuery.clone(options.params);
}
return query;
}
function createNormalQuery(collection, body, options) {
return new Query(collection, body, options);
}