knex-serverless-mysql icon indicating copy to clipboard operation
knex-serverless-mysql copied to clipboard

Provide a way to close the connection

Open jakubjafra opened this issue 4 years ago • 2 comments

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?

jakubjafra avatar Jun 28 '21 10:06 jakubjafra

Hey,

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

MatissJanis avatar Jun 28 '21 17:06 MatissJanis

this is working already or not?

SamXDesc avatar Jul 11 '22 12:07 SamXDesc