Node-mysql2: how to bluebird promisifyAll mysql2

Created on 23 Nov 2015  Â·  9Comments  Â·  Source: sidorares/node-mysql2

Hello,
I'm in the process of converting my current implementation of node-mysql driver to mysql2 and fully utilize promises (bluebird). i figured the upgrade to mysql 2 should be a simple matter of changing 'mysql' to 'mysql2' but i am getting some errors and figured i'd ask for some help.

currently i have this setup for node-mysql

const Promise = require('bluebird');
const mysql = require('mysql');
const Pool = require('mysql/lib/Pool');
const options = { ... };
const Connection = require('mysql/lib/Connection');
Promise.promisifyAll([Pool, Connection]);
const pool = mysql.createPool(options);
module.exports = pool;

i tried

Promise.promisifyAll(require('mysql2'));

i also tried

const Promise = require('bluebird');
const mysql = require('mysql2');
const Pool = require('mysql2/lib/Pool');
const Connection = require('mysql2/lib/Connection');
Promise.promisifyAll([Pool, Connection]);
const options = { ... };
const pool = mysql.createPool(options);
module.exports = pool;

both gives me the error below.

TypeError: pool.getConnectionAsync is not a function

so the question is, how do i promisifyAll this module? Any help would be great, thanks!

question

Most helpful comment

working version

'use strict';

const Promise = require('bluebird');
const mysql = require('mysql2');
const options = { ... };
const Pool = require('mysql2/lib/pool');
const Connection = require('mysql2/lib/connection');
Promise.promisifyAll([Pool, Connection]);
// long stack trace for debug
Promise.longStackTraces();
const pool = mysql.createPool(options);
module.exports = pool;

on top of that, my tests now run faster

before

    ✓ the deleted facility should not exist (130ms)
    ✓ the deleted user should not exist (127ms)


  375 passing (60s)

now

    ✓ the deleted facility should not exist (66ms)
    ✓ the deleted user should not exist (66ms)


  375 passing (36s)

yay for more speed

All 9 comments

Hi!

I'm trying to keep file names lower case, and path to pool is /lib/pool.js, so you should be able to do

const mysql = require('mysql2');
const Pool = require('mysql2/lib/pool');
const Connection = mysql.Connection; // I should probably export mysql.Pool same way - see https://github.com/sidorares/node-mysql2/blob/master/index.js#L11 

not sure where getConnectionAsync comes from, don't see it in node-mysql/lib/Pool.js

hey, thank you for responding so fast, it was the caps that broke it!! thank you, the performance is amazing..

working version

'use strict';

const Promise = require('bluebird');
const mysql = require('mysql2');
const options = { ... };
const Pool = require('mysql2/lib/pool');
const Connection = require('mysql2/lib/connection');
Promise.promisifyAll([Pool, Connection]);
// long stack trace for debug
Promise.longStackTraces();
const pool = mysql.createPool(options);
module.exports = pool;

on top of that, my tests now run faster

before

    ✓ the deleted facility should not exist (130ms)
    ✓ the deleted user should not exist (127ms)


  375 passing (60s)

now

    ✓ the deleted facility should not exist (66ms)
    ✓ the deleted user should not exist (66ms)


  375 passing (36s)

yay for more speed

:+1:

@sidorares getConnectionAsync is created once you promisify the function. It will provide the same functionality as getConnection just that it will return the promise object than executing the callback.

Infact, all the functions of the library for which Promisify operation is performed will create a clone function which returns Promise objects but it will not change the original function. So you can still write a mix of callback and Promise. Other such example will be connection.queryAsync.

@minkesh you can use built-in promisified connection wrapper together with Bluebird

@sidorares yeah, that can be used as well but it is fairly new, I was just answering your doubt about getConnectionAsync function :)

@minkesh it's just a thin wrapper, main flow logic still happens inside promise ( built-in or bluebird, up to you )

Was this page helpful?
0 / 5 - 0 ratings

Related issues

magicxie picture magicxie  Â·  6Comments

HulioEglesias picture HulioEglesias  Â·  4Comments

juliandavidmr picture juliandavidmr  Â·  4Comments

hezjing picture hezjing  Â·  3Comments

sidorares picture sidorares  Â·  7Comments