Skip to content

Commit

Permalink
fix: releaseConnection types and promise
Browse files Browse the repository at this point in the history
  • Loading branch information
wellwelwel committed Jun 11, 2023
1 parent f930f6e commit 4aac9d6
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
8 changes: 5 additions & 3 deletions 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';
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}

Expand Down
9 changes: 4 additions & 5 deletions promise.d.ts
Expand Up @@ -83,12 +83,11 @@ export interface Connection extends EventEmitter {
}

export interface PoolConnection extends Connection {
connection: Connection;
getConnection(): Promise<PoolConnection>;
release(): void;
connection: Connection;
}

export interface Pool extends EventEmitter {
export interface Pool extends EventEmitter, Connection {
query<T extends RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader>(
sql: string
): Promise<[T, FieldPacket[]]>;
Expand Down Expand Up @@ -128,6 +127,7 @@ export interface Pool extends EventEmitter {
): Promise<[T, FieldPacket[]]>;

getConnection(): Promise<PoolConnection>;
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;
Expand All @@ -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;
}

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

4 changes: 4 additions & 0 deletions promise.js
Expand Up @@ -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();
Expand Down
10 changes: 10 additions & 0 deletions test/integration/promise-wrappers/test-promise-wrappers.js
Expand Up @@ -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 => {
Expand Down
2 changes: 2 additions & 0 deletions typings/mysql/lib/Pool.d.ts
Expand Up @@ -61,6 +61,8 @@ declare class Pool extends EventEmitter {

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

releaseConnection(connection: PoolConnection): void;

query<T extends RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader>(sql: string, callback?: (err: Query.QueryError | null, result: T, fields: FieldPacket[]) => any): Query;
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;
query<T extends RowDataPacket[][] | RowDataPacket[] | OkPacket | OkPacket[] | ResultSetHeader>(options: Query.QueryOptions, callback?: (err: Query.QueryError | null, result: T, fields?: FieldPacket[]) => any): Query;
Expand Down

0 comments on commit 4aac9d6

Please sign in to comment.