Fix the property population for schemaless models
This commit is contained in:
parent
28cf9bdd1e
commit
c2eb1f55f0
10
lib/dao.js
10
lib/dao.js
|
@ -230,7 +230,7 @@ DataAccessObject.upsert = DataAccessObject.updateOrCreate = function upsert(data
|
||||||
this.dataSource.connector.updateOrCreate(Model.modelName, inst.toObject(true), function (err, data) {
|
this.dataSource.connector.updateOrCreate(Model.modelName, inst.toObject(true), function (err, data) {
|
||||||
var obj;
|
var obj;
|
||||||
if (data) {
|
if (data) {
|
||||||
inst._initProperties(data);
|
inst._initProperties(data, true);
|
||||||
obj = inst;
|
obj = inst;
|
||||||
} else {
|
} else {
|
||||||
obj = null;
|
obj = null;
|
||||||
|
@ -327,7 +327,7 @@ DataAccessObject.findById = function find(id, cb) {
|
||||||
setIdValue(this, data, id);
|
setIdValue(this, data, id);
|
||||||
}
|
}
|
||||||
obj = new this();
|
obj = new this();
|
||||||
obj._initProperties(data, false);
|
obj._initProperties(data, true);
|
||||||
}
|
}
|
||||||
cb(err, obj);
|
cb(err, obj);
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
@ -424,9 +424,9 @@ DataAccessObject.find = function find(params, cb) {
|
||||||
this.dataSource.connector.all(this.modelName, params, function (err, data) {
|
this.dataSource.connector.all(this.modelName, params, function (err, data) {
|
||||||
if (data && data.forEach) {
|
if (data && data.forEach) {
|
||||||
data.forEach(function (d, i) {
|
data.forEach(function (d, i) {
|
||||||
var obj = new constr;
|
var obj = new constr();
|
||||||
|
|
||||||
obj._initProperties(d, false, params.fields);
|
obj._initProperties(d, true, params.fields);
|
||||||
|
|
||||||
if (params && params.include && params.collect) {
|
if (params && params.include && params.collect) {
|
||||||
data[i] = obj.__cachedRelations[params.collect];
|
data[i] = obj.__cachedRelations[params.collect];
|
||||||
|
@ -620,7 +620,7 @@ DataAccessObject.prototype.save = function (options, callback) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return callback(err, inst);
|
return callback(err, inst);
|
||||||
}
|
}
|
||||||
inst._initProperties(data, false);
|
inst._initProperties(data, true);
|
||||||
updateDone.call(inst, function () {
|
updateDone.call(inst, function () {
|
||||||
saveDone.call(inst, function () {
|
saveDone.call(inst, function () {
|
||||||
callback(err, inst);
|
callback(err, inst);
|
||||||
|
|
20
lib/model.js
20
lib/model.js
|
@ -197,17 +197,15 @@ ModelBaseClass.toString = function () {
|
||||||
*/
|
*/
|
||||||
ModelBaseClass.prototype.toObject = function (onlySchema) {
|
ModelBaseClass.prototype.toObject = function (onlySchema) {
|
||||||
var data = {};
|
var data = {};
|
||||||
var ds = this.constructor.dataSource.definitions[this.constructor.modelName];
|
|
||||||
var properties = ds.properties;
|
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
var schemaless = this.constructor.settings.strict === false || !onlySchema;
|
var schemaLess = this.constructor.settings.strict === false || !onlySchema;
|
||||||
this.constructor.forEachProperty(function (attr) {
|
this.constructor.forEachProperty(function (attr) {
|
||||||
if (self[attr] instanceof List) {
|
if (self[attr] instanceof List) {
|
||||||
data[attr] = self[attr].toObject(!schemaless);
|
data[attr] = self[attr].toObject(!schemaLess);
|
||||||
} else if (self.__data.hasOwnProperty(attr)) {
|
} else if (self.__data.hasOwnProperty(attr)) {
|
||||||
if(self[attr] !== undefined && self[attr]!== null && self[attr].toObject) {
|
if(self[attr] !== undefined && self[attr]!== null && self[attr].toObject) {
|
||||||
data[attr] = self[attr].toObject(!schemaless);
|
data[attr] = self[attr].toObject(!schemaLess);
|
||||||
} else {
|
} else {
|
||||||
data[attr] = self[attr];
|
data[attr] = self[attr];
|
||||||
}
|
}
|
||||||
|
@ -216,18 +214,18 @@ ModelBaseClass.prototype.toObject = function (onlySchema) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (schemaless) {
|
if (schemaLess) {
|
||||||
Object.keys(self).forEach(function (attr) {
|
Object.keys(self.__data).forEach(function (attr) {
|
||||||
if (!data.hasOwnProperty(attr)) {
|
if (!data.hasOwnProperty(attr)) {
|
||||||
if(self[attr] !== undefined && self[attr]!== null && self[attr].toObject) {
|
var val = self.__data[attr];
|
||||||
data[attr] = self[attr].toObject(!schemaless);
|
if(val !== undefined && val!== null && val.toObject) {
|
||||||
|
data[attr] = val.toObject(!schemaLess);
|
||||||
} else {
|
} else {
|
||||||
data[attr] = self[attr];
|
data[attr] = val;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -301,15 +301,22 @@ describe('DataSource define model', function () {
|
||||||
var ds = new DataSource('memory');
|
var ds = new DataSource('memory');
|
||||||
|
|
||||||
var User = ds.define('User', {}, {strict: false});
|
var User = ds.define('User', {}, {strict: false});
|
||||||
|
User.modelName.should.equal('User');
|
||||||
|
|
||||||
User.create({name: 'Joe', age: 20}, function (err, user) {
|
User.create({name: 'Joe', age: 20}, function (err, user) {
|
||||||
|
|
||||||
User.modelName.should.equal('User');
|
|
||||||
user.should.be.a('object').and.have.property('name', 'Joe');
|
user.should.be.a('object').and.have.property('name', 'Joe');
|
||||||
user.should.have.property('name', 'Joe');
|
user.should.have.property('name', 'Joe');
|
||||||
user.should.have.property('age', 20);
|
user.should.have.property('age', 20);
|
||||||
user.should.not.have.property('bio');
|
user.should.not.have.property('bio');
|
||||||
done(null, User);
|
|
||||||
|
User.findById(user.id, function(err, user) {
|
||||||
|
user.should.be.a('object').and.have.property('name', 'Joe');
|
||||||
|
user.should.have.property('name', 'Joe');
|
||||||
|
user.should.have.property('age', 20);
|
||||||
|
user.should.not.have.property('bio');
|
||||||
|
done(null, User);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue