From 1b7c346bca96f363d91069a92a2bc1baba9a2a05 Mon Sep 17 00:00:00 2001 From: dmellonch Date: Tue, 1 Aug 2017 20:15:21 +0200 Subject: [PATCH] Missing the option argument (#1426) * Fix missing option arguments in scope.js Added option arguments in find.relatedmodel Added case test * Add order filter in verify function Fix for cloudant test --- lib/scope.js | 19 ++++++++++------- test/relations.test.js | 47 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 9 deletions(-) diff --git a/lib/scope.js b/lib/scope.js index 84f96974..68af1af6 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -85,9 +85,10 @@ ScopeDefinition.prototype.related = function(receiver, scopeParams, condOrRefres } } cb = cb || utils.createPromiseCallback(); - - if (!self.__cachedRelations || self.__cachedRelations[name] === undefined || - actualRefresh) { + const refreshIsNeeded = !self.__cachedRelations || + self.__cachedRelations[name] === undefined || + actualRefresh; + if (refreshIsNeeded) { // It either doesn't hit the cache or refresh is required var params = mergeQuery(actualCond, scopeParams, {nestedInclude: true}); var targetModel = this.targetModel(receiver); @@ -96,8 +97,8 @@ ScopeDefinition.prototype.related = function(receiver, scopeParams, condOrRefres // run another query to apply filter on relatedModel(targetModel) // see github.com/strongloop/loopback-datasource-juggler/issues/166 var scopeOnRelatedModel = params.collect && - params.include.scope !== null && - typeof params.include.scope === 'object'; + params.include.scope !== null && + typeof params.include.scope === 'object'; if (scopeOnRelatedModel) { var filter = params.include; // The filter applied on relatedModel @@ -144,7 +145,7 @@ ScopeDefinition.prototype.related = function(receiver, scopeParams, condOrRefres queryRelated.where[IdKey] = collectTargetIds(data, IdKey); } - relatedModel.find(queryRelated, cb); + relatedModel.find(queryRelated, options, cb); } else { cb(err, data); } @@ -238,8 +239,10 @@ function defineScope(cls, targetClass, name, params, methods, options) { return self.__cachedRelations[name]; } } else { - if (typeof condOrRefresh === 'function' && - options === undefined && cb === undefined) { + const condOrRefreshIsCallBack = typeof condOrRefresh === 'function' && + options === undefined && + cb === undefined; + if (condOrRefreshIsCallBack) { // customer.orders(cb) cb = condOrRefresh; options = {}; diff --git a/test/relations.test.js b/test/relations.test.js index 85cdecac..5acad922 100644 --- a/test/relations.test.js +++ b/test/relations.test.js @@ -572,7 +572,7 @@ describe('relations', function() { before(function(done) { Physician = db.define('Physician', {name: String}); - Patient = db.define('Patient', {name: String, age: Number, + Patient = db.define('Patient', {name: String, age: Number, realm: String, sequence: {type: Number, index: true}}); Appointment = db.define('Appointment', {date: {type: Date, default: function() { @@ -918,6 +918,51 @@ describe('relations', function() { }; }); + describe('find over related model with options', function() { + after(function() { + Physician.clearObservers('access'); + Patient.clearObservers('access'); + }); + before(function() { + Physician.observe('access', beforeAccessFn); + Patient.observe('access', beforeAccessFn); + + function beforeAccessFn(ctx, next) { + ctx.query.where.realm = ctx.options.realm; + next(); + } + }); + it('should find be filtered from option', function(done) { + var id; + Physician.create(function(err, physician) { + if (err) return done(err); + physician.patients.create({name: 'a', realm: 'test'}, function(err, ch) { + if (err) return done(err); + id = ch.id; + physician.patients.create({name: 'z', realm: 'test'}, function(err) { + if (err) return done(err); + physician.patients.create({name: 'c', realm: 'anotherRealm'}, function(err) { + if (err) return done(err); + verify(physician); + }); + }); + }); + }); + + function verify(physician) { + physician.patients({order: 'name ASC'}, {realm: 'test'}, function(err, records) { + if (err) return done(err); + should.exist(records); + records.length.should.eql(2); + const expected = ['a:test', 'z:test']; + const actual = records.map(function(r) { return r.name + ':' + r.realm; }); + actual.should.eql(expected); + done(); + }); + } + }); + }); + it('should find scoped record', function(done) { var id; Physician.create(function(err, physician) {