-
-
Save tuan9x/ffe8e21a1b25fc839ea0270c3b78831d to your computer and use it in GitHub Desktop.
Revisions
-
Nathan Fritz revised this gist
Apr 6, 2015 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,6 @@ var pg = require ('pg'); pg.connect("postgres://localhost/fritzy", function(err, client) { if(err) { console.log(err); } -
Nathan Fritz revised this gist
Apr 6, 2015 . 3 changed files with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes.File renamed without changes.File renamed without changes. -
Nathan Fritz revised this gist
Apr 6, 2015 . 2 changed files with 42 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,23 @@ > INSERT INTO users (username) values ('fritzy'); *========* type INSERT id 1 table users -========- > UPDATE users SET email='[email protected]' WHERE id=1; *========* type UPDATE id 1 table users -========- > DELETE FROM users WHERE id=1; *========* type DELETE id 1 table users -========- 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,19 @@ var pg = require ('pg'); var pgConString = "postgres://localhost/fritzy" pg.connect(pgConString, 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"); }); -
Nathan Fritz created this gist
Apr 6, 2015 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,23 @@ 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();