Skip to content

Commit 6623b48

Browse files
committed
Changes Model.aggregate() to support multiple orders when calling .order() (#207)
1 parent 7efe5e8 commit 6623b48

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

lib/AggregateFunctions.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var Utilities = require("./Utilities");
2+
13
module.exports = AggregateFunctions;
24

35
function AggregateFunctions(opts) {
@@ -43,8 +45,8 @@ function AggregateFunctions(opts) {
4345
}
4446
return this;
4547
},
46-
order: function (property, order) {
47-
opts.order = [ property, order ];
48+
order: function () {
49+
opts.order = Utilities.standardizeOrder(Array.prototype.slice.apply(arguments));
4850
return this;
4951
},
5052
select: function () {
@@ -79,9 +81,10 @@ function AggregateFunctions(opts) {
7981
}
8082

8183
var query = opts.driver.getQuery().select().from(opts.table).select(opts.properties);
84+
var i, j;
8285

83-
for (var i = 0; i < aggregates.length; i++) {
84-
for (var j = 0; j < aggregates[i].length; j++) {
86+
for (i = 0; i < aggregates.length; i++) {
87+
for (j = 0; j < aggregates[i].length; j++) {
8588
query[aggregates[i][j].f](aggregates[i][j].a, aggregates[i][j].alias);
8689
}
8790
}
@@ -93,17 +96,21 @@ function AggregateFunctions(opts) {
9396
}
9497

9598
if (opts.order) {
96-
query.order.apply(query, opts.order);
99+
for (i = 0; i < opts.order.length; i++) {
100+
query.order(opts.order[i][0], opts.order[i][1]);
101+
}
97102
}
98103
if (opts.limit) {
99104
query.offset(opts.limit[0]).limit(opts.limit[1]);
100105
}
101106

107+
query = query.build();
108+
102109
if (opts.driver.opts && opts.driver.opts.debug) {
103-
require("./Debug").sql(opts.driver_name, query.build());
110+
require("./Debug").sql(opts.driver_name, query);
104111
}
105112

106-
opts.driver.execQuery(query.build(), function (err, data) {
113+
opts.driver.execQuery(query, function (err, data) {
107114
if (err) {
108115
return cb(err);
109116
}

test/integration2/model-aggregate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ describe("Model.aggregate()", function() {
150150
before(setup());
151151

152152
it("should order items", function (done) {
153-
Person.aggregate().count().groupBy('name').order('count', 'Z').get(function (err, rows) {
153+
Person.aggregate().count().groupBy('name').order('-count').get(function (err, rows) {
154154
should.equal(err, null);
155155

156156
rows.should.be.a("object");

0 commit comments

Comments
 (0)