Skip to content

Commit bbc5ebe

Browse files
committed
fix: add tests
Signed-off-by: Muhammad Aaqil <[email protected]>
1 parent 352b803 commit bbc5ebe

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

lib/connectors/memory.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,6 @@ Memory.prototype._findAllSkippingIncludes = function(model, filter) {
457457
let nodes = Object.keys(this.collection(model)).map(function(key) {
458458
return this.fromDb(model, this.collection(model)[key]);
459459
}.bind(this));
460-
461460
if (filter) {
462461
if (!filter.order) {
463462
const idNames = this.idNames(model);
@@ -502,7 +501,41 @@ Memory.prototype._findAllSkippingIncludes = function(model, filter) {
502501
// limit/skip
503502
const skip = filter.skip || filter.offset || 0;
504503
const limit = filter.limit || nodes.length;
504+
// groupBy
505505
nodes = nodes.slice(skip, skip + limit);
506+
if (filter.groupBy) {
507+
nodes = utils.groupBy(nodes, filter.groupBy);
508+
const tempNodes = [];
509+
Object.keys(nodes).forEach(nodeKey => {
510+
let count = undefined;
511+
const tempNode = {...nodes[nodeKey][0]};
512+
if (filter.count) {
513+
count = nodes[nodeKey].filter((obj) => {
514+
const id = obj[filter.count];
515+
return obj[filter.count] === id;
516+
}).length;
517+
tempNode.count = count;
518+
}
519+
if (filter.max) {
520+
tempNode.max = Math.max(...nodes[nodeKey].map(o => o[filter.max]));
521+
}
522+
if (filter.min) {
523+
tempNode.min = Math.min(...nodes[nodeKey].map(o => o[filter.min]));
524+
}
525+
if (filter.sum) {
526+
tempNode.sum = nodes[nodeKey].reduce((accumulator, object) => {
527+
return accumulator + object[filter.sum];
528+
}, 0);
529+
}
530+
if (filter.avg) {
531+
tempNode.avg = nodes[nodeKey].reduce((accumulator, object) => {
532+
return accumulator + object[filter.avg];
533+
}, 0);
534+
tempNode.avg = tempNode.avg / nodes[nodeKey].length;
535+
}
536+
});
537+
nodes = tempNodes;
538+
}
506539
}
507540
return nodes;
508541

@@ -529,7 +562,6 @@ Memory.prototype._findAllSkippingIncludes = function(model, filter) {
529562
Memory.prototype.all = function all(model, filter, options, callback) {
530563
const self = this;
531564
const nodes = self._findAllSkippingIncludes(model, filter);
532-
533565
process.nextTick(function() {
534566
if (filter && filter.include) {
535567
self._models[model].model.include(nodes, filter.include, options, callback);

lib/utils.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ exports.idsHaveDuplicates = idsHaveDuplicates;
3131
exports.isClass = isClass;
3232
exports.escapeRegExp = escapeRegExp;
3333
exports.applyParentProperty = applyParentProperty;
34+
exports.groupBy = groupBy;
3435

3536
const g = require('strong-globalize')();
3637
const traverse = require('traverse');
@@ -893,3 +894,16 @@ function applyParentProperty(element, parent) {
893894
});
894895
}
895896
}
897+
898+
function groupBy(items, key) {
899+
return items.reduce(
900+
(result, item) => ({
901+
...result,
902+
[item[key]]: [
903+
...(result[item[key]] || []),
904+
item,
905+
],
906+
}),
907+
{},
908+
);
909+
}

test/crud-with-options.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,28 @@ describe('crud-with-options', function() {
272272
User.find({limit: 3});
273273
});
274274

275+
it('should allow filter with groupBy, count, max, min, sum & avg', function(done) {
276+
User.find({
277+
groupBy: ['vip'],
278+
count: 'vip',
279+
max: 'id',
280+
min: 'id',
281+
sum: 'id',
282+
avg: 'id',
283+
}, options, function(err, users) {
284+
should.not.exist(err);
285+
should.exist(users);
286+
users.forEach(user => {
287+
user.should.have.property('count', user.count);
288+
user.should.have.property('max');
289+
user.should.have.property('min');
290+
user.should.have.property('sum');
291+
user.should.have.property('avg');
292+
});
293+
done();
294+
});
295+
});
296+
275297
it('should skip trailing undefined args', function(done) {
276298
User.find({limit: 3}, function(err, users) {
277299
should.exists(users);

0 commit comments

Comments
 (0)