Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: provide support for groupby #2175

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions lib/connectors/memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -496,13 +496,53 @@ Memory.prototype._findAllSkippingIncludes = function(model, filter) {

// field selection
if (filter.fields) {
if (filter.count) filter.fields.push('count');
if (filter.max) filter.fields.push('max');
if (filter.min) filter.fields.push('min');
if (filter.sum) filter.fields.push('sum');
if (filter.avg) filter.fields.push('avg');
nodes = nodes.map(utils.selectFields(filter.fields));
}

// limit/skip
const skip = filter.skip || filter.offset || 0;
const limit = filter.limit || nodes.length;
// groupBy
nodes = nodes.slice(skip, skip + limit);
if (filter.groupBy) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will require support for connector too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. And it's implemented here.

nodes = utils.groupBy(nodes, filter.groupBy);
const tempNodes = [];
Object.keys(nodes).forEach(nodeKey => {
let count = undefined;
const tempNode = {...nodes[nodeKey][0]};
if (filter.count) {
count = nodes[nodeKey].filter((obj) => {
const id = obj[filter.count];
return obj[filter.count] === id;
}).length;
tempNode.count = count;
}
if (filter.max) {
tempNode.max = Math.max(...nodes[nodeKey].map(o => o[filter.max]));
}
if (filter.min) {
tempNode.min = Math.min(...nodes[nodeKey].map(o => o[filter.min]));
}
if (filter.sum) {
tempNode.sum = nodes[nodeKey].reduce((accumulator, object) => {
return accumulator + object[filter.sum];
}, 0);
}
if (filter.avg) {
tempNode.avg = nodes[nodeKey].reduce((accumulator, object) => {
return accumulator + object[filter.avg];
}, 0);
tempNode.avg = tempNode.avg / nodes[nodeKey].length;
}
tempNodes.push(tempNode);
});
nodes = tempNodes;
}
}
return nodes;

Expand Down
12 changes: 12 additions & 0 deletions lib/dao.js
Original file line number Diff line number Diff line change
Expand Up @@ -1930,6 +1930,18 @@ DataAccessObject.find = function find(query, options, cb) {
}
}

const keys = Object.keys(data);
keys.forEach(key => {
if (
key.includes('sumOf') ||
key.includes('countOf') ||
key.includes('avgOf') ||
key.includes('minOf') ||
key.includes('maxOf')
) {
obj.__data[key] = data[key];
}
});
callback(null, obj);
}

Expand Down
14 changes: 14 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ exports.idsHaveDuplicates = idsHaveDuplicates;
exports.isClass = isClass;
exports.escapeRegExp = escapeRegExp;
exports.applyParentProperty = applyParentProperty;
exports.groupBy = groupBy;

const g = require('strong-globalize')();
const traverse = require('neotraverse/legacy');
Expand Down Expand Up @@ -893,3 +894,16 @@ function applyParentProperty(element, parent) {
});
}
}

function groupBy(items, key) {
return items.reduce(
(result, item) => ({
...result,
[item[key]]: [
...(result[item[key]] || []),
item,
],
}),
{},
);
}
50 changes: 50 additions & 0 deletions test/crud-with-options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,56 @@ describe('crud-with-options', function() {
User.find({limit: 3});
});

it('should allow filter with groupBy, count, max, min, sum & avg', function(done) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add test cases for when there are other filter queries too and limit and offset is also provided ? Basically to confirm whether this feature works with other features together or not. And whether it has any impact there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @samarpanB. I have added one more test to verify the change with existing filters.

User.find({
groupBy: ['vip'],
count: 'vip',
max: 'id',
min: 'id',
sum: 'id',
avg: 'id',
}, options, function(err, users) {
should.not.exist(err);
should.exist(users);
users.length.should.be.above(0);
users.forEach(user => {
user.should.have.property('count', user.count);
user.should.have.property('max');
user.should.have.property('min');
user.should.have.property('sum');
user.should.have.property('avg');
});
done();
});
});

it('should allow filter with groupBy, aggregate methods and other filters', function(done) {
User.find({
groupBy: ['vip'],
count: 'vip',
max: 'id',
min: 'id',
sum: 'id',
avg: 'id',
limit: 1,
fields: ['name', 'id'],
}, options, function(err, users) {
should.not.exist(err);
should.exist(users);
users.length.should.be.equal(1);
users.forEach(user => {
user.should.have.property('count', user.count);
user.should.have.property('max');
user.should.have.property('min');
user.should.have.property('sum');
user.should.have.property('avg');
user.should.have.property('name');
user.should.have.property('id');
});
done();
});
});

it('should skip trailing undefined args', function(done) {
User.find({limit: 3}, function(err, users) {
should.exists(users);
Expand Down
Loading