-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
26 lines (22 loc) · 722 Bytes
/
utils.js
File metadata and controls
26 lines (22 loc) · 722 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
export function triggerChange(sql, { table, column, expression, security='invoker' }){
let TG_NAME = `${table}_${column}`
let FN_NAME = `${table}_${column}`
sql = String.raw
const out = sql`
create or replace function ${FN_NAME}() returns trigger as $$
begin
${expression};
return NEW;
end;
$$ language plpgsql security ${security} set search_path = '';
create trigger ${TG_NAME}_update
before update on ${table}
for each row
execute function ${FN_NAME}();
create trigger ${TG_NAME}_insert
before insert on ${table}
for each row
execute function ${FN_NAME}();
`
return out
}