Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getContext() gets lost when switching from mysql to mysql2 #583

Open
kashak88 opened this issue Apr 24, 2024 · 0 comments
Open

getContext() gets lost when switching from mysql to mysql2 #583

kashak88 opened this issue Apr 24, 2024 · 0 comments
Labels

Comments

@kashak88
Copy link

loopback: 3.28.0
loopback-context: 3.5.2
loopback-connector-mysql: 7.0.X

Hi,
When we tried switching to connector version 7.0.9 (mysql2) for our project, getContext() would get lost for no apparent reason. 6.2.0 (mysql) worked without issues, but even manually changing to const mysql = require('mysql2') in mysql.js with version 6.2.0 would start producing the same issue.
Googling led me to strongloop/loopback-context#21 (comment) and then to https://github.com/loopbackio/loopback-connector-mongodb/pull/275/commits

I've added

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;
}

....
MySQL.prototype.executeSQL = function(sql, params, options, callback) {
  const self = this;
  const client = this.client;
  const debugEnabled = debug.enabled;
  const db = this.settings.database;
  if (typeof callback !== 'function') {
    throw new Error(g.f('{{callback}} should be a function'));
  }
  if (debugEnabled) {
    debug('SQL: %s, params: %j', sql, params);
  }

  const transaction = options.transaction;
  callback = patchWithContext(callback);
....

and suddenly everything started working for us. Is it possible to implement this patch to loopback-connector-mysql?
Apologies if I'm missing something obvious - I am not a developer myself but a linux admin helping our devs move to nodejs 20/newer package versions.

@kashak88 kashak88 added the bug label Apr 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant