Last active
March 1, 2018 22:39
-
-
Save pianosnake/7783100 to your computer and use it in GitHub Desktop.
Function to send a Postgres Notification with a JSON object payload of the updated fields. Also the trigger to call the function.
This file contains 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
CREATE OR REPLACE FUNCTION send_notification() RETURNS trigger AS $$ | |
BEGIN | |
PERFORM pg_notify('usersupdate', | |
(SELECT row_to_json(r.*)::varchar FROM ( | |
SELECT id, email, name from users where id = NEW.id) | |
r) | |
); | |
RETURN NEW; | |
END; | |
$$ LANGUAGE plpgsql; | |
CREATE TRIGGER users_updated | |
AFTER UPDATE OF email,name | |
ON users | |
FOR EACH ROW | |
EXECUTE PROCEDURE send_notification(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment