MatissJanis / knex-serverless-mysql

Knex.js dialect for serverless-mysql
MIT License
12 stars 3 forks source link

Provide a way to close the connection #2

Open jakubjafra opened 3 years ago

jakubjafra commented 3 years ago

Firstly, thanks a lot for this plugin!

I tried it, and I think, the plugin should use default knex methods to close the connection. Right now calling knex.destroy() does nothing, and the connection hangs.

I've come up with this code to close the connection:

  const mysql = require('serverless-mysql')({ config });
  const knexServerlessMysql = require('knex-serverless-mysql');
  const knexInstance = Knex({
    mysql,
    client: knexServerlessMysql,
  } as any);
  // ...
  const originalDestroy = (knexInstance as any).context.destroy;
  (knexInstance as any).context.destroy = async () => {
    if (knexInstance) {
      await originalDestroy.apply(knexInstance);
    }
    if (mysql) {
      await mysql.end();
      mysql.quit();
    }
  };

Is there any way this could be integrated within knex-serverless-mysql?

MatissJanis commented 3 years ago

Hey,

Great feedback. Feel free to submit a PR with the change and I'll review it in depth.

SamXDesc commented 2 years ago

this is working already or not?