diff --git a/lib/dao.js b/lib/dao.js index ef59eaea..221adbec 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -380,7 +380,15 @@ DataAccessObject.create = function(data, options, cb) { obj.trigger('save', function(saveDone) { const _idName = idName(Model); let val = Model._sanitizeData(obj.toObject(true), options); - function createCallback(err, id, rev) { + + // SPIKE: changed the signature + function createCallback(options) { + + const { err, id, rev, propertyCoercer } = options; + + // SPIKE: apply coercion + propertyCoercer(obj.__data, Model.definition.properties); + if (id) { obj.__data[_idName] = id; defineReadonlyProp(obj, _idName, id); @@ -1628,7 +1636,7 @@ DataAccessObject.find = function find(query, options, cb) { } else { withNotify(item, next); } - + function buildResult(data, callback) { const ctorOpts = { fields: query.fields, @@ -1636,10 +1644,20 @@ DataAccessObject.find = function find(query, options, cb) { persisted: true, // see https://github.com/strongloop/loopback-datasource-juggler/issues/1692 applyDefaultValues: false, + // SPIKE: applyDefaultIdType to control reconversion to default type }; let obj; try { + // SPIKE: This converts string-id back to ObjectID obj = new Model(data, ctorOpts); + +const propDefs = self.dataSource.definitions[self.modelName].properties; +if (propDefs.id.type.name === 'ObjectID') { + // SPIKE: to change the constructor type + const strId = new String(obj.id.toString()); + obj.id = strId; +} + } catch (err) { return callback(err); } @@ -2723,6 +2741,9 @@ DataAccessObject.replaceById = function(id, data, options, cb) { ctx.data[pkName] = id; inst.setAttributes(ctx.data); + // SPIKE: any attempt to change the id before this will not work + inst.id = new String(inst.id); + const context = { Model: Model, instance: inst,