objectid
This commit is contained in:
parent
bf931b50c2
commit
60406efb90
23
lib/dao.js
23
lib/dao.js
|
@ -380,7 +380,15 @@ DataAccessObject.create = function(data, options, cb) {
|
||||||
obj.trigger('save', function(saveDone) {
|
obj.trigger('save', function(saveDone) {
|
||||||
const _idName = idName(Model);
|
const _idName = idName(Model);
|
||||||
let val = Model._sanitizeData(obj.toObject(true), options);
|
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) {
|
if (id) {
|
||||||
obj.__data[_idName] = id;
|
obj.__data[_idName] = id;
|
||||||
defineReadonlyProp(obj, _idName, id);
|
defineReadonlyProp(obj, _idName, id);
|
||||||
|
@ -1636,10 +1644,20 @@ DataAccessObject.find = function find(query, options, cb) {
|
||||||
persisted: true,
|
persisted: true,
|
||||||
// see https://github.com/strongloop/loopback-datasource-juggler/issues/1692
|
// see https://github.com/strongloop/loopback-datasource-juggler/issues/1692
|
||||||
applyDefaultValues: false,
|
applyDefaultValues: false,
|
||||||
|
// SPIKE: applyDefaultIdType to control reconversion to default type
|
||||||
};
|
};
|
||||||
let obj;
|
let obj;
|
||||||
try {
|
try {
|
||||||
|
// SPIKE: This converts string-id back to ObjectID
|
||||||
obj = new Model(data, ctorOpts);
|
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) {
|
} catch (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
|
@ -2723,6 +2741,9 @@ DataAccessObject.replaceById = function(id, data, options, cb) {
|
||||||
ctx.data[pkName] = id;
|
ctx.data[pkName] = id;
|
||||||
inst.setAttributes(ctx.data);
|
inst.setAttributes(ctx.data);
|
||||||
|
|
||||||
|
// SPIKE: any attempt to change the id before this will not work
|
||||||
|
inst.id = new String(inst.id);
|
||||||
|
|
||||||
const context = {
|
const context = {
|
||||||
Model: Model,
|
Model: Model,
|
||||||
instance: inst,
|
instance: inst,
|
||||||
|
|
Loading…
Reference in New Issue