* Fixes#1275 Transform *fields* property into array `Include` filter takes into consideration string property 'fields' and transforms it into an array containing this string. * Added error handling for `include` filter. * ExecTasksWithInterLeave now contains a try-catch block in order to catch any unexpected errors. * LinkManyToMany now checks if *modelToIdName* exists on *target* before continuing. * Added unit test for *include* with string fields
This commit is contained in:
parent
77c4cd7b01
commit
b3a7bc521d
|
@ -97,12 +97,16 @@ function execTasksWithInterLeave(tasks, callback) {
|
||||||
// Context Switch BEFORE Heavy Computation
|
// Context Switch BEFORE Heavy Computation
|
||||||
process.nextTick(function() {
|
process.nextTick(function() {
|
||||||
// Heavy Computation
|
// Heavy Computation
|
||||||
async.parallel(tasks, function(err, info) {
|
try {
|
||||||
// Context Switch AFTER Heavy Computation
|
async.parallel(tasks, function(err, info) {
|
||||||
process.nextTick(function() {
|
// Context Switch AFTER Heavy Computation
|
||||||
callback(err, info);
|
process.nextTick(function() {
|
||||||
|
callback(err, info);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
} catch (err) {
|
||||||
|
callback(err);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -328,6 +332,10 @@ Inclusion.include = function(objects, include, options, cb) {
|
||||||
filter.where = filter.where || {};
|
filter.where = filter.where || {};
|
||||||
// if fields are specified, make sure target foreign key is present
|
// if fields are specified, make sure target foreign key is present
|
||||||
var fields = filter.fields;
|
var fields = filter.fields;
|
||||||
|
if (typeof fields === 'string') {
|
||||||
|
// transform string into array containing this string
|
||||||
|
filter.fields = fields = [fields];
|
||||||
|
}
|
||||||
if (Array.isArray(fields) && fields.indexOf(relation.keyTo) === -1) {
|
if (Array.isArray(fields) && fields.indexOf(relation.keyTo) === -1) {
|
||||||
fields.push(relation.keyTo);
|
fields.push(relation.keyTo);
|
||||||
} else if (isPlainObject(fields) && !fields[relation.keyTo]) {
|
} else if (isPlainObject(fields) && !fields[relation.keyTo]) {
|
||||||
|
@ -481,6 +489,11 @@ Inclusion.include = function(objects, include, options, cb) {
|
||||||
async.each(targets, linkManyToMany, next);
|
async.each(targets, linkManyToMany, next);
|
||||||
function linkManyToMany(target, next) {
|
function linkManyToMany(target, next) {
|
||||||
var targetId = target[modelToIdName];
|
var targetId = target[modelToIdName];
|
||||||
|
if (!targetId) {
|
||||||
|
var err = new Error(g.f('LinkManyToMany received target that doesn\'t contain required "%s"',
|
||||||
|
modelToIdName));
|
||||||
|
return next(err);
|
||||||
|
}
|
||||||
var objList = targetObjsMap[targetId.toString()];
|
var objList = targetObjsMap[targetId.toString()];
|
||||||
async.each(objList, function(obj, next) {
|
async.each(objList, function(obj, next) {
|
||||||
if (!obj) return next();
|
if (!obj) return next();
|
||||||
|
|
|
@ -835,6 +835,24 @@ describe('relations', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
context('findById with include filter that contains string fields', function() {
|
||||||
|
it('should accept string and convert it to array', function(done) {
|
||||||
|
var includeFilter = {include: {relation: 'patients', scope: {fields: 'name'}}};
|
||||||
|
var physicianId = physician.id;
|
||||||
|
Physician.findById(physicianId, includeFilter, function(err, result) {
|
||||||
|
should.not.exist(err);
|
||||||
|
should.exist(result);
|
||||||
|
result.id.should.eql(physicianId);
|
||||||
|
should.exist(result.patients);
|
||||||
|
result.patients().should.be.an.instanceOf(Array);
|
||||||
|
should.exist(result.patients()[0]);
|
||||||
|
should.exist(result.patients()[0].name);
|
||||||
|
should.not.exist(result.patients()[0].age);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
function createSampleData(done) {
|
function createSampleData(done) {
|
||||||
Physician.create(function(err, result) {
|
Physician.create(function(err, result) {
|
||||||
result.patients.create({name: 'a', age: '10'}, function(err, p) {
|
result.patients.create({name: 'a', age: '10'}, function(err, p) {
|
||||||
|
|
Loading…
Reference in New Issue