Only save to database attributes that listed in schema

This commit is contained in:
Anatoliy Chakkaev 2011-10-19 00:36:03 +04:00
parent 6238b738e4
commit 35dd198f70
1 changed files with 6 additions and 4 deletions

View File

@ -102,7 +102,7 @@ AbstractClass.create = function (data) {
// if we come from save // if we come from save
if (data instanceof AbstractClass && !data.id) { if (data instanceof AbstractClass && !data.id) {
obj = data; obj = data;
data = obj.toObject(); data = obj.toObject(true);
} else { } else {
obj = new this(data); obj = new this(data);
@ -211,7 +211,7 @@ AbstractClass.prototype.save = function (options, callback) {
return callback && callback(err); return callback && callback(err);
} }
var modelName = this.constructor.modelName; var modelName = this.constructor.modelName;
var data = this.toObject(); var data = this.toObject(true);
if (this.id) { if (this.id) {
this._adapter().save(modelName, data, function (err) { this._adapter().save(modelName, data, function (err) {
if (err) { if (err) {
@ -240,10 +240,12 @@ AbstractClass.prototype.propertyChanged = function (name) {
return this[name + '_was'] !== this['_' + name]; return this[name + '_was'] !== this['_' + name];
}; };
AbstractClass.prototype.toObject = function () { AbstractClass.prototype.toObject = function (onlySchema) {
// blind faith: we only enumerate properties // blind faith: we only enumerate properties
var data = {}; 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]; data[property] = this[property];
}.bind(this)); }.bind(this));
return data; return data;