andywer/pg-listen

Make Subscriber interface generic

rhyek opened this issue · 5 comments

rhyek commented

Make Subsrciber interface generic such that I can define custom notification events + payload types like so:

interface NotificationEvents {
  'inserted-row': {
    tableName: string;
    row: Record;
  };
}

export default class PgListenerService extends BaseService {
  subscriber: Subscriber<NotificationEvents>;

  constructor() {
    super();
    this.subscriber = createSubscriber({ connectionString: envs.pgDbUrl });
    process.on('exit', () => {
      this.subscriber.close();
    });
  }

  async init() {
    this.subscriber.notifications.on('inserted-row', payload => {
      payload.tableName; // string instead of any
    });
    await this.subscriber.connect();
    await this.subscriber.listenTo('inserted-row');
  }
}

Wow, I had to double-check that it really isn’t generic yet... Don’t ask me why it’s not. Let’s fix it 👍

Check out the pull request #29 😉

rhyek commented

It looks pretty slick. Any chance of it being published today? Would like to use it for something I'm currently working on. 😁

Here we go: v1.5.0 🚀

rhyek commented

@andywer, it works perfectly. Thank you very much and keep up the great work.