From aeb48165d02601a00962a3489bf7ea52f7674486 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl=20F=C3=BCrstenberg?= Date: Tue, 16 Aug 2016 12:09:47 +0200 Subject: [PATCH] Add fix for context issue --- lib/sql.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/sql.js b/lib/sql.js index 1a02407f..e3b209e2 100644 --- a/lib/sql.js +++ b/lib/sql.js @@ -18,6 +18,21 @@ module.exports = SQLConnector; function NOOP() {} +/*! + * 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; +} + /** * Base class for connectors that connect to relational databases using SQL * @class @@ -395,12 +410,15 @@ SQLConnector.prototype.execute = function(sql, params, options, callback) { callback = options; options = {}; } + params = params || []; options = options || {}; assert(Array.isArray(params), 'params must be an array'); assert(typeof options === 'object', 'options must be an object'); assert(typeof callback === 'function', 'callback must be a function'); + callback = patchWithContext(callback); + var self = this; if (!this.dataSource.connected) { return this.dataSource.once('connected', function() {