Skip to content

Commit

Permalink
Fix NOTIFY calls without parameter (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
andywer authored Aug 7, 2019
1 parent 736709c commit 252cca3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
2 changes: 1 addition & 1 deletion dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export interface Subscriber {
close(): Promise<void>;
getSubscribedChannels(): string[];
listenTo(channelName: string): Promise<pg.QueryResult> | undefined;
notify(channelName: string, payload: any): Promise<pg.QueryResult>;
notify(channelName: string, payload?: any): Promise<pg.QueryResult>;
unlisten(channelName: string): Promise<pg.QueryResult> | undefined;
unlistenAll(): Promise<pg.QueryResult>;
}
Expand Down
15 changes: 10 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ function forwardDBNotificationEvents (dbClient: pg.Client, emitter: TypedEventEm

let payload
try {
payload = notification.payload !== undefined ? parse(notification.payload) : undefined
payload = notification.payload ? parse(notification.payload) : undefined
} catch (error) {
error.message = `Error parsing PostgreSQL notification payload: ${error.message}`
return emitter.emit("error", error)
Expand Down Expand Up @@ -181,7 +181,7 @@ export interface Subscriber {
close(): Promise<void>;
getSubscribedChannels(): string[];
listenTo(channelName: string): Promise<pg.QueryResult> | undefined;
notify(channelName: string, payload: any): Promise<pg.QueryResult>;
notify(channelName: string, payload?: any): Promise<pg.QueryResult>;
unlisten(channelName: string): Promise<pg.QueryResult> | undefined;
unlistenAll(): Promise<pg.QueryResult>;
}
Expand Down Expand Up @@ -298,10 +298,15 @@ function createPostgresSubscriber (connectionConfig?: pg.ClientConfig, options:
subscribedChannels = [ ...subscribedChannels, channelName ]
return dbClient.query(`LISTEN ${format.ident(channelName)}`)
},
notify (channelName: string, payload: any) {
notify (channelName: string, payload?: any) {
notificationLogger(`Sending PostgreSQL notification to "${channelName}":`, payload)
const serialized = serialize(payload)
return dbClient.query(`NOTIFY ${format.ident(channelName)}, ${format.literal(serialized)}`)

if (payload !== undefined) {
const serialized = serialize(payload)
return dbClient.query(`NOTIFY ${format.ident(channelName)}, ${format.literal(serialized)}`)
} else {
return dbClient.query(`NOTIFY ${format.ident(channelName)}`)
}
},
unlisten (channelName: string) {
if (subscribedChannels.indexOf(channelName) === -1) {
Expand Down
30 changes: 29 additions & 1 deletion test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,38 @@ test("can listen and notify", async t => {
}
})

test("can use custom `parse` function", async t => {
test("can handle notification without payload", async t => {
const notifications: PgParsedNotification[] = []
const receivedPayloads: any[] = []

const hub = createPostgresSubscriber({ connectionString: "postgres://postgres:postgres@localhost:5432/postgres" })
await hub.connect()

try {
await hub.listenTo("test")
await hub.events.on("notification", (notification: PgParsedNotification) => notifications.push(notification))
await hub.notifications.on("test", (payload: any) => receivedPayloads.push(payload))

await hub.notify("test")
await delay(200)

t.deepEqual(hub.getSubscribedChannels(), ["test"])
t.deepEqual(notifications, [
{
channel: "test",
payload: undefined,
processId: notifications[0].processId
}
])
t.deepEqual(receivedPayloads, [undefined])
} finally {
await hub.close()
}
})

test("can use custom `parse` function", async t => {
const notifications: PgParsedNotification[] = []

const connectionString = "postgres://postgres:postgres@localhost:5432/postgres"

const hub = createPostgresSubscriber(
Expand Down

0 comments on commit 252cca3

Please sign in to comment.