Included models from include operations do not change defined `strict` model option (#1259)

* Fixes #1252

* Allowed setting of dynamic relation property

* Fixed tests to also consider other connectors
This commit is contained in:
Dimitris 2017-03-21 01:48:57 +02:00 committed by Sakib Hasan
parent 2bfc769c4d
commit 77c4cd7b01
3 changed files with 57 additions and 1 deletions

View File

@ -971,7 +971,7 @@ Inclusion.include = function(objects, include, options, cb) {
result = new List(result, relation.modelTo); result = new List(result, relation.modelTo);
} }
obj.__data[relationName] = result; obj.__data[relationName] = result;
obj.setStrict(false); // obj.setStrict(false); issue #1252
} else { } else {
obj[relationName] = result; obj[relationName] = result;
} }

View File

@ -211,7 +211,9 @@ ModelBaseClass.prototype._initProperties = function(data, options) {
var typeName = multiple ? 'Array' : modelTo.modelName; var typeName = multiple ? 'Array' : modelTo.modelName;
var propType = multiple ? [modelTo] : modelTo; var propType = multiple ? [modelTo] : modelTo;
properties[p] = {name: typeName, type: propType}; properties[p] = {name: typeName, type: propType};
/* Issue #1252
this.setStrict(false); this.setStrict(false);
*/
} }
// Relation // Relation

View File

@ -60,6 +60,60 @@ describe('include', function() {
}); });
}); });
it('should not have changed the __strict flag of the model', function(done) {
const originalStrict = User.definition.settings.strict;
User.definition.settings.strict = true; // Change to test regression for issue #1252
const finish = (err) => {
// Restore original user strict property
User.definition.settings.strict = originalStrict;
done(err);
};
User.find({include: 'posts'}, function(err, users) {
if (err) return finish(err);
users.forEach(user => {
user.should.have.property('__strict', true); // we changed it
});
finish();
});
});
it('should not save in db included models, in query returned models', function(done) {
const originalStrict = User.definition.settings.strict;
User.definition.settings.strict = true; // Change to test regression for issue #1252
const finish = (err) => {
// Restore original user strict property
User.definition.settings.strict = originalStrict;
done(err);
};
User.findOne({where: {name: 'User A'}, include: 'posts'}, function(err, user) {
if (err) return finish(err);
if (!user) return finish(new Error('User Not found to check relation not saved'));
user.save(function(err) { // save the returned user
if (err) return finish(err);
// should not store in db the posts
var dsName = User.dataSource.name;
if (dsName === 'memory') {
JSON.parse(User.dataSource.adapter.cache.User[1]).should.not.have.property('posts');
finish();
} else if (dsName === 'mongodb') { // Check native mongodb connector
// get hold of native mongodb collection
var dbCollection = User.dataSource.connector.collection(User.modelName);
dbCollection.findOne({_id: user.id})
.then(function(foundUser) {
if (!foundUser) {
finish(new Error('User not found to check posts not saved'));
}
foundUser.should.not.have.property('posts');
finish();
})
.catch(finish);
} else { // TODO make native checks for other connectors as well
finish();
}
});
});
});
it('should fetch Passport - Owner - Posts', function(done) { it('should fetch Passport - Owner - Posts', function(done) {
Passport.find({include: {owner: 'posts'}}, function(err, passports) { Passport.find({include: {owner: 'posts'}}, function(err, passports) {
should.not.exist(err); should.not.exist(err);