diff --git a/index.d.ts b/index.d.ts index f63883c112..733b5ded48 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,5 +1,6 @@ import { Connection as PromiseConnection, + Pool as PromisePool, PoolConnection as PromisePoolConnection, } from './promise'; @@ -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 | PromisePoolConnection): 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/test/tsc-build/mysql/createPool/callbacks/connection.ts b/test/tsc-build/mysql/createPool/callbacks/connection.ts new file mode 100644 index 0000000000..065db9333c --- /dev/null +++ b/test/tsc-build/mysql/createPool/callbacks/connection.ts @@ -0,0 +1,15 @@ +import { mysql } from '../../../index'; +import { access } from '../../baseConnection'; + +const pool = mysql.createPool(access); + +pool.getConnection((err, conn) => { + conn.connection; + + try { + // @ts-expect-error: The pool can't be a connection itself + pool.connection; + } catch (err) { + console.log('This error is expected', err); + } +}); diff --git a/test/tsc-build/mysql/createPool/callbacks/getConnection.ts b/test/tsc-build/mysql/createPool/callbacks/getConnection.ts new file mode 100644 index 0000000000..2e1bac5c09 --- /dev/null +++ b/test/tsc-build/mysql/createPool/callbacks/getConnection.ts @@ -0,0 +1,13 @@ +import { mysql } from '../../../index'; +import { access } from '../../baseConnection'; + +const pool = mysql.createPool(access); + +pool.getConnection((err, conn) => { + try { + // @ts-expect-error: The connection can't get another connection + conn.getConnection(); + } catch (err) { + console.log('This error is expected', err); + } +}); diff --git a/test/tsc-build/mysql/createPool/callbacks/release.ts b/test/tsc-build/mysql/createPool/callbacks/release.ts new file mode 100644 index 0000000000..346b5d1a7c --- /dev/null +++ b/test/tsc-build/mysql/createPool/callbacks/release.ts @@ -0,0 +1,15 @@ +import { mysql } from '../../../index'; +import { access } from '../../baseConnection'; + +const pool = mysql.createPool(access); + +pool.getConnection((err, conn) => { + conn.release(); + + try { + // @ts-expect-error: The pool isn't a connection itself, so it doesn't have the connection methods + pool.release(); + } catch (err) { + console.log('This error is expected', err); + } +}); diff --git a/test/tsc-build/mysql/createPool/callbacks/releaseConnection.ts b/test/tsc-build/mysql/createPool/callbacks/releaseConnection.ts new file mode 100644 index 0000000000..49c7dc9875 --- /dev/null +++ b/test/tsc-build/mysql/createPool/callbacks/releaseConnection.ts @@ -0,0 +1,8 @@ +import { mysql } from '../../../index'; +import { access } from '../../baseConnection'; + +const pool = mysql.createPool(access); + +pool.getConnection((err, conn) => { + pool.releaseConnection(conn); +}); diff --git a/test/tsc-build/mysql/createPool/promise/connection.ts b/test/tsc-build/mysql/createPool/promise/connection.ts new file mode 100644 index 0000000000..e6e5154d44 --- /dev/null +++ b/test/tsc-build/mysql/createPool/promise/connection.ts @@ -0,0 +1,16 @@ +import { mysql } from '../../../index'; +import { access } from '../../baseConnection'; + +(async () => { + const pool = mysql.createPool(access); + const conn = await pool.promise().getConnection(); + + conn.connection; + + try { + // @ts-expect-error: The pool can't be a connection itself + pool.connection; + } catch (err) { + console.log('This error is expected', err); + } +})(); diff --git a/test/tsc-build/mysql/createPool/promise/getConnection.ts b/test/tsc-build/mysql/createPool/promise/getConnection.ts new file mode 100644 index 0000000000..7acc34e962 --- /dev/null +++ b/test/tsc-build/mysql/createPool/promise/getConnection.ts @@ -0,0 +1,14 @@ +import { mysql } from '../../../index'; +import { access } from '../../baseConnection'; + +(async () => { + const pool = mysql.createPool(access); + const conn = await pool.promise().getConnection(); + + try { + // @ts-expect-error: The connection can't get another connection + conn.getConnection(); + } catch (err) { + console.log('This error is expected', err); + } +})(); diff --git a/test/tsc-build/mysql/createPool/promise/release.ts b/test/tsc-build/mysql/createPool/promise/release.ts new file mode 100644 index 0000000000..483bcdba79 --- /dev/null +++ b/test/tsc-build/mysql/createPool/promise/release.ts @@ -0,0 +1,16 @@ +import { mysql } from '../../../index'; +import { access } from '../../baseConnection'; + +(async () => { + const pool = mysql.createPool(access); + const conn = await pool.promise().getConnection(); + + conn.release(); + + try { + // @ts-expect-error: The pool isn't a connection itself, so it doesn't have the connection methods + pool.release(); + } catch (err) { + console.log('This error is expected', err); + } +})(); diff --git a/test/tsc-build/mysql/createPool/promise/releaseConnection.ts b/test/tsc-build/mysql/createPool/promise/releaseConnection.ts new file mode 100644 index 0000000000..f74de45d71 --- /dev/null +++ b/test/tsc-build/mysql/createPool/promise/releaseConnection.ts @@ -0,0 +1,9 @@ +import { mysql } from '../../../index'; +import { access } from '../../baseConnection'; + +(async () => { + const pool = mysql.createPool(access); + const conn = await pool.promise().getConnection(); + + pool.releaseConnection(conn); +})(); diff --git a/test/tsc-build/promise/createPool/connection.ts b/test/tsc-build/promise/createPool/connection.ts new file mode 100644 index 0000000000..a49a16e988 --- /dev/null +++ b/test/tsc-build/promise/createPool/connection.ts @@ -0,0 +1,16 @@ +import { mysqlp as mysql } from '../../index'; +import { access } from '../baseConnection'; + +(async () => { + const pool = mysql.createPool(access); + const conn = await pool.getConnection(); + + conn.connection; + + try { + // @ts-expect-error: The pool can't be a connection itself + pool.connection; + } catch (err) { + console.log('This error is expected', err); + } +})(); diff --git a/test/tsc-build/promise/createPool/getConnection.ts b/test/tsc-build/promise/createPool/getConnection.ts new file mode 100644 index 0000000000..8299d688e4 --- /dev/null +++ b/test/tsc-build/promise/createPool/getConnection.ts @@ -0,0 +1,16 @@ +import { mysqlp as mysql } from '../../index'; +import { access } from '../baseConnection'; + +(async () => { + const pool = mysql.createPool(access); + const conn = await pool.getConnection(); + + conn.connection; + + try { + // @ts-expect-error: The connection can't get another connection + conn.getConnection(); + } catch (err) { + console.log('This error is expected', err); + } +})(); diff --git a/test/tsc-build/promise/createPool/release.ts b/test/tsc-build/promise/createPool/release.ts new file mode 100644 index 0000000000..f09b2e214b --- /dev/null +++ b/test/tsc-build/promise/createPool/release.ts @@ -0,0 +1,16 @@ +import { mysqlp as mysql } from '../../index'; +import { access } from '../baseConnection'; + +(async () => { + const pool = mysql.createPool(access); + const conn = await pool.getConnection(); + + conn.release(); + + try { + // @ts-expect-error: The pool isn't a connection itself, so it doesn't have the connection methods + pool.release(); + } catch (err) { + console.log('This error is expected', err); + } +})(); diff --git a/test/tsc-build/promise/createPool/releaseConnection.ts b/test/tsc-build/promise/createPool/releaseConnection.ts new file mode 100644 index 0000000000..783aaef730 --- /dev/null +++ b/test/tsc-build/promise/createPool/releaseConnection.ts @@ -0,0 +1,9 @@ +import { mysqlp as mysql } from '../../index'; +import { access } from '../baseConnection'; + +(async () => { + const pool = mysql.createPool(access); + const conn = await pool.getConnection(); + + pool.releaseConnection(conn); +})(); 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;