Remove non-schema properties on reload

This commit is contained in:
Anatoliy Chakkaev 2011-12-11 12:58:34 +04:00
parent 877e30947f
commit 44267a3ebb
2 changed files with 17 additions and 0 deletions

View File

@ -406,6 +406,9 @@ AbstractClass.prototype.reload = function (cb) {
AbstractClass.prototype.reset = function () { AbstractClass.prototype.reset = function () {
var obj = this; var obj = this;
Object.keys(obj).forEach(function (k) { 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)) { if (obj.propertyChanged(k)) {
obj[k] = obj[k + '_was']; obj[k] = obj[k + '_was'];
} }

View File

@ -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) { it('should not create new instances for the same object', function (test) {
var title = 'Initial title'; var title = 'Initial title';
Post.create({ title: title }, function (err, post) { Post.create({ title: title }, function (err, post) {