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
This commit is contained in:
dmellonch 2017-08-01 20:15:21 +02:00 committed by Sakib Hasan
parent 25ead1ce97
commit 1b7c346bca
2 changed files with 57 additions and 9 deletions

View File

@ -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 = {};

View File

@ -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) {