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 🙏

© 2024 – Pkg Stats / Ryan Hefner

pg-promise-robust-connection

v1.0.0

Published

Initates a direct pg-promise connection that will try to re-establish itself on connection loss

Downloads

5

Readme

Commitizen friendly Build Status

Robust Direct Connections for pg-promise

This is a helper function to assist in creating a 'robust' direct connection to a Postgresql database when using the excellent pg-promise library. The implementation is strongly inspired by https://github.com/vitaly-t/pg-promise/wiki/Robust-Listeners, with some additional configuration and logging hooks added.

As is strongly stated and re-stated in the pg-promise documentation, you probably don't need a direct connection for the majority of use-cases, and should stick to pg-promises own connection management and retry logic. However, if you're using LISTEN and NOTIFY, then some manual retry management is essential- if a network partition or other outage breaks connectivity between your application and the database, you will miss out LISTEN events that occur, even after the outage is over.

Basic Usage


// your-app.ts
import {robustConnection} from 'pg-promise-robust-connection';

const myDb = pgPromise()({/** your connection options **/});

// Your app code, using connection pooling against myDB

robustConnection({
  // The pg-promise connection to spawn the direct connection from
  db: myDB

  // Connect established for the first time or after being disconnected previously
  onConnect: (connection) => {
    console.log('connected!');
    connection.client.on('notification', messageHandler);
    connection.none('LISTEN somechannel');
  },

  // Connection was lost; will attempt to reconnect, but clean up in the meantime
  onDisconnect: (err, context) => {
    console.log('disconnected');
    context.client.removeListener('notification', messageHandler);
  }
});

// Allow a reference to messageHandler to be kept so that we may clean it up on disconnect
const messageHandler = (message) => {
  console.log('Got a message', message)
}

By default, when a connection is lost, it will be retried every 1 second a maximum of 10 times. If the last attempt fails, the connection will be considered to have 'failed permanently'. In this case, process.exit() will be called. Then, it's up to your higher level process handler to try to restart the service. Better to have your Dashboard reporting a dead app than one that is half-functional.

Advanced Usage

Please refer to the documentation of the Options interface for details about the other options that are available.