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:
parent
25ead1ce97
commit
1b7c346bca
19
lib/scope.js
19
lib/scope.js
|
@ -85,9 +85,10 @@ ScopeDefinition.prototype.related = function(receiver, scopeParams, condOrRefres
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cb = cb || utils.createPromiseCallback();
|
cb = cb || utils.createPromiseCallback();
|
||||||
|
const refreshIsNeeded = !self.__cachedRelations ||
|
||||||
if (!self.__cachedRelations || self.__cachedRelations[name] === undefined ||
|
self.__cachedRelations[name] === undefined ||
|
||||||
actualRefresh) {
|
actualRefresh;
|
||||||
|
if (refreshIsNeeded) {
|
||||||
// It either doesn't hit the cache or refresh is required
|
// It either doesn't hit the cache or refresh is required
|
||||||
var params = mergeQuery(actualCond, scopeParams, {nestedInclude: true});
|
var params = mergeQuery(actualCond, scopeParams, {nestedInclude: true});
|
||||||
var targetModel = this.targetModel(receiver);
|
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)
|
// run another query to apply filter on relatedModel(targetModel)
|
||||||
// see github.com/strongloop/loopback-datasource-juggler/issues/166
|
// see github.com/strongloop/loopback-datasource-juggler/issues/166
|
||||||
var scopeOnRelatedModel = params.collect &&
|
var scopeOnRelatedModel = params.collect &&
|
||||||
params.include.scope !== null &&
|
params.include.scope !== null &&
|
||||||
typeof params.include.scope === 'object';
|
typeof params.include.scope === 'object';
|
||||||
if (scopeOnRelatedModel) {
|
if (scopeOnRelatedModel) {
|
||||||
var filter = params.include;
|
var filter = params.include;
|
||||||
// The filter applied on relatedModel
|
// The filter applied on relatedModel
|
||||||
|
@ -144,7 +145,7 @@ ScopeDefinition.prototype.related = function(receiver, scopeParams, condOrRefres
|
||||||
queryRelated.where[IdKey] = collectTargetIds(data, IdKey);
|
queryRelated.where[IdKey] = collectTargetIds(data, IdKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
relatedModel.find(queryRelated, cb);
|
relatedModel.find(queryRelated, options, cb);
|
||||||
} else {
|
} else {
|
||||||
cb(err, data);
|
cb(err, data);
|
||||||
}
|
}
|
||||||
|
@ -238,8 +239,10 @@ function defineScope(cls, targetClass, name, params, methods, options) {
|
||||||
return self.__cachedRelations[name];
|
return self.__cachedRelations[name];
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (typeof condOrRefresh === 'function' &&
|
const condOrRefreshIsCallBack = typeof condOrRefresh === 'function' &&
|
||||||
options === undefined && cb === undefined) {
|
options === undefined &&
|
||||||
|
cb === undefined;
|
||||||
|
if (condOrRefreshIsCallBack) {
|
||||||
// customer.orders(cb)
|
// customer.orders(cb)
|
||||||
cb = condOrRefresh;
|
cb = condOrRefresh;
|
||||||
options = {};
|
options = {};
|
||||||
|
|
|
@ -572,7 +572,7 @@ describe('relations', function() {
|
||||||
|
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
Physician = db.define('Physician', {name: String});
|
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}});
|
sequence: {type: Number, index: true}});
|
||||||
Appointment = db.define('Appointment', {date: {type: Date,
|
Appointment = db.define('Appointment', {date: {type: Date,
|
||||||
default: function() {
|
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) {
|
it('should find scoped record', function(done) {
|
||||||
var id;
|
var id;
|
||||||
Physician.create(function(err, physician) {
|
Physician.create(function(err, physician) {
|
||||||
|
|
Loading…
Reference in New Issue