From 77c4cd7b01fd43140175b53f60e62390f88b3b32 Mon Sep 17 00:00:00 2001 From: Dimitris Date: Tue, 21 Mar 2017 01:48:57 +0200 Subject: [PATCH] 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 --- lib/include.js | 2 +- lib/model.js | 2 ++ test/include.test.js | 54 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/lib/include.js b/lib/include.js index bc22c58a..bc8b6d34 100644 --- a/lib/include.js +++ b/lib/include.js @@ -971,7 +971,7 @@ Inclusion.include = function(objects, include, options, cb) { result = new List(result, relation.modelTo); } obj.__data[relationName] = result; - obj.setStrict(false); + // obj.setStrict(false); issue #1252 } else { obj[relationName] = result; } diff --git a/lib/model.js b/lib/model.js index f1f81bbd..0549a90e 100644 --- a/lib/model.js +++ b/lib/model.js @@ -211,7 +211,9 @@ ModelBaseClass.prototype._initProperties = function(data, options) { var typeName = multiple ? 'Array' : modelTo.modelName; var propType = multiple ? [modelTo] : modelTo; properties[p] = {name: typeName, type: propType}; + /* Issue #1252 this.setStrict(false); + */ } // Relation diff --git a/test/include.test.js b/test/include.test.js index fe23dcd7..03b9f8e7 100644 --- a/test/include.test.js +++ b/test/include.test.js @@ -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) { Passport.find({include: {owner: 'posts'}}, function(err, passports) { should.not.exist(err);