Skip to content

Instantly share code, notes, and snippets.

@psteinroe
Created September 2, 2022 09:19
Show Gist options
  • Save psteinroe/121a8c2162f7fc6c8408339315033ab3 to your computer and use it in GitHub Desktop.
Save psteinroe/121a8c2162f7fc6c8408339315033ab3 to your computer and use it in GitHub Desktop.
Example for using pg_net with triggers
create or replace function delete_external_provider()
returns trigger
language plpgsql
security invoker
as
$$
declare
v_api_base_url text;
v_delete_provider_api_url text;
v_provider provider := old;
begin
select value from setting where name = 'api_base_url' into v_api_base_url;
if v_api_base_url is null then
raise exception using
message = 'API base URL cannot be null',
hint = 'The delete provider URL will be built with <base_url>/providers/<provider_id>/delete',
errcode = 'INVIN';
end if;
v_delete_provider_api_url := v_api_base_url || '/' || 'providers' || '/' || v_provider.id::text || '/' || 'delete';
perform
net.http_post(
url := v_delete_provider_api_url,
body := to_jsonb(v_provider),
headers := jsonb_build_object(
'Authorization', (select current_setting('request.headers', true)::json ->> 'authorization'),
'Content-Type', 'application/json'
),
timeout_milliseconds := 30000
);
return old;
end
$$;
create trigger delete_external_provider
after delete
on provider
for each row
execute procedure delete_external_provider();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment