Skip to content

Promise Pool events inherit original pool connection class, not promise pool #3238

Open
@ashleybartlett

Description

@ashleybartlett

When using a Promise Pool, and subscribing to events (e.g.pool.on('acquire', (connection) => {})) the connection object does not match the type.

In the types, the connection type is exposed as a Promise PoolConnection, where in reality there is no conversion from the original, and so it's actually a base PoolConnection type. Meaning if I want to use it, i have to call the Promise() helper, which isn't on the Promise PoolConnection type.

This is causing a bit of confusion on my end, and i'm not sure what the expected behaviour is? Should these be converted to PromisePoolConnections, or should the types be updated appropriately?

import { PoolConnection } from 'mysql2'
import mysql from 'mysql2/promise'

// this is a PromisePool
const pool = mysql.createPool(options);

// PoolConnection here is NOT a PoolConnection from the Promise lib
// but typescript thinks it is, and so is giving an error.

pool.on('acquire', async (connection: PoolConnection) => {
    await connection.promise().beginTransaction();
  });

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions