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 🙏

© 2025 – Pkg Stats / Ryan Hefner

pg-live-query

v1.0.1

Published

(Almost) realtime query update events in PostgreSQL

Downloads

12

Readme

pg-live-query

This package makes it possible in PostgreSQL to get (almost) realtime notifications whenever the results of a query change.

Usage

To use this package, you need to also use the amazing pg driver for PostgreSQL.

const Pool      = require('pg').Pool;
const LiveQuery = require('pg-live-query');

const pool = new Pool(); // Or however you set up pg

pool.connect((error, client, done) => {
    const lq = new LiveQuery(client);

    const sql = `
        SELECT
            *
        FROM
            users u JOIN
            logins l ON
                l.user_id = u.id
        WHERE
            l.date > '2016-01-01'
    `;

    // To get 'insert', 'update', 'delete', and 'changes' events
    const handle = lq.watch(sql);
    // To get the above plus an additional 'rows' event with the full rowset
    // This consumes more memory as it has to maintain the current state of the rowset
    const handle = lq.query(sql);

    // The "update" event looks the same as "insert"
    // They contain the id, row data (as an array), and column names
    handle.on('insert', (id, row, cols) => {
        const out = {};

        cols.forEach((col, i) => {
            out[col] = row[i] || null;
        });

        console.log('row inserted', id, row);
    });

    // The "delete" event only contains the id
    handle.on('delete', (id) => {
        console.log('row deleted', id);
    });

    // The "changes" event contains several changes batched together
    handle.on('changes', (changes) => {
        changes.forEach(({ id, rn, data }) => {
            if(data) {
                console.log(`upsert: ${id} at row ${rn}`, data);
            }
            else {
                console.log(`delete: ${id}`);
            }
        });
    });

    // The "rows" event contains an array of objects
    // that represent the entire current result set
    handle.on('rows', (rows) => {
        console.log('all rows', rows);
    });
});

How it Works

This package should provide much better performance than it used to. I definitely wouldn't consider it production-ready, but that's at least a feasible goal now.

The way it works is by computing an aggregate unique id and latest revision based on columns (that this package adds) from each input row that contributed to a particular output row. It also stores the previous state of the result set in a temporary table. This makes it trivial to compute the differences between two result sets from different executions of the same query inside the database, instead of in Node.

Known Issues

  • Queries with implicit grouping don't work (SELECT MIN(id) FROM foo)

To Test

  • Queries with CTEs
  • Queries with UNIONs
  • Queries that select scalar values that change each time the query is run (nextval, current_timestamp)

Note: This package was previously "deprecated" in favor of pg-live-select. It has since been completely rewritten with a fundamentally different mechanism for determining changes. It should greatly outperform both pg-live-select and any previous versions of this package.