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

node-wal2json

v0.0.6

Published

<h2 align="center"> Warning! This package is still in alpha</h2> <h1 align="center"> Node wal2json listener</h1> <p align="center"> <b ></b> </p> <br>

Downloads

2,213

Readme

npm version License: MIT

Description :

This is a node.js service for listening to postgres logical replication generated using the wal2json output plugin. In order to use this package you must enable replication, install, and setup the wal2json plugin. For instructions on how to do so please visit https://github.com/eulerto/wal2json. Once started the listener will emit changes whenever the next method is called on it.

Installation

Stable version:

npm i node-wal2json --save

Usage :

Simple Example

// index.js
const pg = require('pg');
const Wal2JSONListener = require('node-wal2json');

const client = new pg.Client();

const walOptions = {
    'include-type-oids': 1,
    'include-types': 1
};

const options = {
    slotName: 'test_slot',
    timeout: 500
};

const wal2JSONListener = new Wal2JSONListener(client, options, walOptions);

wal2JSONListener.on('changes', function(changes){
    console.log('changes: ', changes);
    wal2JSONListener.next();
});

wal2JSONListener.on('error', function(err){
    console.log('err: ', err);
});

wal2JSONListener.start();

Documentation :

API

constructor

The constructor method takes three params.

  • client: The pg client
  • options(optional):
    • slotName (string)(default: 'test_slot'): The name of the replication slot you will be listening to.
    • timeout (int): The amount of time before the listener runs another fetch after you're done processing the last batch of changes. If set empty there will be no wait time.
    • temporary(boolean): This will create a temporary replication slot on the server.
  • walOptions(optional)(same options as the wal2json plugin):
    • include-xids: add xid to each changeset. Default is false.
    • include-timestamp: add timestamp to each changeset. Default is false.
    • include-schemas: add schema to each change. Default is true.
    • include-types: add type to each change. Default is true.
    • include-typmod: add modifier to types that have it (eg. varchar(20) instead of varchar). Default is true.
    • include-type-oids: add type oids. Default is false.
    • include-not-null: add not null information as columnoptionals. Default is false.
    • pretty-print: add spaces and indentation to JSON structures. Default is false.
    • write-in-chunks: write after every change instead of every changeset. Default is false.
    • include-lsn: add nextlsn to each changeset. Default is false.
    • include-unchanged-toast (deprecated): add TOAST value even if it was not modified. Since TOAST values are usually large, this option could save IO and bandwidth if it is disabled. Default is true.
    • filter-tables: exclude rows from the specified tables. Default is empty which means that no table will be filtered. It is a comma separated value. The tables should be schema-qualified. *.foo means table foo in all schemas and bar.* means all tables in schema bar. Special characters (space, single quote, comma, period, asterisk) must be escaped with backslash. Schema and table are case-sensitive. Table "public"."Foo bar" should be specified as public.Foo\ bar.
    • add-tables: include only rows from the specified tables. Default is all tables from all schemas. It has the same rules from filter-tables.
    • format-version: defines which format to use. Default is 1.

example:

// index.js
const pg = require('pg');
const Wal2JSONListener = require('node-wal2json');

const client = new pg.Client();

const walOptions = {
    'include-type-oids': 1,
    'include-types': 1
};

const options = {
    slotName: 'test_slot',
    timeout: 500
};

const wal2JSONListener = new Wal2JSONListener(client, options, walOptions);

start

The start method starts the listening and fetches the first set of changes which emits the changes event. It also emits the start event.

example:

// index.js
wal2JSONListener.on('start', function(){
    console.log('service started');
});

wal2JSONListener.on('changes', function(changes){
    console.log('changes: ', changes);
});

wal2JSONListener.start();

next

The next method tells the listener to fetch the next set of changes and emits the changes event. example:

// index.js
wal2JSONListener.on('changes', function(changes){
    console.log('changes: ', changes);
});

wal2JSONListener.next();

stop

The stop method stops the listener and closes the client. It also fires the stop event. example:

// index.js
wal2JSONListener.on('stop', function(){
    console.log('stopped');
});

wal2JSONListener.stop();

running

You can use the running property to determine if the listener is currently running or not. example:

// index.js
if(wal2JSONListener.running){
    console.log('service is running');
}
else{
    console.log('service is stopped');
}

restart

If the listener is running the restart method stops it and starts it with a new client, otherwise it just starts it with a new client. params:

  • client: pg client

example:

// index.js
const client = new pg.Client();

wal2JSONListener.restart(client);