Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modified createQuery to accept options and implemented resolver queries
- Loading branch information
1 parent
9cc0834
commit 4802724
Showing
39 changed files
with
662 additions
and
370 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import Query from './query/query.js'; | ||
import NamedQuery from './namedQuery/namedQuery.js'; | ||
import NamedQueryStore from './namedQuery/store.js'; | ||
|
||
_.extend(Mongo.Collection.prototype, { | ||
createQuery(...args) { | ||
if (typeof args[0] === 'string') { | ||
//NamedQuery | ||
const [name, body, options] = args; | ||
const query = new NamedQuery(name, this, body, options); | ||
NamedQueryStore.add(name, query); | ||
|
||
return query; | ||
} else { | ||
const [body, params] = args; | ||
|
||
return new Query(this, body, params); | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default { | ||
LINK_STORAGE: '__links' | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.