1

We can promisify mysql pool getConnection function if we have already created the pool, like this:

const mysql = require('mysql');
const util = require('util');

const mysqlPool = mysql.createPool(CONFIG);

const getConnectionPm = util.promisify(mysqlPool.getConnection).bind(mysqlPool);

And the usage can be something like:

(async() => {
    const connection = await mysqlPool.getConnectionPm();

});

How can I promisify the mysqlPool.getConnection.query function to use connection variable? To make something like this:

(async() => {
    const connection = await mysqlPool.getConnectionPm();

    const result = await connection.queryPm(SQL_QUERY, SQL_ARGS);
});

The problem is that I don't know how to pass the connection variable to the promisify function. I guess one different way is to create a connection object with getConnectionPm function, then bind the query to that object like what we did to promisify the getConnection itself. But how we can achieve something like the code above?

For example, the code below gives an undefined error:

const getConnectionPm = util.promisify(mysql.createPool.getConnection);
Amir
  • 996
  • 7
  • 17
  • what about `util.promisify(connection.query.bind(connection))`? – Lux Feb 18 '20 at 12:00
  • @Lux I can do something like `mysqlPool.getConnectionPm = util.promisify(mysqlPool.getConnection.bind(mysqlPool)); `. But I don't have the connection variable to promisify it before getting the connection from `getConnection` function. Something like this doesn't work `util.promisify(mysqlPool.getConnection.query.bind(mysqlPool.getConnection))` because the query is undefined. – Amir Feb 18 '20 at 12:27
  • Just promisify later then after you've created the connection. – Lux Feb 18 '20 at 17:35

1 Answers1

1

You can change your getConnectionPm, and add a queryPM promisified query method to each connection.

const getConnectionPm = async () => {
    const conn = await util.promisify(mysqlPool.getConnection).call(mysqlPool);
    conn.queryPm = util.promisify(conn.query).bind(conn);
    return conn;
}

Then you can query as in your example.

(async() => {
    const connection = await mysqlPool.getConnectionPm();
    const result = await connection.queryPm(SQL_QUERY, SQL_ARGS);
});
Hurried-Helpful
  • 1,850
  • 5
  • 15
  • Thanks for the answer. I think we should create another variable after defining the `conn` to call it and create a connection instance, because in the third line `conn.query` is not defined. It worked after this change. – Amir Feb 18 '20 at 13:26
  • That's odd. `await` should've created an instance of `Connection`. Are you sure you added `await` in front of `util.promisify(mysqlPool...)`? – Hurried-Helpful Feb 18 '20 at 13:38
  • Yeah, I tried it again, the exact error is `The "original" argument must be of type Function. Received type undefined`, which happens when I'll try to access `conn.query`. I don't think that's a valid assumption on what's `await`s job. Actually we don't need `await` there, but for creating a connection from this `conn` function. I've edited the code. – Amir Feb 18 '20 at 14:11
  • 1
    Ok I found my mistake. I didn't actually call `getConnection`. I needed to add `()` at the end of the first line like `util.promisify(mysqlPool.getConnection).bind(mysqlPool)()`. – Hurried-Helpful Feb 18 '20 at 15:02