From 4aac9d6a1b379253fa90195ffdc98886b3b87a1b Mon Sep 17 00:00:00 2001 From: wellwelwel <46850407+wellwelwel@users.noreply.github.com> Date: Sun, 11 Jun 2023 00:34:12 -0300 Subject: [PATCH] fix: `releaseConnection` types and promise --- index.d.ts | 8 +++++--- promise.d.ts | 9 ++++----- promise.js | 4 ++++ .../promise-wrappers/test-promise-wrappers.js | 10 ++++++++++ typings/mysql/lib/Pool.d.ts | 2 ++ 5 files changed, 25 insertions(+), 8 deletions(-) diff --git a/index.d.ts b/index.d.ts index f63883c112..e718ea577b 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,6 +1,7 @@ import { Connection as PromiseConnection, - PoolConnection as PromisePoolConnection, + Pool as PromisePool, + PoolConnection as PromisePoolConnection2, } from './promise'; import * as mysql from './typings/mysql'; @@ -83,7 +84,7 @@ export interface Connection extends mysql.Connection { } export interface PoolConnection extends mysql.PoolConnection { - promise(promiseImpl?: PromiseConstructor): PromisePoolConnection; + promise(promiseImpl?: PromiseConstructor): PromisePool; } export interface Pool extends mysql.Connection { @@ -152,13 +153,14 @@ export interface Pool extends mysql.Connection { getConnection( callback: (err: NodeJS.ErrnoException, connection: PoolConnection) => any ): void; + releaseConnection(connection: PoolConnection | PromisePoolConnection2): void; on(event: 'connection', listener: (connection: PoolConnection) => any): this; on(event: 'acquire', listener: (connection: PoolConnection) => any): this; on(event: 'release', listener: (connection: PoolConnection) => any): this; on(event: 'enqueue', listener: () => any): this; unprepare(sql: string): mysql.PrepareStatementInfo; prepare(sql: string, callback?: (err: mysql.QueryError | null, statement: mysql.PrepareStatementInfo) => any): mysql.Prepare; - promise(promiseImpl?: PromiseConstructor): PromisePoolConnection; + promise(promiseImpl?: PromiseConstructor): PromisePool; config: mysql.PoolOptions; } diff --git a/promise.d.ts b/promise.d.ts index 5fad9bacdf..ceee67cb90 100644 --- a/promise.d.ts +++ b/promise.d.ts @@ -83,12 +83,11 @@ export interface Connection extends EventEmitter { } export interface PoolConnection extends Connection { - connection: Connection; - getConnection(): Promise; release(): void; + connection: Connection; } -export interface Pool extends EventEmitter { +export interface Pool extends EventEmitter, Connection { query( sql: string ): Promise<[T, FieldPacket[]]>; @@ -128,6 +127,7 @@ export interface Pool extends EventEmitter { ): Promise<[T, FieldPacket[]]>; getConnection(): Promise; + releaseConnection(connection: PoolConnection): void; on(event: 'connection', listener: (connection: PoolConnection) => any): this; on(event: 'acquire', listener: (connection: PoolConnection) => any): this; on(event: 'release', listener: (connection: PoolConnection) => any): this; @@ -138,7 +138,7 @@ export interface Pool extends EventEmitter { escapeId(value: string): string; escapeId(values: string[]): string; format(sql: string, values?: any | any[] | { [param: string]: any }): string; - + pool: CorePool; } @@ -153,4 +153,3 @@ export interface PreparedStatementInfo { close(): Promise; execute(parameters: any[]): Promise<[RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader, FieldPacket[]]>; } - diff --git a/promise.js b/promise.js index a264e77d5f..cde9c5035d 100644 --- a/promise.js +++ b/promise.js @@ -346,6 +346,10 @@ class PromisePool extends EventEmitter { }); } + releaseConnection(connection) { + if (connection instanceof PromisePoolConnection) connection.release(); + } + query(sql, args) { const corePool = this.pool; const localErr = new Error(); diff --git a/test/integration/promise-wrappers/test-promise-wrappers.js b/test/integration/promise-wrappers/test-promise-wrappers.js index 08fc554d5d..6a1fc17448 100644 --- a/test/integration/promise-wrappers/test-promise-wrappers.js +++ b/test/integration/promise-wrappers/test-promise-wrappers.js @@ -207,6 +207,16 @@ function testEventsConnect() { function testBasicPool() { const pool = createPool(config); + const promiseConn = pool.getConnection(); + + promiseConn + .then(connResolved => { + pool.releaseConnection(connResolved); + }) + .catch(err => { + throw err; + }); + pool .query('select 1+2 as ttt') .then(result1 => { diff --git a/typings/mysql/lib/Pool.d.ts b/typings/mysql/lib/Pool.d.ts index 4ad74e772f..29c4891f9d 100644 --- a/typings/mysql/lib/Pool.d.ts +++ b/typings/mysql/lib/Pool.d.ts @@ -61,6 +61,8 @@ declare class Pool extends EventEmitter { getConnection(callback: (err: NodeJS.ErrnoException | null, connection: PoolConnection) => any): void; + releaseConnection(connection: PoolConnection): void; + query(sql: string, callback?: (err: Query.QueryError | null, result: T, fields: FieldPacket[]) => any): Query; query(sql: string, values: any | any[] | { [param: string]: any }, callback?: (err: Query.QueryError | null, result: T, fields: FieldPacket[]) => any): Query; query(options: Query.QueryOptions, callback?: (err: Query.QueryError | null, result: T, fields?: FieldPacket[]) => any): Query;