-
-
Save jgcmarins/63e03ce8be039a7264ec73426d7cf917 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