From 35dd198f7057ef34a31b87efe5fb05cd197ec41f Mon Sep 17 00:00:00 2001 From: Anatoliy Chakkaev Date: Wed, 19 Oct 2011 00:36:03 +0400 Subject: [PATCH] Only save to database attributes that listed in schema --- lib/abstract-class.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/abstract-class.js b/lib/abstract-class.js index 314c4385..9c7aa885 100644 --- a/lib/abstract-class.js +++ b/lib/abstract-class.js @@ -102,7 +102,7 @@ AbstractClass.create = function (data) { // if we come from save if (data instanceof AbstractClass && !data.id) { obj = data; - data = obj.toObject(); + data = obj.toObject(true); } else { obj = new this(data); @@ -211,7 +211,7 @@ AbstractClass.prototype.save = function (options, callback) { return callback && callback(err); } var modelName = this.constructor.modelName; - var data = this.toObject(); + var data = this.toObject(true); if (this.id) { this._adapter().save(modelName, data, function (err) { if (err) { @@ -240,10 +240,12 @@ AbstractClass.prototype.propertyChanged = function (name) { return this[name + '_was'] !== this['_' + name]; }; -AbstractClass.prototype.toObject = function () { +AbstractClass.prototype.toObject = function (onlySchema) { // blind faith: we only enumerate properties var data = {}; - Object.keys(this).forEach(function (property) { + var ds = this.constructor.schema.definitions[this.constructor.modelName]; + var properties = ds.properties; + Object.keys(onlySchema ? properties : this).concat(['id']).forEach(function (property) { data[property] = this[property]; }.bind(this)); return data;