From 7c22db6ea7e3270f03ea847514c9ae10be249f60 Mon Sep 17 00:00:00 2001 From: mdartic Date: Fri, 16 Oct 2015 18:21:04 +0200 Subject: [PATCH 1/2] Filtering relations of a model with an order specified --- lib/dao.js | 7 ++++++- test/relations.test.js | 40 ++++++++++++++++++++++++++++++++++------ 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/lib/dao.js b/lib/dao.js index 0b042700..e9f006e3 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..b8cdbf54 100644 --- a/test/relations.test.js +++ b/test/relations.test.js @@ -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) { From ef7fe45d9c7cf54fb96db74ada06dd766ae5cca6 Mon Sep 17 00:00:00 2001 From: mdartic Date: Fri, 16 Oct 2015 18:25:23 +0200 Subject: [PATCH 2/2] Rewrite of variable --- test/relations.test.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/relations.test.js b/test/relations.test.js index b8cdbf54..aca1ad59 100644 --- a/test/relations.test.js +++ b/test/relations.test.js @@ -4835,13 +4835,13 @@ describe('relations', function () { }); it('should create record on scope', function (done) { - 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) { + 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) { should.not.exist(err); - catA.jobIds.should.have.length(1); - catA.jobIds.should.eql([p.id]); + cat.jobIds.should.have.length(1); + cat.jobIds.should.eql([p.id]); p.name.should.equal('Job 2'); job2 = p; done();