Merge pull request #652 from strongloop/feature/fix-objectid-coercion

Fix coercion from ObjectID to String
This commit is contained in:
Raymond Feng 2015-07-02 10:21:03 -07:00
commit 50ac143cea
2 changed files with 20 additions and 13 deletions

View File

@ -294,9 +294,9 @@ DataAccessObject.create = function (data, options, cb) {
};
Model.notifyObserversOf('loaded', context, function(err) {
// By default, the instance passed to create callback is NOT updated
// with the changes made through persist/loaded hooks. To preserve
// backwards compatibility, we introduced a new setting updateOnLoad,
// By default, the instance passed to create callback is NOT updated
// with the changes made through persist/loaded hooks. To preserve
// backwards compatibility, we introduced a new setting updateOnLoad,
// which if set, will apply these changes to the model instance too.
if(Model.settings.updateOnLoad) {
obj.setAttributes(context.data);
@ -1139,12 +1139,7 @@ DataAccessObject._coerce = function (where) {
}
// Check there is an operator
var operator = null;
if ('object' === typeof val) {
if (Object.keys(val).length !== 1) {
// Skip if there are not only one properties
// as the assumption for operators is not true here
continue;
}
if (val.constructor === Object) {
for (var op in operators) {
if (op in val) {
val = val[op];
@ -1186,7 +1181,7 @@ DataAccessObject._coerce = function (where) {
}
}
} else {
if (val !== null && val !== undefined) {
if (val != null) {
val = DataType(val);
}
}
@ -2365,9 +2360,9 @@ DataAccessObject.prototype.updateAttributes = function updateAttributes(data, op
Model.notifyObserversOf('loaded', context, function(err) {
if (!err) inst.__persisted = true;
// By default, the instance passed to updateAttributes callback is NOT updated
// with the changes made through persist/loaded hooks. To preserve
// backwards compatibility, we introduced a new setting updateOnLoad,
// By default, the instance passed to updateAttributes callback is NOT updated
// with the changes made through persist/loaded hooks. To preserve
// backwards compatibility, we introduced a new setting updateOnLoad,
// which if set, will apply these changes to the model instance too.
if(Model.settings.updateOnLoad) {
inst.setAttributes(context.data);

View File

@ -1269,6 +1269,18 @@ describe('DataAccessObject', function () {
assert.deepEqual(where, {id: '1'});
where = model._coerce({id: '1'});
assert.deepEqual(where, {id: '1'});
// Mockup MongoDB ObjectID
function ObjectID(id) {
this.id = id;
}
ObjectID.prototype.toString = function() {
return this.id;
};
where = model._coerce({id: new ObjectID('1')});
assert.deepEqual(where, {id: '1'});
});
it('should be able to coerce where clause for number types', function () {