Skip to content

Commit abb7f0f

Browse files
committed
feat: add notice event support
1 parent d6c144d commit abb7f0f

File tree

3 files changed

+69
-10
lines changed

3 files changed

+69
-10
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Supported features:
4040
* `pool.connect`
4141
* `connection.query`
4242
* `connect` event
43+
* `notice` event
4344

4445
Please submit PR if you require additional compatibility.
4546

src/bridge.ts

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,37 @@ type QueryResult = {
3737
};
3838

3939
export const bridge = (postgres: typeof Postgres, poolConfiguration: PgPool) => {
40-
const events = new EventEmitter();
40+
const poolEvents = new EventEmitter();
4141

42-
const pool = genericPool.createPool<AnySql>({
42+
const pool = genericPool.createPool<AnySql & {events: EventEmitter, }>({
4343
create: async () => {
44-
return postgres({
44+
const connectionEvents = new EventEmitter();
45+
46+
const connection = postgres({
4547
database: poolConfiguration.database,
4648
host: poolConfiguration.host ?? 'localhost',
4749
idle_timeout: poolConfiguration.idleTimeoutMillis ? poolConfiguration.idleTimeoutMillis / 1_000 : 0,
4850
max: 1,
51+
onnotice: (notice) => {
52+
connectionEvents.emit('notice', {
53+
code: notice.code,
54+
file: notice.file,
55+
line: notice.line,
56+
message: notice.message,
57+
routine: notice.routine,
58+
severity: notice.severity,
59+
where: notice.where,
60+
});
61+
},
4962
password: poolConfiguration.password,
5063
port: poolConfiguration.port ?? 5_432,
5164
ssl: poolConfiguration.ssl,
5265
username: poolConfiguration.user,
53-
});
66+
}) as AnySql & {events: EventEmitter, };
67+
68+
connection.events = connectionEvents;
69+
70+
return connection;
5471
},
5572
destroy: (client: Sql<{}>) => {
5673
return client.end({
@@ -67,29 +84,31 @@ export const bridge = (postgres: typeof Postgres, poolConfiguration: PgPool) =>
6784
const connection = await pool.acquire();
6885

6986
const compatibleConnection = {
87+
off: connection.events.off.bind(connection.events),
88+
on: connection.events.on.bind(connection.events),
7089
query: async (sql: string): Promise<QueryResult> => {
7190
// https://github.com/porsager/postgres#result-array
7291
const resultArray = await connection.unsafe(sql);
7392

7493
return {
7594
command: resultArray.command as Command,
76-
fields: resultArray.columns.map((column) => {
95+
fields: resultArray.columns?.map((column) => {
7796
return {
7897
dataTypeID: column.type,
7998
name: column.name,
8099
};
81-
}),
100+
}) ?? [],
82101
rowCount: resultArray.count,
83102
rows: Array.from(resultArray),
84103
};
85104
},
86105
};
87106

88-
events.emit('connect', compatibleConnection);
107+
poolEvents.emit('connect', compatibleConnection);
89108

90109
return compatibleConnection;
91110
},
92-
off: events.off.bind(events),
93-
on: events.on.bind(events),
111+
off: poolEvents.off.bind(poolEvents),
112+
on: poolEvents.on.bind(poolEvents),
94113
};
95114
};

test/postgres-bridge/bridge.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const createPool = (clientName: string, poolConfiguration) => {
2424
};
2525

2626
for (const client of clients) {
27-
test(client + ': connect event is fired when a new connection is made', async (t) => {
27+
test(client + ': "connect" event is fired when a new connection is made', async (t) => {
2828
const pool = createPool(client, {
2929
user: 'postgres',
3030
});
@@ -40,6 +40,45 @@ for (const client of clients) {
4040
t.is(spy.firstCall.args[0], connection);
4141
});
4242

43+
test(client + ': "notice event is fired when connection produces a notice"', async (t) => {
44+
const pool = createPool(client, {
45+
user: 'postgres',
46+
});
47+
48+
const connection = await pool.connect();
49+
50+
const spy = sinon.spy();
51+
52+
connection.on('notice', spy);
53+
54+
await connection.query(`
55+
CREATE OR REPLACE PROCEDURE raise_notice () AS $$
56+
DECLARE
57+
a INT:= 10;
58+
BEGIN
59+
RAISE NOTICE 'value of a: %', a;
60+
END;
61+
$$
62+
LANGUAGE plpgsql;
63+
`);
64+
65+
await connection.query(`
66+
CALL raise_notice();
67+
`);
68+
69+
t.true(spy.called);
70+
71+
t.like(spy.firstCall.args[0], {
72+
code: '00000',
73+
file: 'pl_exec.c',
74+
line: '3859',
75+
message: 'value of a: 10',
76+
routine: 'exec_stmt_raise',
77+
severity: 'NOTICE',
78+
where: 'PL/pgSQL function raise_notice() line 5 at RAISE',
79+
});
80+
});
81+
4382
test(client + ': query method', async (t) => {
4483
const pool = createPool(client, {
4584
user: 'postgres',

0 commit comments

Comments
 (0)