File tree 1 file changed +44
-1
lines changed 1 file changed +44
-1
lines changed Original file line number Diff line number Diff line change @@ -24,6 +24,49 @@ This notifications database contains 2 tables, 1 regular function and 1 trigger
24
24
25
25
1 . notify_notification_changes
26
26
27
+ ```
28
+ CREATE FUNCTION public.notify_notification_changes()
29
+ RETURNS trigger
30
+ LANGUAGE 'plpgsql'
31
+ COST 100
32
+ VOLATILE NOT LEAKPROOF
33
+ AS $BODY$
34
+ DECLARE
35
+ tr_record RECORD;
36
+ trigger_data json;
37
+ notification_data json;
38
+ BEGIN
39
+ IF (TG_OP = 'DELETE') THEN
40
+ tr_record = OLD;
41
+ ELSE
42
+ tr_record = NEW;
43
+ END IF;
44
+
45
+ trigger_data = row_to_json(r)
46
+ FROM (
47
+ SELECT
48
+ n.*,
49
+ nt.name notification_type
50
+ FROM
51
+ (SELECT tr_record.*) n
52
+ JOIN public.notification_type nt on n.notification_type_id = nt.id
53
+ ) r;
54
+
55
+ notification_data = json_build_object(
56
+ 'table', TG_TABLE_NAME,
57
+ 'operation', TG_OP,
58
+ 'data', trigger_data
59
+ );
60
+
61
+ PERFORM pg_notify(
62
+ 'notifications_data_changed',
63
+ notification_data::text
64
+ );
65
+
66
+ RETURN tr_record;
67
+ END;
68
+ $BODY$;
69
+ ```
27
70
28
71
## function calls:
29
72
@@ -36,4 +79,4 @@ SELECT public.new_notification(
36
79
37
80
38
81
## pgadmin
39
- ![ files] ( ./pgadmin.png )
82
+ ![ files] ( ./pgadmin.png )
You can’t perform that action at this time.
0 commit comments