voxpelli / node-pg-pubsub

A Publish/Subscribe implementation on top of PostgreSQL NOTIFY/LISTEN
https://www.npmjs.com/package/pg-pubsub
MIT License
236 stars 16 forks source link

Publish without arguments #61

Closed IlyaSemenov closed 5 years ago

IlyaSemenov commented 5 years ago

Currently the second argument in publish(channel, arg) is required. This contradict the typical event emitter pattern where the argument is not necessary required (sometimes only the event itself matters). I propose to make the second argument optional. If it's required due to downstream implementation details, I propose to make it equal to null by default.

See demos:

1) works fine when sending null:

> PGPubsub = require('pg-pubsub'); new PGPubsub('postgres://localhost/xxx').publish('foo', null)
Promise {
  <pending>,
  domain:
   Domain {
     domain: null,
     _events:
      { removeListener: [Function: updateExceptionCapture],
        newListener: [Function: updateExceptionCapture],
        error: [Function: debugDomainError] },
     _eventsCount: 3,
     _maxListeners: undefined,
     members: [] } }
> Successful retry attempt for pubsub

Throws when sending without arguments:

> PGPubsub = require('pg-pubsub'); new PGPubsub('postgres://localhost/xxx').publish('foo')
Promise {
  <pending>,
  domain:
   Domain {
     domain: null,
     _events:
      { removeListener: [Function: updateExceptionCapture],
        newListener: [Function: updateExceptionCapture],
        error: [Function: debugDomainError] },
     _eventsCount: 3,
     _maxListeners: undefined,
     members: [] } }
> Successful retry attempt for pubsub
(node:56886) UnhandledPromiseRejectionWarning: VError: Failed to publish to channel: syntax error at or near "NULL"
    at _getDB.then.catch.err (/Users/semenov/xxx/node_modules/pg-pubsub/index.js:126:34)
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:56886) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 4)