Skip to content

Latest commit

 

History

History
60 lines (52 loc) · 1.78 KB

readme.md

File metadata and controls

60 lines (52 loc) · 1.78 KB

PG Dispatch

Postgres pub/sub

NPM

Setup

npm install pg-dispatch
psql -h localhost -d mydb < install.sql

Example

var dispatcher = require('pg-dispatch')({
    // client: ... // pg client can be passed or
    conString: '', // same as for pg driver
    // Default options:
    // changesChannel: 'data_changes',
    // names: {
    //    notifyTrigger: 'data_change_notify_trigger',
    //    notifyTriggerFunc: 'data_change_notify_trigger_func',
    //    addNotifyTrigger: 'add_notify_trigger_to_table',
    //    removeNotifyTrigger: 'remove_notify_trigger_to_table'
    // },
    // operationEvents: true, // false to NOT send events on each operation
    // checkUpdates: true // only notify if update changes record
    // sendRecordId: false // send only record IDs, true - to send column 'id',
                           // string to send column with a given name.
                           // this column should be a number.
                           // by default the whole record data is sent.
});

dispatcher.install(function(err) {
    if (err) return console.log(err);
    dispatcher.subscribe('users', function(error, data) {
    	if(error) throw error;
		console.log('Subscribed:' data);
    });
});

dispatcher.on('users:insert', function (error, data) {
	if (error) throw error;
	console.log(data);
});

API

.install(callback)

.subscribe('table_name', callback) // array of tables can be passed

.unsubscribe('table_name', callback) // -"-

.on('table_name', callback)

.on('table_name:insert', callback) // when operationEvents is true (default)

.on('table_name:update', callback) // -"-

.on('table_name:delete', callback) // -"-

License

MIT