Skip to content

Commit

Permalink
fix: apply context.bind where the context was lost
Browse files Browse the repository at this point in the history
Patch with code from PR loopbackio#275.
  • Loading branch information
JonnyBGod committed Dec 11, 2019
1 parent 08c7314 commit 62680ce
Showing 1 changed file with 63 additions and 44 deletions.
107 changes: 63 additions & 44 deletions lib/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,21 @@ function generateMongoDBURL(options) {
}
}

/*!
* Tries to get the context and patch the function passed as argument
* @param {Function} the function to patch
* @param {String} [scopeName=loopback] the scope name
* @returns {Function} the function patched
*/
function patchWithContext(fn, scopeName) {
scopeName = scopeName || 'loopback';
const ns = process && process.context && process.context[scopeName];
if (ns && ns.bind) {
fn = ns.bind(fn);
}
return fn;
}

/**
* Initialize the MongoDB connector for the given data source
* @param {DataSource} dataSource The data source instance
Expand Down Expand Up @@ -469,7 +484,7 @@ MongoDB.prototype.execute = function(modelName, command) {
// Get the parameters for the given command
const args = [].slice.call(arguments, 2);
// The last argument must be a callback function
const callback = args[args.length - 1];
const callback = patchWithContext(args[args.length - 1]);

// Topology is destroyed when the server is disconnected
// Execute if DB is connected and functional otherwise connect/reconnect first
Expand Down Expand Up @@ -832,33 +847,35 @@ MongoDB.prototype.updateOrCreate = function updateOrCreate(
returnOriginal: false,
sort: [['_id', 'asc']],
},
function(err, result) {
if (self.debug) {
debug('updateOrCreate.callback', modelName, id, err, result);
}
const object = result && result.value;
if (!err && !object) {
// No result
err = 'No ' + modelName + ' found for id ' + id;
}
if (!err) {
self.setIdValue(modelName, object, oid);
if (object && idName !== '_id') {
delete object._id;
patchWithContext(
function(err, result) {
if (self.debug) {
debug('updateOrCreate.callback', modelName, id, err, result);
}
const object = result && result.value;
if (!err && !object) {
// No result
err = 'No ' + modelName + ' found for id ' + id;
}
if (!err) {
self.setIdValue(modelName, object, oid);
if (object && idName !== '_id') {
delete object._id;
}
}
}

let info;
if (result && result.lastErrorObject) {
info = {isNewInstance: !result.lastErrorObject.updatedExisting};
} else {
debug('updateOrCreate result format not recognized: %j', result);
}
let info;
if (result && result.lastErrorObject) {
info = {isNewInstance: !result.lastErrorObject.updatedExisting};
} else {
debug('updateOrCreate result format not recognized: %j', result);
}

if (callback) {
callback(err, self.fromDatabase(modelName, object), info);
}
},
if (callback) {
callback(err, self.fromDatabase(modelName, object), info);
}
},
)
);
};

Expand Down Expand Up @@ -1372,7 +1389,7 @@ MongoDB.prototype.all = function all(modelName, filter, options, callback) {
const shouldSetIdValue = idIncluded(fields, idName);
const deleteMongoId = !shouldSetIdValue || idName !== '_id';

cursor.toArray(function(err, data) {
cursor.toArray(patchWithContext(function(err, data) {
if (self.debug) {
debug('all', modelName, filter, err, data);
}
Expand Down Expand Up @@ -1401,7 +1418,7 @@ MongoDB.prototype.all = function all(modelName, filter, options, callback) {
} else {
callback(null, objs);
}
});
}));
}
};

Expand Down Expand Up @@ -1583,23 +1600,25 @@ MongoDB.prototype.updateAttributes = function updateAttrs(
{
sort: [['_id', 'asc']],
},
function(err, result) {
if (self.debug) {
debug('updateAttributes.callback', modelName, id, err, result);
}
const object = result && result.value;
if (!err && !object) {
// No result
err = errorIdNotFoundForUpdate(modelName, id);
}
self.setIdValue(modelName, object, id);
if (object && idName !== '_id') {
delete object._id;
}
if (cb) {
cb(err, object);
}
},
patchWithContext(
function(err, result) {
if (self.debug) {
debug('updateAttributes.callback', modelName, id, err, result);
}
const object = result && result.value;
if (!err && !object) {
// No result
err = errorIdNotFoundForUpdate(modelName, id);
}
self.setIdValue(modelName, object, id);
if (object && idName !== '_id') {
delete object._id;
}
if (cb) {
cb(err, object);
}
},
)
);
};

Expand Down

0 comments on commit 62680ce

Please sign in to comment.