Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

PoolConnection type error after update v3.3.4 #2059

Closed
matvejs16 opened this issue Jun 12, 2023 · 13 comments
Closed

PoolConnection type error after update v3.3.4 #2059

matvejs16 opened this issue Jun 12, 2023 · 13 comments

Comments

@matvejs16
Copy link

Getting error: TS2551: Property 'getConnection' does not exist on type 'PoolConnection'. Did you mean 'connection'?

const connection = await DB.Handle_Promise.getConnection()
@wellwelwel
Copy link
Collaborator

wellwelwel commented Jun 12, 2023

Hi @matvejs16, I'm gradually looking for typings.

The PoolConnection is the pool connection itself, in theory, it shouldn't extend itself. After a lot tests in JS to get the conflicts between the types, always when I tried to get a connection from a Pool Connection, it gives me an error. Then in PR #2053, I changed it to only the Pool can get a connection, as seen in the documentation and according with ./lib/pool_connection.js, you can see that it doesn't have the getConnection method.

If your code works in JS (ignoring the type errors), can you share a basic repro example?

@matvejs16
Copy link
Author

matvejs16 commented Jun 12, 2023

Yep, JS is working ok

import mysql from 'mysql2'
import mysqlPromise from 'mysql2/promise'

interface DB {
    Handle: mysql.Pool | null,
    Handle_Promise: mysqlPromise.PoolConnection | null,
    Connect(callback: Function): void,
    escape(string: string | number | object | Array<any>): string | number | null,
    getPromiseConnection(): mysqlPromise.PoolConnection | undefined
}

const DB: DB = {
    Handle: null,
    Handle_Promise: null,
    Connect: function(callback: Function) {
        this.Handle = mysql.createPool({
            connectionLimit: 1000,
            host: 'IP',
            user: 'USER',
            password: 'PASS',
            database: 'DATABASE',
            debug: false,
            connectTimeout: 10000,
            multipleStatements: true
        })
        this.Handle.query('SET NAMES utf8mb4;');
        this.Handle.query('SET CHARACTER SET utf8mb4');
        this.Handle.query('SET COLLATION_CONNECTION="utf8mb4_general_ci"');
        this.Handle_Promise = this.Handle.promise()
        callback()
    },
    escape: function(string) {
        if (string == 'NULL') return string
        return mysql.escape(string)
    }
}

DB.Connect(function () {
    // @ts-ignore
    const connection = await DB.Handle_Promise.getConnection() as mysqlPromise.PoolConnection 
    await connection.beginTransaction()
    // RUN MySQL Query
    await connection.commit()
    connection.release()
})

@wellwelwel
Copy link
Collaborator

Yep, JS is working ok

@matvejs16, thanks. I'll test your example and look into it carefully.

@wellwelwel
Copy link
Collaborator

wellwelwel commented Jun 12, 2023

@matvejs16

So, I took a look at your example.

At first moment, I just commented the getPromiseConnection from DB Interface since it isn't being used. This resolved the const DB: DB missing getPromiseConnection type problem.

So, both the Handle and Handle_Promise can be null. To use some of their methods, you need before to check if they aren't null.

For example:

  • Interface changes
interface DB {
    Handle: mysql.Pool | null;
    Handle_Promise: mysqlPromise.Pool | null; // Now, it's a Pool from Promise 🙋🏻‍♂️
    Connect(callback: Function): void;
    escape(string: string | number | object | Array<any>): string | number | null;
    // getPromiseConnection(): mysqlPromise.PoolConnection | undefined;
}
  • Then
DB.Connect(async function () {
    const pool = DB.Handle_Promise;

    /**
     * Checking if pool isn't `null` will allow to use the pool methods.
     * Note that now, the `Handle_Promise` is a Pool and not a PoolConnection as previously
     */
    if (pool === null) return;

    const connection = await pool.getConnection();
    await connection.beginTransaction();
   // RUN MySQL Query
   await connection.commit();
   connection.release();
});

@matvejs16
Copy link
Author

matvejs16 commented Jun 12, 2023

Yep, I just took code blocks from working script and just haven't checked these TS errors, sorry.
How there is other TS error after changing to "Handle_Promise: mysqlPromise.Pool | null;": Type 'PoolConnection' is missing the following properties from type 'Pool': getConnection, releaseConnection, pool ts(2739)
image

@wellwelwel
Copy link
Collaborator

wellwelwel commented Jun 12, 2023

@matvejs16

So, in this example code that I do, you can test it and see after the null check, every method from pool will be available now and then, the const connection is a PoolConnection.

In fact, the PoolConnection doesn't have the methods getConnection and releaseConnection. These methods are from Pool.

@matvejs16
Copy link
Author

@matvejs16

So, in this example code that I do, you can test it and see after the null check, every method from pool will be available now and then, the const connection is a PoolConnection.

In fact, the PoolConnection doesn't have the methods getConnection and releaseConnection. These methods are from Pool.

Error is here:
image

@matvejs16
Copy link
Author

@matvejs16
So, in this example code that I do, you can test it and see after the null check, every method from pool will be available now and then, the const connection is a PoolConnection.
In fact, the PoolConnection doesn't have the methods getConnection and releaseConnection. These methods are from Pool.

Error is here: image

In this error code

this.Handle.promise()

Is returning type: mysqlPromise.PoolConnection

@wellwelwel
Copy link
Collaborator

@matvejs16, you right! I got this error after perform a tsc --init.

The .promise() still returning PoolConnection instead Pool.

Thanks for report 🙋🏻‍♂️

@wellwelwel
Copy link
Collaborator

@matvejs16, can you check this in ^3.3.5 version?

@matvejs16
Copy link
Author

matvejs16 commented Jun 13, 2023

@matvejs16, can you check this in ^3.3.5 version?

Sure, but not now, after I wake up 😉

@matvejs16
Copy link
Author

@matvejs16, can you check this in ^3.3.5 version?

After updating I got strange error.

Error: connect ETIMEDOUT
    at PoolConnection._handleTimeoutError (webpack://socketio-admin-backend/./node_modules/mysql2/lib/connection.js?:205:17)
    at listOnTimeout (node:internal/timers:569:17)
    at process.processTimers (node:internal/timers:512:7) {
  errorno: 'ETIMEDOUT',
  code: 'ETIMEDOUT',
  syscall: 'connect',
  fatal: true
}

@matvejs16
Copy link
Author

@matvejs16, can you check this in ^3.3.5 version?

After updating I got strange error.

Error: connect ETIMEDOUT
    at PoolConnection._handleTimeoutError (webpack://socketio-admin-backend/./node_modules/mysql2/lib/connection.js?:205:17)
    at listOnTimeout (node:internal/timers:569:17)
    at process.processTimers (node:internal/timers:512:7) {
  errorno: 'ETIMEDOUT',
  code: 'ETIMEDOUT',
  syscall: 'connect',
  fatal: true
}

Sorry, my fault!
On v3.3.5 everything works good

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

2 participants