Fix coercion from ObjectID to String
This commit is contained in:
parent
72897c9f1a
commit
eb6fa410fe
21
lib/dao.js
21
lib/dao.js
|
@ -294,9 +294,9 @@ DataAccessObject.create = function (data, options, cb) {
|
||||||
};
|
};
|
||||||
Model.notifyObserversOf('loaded', context, function(err) {
|
Model.notifyObserversOf('loaded', context, function(err) {
|
||||||
|
|
||||||
// By default, the instance passed to create callback is NOT updated
|
// By default, the instance passed to create callback is NOT updated
|
||||||
// with the changes made through persist/loaded hooks. To preserve
|
// with the changes made through persist/loaded hooks. To preserve
|
||||||
// backwards compatibility, we introduced a new setting updateOnLoad,
|
// backwards compatibility, we introduced a new setting updateOnLoad,
|
||||||
// which if set, will apply these changes to the model instance too.
|
// which if set, will apply these changes to the model instance too.
|
||||||
if(Model.settings.updateOnLoad) {
|
if(Model.settings.updateOnLoad) {
|
||||||
obj.setAttributes(context.data);
|
obj.setAttributes(context.data);
|
||||||
|
@ -1135,12 +1135,7 @@ DataAccessObject._coerce = function (where) {
|
||||||
}
|
}
|
||||||
// Check there is an operator
|
// Check there is an operator
|
||||||
var operator = null;
|
var operator = null;
|
||||||
if ('object' === typeof val) {
|
if (val.constructor === Object) {
|
||||||
if (Object.keys(val).length !== 1) {
|
|
||||||
// Skip if there are not only one properties
|
|
||||||
// as the assumption for operators is not true here
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
for (var op in operators) {
|
for (var op in operators) {
|
||||||
if (op in val) {
|
if (op in val) {
|
||||||
val = val[op];
|
val = val[op];
|
||||||
|
@ -1182,7 +1177,7 @@ DataAccessObject._coerce = function (where) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (val !== null && val !== undefined) {
|
if (val != null) {
|
||||||
val = DataType(val);
|
val = DataType(val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2361,9 +2356,9 @@ DataAccessObject.prototype.updateAttributes = function updateAttributes(data, op
|
||||||
Model.notifyObserversOf('loaded', context, function(err) {
|
Model.notifyObserversOf('loaded', context, function(err) {
|
||||||
if (!err) inst.__persisted = true;
|
if (!err) inst.__persisted = true;
|
||||||
|
|
||||||
// By default, the instance passed to updateAttributes callback is NOT updated
|
// By default, the instance passed to updateAttributes callback is NOT updated
|
||||||
// with the changes made through persist/loaded hooks. To preserve
|
// with the changes made through persist/loaded hooks. To preserve
|
||||||
// backwards compatibility, we introduced a new setting updateOnLoad,
|
// backwards compatibility, we introduced a new setting updateOnLoad,
|
||||||
// which if set, will apply these changes to the model instance too.
|
// which if set, will apply these changes to the model instance too.
|
||||||
if(Model.settings.updateOnLoad) {
|
if(Model.settings.updateOnLoad) {
|
||||||
inst.setAttributes(context.data);
|
inst.setAttributes(context.data);
|
||||||
|
|
|
@ -1269,6 +1269,18 @@ describe('DataAccessObject', function () {
|
||||||
assert.deepEqual(where, {id: '1'});
|
assert.deepEqual(where, {id: '1'});
|
||||||
where = model._coerce({id: '1'});
|
where = model._coerce({id: '1'});
|
||||||
assert.deepEqual(where, {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 () {
|
it('should be able to coerce where clause for number types', function () {
|
||||||
|
|
Loading…
Reference in New Issue