Skip to content

Commit 4aac9d6

Browse files
committed
fix: releaseConnection types and promise
1 parent f930f6e commit 4aac9d6

File tree

5 files changed

+25
-8
lines changed

5 files changed

+25
-8
lines changed

index.d.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
Connection as PromiseConnection,
3-
PoolConnection as PromisePoolConnection,
3+
Pool as PromisePool,
4+
PoolConnection as PromisePoolConnection2,
45
} from './promise';
56

67
import * as mysql from './typings/mysql';
@@ -83,7 +84,7 @@ export interface Connection extends mysql.Connection {
8384
}
8485

8586
export interface PoolConnection extends mysql.PoolConnection {
86-
promise(promiseImpl?: PromiseConstructor): PromisePoolConnection;
87+
promise(promiseImpl?: PromiseConstructor): PromisePool;
8788
}
8889

8990
export interface Pool extends mysql.Connection {
@@ -152,13 +153,14 @@ export interface Pool extends mysql.Connection {
152153
getConnection(
153154
callback: (err: NodeJS.ErrnoException, connection: PoolConnection) => any
154155
): void;
156+
releaseConnection(connection: PoolConnection | PromisePoolConnection2): void;
155157
on(event: 'connection', listener: (connection: PoolConnection) => any): this;
156158
on(event: 'acquire', listener: (connection: PoolConnection) => any): this;
157159
on(event: 'release', listener: (connection: PoolConnection) => any): this;
158160
on(event: 'enqueue', listener: () => any): this;
159161
unprepare(sql: string): mysql.PrepareStatementInfo;
160162
prepare(sql: string, callback?: (err: mysql.QueryError | null, statement: mysql.PrepareStatementInfo) => any): mysql.Prepare;
161-
promise(promiseImpl?: PromiseConstructor): PromisePoolConnection;
163+
promise(promiseImpl?: PromiseConstructor): PromisePool;
162164
config: mysql.PoolOptions;
163165
}
164166

promise.d.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,11 @@ export interface Connection extends EventEmitter {
8383
}
8484

8585
export interface PoolConnection extends Connection {
86-
connection: Connection;
87-
getConnection(): Promise<PoolConnection>;
8886
release(): void;
87+
connection: Connection;
8988
}
9089

91-
export interface Pool extends EventEmitter {
90+
export interface Pool extends EventEmitter, Connection {
9291
query<T extends RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader>(
9392
sql: string
9493
): Promise<[T, FieldPacket[]]>;
@@ -128,6 +127,7 @@ export interface Pool extends EventEmitter {
128127
): Promise<[T, FieldPacket[]]>;
129128

130129
getConnection(): Promise<PoolConnection>;
130+
releaseConnection(connection: PoolConnection): void;
131131
on(event: 'connection', listener: (connection: PoolConnection) => any): this;
132132
on(event: 'acquire', listener: (connection: PoolConnection) => any): this;
133133
on(event: 'release', listener: (connection: PoolConnection) => any): this;
@@ -138,7 +138,7 @@ export interface Pool extends EventEmitter {
138138
escapeId(value: string): string;
139139
escapeId(values: string[]): string;
140140
format(sql: string, values?: any | any[] | { [param: string]: any }): string;
141-
141+
142142
pool: CorePool;
143143
}
144144

@@ -153,4 +153,3 @@ export interface PreparedStatementInfo {
153153
close(): Promise<void>;
154154
execute(parameters: any[]): Promise<[RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader, FieldPacket[]]>;
155155
}
156-

promise.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,10 @@ class PromisePool extends EventEmitter {
346346
});
347347
}
348348

349+
releaseConnection(connection) {
350+
if (connection instanceof PromisePoolConnection) connection.release();
351+
}
352+
349353
query(sql, args) {
350354
const corePool = this.pool;
351355
const localErr = new Error();

test/integration/promise-wrappers/test-promise-wrappers.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,16 @@ function testEventsConnect() {
207207

208208
function testBasicPool() {
209209
const pool = createPool(config);
210+
const promiseConn = pool.getConnection();
211+
212+
promiseConn
213+
.then(connResolved => {
214+
pool.releaseConnection(connResolved);
215+
})
216+
.catch(err => {
217+
throw err;
218+
});
219+
210220
pool
211221
.query('select 1+2 as ttt')
212222
.then(result1 => {

typings/mysql/lib/Pool.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ declare class Pool extends EventEmitter {
6161

6262
getConnection(callback: (err: NodeJS.ErrnoException | null, connection: PoolConnection) => any): void;
6363

64+
releaseConnection(connection: PoolConnection): void;
65+
6466
query<T extends RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader>(sql: string, callback?: (err: Query.QueryError | null, result: T, fields: FieldPacket[]) => any): Query;
6567
query<T extends RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader>(sql: string, values: any | any[] | { [param: string]: any }, callback?: (err: Query.QueryError | null, result: T, fields: FieldPacket[]) => any): Query;
6668
query<T extends RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader>(options: Query.QueryOptions, callback?: (err: Query.QueryError | null, result: T, fields?: FieldPacket[]) => any): Query;

0 commit comments

Comments
 (0)