Skip to content

Commit

Permalink
Modified createQuery to accept options and implemented resolver queries
Browse files Browse the repository at this point in the history
  • Loading branch information
theodorDiaconu committed Nov 28, 2017
1 parent 9cc0834 commit 4802724
Show file tree
Hide file tree
Showing 39 changed files with 662 additions and 370 deletions.
5 changes: 5 additions & 0 deletions .npm/package/npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 67 additions & 0 deletions lib/createQuery.js
@@ -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);
}
109 changes: 0 additions & 109 deletions lib/documentor/index.js

This file was deleted.

4 changes: 3 additions & 1 deletion lib/exposure/exposure.config.schema.js
Expand Up @@ -8,7 +8,9 @@ export const ExposureDefaults = {
};

export const ExposureSchema = {
firewall: Match.Maybe(Function),
firewall: Match.Maybe(
Match.OneOf(Function, [Function])
),
maxLimit: Match.Maybe(Match.Integer),
maxDepth: Match.Maybe(Match.Integer),
publication: Match.Maybe(Boolean),
Expand Down
22 changes: 19 additions & 3 deletions lib/exposure/exposure.js
Expand Up @@ -215,9 +215,7 @@ export default class Exposure {

collection.firewall = (filters, options, userId) => {
if (userId !== undefined) {
if (firewall) {
firewall.call({collection: collection}, filters, options, userId);
}
this._callFirewall({collection: collection}, filters, options, userId);

enforceMaxLimit(options, maxLimit);

Expand Down Expand Up @@ -257,4 +255,22 @@ export default class Exposure {
return findOne(filters, options);
}
}

/**
* @private
*/
_callFirewall(...args) {
const {firewall} = this.config;
if (!firewall) {
return;
}

if (_.isArray(firewall)) {
firewall.forEach(fire => {
fire.call(...args);
})
} else {
firewall.call(...args);
}
}
};
20 changes: 20 additions & 0 deletions lib/extension.js
@@ -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);
}
}
});
4 changes: 2 additions & 2 deletions lib/links/config.schema.js
@@ -1,7 +1,7 @@
import {Match} from 'meteor/check';
import {Mongo} from 'meteor/mongo';

export const CacheSchema = {
export const DenormalizeSchema = {
field: String,
body: Object,
bypassSchema: Match.Maybe(Boolean)
Expand All @@ -23,5 +23,5 @@ export const LinkConfigSchema = {
index: Match.Maybe(Boolean),
unique: Match.Maybe(Boolean),
autoremove: Match.Maybe(Boolean),
cache: Match.Maybe(Match.ObjectIncluding(CacheSchema)),
denormalize: Match.Maybe(Match.ObjectIncluding(DenormalizeSchema)),
};
3 changes: 3 additions & 0 deletions lib/links/constants.js
@@ -0,0 +1,3 @@
export default {
LINK_STORAGE: '__links'
};
22 changes: 11 additions & 11 deletions lib/links/extension.js
@@ -1,49 +1,49 @@
import { Mongo } from 'meteor/mongo';
import { linkStorage } from './symbols.js';
import {LINK_STORAGE} from './constants.js';
import Linker from './linker.js';

_.extend(Mongo.Collection.prototype, {
/**
* The data we add should be valid for config.schema.js
*/
addLinks(data) {
if (!this[linkStorage]) {
this[linkStorage] = {};
if (!this[LINK_STORAGE]) {
this[LINK_STORAGE] = {};
}

_.each(data, (linkConfig, linkName) => {
if (this[linkStorage][linkName]) {
if (this[LINK_STORAGE][linkName]) {
throw new Meteor.Error(`You cannot add the link with name: ${linkName} because it was already added to ${this._name} collection`)
}

const linker = new Linker(this, linkName, linkConfig);

_.extend(this[linkStorage], {
_.extend(this[LINK_STORAGE], {
[linkName]: linker
});
});
},

getLinks() {
return this[linkStorage];
return this[LINK_STORAGE];
},

getLinker(name) {
if (this[linkStorage]) {
return this[linkStorage][name];
if (this[LINK_STORAGE]) {
return this[LINK_STORAGE][name];
}
},

hasLink(name) {
if (!this[linkStorage]) {
if (!this[LINK_STORAGE]) {
return false;
}

return !!this[linkStorage][name];
return !!this[LINK_STORAGE][name];
},

getLink(objectOrId, name) {
let linkData = this[linkStorage];
let linkData = this[LINK_STORAGE];

if (!linkData) {
throw new Meteor.Error(`There are no links defined for collection: ${this._name}`);
Expand Down

0 comments on commit 4802724

Please sign in to comment.