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

Property '_freeConnections' does not exist on type 'Pool'.

Open FazilaMehtab opened this issue 2 years ago โ€ข 16 comments

Hello,

I am having an error Property '_freeConnections' does not exist on type 'Pool'.

image

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

FazilaMehtab avatar Aug 28 '23 16:08 FazilaMehtab

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 avatar Aug 28 '23 16:08 wellwelwel

@wellwelwel I can't access at any place, right now I am accessing in pool.on event to find the idle connection

image

FazilaMehtab avatar Aug 28 '23 16:08 FazilaMehtab

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 avatar Aug 28 '23 16:08 FazilaMehtab

@FazilaMehtab, when using images, I can't copy your code to test it locally ๐Ÿ˜•

wellwelwel avatar Aug 28 '23 16:08 wellwelwel

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 ๐Ÿ™‹๐Ÿปโ€โ™‚๏ธ

wellwelwel avatar Aug 28 '23 17:08 wellwelwel


 
  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')
            });
          } 
  }
  `

FazilaMehtab avatar Aug 28 '23 17:08 FazilaMehtab

@wellwelwel

FazilaMehtab avatar Aug 28 '23 17:08 FazilaMehtab

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 avatar Aug 28 '23 17:08 wellwelwel

@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?

FazilaMehtab avatar Aug 28 '23 17:08 FazilaMehtab

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

wellwelwel avatar Aug 28 '23 17:08 wellwelwel

yes its undefined at my end also I have checked @wellwelwel what do you think the issue is now?

FazilaMehtab avatar Aug 28 '23 18:08 FazilaMehtab

I have deleted my previous comment after some checks

@FazilaMehtab, just a typo fix: it's _freeConnections instead __freeConnections ๐Ÿ™‹๐Ÿปโ€โ™‚๏ธ


  1. JavaScript Issue In fact the _freeConnections works on JS, but only for callbacks. In mysql2/promise, it isn't exported.

  2. ~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 avatar Aug 28 '23 18:08 wellwelwel

@wellwelwel should I update package now?

FazilaMehtab avatar Aug 28 '23 18:08 FazilaMehtab

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 ๐Ÿคน๐Ÿปโ€โ™€๏ธ

wellwelwel avatar Aug 28 '23 18:08 wellwelwel

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/ )

sidorares avatar Aug 30 '23 01:08 sidorares

_freeConnections are 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.

wellwelwel avatar Aug 30 '23 01:08 wellwelwel