added count method + added tests + updated restrictedFields to throw propper error

This commit is contained in:
Theodor Diaconu 2016-09-26 10:01:29 +03:00
parent 0dc2dff223
commit 8cff81e0ff
8 changed files with 61 additions and 4 deletions

View file

@ -79,6 +79,12 @@ export default class Exposure {
this.userId
);
}
});
Meteor.methods({
[this.name + '.count'](body) {
return collection.find(body.$filters || {}, {}, this.userId).count();
}
})
}

View file

@ -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 {

View file

@ -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();
})
})
});

View file

@ -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 {*}

View file

@ -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;

View file

@ -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;
}

View file

@ -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);

View file

@ -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.