Skip to content

Instantly share code, notes, and snippets.

@jjzabkar
Forked from fritzy/1_triggers.sql
Created March 29, 2018 04:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jjzabkar/4d3210971e35e1bfc2a364217d82f9b6 to your computer and use it in GitHub Desktop.
Save jjzabkar/4d3210971e35e1bfc2a364217d82f9b6 to your computer and use it in GitHub Desktop.
Get table change notifications from Postgres as JSON
CREATE OR REPLACE FUNCTION table_update_notify() RETURNS trigger AS $$
DECLARE
id bigint;
BEGIN
IF TG_OP = 'INSERT' OR TG_OP = 'UPDATE' THEN
id = NEW.id;
ELSE
id = OLD.id;
END IF;
PERFORM pg_notify('table_update', json_build_object('table', TG_TABLE_NAME, 'id', id, 'type', TG_OP)::text);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
DROP TRIGGER users_notify_update ON users;
CREATE TRIGGER users_notify_update AFTER UPDATE ON users FOR EACH ROW EXECUTE PROCEDURE table_update_notify();
DROP TRIGGER users_notify_insert ON users;
CREATE TRIGGER users_notify_insert AFTER INSERT ON users FOR EACH ROW EXECUTE PROCEDURE table_update_notify();
DROP TRIGGER users_notify_delete ON users;
CREATE TRIGGER users_notify_delete AFTER DELETE ON users FOR EACH ROW EXECUTE PROCEDURE table_update_notify();
var pg = require ('pg');
pg.connect("postgres://localhost/fritzy", function(err, client) {
if(err) {
console.log(err);
}
client.on('notification', function(msg) {
if (msg.name === 'notification' && msg.channel === 'table_update') {
var pl = JSON.parse(msg.payload);
console.log("*========*");
Object.keys(pl).forEach(function (key) {
console.log(key, pl[key]);
});
console.log("-========-");
}
});
client.query("LISTEN table_update");
});
> INSERT INTO users (username) values ('fritzy');
*========*
type INSERT
id 1
table users
-========-
> UPDATE users SET email='fritzy@andyet.com' WHERE id=1;
*========*
type UPDATE
id 1
table users
-========-
> DELETE FROM users WHERE id=1;
*========*
type DELETE
id 1
table users
-========-
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment