Skip to content

Commit

Permalink
Adds groupBy to aggregate methods (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
dresende committed Apr 8, 2013
1 parent a4cf481 commit 8bf7240
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 7 deletions.
15 changes: 10 additions & 5 deletions lib/AggregateFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ function AggregateFunctions(opts) {
throw new Error("Missing aggregate functions");
}

var query = opts.driver.getQuery().select().from(opts.table);
var query = opts.driver.getQuery().select().from(opts.table).select(opts.properties);

for (var i = 0; i < aggregates.length; i++) {
for (var j = 0; j < aggregates[i].length; j++) {
query[aggregates[i][j].f](aggregates[i][j].a, 'aggregate' + i);
aggregates[i][j].alias = aggregateAlias(aggregates[i][j].f, aggregates[i][j].a);
query[aggregates[i][j].f](aggregates[i][j].a, aggregates[i][j].alias);
}
}

Expand All @@ -55,8 +56,6 @@ function AggregateFunctions(opts) {
query.groupBy.apply(query, group_by);
}

console.log(query.build());

opts.driver.execQuery(query.build(), function (err, data) {
if (err) {
return cb(err);
Expand All @@ -69,7 +68,9 @@ function AggregateFunctions(opts) {
var items = [];

for (var i = 0; i < aggregates.length; i++) {
items.push(data[0]['aggregate' + i] || null);
for (var j = 0; j < aggregates[i].length; j++) {
items.push(data[0][aggregates[i][j].alias] || null);
}
}

items.unshift(null);
Expand All @@ -93,3 +94,7 @@ function addAggregate(proto, fun, builder) {
proto[fun.toLowerCase()] = builder(fun.toLowerCase());
}
}

function aggregateAlias(fun, fields) {
return fun + (fields && fields.length ? "_" + fields.join("_") : "");
}
18 changes: 16 additions & 2 deletions lib/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,11 +295,25 @@ function Model(opts) {
return this;
};

model.aggregate = function (conditions) {
model.aggregate = function () {
var conditions = {};
var properties = [];

for (var i = 0; i < arguments.length; i++) {
if (typeof arguments[i] == "object") {
if (Array.isArray(arguments[i])) {
properties = arguments[i];
} else {
conditions = arguments[i];
}
}
}

return new require("./AggregateFunctions")({
table : opts.table,
driver : opts.driver,
conditions : conditions || {}
conditions : conditions,
properties : properties
});
};

Expand Down
28 changes: 28 additions & 0 deletions test/integration/test-aggregate-groupby.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var common = require('../common');
var assert = require('assert');

common.createConnection(function (err, db) {
common.createModelTable('test_aggregate_groupby', db.driver.db, function () {
common.insertModelData('test_aggregate_groupby', db.driver.db, [
{ id : 2, name : 'test1' },
{ id : 3, name : 'test1' },
{ id : 4, name : 'test1' },
{ id : 5, name : 'test2' }
], function (err) {
if (err) throw err;

var TestModel = db.define('test_aggregate_groupby', common.getModelProperties());

TestModel.aggregate().avg('id').count().groupBy('name').get(function (err, rows) {
assert.equal(err, null);
assert.equal(Array.isArray(rows), true);
assert.equal(rows.length, 2);
assert.equal(rows[0].avg_id, 3);
assert.equal(rows[0].count, 3);
assert.equal(rows[1].avg_id, 5);
assert.equal(rows[1].count, 1);
db.close();
});
});
});
});

0 comments on commit 8bf7240

Please sign in to comment.