sidorares / node-mysql2

:zap: fast mysqljs/mysql compatible mysql driver for node.js
https://sidorares.github.io/node-mysql2/
MIT License
3.99k stars 603 forks source link

Incorrect typings lead to pool.destroy is not a function #2667

Open dancrumb opened 1 month ago

dancrumb commented 1 month ago

I'm using version 3.9.7 of mysql2, running TypeScript 5.4.3

I'm importing this module with import mysql from "mysql2/promise";

When I create a Pool with mysql.createPool, I get my pool and am able to use it without any problems.

However, when I come to wrap things up, I call pool.destroy(). No complaints from TS, but when I run the code, I get:

<REDACTED>/db/stores/SqlStore.js:19
        pool.destroy();
             ^

TypeError: pool.destroy is not a function
    at SqlStore.shutdown (<REDACTED>/db/stores/SqlStore.js:19:14)
    at <REDACTED>/tools/migrate-to-mongo.mts:85:54
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

I took a look at promises.d.ts in the code base and I can see why there's no type error:

export interface Pool extends Connection {

Since destroy is a method in the Connection interface, it's also a method in the Pool interface.

I'm not sure if the bug is the assertion that Pool extends Connection or whether it's just that Pool only supports some methods of Connection.

dancrumb commented 1 month ago

I'm guessing that this is probably what's going on with https://github.com/sidorares/node-mysql2/issues/2465

wellwelwel commented 1 month ago

Thanks, @dancrumb 🙋🏻‍♂️


I'll keep some debugging to check later:

import mysql from 'mysql2';
// import mysql from 'mysql2/promise';

const pool = mysql.createPool({});

typeof pool.addListener === 'undefined' && console.log('addListener');
typeof pool.beginTransaction === 'undefined' && console.log('beginTransaction');
typeof pool.changeUser === 'undefined' && console.log('changeUser');
typeof pool.commit === 'undefined' && console.log('commit');
typeof pool.config === 'undefined' && console.log('config');
typeof pool.connect === 'undefined' && console.log('connect');
typeof pool.destroy === 'undefined' && console.log('destroy');
typeof pool.emit === 'undefined' && console.log('emit');
typeof pool.end === 'undefined' && console.log('end');
typeof pool.escape === 'undefined' && console.log('escape');
typeof pool.escapeId === 'undefined' && console.log('escapeId');
typeof pool.eventNames === 'undefined' && console.log('eventNames');
typeof pool.execute === 'undefined' && console.log('execute');
typeof pool.format === 'undefined' && console.log('format');
typeof pool.getConnection === 'undefined' && console.log('getConnection');
typeof pool.getMaxListeners === 'undefined' && console.log('getMaxListeners');
typeof pool.listenerCount === 'undefined' && console.log('listenerCount');
typeof pool.listeners === 'undefined' && console.log('listeners');
typeof pool.off === 'undefined' && console.log('off');
typeof pool.on === 'undefined' && console.log('on');
typeof pool.once === 'undefined' && console.log('once');
typeof pool.pause === 'undefined' && console.log('pause');
typeof pool.ping === 'undefined' && console.log('ping');
typeof pool.pool === 'undefined' && console.log('pool');
typeof pool.prepare === 'undefined' && console.log('prepare');
typeof pool.prependListener === 'undefined' && console.log('prependListener');
typeof pool.prependOnceListener === 'undefined' && console.log('prependOnceListener');
typeof pool.query === 'undefined' && console.log('query');
typeof pool.rawListeners === 'undefined' && console.log('rawListeners');
typeof pool.releaseConnection === 'undefined' && console.log('releaseConnection');
typeof pool.removeAllListeners === 'undefined' && console.log('removeAllListeners');
typeof pool.removeListener === 'undefined' && console.log('removeListener');
typeof pool.resume === 'undefined' && console.log('resume');
typeof pool.rollback === 'undefined' && console.log('rollback');
typeof pool.setMaxListeners === 'undefined' && console.log('setMaxListeners');
typeof pool.threadId === 'undefined' && console.log('threadId');
typeof pool.unprepare === 'undefined' && console.log('unprepare');
typeof pool.promise === 'undefined' && console.log('promise');

✅ = Properly typed (no typings for non-existent methods) ❌ = Incorrectly typed (probably) — Need to look into them

pool mysql2 mysql2/promise
beginTransaction
changeUser
commit
config
connect
destroy
pause
ping
prepare
resume
rollback
threadId
unprepare

All cited methods are based in undefined type results, but have typings brought directly from the Connection interface.