npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@cisstech/nestjs-pg-pubsub

v1.15.0

Published

A NestJS module to provide PostgreSQL PubSub

Downloads

2,699

Readme

@cisstech/nestjs-pg-pubsub

A NestJS module for real-time PostgreSQL change data capture using triggers and a persistent queue.

CI codecov codefactor GitHub Tag npm package NPM downloads licence code style: prettier

What It Does

PostgreSQL triggers detect INSERTs, UPDATEs and DELETEs on your tables. Changes are persisted in a queue table, then pulled and dispatched to typed NestJS listeners. No polling-only design, no lost messages, no external broker.

Diagram

Why This Exists

PostgreSQL's LISTEN/NOTIFY is great for real-time but unreliable: notifications are fire-and-forget, lost during disconnects, and have a payload size limit. This library wraps it with a durable queue so you get both reactivity and guaranteed delivery.

Key Properties

  • Durable: changes are persisted in a SQL table before notification, surviving crashes and restarts
  • Reactive: LISTEN/NOTIFY triggers immediate processing, with a fallback poller as safety net
  • Ordered: messages are processed by ID, in batches, with SELECT FOR UPDATE SKIP LOCKED
  • Retry with backoff: failed messages are retried with exponential backoff ($2^{n}$ minutes)
  • Backpressure: concurrent notifications are coalesced so that at most one pull cycle runs at a time
  • Isolated pool: uses its own pg.Pool, independent of TypeORM, to avoid pool contention
  • Zero-downtime DDL: triggers are fingerprinted (MD5) and only recreated when config changes

Installation

yarn add @cisstech/nestjs-pg-pubsub pg

Supports NestJS v10+ and v11+.

Quick Start

// app.module.ts
@Module({
  imports: [
    TypeOrmModule.forRoot({
      /* ... */
    }),
    PgPubSubModule.forRoot({
      databaseUrl: process.env.DATABASE_URL,
    }),
  ],
  providers: [UserChangeListener],
})
export class AppModule {}
// user-change.listener.ts
@Injectable()
@RegisterPgTableChangeListener(User)
export class UserChangeListener implements PgTableChangeListener<User> {
  async process(changes: PgTableChanges<User>, ctx: PgTableChangeContext): Promise<void> {
    for (const insert of changes.INSERT) {
      console.log(`New user: ${insert.data.email}`)
    }

    for (const update of changes.UPDATE) {
      console.log(`Updated fields: ${update.data.updatedFields.join(', ')}`)
    }
  }
}

That's it. The library auto-creates triggers, the queue table, and starts listening.

How It Works

  1. A PostgreSQL trigger fires on table change and inserts a row into pg_pubsub_queue
  2. The trigger sends a NOTIFY with the channel name
  3. The library receives the notification and pulls pending messages from the queue
  4. Messages are dispatched to the matching @RegisterPgTableChangeListener classes
  5. Processed messages are marked as such; failed ones are retried with exponential backoff
  6. A background poller runs every 60s as a safety net for missed notifications

Important Constraints

  • Listeners must be fast. A slow listener delays the entire batch for that table. Offload heavy work to a queue (Bull, etc.) and just enqueue from the listener.
  • Use TransactionAdapter for transactional writes. If a listener needs to write to the DB inside a transaction, configure a transactionAdapter and mark the listener with @RegisterPgTableChangeListener(Entity, { transactional: true }). The library wraps the listener call in the adapter, passing an opaque transaction token via ctx.transaction. Without the adapter, use ctx.onError to signal failures.

Configuration

All options are optional except databaseUrl. See the full configuration reference for details.

Key tuning knobs:

| Option | Default | What it controls | | ------------------------- | ------- | --------------------------------------------------------------------- | | queue.batchSize | 100 | Max messages fetched per pull cycle | | queue.drainInterval | 50ms | Pause between drain loop iterations (DB breathing room) | | queue.processingTimeout | 5min | After this, a processing message is considered orphaned and retried | | queue.concurrency | 5 | Max listeners executing in parallel per batch | | transactionAdapter | - | ORM-agnostic adapter for wrapping listeners in transactions | | pool.max | 5 | Connections in the dedicated pg-pubsub pool |

Documentation

Full documentation: https://cisstech.github.io/nestkit/docs/nestjs-pg-pubsub/getting-started

License

MIT © Mamadou Cisse