@@ -64,6 +64,18 @@ export interface Options {
64
64
* Timeout in ms after which to stop retrying and just fail. Defaults to 3000 ms.
65
65
*/
66
66
retryTimeout ?: number
67
+
68
+ /**
69
+ * Custom function to control how the payload data is stringified on `.notify()`.
70
+ * Use together with the `serialize` option. Defaults to `JSON.parse`.
71
+ */
72
+ parse ?: ( serialized : string ) => any
73
+
74
+ /**
75
+ * Custom function to control how the payload data is stringified on `.notify()`.
76
+ * Use together with the `parse` option. Defaults to `JSON.stringify`.
77
+ */
78
+ serialize ?: ( data : any ) => string
67
79
}
68
80
69
81
function connect ( connectionConfig : pg . ClientConfig | undefined , options : Options ) {
@@ -113,13 +125,13 @@ function connect (connectionConfig: pg.ClientConfig | undefined, options: Option
113
125
}
114
126
}
115
127
116
- function forwardDBNotificationEvents ( dbClient : pg . Client , emitter : TypedEventEmitter < PgListenEvents > ) {
128
+ function forwardDBNotificationEvents ( dbClient : pg . Client , emitter : TypedEventEmitter < PgListenEvents > , parse : ( stringifiedData : string ) => any ) {
117
129
const onNotification = ( notification : PgNotification ) => {
118
130
notificationLogger ( `Received PostgreSQL notification on "${ notification . channel } ":` , notification . payload )
119
131
120
132
let payload
121
133
try {
122
- payload = notification . payload ? JSON . parse ( notification . payload ) : notification . payload
134
+ payload = notification . payload !== undefined ? parse ( notification . payload ) : undefined
123
135
} catch ( error ) {
124
136
error . message = `Error parsing PostgreSQL notification payload: ${ error . message } `
125
137
return emitter . emit ( "error" , error )
@@ -173,7 +185,11 @@ export interface Subscriber {
173
185
}
174
186
175
187
function createPostgresSubscriber ( connectionConfig ?: pg . ClientConfig , options : Options = { } ) : Subscriber {
176
- const { paranoidChecking = 30000 } = options
188
+ const {
189
+ paranoidChecking = 30000 ,
190
+ parse = JSON . parse ,
191
+ serialize = JSON . stringify
192
+ } = options
177
193
178
194
const emitter = new EventEmitter ( ) as TypedEventEmitter < PgListenEvents >
179
195
emitter . setMaxListeners ( 0 ) // unlimited listeners
@@ -197,7 +213,7 @@ function createPostgresSubscriber (connectionConfig?: pg.ClientConfig, options:
197
213
198
214
const initialize = ( client : pg . Client ) => {
199
215
// Wire the DB client events to our exposed emitter's events
200
- cancelEventForwarding = forwardDBNotificationEvents ( client , emitter )
216
+ cancelEventForwarding = forwardDBNotificationEvents ( client , emitter , parse )
201
217
202
218
dbClient . on ( "error" , ( error : any ) => {
203
219
if ( ! reinitializingRightNow ) {
@@ -282,7 +298,8 @@ function createPostgresSubscriber (connectionConfig?: pg.ClientConfig, options:
282
298
} ,
283
299
notify ( channelName : string , payload : any ) {
284
300
notificationLogger ( `Sending PostgreSQL notification to "${ channelName } ":` , payload )
285
- return dbClient . query ( `NOTIFY ${ format . ident ( channelName ) } , ${ format . literal ( JSON . stringify ( payload ) ) } ` )
301
+ const serialized = serialize ( payload )
302
+ return dbClient . query ( `NOTIFY ${ format . ident ( channelName ) } , ${ format . literal ( serialized ) } ` )
286
303
} ,
287
304
unlisten ( channelName : string ) {
288
305
if ( subscribedChannels . indexOf ( channelName ) === - 1 ) {
0 commit comments