updated samples
parent
8770e5d47f
commit
5536eacc0a
|
@ -26,7 +26,7 @@ func parseflags() {
|
|||
flag.Parse()
|
||||
}
|
||||
|
||||
func handleevent(m pglisten.Message) {
|
||||
func handleevent(m pglisten.Message) bool {
|
||||
if m.Table != "" && m.Op != "" {
|
||||
println(fmt.Sprintf("%s on table %s", m.Op, m.Table))
|
||||
}
|
||||
|
@ -37,7 +37,7 @@ func handleevent(m pglisten.Message) {
|
|||
if k == "id" {
|
||||
if str, ok := v.(string); ok {
|
||||
go func() {
|
||||
cmd := exec.Command("touch", "/tmp/" + str)
|
||||
cmd := exec.Command("touch", "/tmp/"+str)
|
||||
cmd.Run()
|
||||
}()
|
||||
}
|
||||
|
@ -47,6 +47,8 @@ func handleevent(m pglisten.Message) {
|
|||
default:
|
||||
fmt.Println(m.Payload)
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
|
22
_sample.sql
22
_sample.sql
|
@ -1,7 +1,9 @@
|
|||
create table pg_notifications_queue (message jsonb);
|
||||
|
||||
create function notify_event()
|
||||
returns trigger as $$
|
||||
declare
|
||||
message json;
|
||||
msg json;
|
||||
payload json;
|
||||
id uuid;
|
||||
|
||||
|
@ -12,19 +14,23 @@ begin
|
|||
id = new.id;
|
||||
end if;
|
||||
|
||||
message = json_build_object(
|
||||
msg = jsonb_build_object(
|
||||
'id', gen_random_uuid(),
|
||||
'table', tg_table_name,
|
||||
'op', tg_op,
|
||||
'trigger', tg_argv[0],
|
||||
'payload', json_build_object('id', id)
|
||||
'payload', json_build_object('id', new.id, 'source', tg_argv[1], 'target', tg_argv[2])
|
||||
);
|
||||
|
||||
perform pg_notify('events', message::text);
|
||||
insert into pg_notifications_queue (message) values (msg::jsonb);
|
||||
|
||||
perform pg_notify('events', msg::text);
|
||||
|
||||
return null;
|
||||
end $$ language plpgsql;
|
||||
|
||||
|
||||
-- create trigger <collection>_notify_event
|
||||
-- after insert or update or delete on <collection>
|
||||
-- for each row execute function notify_event();
|
||||
-- create trigger <table>_notify_event
|
||||
-- after update on <table>
|
||||
-- for each row
|
||||
-- when (old.<something> is distinct from new.<something>)
|
||||
-- execute procedure notify_event('kramdown', '<something>', '<html_something>');
|
||||
|
|
Loading…
Reference in New Issue