Merge pull request #739 from mdartic/master
Fix filtering relations of a model with an order specified
This commit is contained in:
commit
d0072b68a1
|
@ -917,8 +917,13 @@ DataAccessObject.findByIds = function(ids, query, options, cb) {
|
||||||
var pk = idName(this);
|
var pk = idName(this);
|
||||||
filter.where[pk] = { inq: [].concat(ids) };
|
filter.where[pk] = { inq: [].concat(ids) };
|
||||||
mergeQuery(filter, query || {});
|
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) {
|
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;
|
return cb.promise;
|
||||||
};
|
};
|
||||||
|
|
|
@ -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) {
|
it('should allow custom scope methods - reverse', function(done) {
|
||||||
Category.findOne(function(err, cat) {
|
Category.findOne(function(err, cat) {
|
||||||
cat.jobs.reverse(function(err, ids) {
|
cat.jobs.reverse(function(err, ids) {
|
||||||
|
@ -5254,6 +5267,21 @@ describe('relations', function () {
|
||||||
.catch(done);
|
.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) {
|
it('should allow custom scope methods with promises - reverse', function(done) {
|
||||||
Category.findOne()
|
Category.findOne()
|
||||||
.then(function (cat) {
|
.then(function (cat) {
|
||||||
|
|
Loading…
Reference in New Issue