Created
June 17, 2016 09:44
-
-
Save freddi301/207b8ffbb05f743de858f26f4199a410 to your computer and use it in GitHub Desktop.
Postgres Notify Trigger on eany table in JSON + node listener
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
var pg = require ('pg'); | |
var pgConString = "postgres://postgres:mysecretpassword@localhost/postgres" | |
pg.connect(pgConString, function(err, client) { | |
if(err) { | |
console.log(err); | |
} | |
client.on('notification', function(msg) { | |
console.log(msg); | |
}); | |
var query = client.query("LISTEN watchers"); | |
}); | |
/* | |
create or replace function table_notify() returns trigger as $$ | |
declare begin | |
perform pg_notify(cast(TG_TABLE_NAME as text), cast(row_to_json(new) as text)); | |
return new; | |
end; $$ language plpgsql; | |
create table events( | |
serial bigserial private key, | |
payload json | |
); | |
drop trigger table_notify on events; | |
create trigger event_notify after insert or update on events | |
for each row execute procedure notify_events(); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment