Filtering relations of a model with an order specified
This commit is contained in:
parent
a2ed2a401b
commit
7c22db6ea7
|
@ -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;
|
||||
};
|
||||
|
|
|
@ -4835,13 +4835,13 @@ describe('relations', function () {
|
|||
});
|
||||
|
||||
it('should create record on scope', function (done) {
|
||||
Category.create({ name: 'Category A' }, function(err, cat) {
|
||||
cat.jobIds.should.be.an.array;
|
||||
cat.jobIds.should.have.length(0);
|
||||
cat.jobs.create({ name: 'Job 2' }, function(err, p) {
|
||||
Category.create({ name: 'Category A' }, function(err, catA) {
|
||||
catA.jobIds.should.be.an.array;
|
||||
catA.jobIds.should.have.length(0);
|
||||
catA.jobs.create({ name: 'Job 2' }, function(err, p) {
|
||||
should.not.exist(err);
|
||||
cat.jobIds.should.have.length(1);
|
||||
cat.jobIds.should.eql([p.id]);
|
||||
catA.jobIds.should.have.length(1);
|
||||
catA.jobIds.should.eql([p.id]);
|
||||
p.name.should.equal('Job 2');
|
||||
job2 = p;
|
||||
done();
|
||||
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue