From 8cff81e0ff540cbe7aa142fba9e9ea66edbecb85 Mon Sep 17 00:00:00 2001 From: Theodor Diaconu Date: Mon, 26 Sep 2016 10:01:29 +0300 Subject: [PATCH] added count method + added tests + updated restrictedFields to throw propper error --- lib/exposure/exposure.js | 6 ++++++ lib/exposure/lib/restrictFields.js | 4 ++++ lib/exposure/testing/client.js | 22 ++++++++++++++++++++++ lib/links/linker.js | 11 +++++++++++ lib/query/hypernova/assembler.js | 2 +- lib/query/lib/recursiveFetch.js | 10 ++++++++-- lib/query/query.js | 8 ++++++++ package.js | 2 +- 8 files changed, 61 insertions(+), 4 deletions(-) diff --git a/lib/exposure/exposure.js b/lib/exposure/exposure.js index a150dea..494f259 100644 --- a/lib/exposure/exposure.js +++ b/lib/exposure/exposure.js @@ -79,6 +79,12 @@ export default class Exposure { this.userId ); } + }); + + Meteor.methods({ + [this.name + '.count'](body) { + return collection.find(body.$filters || {}, {}, this.userId).count(); + } }) } diff --git a/lib/exposure/lib/restrictFields.js b/lib/exposure/lib/restrictFields.js index e64ea17..fd36708 100644 --- a/lib/exposure/lib/restrictFields.js +++ b/lib/exposure/lib/restrictFields.js @@ -7,6 +7,10 @@ * @param restrictedFields */ export default (filters, options, restrictedFields) => { + if (!_.isArray(restrictedFields)) { + throw new Meteor.Error('Please specify an array of restricted fields.'); + } + if (options.fields) { options.fields = _.omit(options.fields, ...restrictedFields); } else { diff --git a/lib/exposure/testing/client.js b/lib/exposure/testing/client.js index 7d5daef..158612f 100644 --- a/lib/exposure/testing/client.js +++ b/lib/exposure/testing/client.js @@ -31,5 +31,27 @@ describe('Exposure', function () { done(); }); + }); + + it('Should return the correct count', function (done) { + Meteor.call('exposure_exposure_test.count', {}, function (err, res) { + assert.isUndefined(err); + + assert.equal(3, res); + done(); + }) + }); + + it('Should return the correct count via query', function (done) { + const query = Demo.createQuery({ + $options: {limit: 1} + }); + + query.getCount(function (err, res) { + assert.isUndefined(err); + + assert.equal(3, res); + done(); + }) }) }); diff --git a/lib/links/linker.js b/lib/links/linker.js index cccddf1..7db2e50 100644 --- a/lib/links/linker.js +++ b/lib/links/linker.js @@ -135,6 +135,17 @@ export default class Linker { return _.isFunction(this.linkConfig.resolve); } + /** + * Should return a single result. + */ + isOneResult() + { + return ( + (this.isVirtual() && this.linkConfig.relatedLinker.linkConfig.unique) + || (!this.isVirtual() && this.isSingle()) + ); + } + /** * @param object * @returns {*} diff --git a/lib/query/hypernova/assembler.js b/lib/query/hypernova/assembler.js index d9396f9..4edb39c 100644 --- a/lib/query/hypernova/assembler.js +++ b/lib/query/hypernova/assembler.js @@ -9,7 +9,7 @@ export default (childCollectionNode, {limit, skip}) => { const isVirtual = linker.isVirtual(); const isSingle = linker.isSingle(); const removeStorageField = !childCollectionNode.parentHasMyLinkStorageFieldSpecified(); - const oneResult = (isVirtual && linker.linkConfig.relatedLinker.linkConfig.unique) || (!isVirtual) && isSingle; + const oneResult = linker.isOneResult(); const fieldStorage = linker.linkStorageField; diff --git a/lib/query/lib/recursiveFetch.js b/lib/query/lib/recursiveFetch.js index 160a66d..feacd92 100644 --- a/lib/query/lib/recursiveFetch.js +++ b/lib/query/lib/recursiveFetch.js @@ -27,12 +27,18 @@ export default function fetch(node, parentObject, userId) { results = node.collection.find(filters, options, userId).fetch(); } - _.each(results, result => { - _.each(node.collectionNodes, collectionNode => { + _.each(node.collectionNodes, collectionNode => { + _.each(results, result => { result[collectionNode.linkName] = fetch(collectionNode, result, userId); //delete result[node.linker.linkStorageField]; }) }); + if (parentObject) { + if (node.linker.isOneResult()) { + return _.first(results); + } + } + return results; } diff --git a/lib/query/query.js b/lib/query/query.js index a639a39..e8dd2b9 100644 --- a/lib/query/query.js +++ b/lib/query/query.js @@ -85,6 +85,14 @@ export default class Query { } } + getCount(callback) { + if (Meteor.isClient && !callback) { + throw new Meteor.Error('not-allowed', 'You are on client so you must either provide a callback to get the count.'); + } + + return Meteor.call(this.name + '.count', applyFilterFunction(this.body, this.params), callback); + } + setParams(data) { _.extend(this._params, data); diff --git a/package.js b/package.js index c10d81f..f77d9b6 100644 --- a/package.js +++ b/package.js @@ -1,6 +1,6 @@ Package.describe({ name: 'cultofcoders:grapher', - version: '1.1.2', + version: '1.1.3', // Brief, one-line summary of the package. summary: 'Grapher makes linking collections easily. And fetching data as a graph.', // URL to the Git repository containing the source code for this package.