This commit is contained in:
Yaapa Hage 2020-08-13 13:32:32 +05:30
parent bf931b50c2
commit 60406efb90
1 changed files with 23 additions and 2 deletions

View File

@ -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,