Last active
August 29, 2015 13:56
-
-
Save straps/8951655 to your computer and use it in GitHub Desktop.
Simple dbmon usage sample to call a callback function on database table insert/update/delete/truncate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//PARAMETERS TO BE CHANGED | |
var PGCONN='tcp://postgres:password@localhost/database', | |
TABLE='testtable', | |
KEYNAME='id', | |
KEYTYPE='integer'; | |
pg=require('pg'), | |
cli=new pg.Client(PGCONN), | |
dbmon=require('dbmon'); | |
cli.connect(); | |
//Use EventEmitter to call a callback when something changes | |
var EventEmitter=new require('events').EventEmitter, | |
eventEmitter=new EventEmitter(); | |
eventEmitter.addListener('insert', function(info){ | |
console.log('Row Inserted, info='+JSON.stringify(info)); | |
}); | |
eventEmitter.addListener('update', function(info){ | |
console.log('Row Updated, info='+JSON.stringify(info)); | |
}); | |
eventEmitter.addListener('delete', function(info){ | |
console.log('Row Deleted, info='+JSON.stringify(info)); | |
}); | |
eventEmitter.addListener('truncate', function(info){ | |
console.log('Table Truncated, info='+JSON.stringify(info)); | |
}); | |
var channel=dbmon.channel({ | |
driver:'postgresql', | |
driverOpts:{ | |
postgresql:{ | |
cli:cli, | |
baseObjectsName:TABLE+'_'+KEYNAME | |
} | |
}, | |
table:TABLE, | |
monitor:'all', | |
keyfld:{ | |
name:KEYNAME,type:KEYTYPE | |
}, | |
transports:'eventEmitter', | |
transportsOpts:{ | |
eventEmitter:{ | |
eventEmitter:eventEmitter | |
} | |
} | |
}); | |
//Clear dbmon support tables/functions/triggers when app is stopped | |
var onExitSignal=function(){ | |
channel.stop(function(){ | |
process.exit(0); | |
}); | |
}; | |
process.on('SIGINT', onExitSignal); | |
process.on('SIGTERM', onExitSignal); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!