node-mysql2
node-mysql2 copied to clipboard
Property '_freeConnections' does not exist on type 'Pool'.
Hello,
I am having an error Property '_freeConnections' does not exist on type 'Pool'.
because _freeConnections key missing in pool interface https://github.com/sidorares/node-mysql2/blob/master/typings/mysql/lib/Pool.d.ts
although I can find it https://github.com/sidorares/node-mysql2/blob/master/lib/pool.js file
@silentrob need your assistance to fix this issue. thanks
Hey @FazilaMehtab ๐๐ปโโ๏ธ
Can you show a way to reproduce this error?
Does it happen during the code or after run a specific method like query, getConnection(), etc.?
@wellwelwel I can't access at any place, right now I am accessing in pool.on event to find the idle connection
I have tried to access on different level also like after getConnection but it didn't work.
Actually I need to find idle/active connection of pool but I didn't find a proper way to do
@FazilaMehtab, when using images, I can't copy your code to test it locally ๐
Could you test if ignoring that TypeScript error makes your code work in JavaScript?
If you can, please share a minimalist example to test this error in practice ๐๐ปโโ๏ธ
export abstract class DbConn {
private static Pool: Pool | null = null;
private static poolIdelOpenConnections: Record<string, IPoolConnection> = {};
public static async initialize(): Promise<void> {
DbConnections.Pool = createPool({
host:'',
port:3306,
database: 'dbName',
user: DbConnections.dbCredentials.user,
password: DbConnections.dbCredentials.password,
waitForConnections: true,
});
DbConnections.subscribePoolEvents(
"dbname"
);
}
private static subscribePoolEvents( database: string): void {
DbConnections.poolIdelOpenConnections[database] = {
acquiredConnections: 0,
poolSize: 0,
queueSize: 0,
};
const pool = createPool({
host:'',
port:3306,
database: database,
user: user,
password: password,
waitForConnections: true,
});
pool.on(POOL_EVENTS.ACQUIRE, async (con) => {
DbConnections.poolIdelOpenConnections[database].acquiredConnections += 1;
});
pool.on(POOL_EVENTS.CONNECTION, () => {
DbConnections.poolIdelOpenConnections[database].poolSize += 1;
});
pool.on(POOL_EVENTS.ENQUEUE, () => {
DbConnections.poolIdelOpenConnections[database].queueSize += 1;
});
pool.on(POOL_EVENTS.RELEASE, async (con) => {
const freeCon = pool.__freeConnections
DbConnections.poolIdelOpenConnections[database].acquiredConnections -= 1;
// const intervalConnection = setInterval(() => {
// con.query("select 1", function (error: any, results: any, fields: any) {
// if (error) {
// DbConnections.poolIdelOpenConnections[database].queueSize -= 1;
// clearInterval(intervalConnection);
// }
// });
// }, 10000);
const idle =
DbConnections.poolIdelOpenConnections[database].poolSize -
DbConnections.poolIdelOpenConnections[database].acquiredConnections;
console.log(DbConnections.poolIdelOpenConnections);
console.log( DbConnections.Pool?._freeConnections,'-----_freeConnections')
});
}
}
`
@wellwelwel
Could you test if ignoring that TypeScript error makes your code work in JavaScript?
@FazilaMehtab, it will help to understand if this error is part of the types (incompatibility with the main code in JS).
@wellwelwel let me try code on javascript, yes this error is related to types for sure because I cant find these properties in interface https://github.com/sidorares/node-mysql2/blob/master/typings/mysql/lib/Pool.d.ts
anyway to bypass typescript of particular area not for whole script?
anyway to bypass typescript of particular area not for whole script?
Yes, you can ignore the next line by inserting the comment:
// @ts-ignore
In case you use the ban-ts-comment from ESLint:
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
yes its undefined at my end also I have checked @wellwelwel what do you think the issue is now?
I have deleted my previous comment after some checks
@FazilaMehtab, just a typo fix: it's _freeConnections instead __freeConnections ๐๐ปโโ๏ธ
-
JavaScript Issue In fact the
_freeConnectionsworks on JS, but only for callbacks. Inmysql2/promise, it isn't exported. -
~TypeScript Issue~ ~Since it exists, yes, the types really are missing, but it needs to be fixed first in
mysql2/promise(JS).~
โ It will work perfectly:
import { createPool } from 'mysql2';
const pool = createPool({
host: 'localhost',
user: 'root',
database: 'test',
});
pool.on('release', () => {
// @ts-ignore
console.log(pool._freeConnections);
});
pool.getConnection((_, conn) => {
pool.releaseConnection(conn);
});
pool.getConnection((_, conn) => {
conn.release();
});
โ It will not work:
import { createPool } from 'mysql2/promise';
const pool = createPool({
host: 'localhost',
user: 'root',
database: 'test',
});
pool.on('release', () => {
// @ts-ignore
console.log(pool._freeConnections);
});
const conn = await pool.getConnection();
pool.releaseConnection(conn);
const conn2 = await pool.getConnection();
conn2.release();
@FazilaMehtab, thanks for your report ๐๐ปโโ๏ธ
@wellwelwel should I update package now?
should I update package now?
@FazilaMehtab, feel free to contribute ๐
I don't have much time these days ๐
But, if it's not fixed until I have some time, I'll put it on my to-do list ๐คน๐ปโโ๏ธ
We need to think or possible api to diagnose state of the pool. _freeConnections are internal variables and not ment to be used ( but of course, https://www.hyrumslaw.com/ )
_freeConnectionsare internal variables and not ment to be used
@sidorares, I wanted to ask you that, thanks for bringing this forward ๐๐ปโโ๏ธ
We need to think or possible api to diagnose state of the pool.
Also, I think this would be a good feature.