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 May 1, 2019
1 parent 56172b8 commit ef3b1c9
Showing 1 changed file with 61 additions and 41 deletions.
102 changes: 61 additions & 41 deletions lib/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,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';
var 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 @@ -449,6 +464,7 @@ MongoDB.prototype.execute = function(model, command) {
var args = [].slice.call(arguments, 2);
// The last argument must be a callback function
var callback = args[args.length - 1];
callback = patchWithContext(callback);

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

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

if (callback) {
callback(err, object, info);
if (callback) {
callback(err, object, info);
}
}
}
)
);
};

Expand Down Expand Up @@ -1340,7 +1358,7 @@ MongoDB.prototype.all = function all(model, filter, options, callback) {
var shouldSetIdValue = idIncluded(fields, idName);
var deleteMongoId = !shouldSetIdValue || idName !== '_id';

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

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

Expand Down

0 comments on commit ef3b1c9

Please sign in to comment.