Skip to content

strongloop/strong-db-watcher

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

strong-db-watcher

Event broadcast/watch mechanism triggered by internal database events such as Create, Update or Delete.

DbWatcher: Class, inherits EventEmitter

  • dbClient a postgres handle already connected to the DB

  • changeListener {Function} called at every watched event with (result) where,

    • result: {Object}
      • table {String} table name
      • op {String} INSERT, UPDATE, INSERTorUPDATE, or DELETE
      • when {String} BEFORE, AFTER, or UNSPECIFIED
      • payload{Object} database record
      • receivedAt {Integer} timestamp in msec since the epoch when the broadcast msg is received by the watcher, not when the broadcast event is generated by the broadcaster.

    Event is emitted per each table with result {Object}. In case changeListener is set, it's called for every event in addition to event. Usage below shows how to watch events.

watchTable

  • name {String} table name
  • callback {Function} called with err

isWatching

returns {Boolean}

  • name {String} table name
  • fn {Function} listener function

unwatchTable

unwatch one table

  • name {String} table name,
  • callback {Function} called with err

close

unwatch all the watching tables

  • callback {Function} called with err

Usage: notified by changeListener

    var pg = require('pg');
    var DbWatcher = require('strong-db-watcher');
    var tableName = 'serviceinstance';

    var client = new pg.Client(<connection string>);
    client.connect(function(err) {
      var sdw = new DbWatcher(client,
        function(result) {
          ... all events are watched ...
          ... switch (result.table) case to ...
          ... process all events here or emit your own event ...
        });
    sdw.watchTable(name, function(err) {
      console.log('watchTable error');
    });

Usage: notified by Events

    var pg = require('pg');
    var DbWatcher = require('strong-db-watcher');
    var tableName = 'serviceinstance';

    var client = new pg.Client(<connection string>);
    client.connect(function(err) {
      var sdw = new DbWatcher(client);
      sdw.on(name,
        function(result) {
          ... process the result here ...
          ... events for the table only ...
        });
    sdw.watchTable(name, function(err) {
      console.log('watchTable error');
    });

Result:

	{
	  table: 'serviceinstance',
	  op: 'UPDATE',
	  when: 'BEFORE',
	  payload:
	    {
	      executorid: 155,
	      serverserviceid: 1,
	      groupid: 1,
	      currentdeploymentid: 'some commit',
	      containerversioninfo: {},
	      token: 'bc1357e132318ade13dd008a882d2d0fe5fb1980e7d98c0a',
	      started: false,
	      setsize: 0,
	      cpus: 'CPU',
	      tracingenabled: false,
	      id: 153
	    },
	  receivedAt: 1440828036502
	}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •