From 44267a3ebbd123c321ef39f1fd3023e0545617aa Mon Sep 17 00:00:00 2001 From: Anatoliy Chakkaev Date: Sun, 11 Dec 2011 12:58:34 +0400 Subject: [PATCH] Remove non-schema properties on reload --- lib/abstract-class.js | 3 +++ test/common_test.js | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/lib/abstract-class.js b/lib/abstract-class.js index aec06be6..0d7b2d5c 100644 --- a/lib/abstract-class.js +++ b/lib/abstract-class.js @@ -406,6 +406,9 @@ AbstractClass.prototype.reload = function (cb) { AbstractClass.prototype.reset = function () { var obj = this; Object.keys(obj).forEach(function (k) { + if (k !== 'id' && !obj.constructor.schema.definitions[obj.constructor.modelName].properties[k]) { + delete obj[k]; + } if (obj.propertyChanged(k)) { obj[k] = obj[k + '_was']; } diff --git a/test/common_test.js b/test/common_test.js index c7274312..3ebd23ba 100644 --- a/test/common_test.js +++ b/test/common_test.js @@ -179,6 +179,20 @@ function testOrm(schema) { }); }); + it('should save only schema-defined field in database', function (test) { + Post.create({title: '1602', nonSchemaField: 'some value'}, function (err, post) { + test.ok(!post.nonSchemaField); + post.a = 1; + post.save(function () { + test.ok(post.a); + post.reload(function (err, psto) { + test.ok(!post.a); + test.done(); + }); + }); + }); + }); + it('should not create new instances for the same object', function (test) { var title = 'Initial title'; Post.create({ title: title }, function (err, post) {