Skip to content

Commit

Permalink
Adds support for descending ordering using "-property" (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
dresende committed Apr 15, 2013
1 parent e980ea5 commit 792279b
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 5 deletions.
5 changes: 4 additions & 1 deletion lib/ChainFind.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ function ChainFind(opts) {
return this;
},
order: function (property, order) {
opts.order = [ property, order ];
if (!Array.isArray(opts.order)) {
opts.order = [];
}
opts.order.push([ property, (order && order.toUpperCase() == "Z" ? "Z" : "A") ]);
return this;
},
count: function (cb) {
Expand Down
4 changes: 3 additions & 1 deletion lib/Drivers/DML/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ Driver.prototype.find = function (fields, table, conditions, opts, cb) {
q.limit('18446744073709551615');
}
if (opts.order) {
q.order(opts.order[0], opts.order[1]);
for (var i = 0; i < opts.order.length; i++) {
q.order(opts.order[i][0], opts.order[i][1]);
}
}

if (opts.merge) {
Expand Down
4 changes: 3 additions & 1 deletion lib/Drivers/DML/postgres.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ Driver.prototype.find = function (fields, table, conditions, opts, cb) {
q.limit(opts.limit);
}
if (opts.order) {
q.order(opts.order[0], opts.order[1]);
for (var i = 0; i < opts.order.length; i++) {
q.order(opts.order[i][0], opts.order[i][1]);
}
}

if (opts.merge) {
Expand Down
4 changes: 3 additions & 1 deletion lib/Drivers/DML/postgresaxomic.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ Driver.prototype.find = function (fields, table, conditions, opts, cb) {
q.limit(opts.limit);
}
if (opts.order) {
q.order(opts.order[0], opts.order[1]);
for (var i = 0; i < opts.order.length; i++) {
q.order(opts.order[i][0], opts.order[i][1]);
}
}

if (opts.merge) {
Expand Down
4 changes: 3 additions & 1 deletion lib/Drivers/DML/sqlite.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ Driver.prototype.find = function (fields, table, conditions, opts, cb) {
q.limit('9223372036854775807');
}
if (opts.order) {
q.order(opts.order[0], opts.order[1]);
for (var i = 0; i < opts.order.length; i++) {
q.order(opts.order[i][0], opts.order[i][1]);
}
}

if (opts.merge) {
Expand Down
5 changes: 5 additions & 0 deletions lib/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var OneAssociation = require("./Associations/One");
var ManyAssociation = require("./Associations/Many");
var ChainFind = require("./ChainFind");
var LazyLoad = require("./LazyLoad");
var Utilities = require("./Utilities");

exports.Model = Model;

Expand Down Expand Up @@ -224,6 +225,10 @@ function Model(opts) {
options.cache = opts.cache;
}

if (order) {
order = Utilities.standardizeOrder(order);
}

var chain = new ChainFind({
only : options.only || model_fields,
id : opts.id,
Expand Down
46 changes: 46 additions & 0 deletions lib/Utilities.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Order should be a String (with the property name assumed ascending)
* or an Array or property String names.
*
* Examples:
*
* 1. 'property1' (ORDER BY property1 ASC)
* 2. '-property1' (ORDER BY property1 DESC)
* 3. [ 'property1' ] (ORDER BY property1 ASC)
* 4. [ '-property1' ] (ORDER BY property1 DESC)
* 5. [ 'property1', 'A' ] (ORDER BY property1 ASC)
* 6. [ 'property1', 'Z' ] (ORDER BY property1 DESC)
* 7. [ '-property1', 'A' ] (ORDER BY property1 ASC)
* 8. [ 'property1', 'property2' ] (ORDER BY property1 ASC, property2 ASC)
* 9. [ 'property1', '-property2' ] (ORDER BY property1 ASC, property2 DESC)
* ...
*/

exports.standardizeOrder = function (order) {
if (typeof order == "string") {
if (order[0] == "-") {
return [ [ order.substr(1), "Z" ] ];
}
return [ [ order, "A" ] ];
}

var new_order = [], minus;

for (var i = 0; i < order.length; i++) {
minus = (order[i][0] == "-");

if (i < order.length - 1 && [ "A", "Z" ].indexOf(order[i + 1].toUpperCase()) >= 0) {
new_order.push([
(minus ? order[i].substr(1) : order[i]),
order[i + 1]
]);
i += 1;
} else if (minus) {
new_order.push([ order[i].substr(1), "Z" ]);
} else {
new_order.push([ order[i], "A" ]);
}
}

return new_order;
};

0 comments on commit 792279b

Please sign in to comment.