node-mysql2 icon indicating copy to clipboard operation
node-mysql2 copied to clipboard

Incorrect typings lead to pool.destroy is not a function

Open dancrumb opened this issue 1 year ago • 2 comments

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 avatar May 10 '24 15:05 dancrumb

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

dancrumb avatar May 10 '24 15:05 dancrumb

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.

wellwelwel avatar May 12 '24 06:05 wellwelwel