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) {
|
||||
var obj;
|
||||
if (data) {
|
||||
inst._initProperties(data);
|
||||
inst._initProperties(data, true);
|
||||
obj = inst;
|
||||
} else {
|
||||
obj = null;
|
||||
|
@ -327,7 +327,7 @@ DataAccessObject.findById = function find(id, cb) {
|
|||
setIdValue(this, data, id);
|
||||
}
|
||||
obj = new this();
|
||||
obj._initProperties(data, false);
|
||||
obj._initProperties(data, true);
|
||||
}
|
||||
cb(err, obj);
|
||||
}.bind(this));
|
||||
|
@ -424,9 +424,9 @@ DataAccessObject.find = function find(params, cb) {
|
|||
this.dataSource.connector.all(this.modelName, params, function (err, data) {
|
||||
if (data && data.forEach) {
|
||||
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) {
|
||||
data[i] = obj.__cachedRelations[params.collect];
|
||||
|
@ -620,7 +620,7 @@ DataAccessObject.prototype.save = function (options, callback) {
|
|||
if (err) {
|
||||
return callback(err, inst);
|
||||
}
|
||||
inst._initProperties(data, false);
|
||||
inst._initProperties(data, true);
|
||||
updateDone.call(inst, function () {
|
||||
saveDone.call(inst, function () {
|
||||
callback(err, inst);
|
||||
|
|
20
lib/model.js
20
lib/model.js
|
@ -197,17 +197,15 @@ ModelBaseClass.toString = function () {
|
|||
*/
|
||||
ModelBaseClass.prototype.toObject = function (onlySchema) {
|
||||
var data = {};
|
||||
var ds = this.constructor.dataSource.definitions[this.constructor.modelName];
|
||||
var properties = ds.properties;
|
||||
var self = this;
|
||||
|
||||
var schemaless = this.constructor.settings.strict === false || !onlySchema;
|
||||
var schemaLess = this.constructor.settings.strict === false || !onlySchema;
|
||||
this.constructor.forEachProperty(function (attr) {
|
||||
if (self[attr] instanceof List) {
|
||||
data[attr] = self[attr].toObject(!schemaless);
|
||||
data[attr] = self[attr].toObject(!schemaLess);
|
||||
} else if (self.__data.hasOwnProperty(attr)) {
|
||||
if(self[attr] !== undefined && self[attr]!== null && self[attr].toObject) {
|
||||
data[attr] = self[attr].toObject(!schemaless);
|
||||
data[attr] = self[attr].toObject(!schemaLess);
|
||||
} else {
|
||||
data[attr] = self[attr];
|
||||
}
|
||||
|
@ -216,18 +214,18 @@ ModelBaseClass.prototype.toObject = function (onlySchema) {
|
|||
}
|
||||
});
|
||||
|
||||
if (schemaless) {
|
||||
Object.keys(self).forEach(function (attr) {
|
||||
if (schemaLess) {
|
||||
Object.keys(self.__data).forEach(function (attr) {
|
||||
if (!data.hasOwnProperty(attr)) {
|
||||
if(self[attr] !== undefined && self[attr]!== null && self[attr].toObject) {
|
||||
data[attr] = self[attr].toObject(!schemaless);
|
||||
var val = self.__data[attr];
|
||||
if(val !== undefined && val!== null && val.toObject) {
|
||||
data[attr] = val.toObject(!schemaLess);
|
||||
} else {
|
||||
data[attr] = self[attr];
|
||||
data[attr] = val;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
|
|
|
@ -301,15 +301,22 @@ describe('DataSource define model', function () {
|
|||
var ds = new DataSource('memory');
|
||||
|
||||
var User = ds.define('User', {}, {strict: false});
|
||||
User.modelName.should.equal('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.have.property('name', 'Joe');
|
||||
user.should.have.property('age', 20);
|
||||
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