types / mysql2

Typings for https://github.com/sidorares/node-mysql2
ISC License
41 stars 23 forks source link

Missing promise() function (typings out of date) #28

Closed HoldYourWaffle closed 4 years ago

HoldYourWaffle commented 5 years ago

These typings are unfortunately out of date, missing at least the promise() shortcut function on Pool and Connection.

I would love to contribute a PR to fix this but I don't know this library well enough to know what the return type would be.

NiekVelde commented 5 years ago

Promises are already implemented in the types. You can use mysql2 with promises with a slightly different import statement: import mysql from 'mysql2/promise'; After that import you can use mysql2 with promises. Simple example:

import mysql, { Pool } from 'mysql2/promise';

let mysqlPool: Pool = mysql.createPool({
    /* Connection params */
});

mysqlPool
    .execute(queryString)
    .then(
        (result): void => {
            console.log('Query executed!');
            console.log(result);
        },
    )
    .catch(
        (error): void => {
            console.log('Something went wrong...');
            console.log(error.message);
        },
    );

Hope this helps you out.

HoldYourWaffle commented 5 years ago

I know how to use mysql2 in 'promise mode' but I'm referring to the promise() function on Pool and Connection used to 'upgrade' the standard variants (as mentioned in the third and fourth block of the linked section).

alexiva0 commented 4 years ago

Please, consider this PR. I have same problem in my project and forced to use a workaround to fix this issue. This fix looks great.