Skip to content
Open
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
8 changes: 7 additions & 1 deletion lib/promise/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ class PromisePool extends EventEmitter {
super();
this.pool = pool;
this.Promise = thePromise || Promise;
inheritEvents(pool, this, ['acquire', 'connection', 'enqueue', 'release']);
inheritEvents(pool, this, [
'acquire',
'connection',
'enqueue',
'error',
'release',
]);
}

getConnection() {
Expand Down
1 change: 1 addition & 0 deletions promise.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export interface Pool extends Connection {

releaseConnection(connection: PoolConnection): void;

on(event: 'error', listener: (err: NodeJS.ErrnoException) => any): this;
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 Down
12 changes: 11 additions & 1 deletion test/integration/promise-wrappers/test-promise-wrappers.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ await describe('testEventsPool', async () => {
acquire: 0,
connection: 0,
enqueue: 0,
error: 0,
release: 0,
};
for (const eventName in expectedListeners) {
Expand Down Expand Up @@ -291,6 +292,14 @@ await describe('testEventsPool', async () => {
++events;
}.bind(pool)
)
.once(
'error',
function (err: Error) {
strict.equal(this, pool);
strict.equal(err.message, 'pool error');
++events;
}.bind(pool)
)
.once(
'release',
function () {
Expand All @@ -302,9 +311,10 @@ await describe('testEventsPool', async () => {
pool.pool.emit('acquire');
pool.pool.emit('connection');
pool.pool.emit('enqueue');
pool.pool.emit('error', new Error('pool error'));
pool.pool.emit('release');

strict.equal(events, 4, 'wrong number of pool connection events');
strict.equal(events, 5, 'wrong number of pool connection events');

for (const eventName in expectedListeners) {
strict.equal(
Expand Down
14 changes: 14 additions & 0 deletions test/tsc-build/promise/createPool/on.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { mysqlp as mysql } from '../../index.test.js';
import { access } from '../baseConnection.test.js';

const pool = mysql.createPool(access);

pool.on('error', (err) => {
const code: string | undefined = err.code;
code;
err.message;
});

pool.on('connection', (connection) => {
connection.threadId;
});
Loading