Skip to content

Commit 155af92

Browse files
committed
feat: fire connect event
1 parent a111da0 commit 155af92

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Supported features:
3939

4040
* `pool.connect`
4141
* `connection.query`
42+
* `connect` event
4243

4344
Please submit PR if you require additional compatibility.
4445

src/bridge.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import {
2+
EventEmitter,
3+
} from 'node:events';
14
import genericPool from 'generic-pool';
25
import type Postgres from 'postgres';
36
import {
@@ -34,6 +37,8 @@ type QueryResult = {
3437
};
3538

3639
export const bridge = (postgres: typeof Postgres, poolConfiguration: PgPool) => {
40+
const events = new EventEmitter();
41+
3742
const pool = genericPool.createPool<AnySql>({
3843
create: async () => {
3944
return postgres({
@@ -61,7 +66,7 @@ export const bridge = (postgres: typeof Postgres, poolConfiguration: PgPool) =>
6166
connect: async () => {
6267
const connection = await pool.acquire();
6368

64-
return {
69+
const compatibleConnection = {
6570
query: async (sql: string): Promise<QueryResult> => {
6671
// https://github.com/porsager/postgres#result-array
6772
const resultArray = await connection.unsafe(sql);
@@ -79,6 +84,12 @@ export const bridge = (postgres: typeof Postgres, poolConfiguration: PgPool) =>
7984
};
8085
},
8186
};
87+
88+
events.emit('connect', compatibleConnection);
89+
90+
return compatibleConnection;
8291
},
92+
off: events.off.bind(events),
93+
on: events.on.bind(events),
8394
};
8495
};

0 commit comments

Comments
 (0)