Created
June 4, 2020 02:46
-
-
Save raphox/bf2e9675e780d9611ce1127eb541ef8a to your computer and use it in GitHub Desktop.
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 notify_rows_changes() | |
RETURNS trigger AS $$ | |
DECLARE | |
record JSON; | |
BEGIN | |
record := row_to_json(CASE TG_OP | |
WHEN 'INSERT' THEN NEW | |
WHEN 'UPDATE' THEN NEW | |
ELSE OLD | |
END); | |
PERFORM pg_notify( | |
'row_changed', | |
json_build_object( | |
'operation', TG_OP, | |
'record', record | |
)::text | |
); | |
RETURN COALESCE(NEW, OLD); | |
END; | |
$$ LANGUAGE plpgsql; | |
CREATE TRIGGER rooms_changed | |
AFTER INSERT OR UPDATE OR DELETE | |
ON rooms | |
FOR EACH ROW | |
EXECUTE PROCEDURE notify_rows_changes(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment