question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

how to bluebird promisifyAll mysql2

See original GitHub issue

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!

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
thewillhuangcommented, Nov 24, 2015

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

1reaction
sidorarescommented, Nov 23, 2015

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 
Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - How to promisify a MySql function using bluebird?
I have done this way and it is working fine. const connection = mysql.createConnection({.....}); global.db = Bluebird.
Read more >
Promisification | bluebird
The usual way to use promises in node is to Promise.promisifyAll some API and start exclusively calling promise returning versions of the APIs...
Read more >
mysql2-bluebird
Promises wrapper for mysql2 , powered by bluebird . Build Status. Installation. This module is installed via npm: $ npm install mysql2 ......
Read more >
Bluebird NPM: Bluebird JS Promise with Example
Bluebird is a fully featured Promise library for JavaScript. In this Bluebird JS tutorial, you will learn about Bluebird JS Promises with ...
Read more >
promise-mysql | Yarn - Package Manager
Promise -mysql is a wrapper for mysqljs/mysql that wraps function calls with Bluebird promises. API. mysql.createConnection(connectionOptions). This will return ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found