Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: releaseConnection types and promise #2053

Merged
merged 6 commits into from
Jun 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
Connection as PromiseConnection,
Pool as PromisePool,
PoolConnection as PromisePoolConnection,
} from './promise';

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 | 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;
}

Expand Down
9 changes: 4 additions & 5 deletions promise.d.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
15 changes: 15 additions & 0 deletions test/tsc-build/mysql/createPool/callbacks/connection.ts
Original file line number Diff line number Diff line change
@@ -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);
}
});
13 changes: 13 additions & 0 deletions test/tsc-build/mysql/createPool/callbacks/getConnection.ts
Original file line number Diff line number Diff line change
@@ -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);
}
});
15 changes: 15 additions & 0 deletions test/tsc-build/mysql/createPool/callbacks/release.ts
Original file line number Diff line number Diff line change
@@ -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);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { mysql } from '../../../index';
import { access } from '../../baseConnection';

const pool = mysql.createPool(access);

pool.getConnection((err, conn) => {
pool.releaseConnection(conn);
});
16 changes: 16 additions & 0 deletions test/tsc-build/mysql/createPool/promise/connection.ts
Original file line number Diff line number Diff line change
@@ -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);
}
})();
14 changes: 14 additions & 0 deletions test/tsc-build/mysql/createPool/promise/getConnection.ts
Original file line number Diff line number Diff line change
@@ -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);
}
})();
16 changes: 16 additions & 0 deletions test/tsc-build/mysql/createPool/promise/release.ts
Original file line number Diff line number Diff line change
@@ -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);
}
})();
9 changes: 9 additions & 0 deletions test/tsc-build/mysql/createPool/promise/releaseConnection.ts
Original file line number Diff line number Diff line change
@@ -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);
})();
16 changes: 16 additions & 0 deletions test/tsc-build/promise/createPool/connection.ts
Original file line number Diff line number Diff line change
@@ -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);
}
})();
16 changes: 16 additions & 0 deletions test/tsc-build/promise/createPool/getConnection.ts
Original file line number Diff line number Diff line change
@@ -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);
}
})();
16 changes: 16 additions & 0 deletions test/tsc-build/promise/createPool/release.ts
Original file line number Diff line number Diff line change
@@ -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);
}
})();
9 changes: 9 additions & 0 deletions test/tsc-build/promise/createPool/releaseConnection.ts
Original file line number Diff line number Diff line change
@@ -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);
})();
2 changes: 2 additions & 0 deletions typings/mysql/lib/Pool.d.ts
Original file line number Diff line number Diff line change
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