diff --git a/lib/dao.js b/lib/dao.js index 801de311..fc096aed 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -917,8 +917,13 @@ DataAccessObject.findByIds = function(ids, query, options, cb) { var pk = idName(this); filter.where[pk] = { inq: [].concat(ids) }; mergeQuery(filter, query || {}); + + // to know if the result need to be sorted by ids or not + // this variable need to be initialized before the call to find, because filter is updated during the call with an order + var toSortObjectsByIds = filter.order ? false : true ; + this.find(filter, options, function(err, results) { - cb(err, err ? results : utils.sortObjectsByIds(pk, ids, results)); + cb(err, toSortObjectsByIds ? utils.sortObjectsByIds(pk, ids, results) : results ); }); return cb.promise; }; diff --git a/test/relations.test.js b/test/relations.test.js index 9ad91517..aca1ad59 100644 --- a/test/relations.test.js +++ b/test/relations.test.js @@ -4996,6 +4996,19 @@ describe('relations', function () { }); }); + it('should find items on scope and ordered them by name DESC', function(done) { + Category.find(function(err, categories) { + categories.should.have.length(1); + categories[0].jobs({order: 'name DESC'}, function (err, jobs) { + should.not.exist(err); + jobs.should.have.length(2); + jobs[0].id.should.eql(job3.id) + jobs[1].id.should.eql(job2.id) + done(); + }) + }); + }); + it('should allow custom scope methods - reverse', function(done) { Category.findOne(function(err, cat) { cat.jobs.reverse(function(err, ids) { @@ -5254,6 +5267,21 @@ describe('relations', function () { .catch(done); }); + it('should find items on scope and ordered them by name DESC', function (done) { + Category.find() + .then(function (categories) { + categories.should.have.length(1); + return categories[0].jobs.getAsync({order: 'name DESC'}) + }) + .then(function (jobs) { + jobs.should.have.length(2); + jobs[0].id.should.eql(job3.id); + jobs[1].id.should.eql(job2.id); + done(); + }) + .catch(done); + }); + it('should allow custom scope methods with promises - reverse', function(done) { Category.findOne() .then(function (cat) {