Skip to content

Commit

Permalink
Remove non-schema properties on reload
Browse files Browse the repository at this point in the history
  • Loading branch information
1602 committed Dec 11, 2011
1 parent 877e309 commit 44267a3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/abstract-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}
Expand Down
14 changes: 14 additions & 0 deletions test/common_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 44267a3

Please sign in to comment.