Skip to content

Commit 855c43c

Browse files
authored
Merge pull request #2053 from wellwelwel/release-connection
fix: releaseConnection types and promise
2 parents a6c3640 + 76db54a commit 855c43c

File tree

17 files changed

+187
-7
lines changed

17 files changed

+187
-7
lines changed

index.d.ts

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

@@ -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 | PromisePoolConnection): 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 => {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { mysql } from '../../../index';
2+
import { access } from '../../baseConnection';
3+
4+
const pool = mysql.createPool(access);
5+
6+
pool.getConnection((err, conn) => {
7+
conn.connection;
8+
9+
try {
10+
// @ts-expect-error: The pool can't be a connection itself
11+
pool.connection;
12+
} catch (err) {
13+
console.log('This error is expected', err);
14+
}
15+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { mysql } from '../../../index';
2+
import { access } from '../../baseConnection';
3+
4+
const pool = mysql.createPool(access);
5+
6+
pool.getConnection((err, conn) => {
7+
try {
8+
// @ts-expect-error: The connection can't get another connection
9+
conn.getConnection();
10+
} catch (err) {
11+
console.log('This error is expected', err);
12+
}
13+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { mysql } from '../../../index';
2+
import { access } from '../../baseConnection';
3+
4+
const pool = mysql.createPool(access);
5+
6+
pool.getConnection((err, conn) => {
7+
conn.release();
8+
9+
try {
10+
// @ts-expect-error: The pool isn't a connection itself, so it doesn't have the connection methods
11+
pool.release();
12+
} catch (err) {
13+
console.log('This error is expected', err);
14+
}
15+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { mysql } from '../../../index';
2+
import { access } from '../../baseConnection';
3+
4+
const pool = mysql.createPool(access);
5+
6+
pool.getConnection((err, conn) => {
7+
pool.releaseConnection(conn);
8+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { mysql } from '../../../index';
2+
import { access } from '../../baseConnection';
3+
4+
(async () => {
5+
const pool = mysql.createPool(access);
6+
const conn = await pool.promise().getConnection();
7+
8+
conn.connection;
9+
10+
try {
11+
// @ts-expect-error: The pool can't be a connection itself
12+
pool.connection;
13+
} catch (err) {
14+
console.log('This error is expected', err);
15+
}
16+
})();
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { mysql } from '../../../index';
2+
import { access } from '../../baseConnection';
3+
4+
(async () => {
5+
const pool = mysql.createPool(access);
6+
const conn = await pool.promise().getConnection();
7+
8+
try {
9+
// @ts-expect-error: The connection can't get another connection
10+
conn.getConnection();
11+
} catch (err) {
12+
console.log('This error is expected', err);
13+
}
14+
})();

0 commit comments

Comments
 (0)