Created
June 27, 2014 10:37
-
-
Save bastjan/7416ea5a2907ed38e92d to your computer and use it in GitHub Desktop.
calculate delta of updated row in postgres and notify listener with json
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 characters
-- Function: tacmon.test_v8() | |
-- DROP FUNCTION tacmon.test_v8(); | |
CREATE OR REPLACE FUNCTION tacmon.test_v8() | |
RETURNS trigger AS | |
$BODY$// calculate delta | |
var key, delta = []; | |
for (key in NEW) { | |
var valueOld = OLD[key]; | |
var valueNew = NEW[key]; | |
if (valueOld !== valueNew) { | |
delta.push([key, valueOld, valueNew]); | |
} | |
} | |
var info = { | |
"table": TG_TABLE_NAME, | |
"id": NEW.id, | |
"delta:": delta | |
} | |
// notify listeners on channel targets | |
plv8.execute("SELECT pg_notify('targets', $1);", [JSON.stringify(info)] ); | |
// debug message for pgadmin3 | |
// plv8.elog(ERROR, "Delta:", JSON.stringify(info)); | |
return void 0;$BODY$ | |
LANGUAGE plv8 VOLATILE | |
COST 100; | |
ALTER FUNCTION tacmon.test_v8() | |
OWNER TO tacmon; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment