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

@becual/pg-notify

v2.0.5

Published

NodeJS PostgreSQL pg-notify subscription library

Downloads

67

Readme

pg-notify

Configure and subscribe to Postgres Notify automatically for a given set of tables.

| Param | Type | Default | Description | | --- | --- | --- | --- | | client | object | | node-postgres Client instance | | configObject | object | | Configuration object | | configObject.schema | string | "public" | Name of the schema where the tables exists. | | configObject.functionName | string | "notify_table_change" | Name of the function to use notify. | | configObject.channelName | string | "notify_table_change_channel" | Name of the channel where the function will notify. |

  • pg-notify
    • [~Subscribe to all database events of a given list of tables(tables)](#module_pg-notify..Subscribe to all database events of a given list of tables) ⇒ Promise
    • [~Create the functions and triggers to configure pg-notify.(tables)](#module_pg-notify..Create the functions and triggers to configure pg-notify.) ⇒ Promise

pg-notify~Subscribe to all database events of a given list of tables(tables) ⇒ Promise

Kind: inner method of pg-notify
Returns: Promise - a promise wich resolves an NodeJS EventEmitter

| Param | Type | Description | | --- | --- | --- | | tables | Array.<string> | An array of table names to listen |

Example

const { Client }  = require('pg');
 const pgNotify = require('@becual/pg-notify');

 let eventHandler = evt => {
     console.log(JSON.stringify(evt, null, 4));
 };

 (async () => {
     // Use your connection string
     const client = new Client({ connectionString: process.env.PG_CONNECTION_STRING });

     // Choose your tables to listen
     const tables = ['customer', 'order_detail'];

    try {
        // Connect client
        await client.connect();

        // By default schema is public
        const sub = await pgNotify(client, {schema: 'mySchema'}).subscribe(tables);

        // Listen for changes
        sub.on('INSERT', eventHandler);
        sub.on('UPDATE', eventHandler);
        sub.on('DELETE', eventHandler);
    }
    catch(error) {
        console.log(error.message);
    }
    finally {
        await client.end();
    }
 })();

pg-notify~Create the functions and triggers to configure pg-notify.(tables) ⇒ Promise

Kind: inner method of pg-notify
Returns: Promise - A promise that will implement the pg-notify config.

| Param | Type | Description | | --- | --- | --- | | tables | Array.<string> | The list of tables to create pg notify configuration. |

Example

const { Client } = require('pg');
 const pgNotify = require('@becual/pg-notify');

 (async () => {

     const client = new Client({ connectionString: process.env.PG_CONNECTION_STRING });
     const tableList = ['customer', 'order_detail'];

     try {
         // Try to generate configuration
         await client.connect();
         await pgNotify(client, {schema: 'mySchema'}).config(tableList);
     }
     catch(error) {
         // Show errors
         console.log(error.message);
     }
     finally {
         // Close connection
         await client.end();
     }
 })();