Skip to content

Commit

Permalink
Fix sort order for Mongoose 3
Browse files Browse the repository at this point in the history
  • Loading branch information
biggora committed Feb 20, 2013
1 parent 7a83365 commit 4dc53c7
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions lib/adapters/mongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var safeRequire = require('../utils').safeRequire;
var mongoose = safeRequire('mongoose');

exports.initialize = function initializeSchema(schema, callback) {
console.error('WARN: mongoose adapter is not supported, please use "mongodb" adapter instead');

if (!mongoose) return;

if (!schema.settings.url) {
Expand Down Expand Up @@ -35,7 +35,7 @@ exports.initialize = function initializeSchema(schema, callback) {
} else {
schema.client = mongoose.connectSet(schema.settings.url, {rs_name: schema.settings.rs});
}

schema.adapter = new MongooseAdapter(schema.client);
process.nextTick(callback);
};
Expand Down Expand Up @@ -223,16 +223,23 @@ MongooseAdapter.prototype.all = function all(model, filter, cb) {
if (typeof(keys) == "string") {
keys = keys.split(',');
}
var args = [];

for(index in keys) {
var m = keys[index].match(/\s+(A|DE)SC$/);

keys[index] = keys[index].replace(/\s+(A|DE)SC$/, '');
if (m && m[1] === 'DE') {
query.sort('-' + keys[index].trim());
if(parseInt(mongoose.version.substr(0,1)) >= 3) {
if (m && m[1] === 'DE') {
query.sort('-' + keys[index].trim());
} else {
query.sort(keys[index].trim());
}
} else {
query.sort(keys[index].trim());
if (m && m[1] === 'DE') {
query.desc(keys[index].trim());
} else {
query.asc(keys[index].trim());
}
}
}
}
Expand Down

0 comments on commit 4dc53c7

Please sign in to comment.