Hello, If i use triggers to call external API, wil...
# help
t
Hello, If i use triggers to call external API, will they block IO until it is completed or are they running in background?
n
Hello @theuknowner! This thread has been automatically created from your message in #843999948717555735 a ``few seconds ago``. Pinging @User so that they see this as well! Want to unsubscribe from this thread? Right-click the thread in Discord (or use the ... menu) and select Leave Thread to unsubscribe from future updates. Want to change the title? Use the
/title
command! We have solved your problem? Click the button below to archive it.
g
The Supabase function hooks are async. If you call a trigger function and use http extension it blocks…your request must return. You can probably use Supabase pg_net extension in a trigger function but not sure what state it is in, besides being used for hooks.
n
theuknowner (2022-04-05)
t
@User I try to find ways in order to send push notifications to mobs when a new message has been added. Are there docs/examples for pg_net?
g
t
I created the trigger and works as expected, but I don't know how can I include data to send from the changed row. Is there any example? I checked. the docs
I have set the function to trigger when a new message has been added to table "messages". I want to get the body of the message and include it to the request
g
In your trigger function the changed data is in "new". So new.id, new.body, etc. You would put those into the body as json pairs... body:='{"hello": "world"}'::jsonb
t
I tried it
begin perform net.http_post( url:='https://httpbin.org/post', body:='{"lastMes": "'+NEW.last_message_body+'" ,"hello": "world"}'::jsonb ) as request_id; RETURN NULL; end;
this is my trigger function
but I get this error -> No operator matches the given name and argument types. You might need to add explicit type casts.
operator does not exist: unknown + text
Is "+" symbol used in Postgres Functions?
g
Yeah you need to use proper postgres sql or plpgsql syntax. There are was to do formatted text and concatenate. I would have to google examples, so will leave that to you.. I have one function handy where I did in plpgsql:
Copy code
declare
myurl varchar := 'https://zkxxxxxxxxxxxxlxrq.supabase.co';
url varchar := myurl||'/storage/v1/object/'||bucket||'/'||pathname;
But I'm sure there are better ways.... || is string concat
t
I use plpgsql because I can't create trigger with sql
g
Hey I'm traveling and using ipad, so hopefully got you moving right way. There are lots of examples of formatting strings out there.
t
tried with || -> body:='{"lastMes":'||NEW.last_message_body||',"hello": "world"}'::jsonb
I get -> Expected JSON value, but found \",\". . Invalid syntax json
So where is the issue? I don't understand
g
you need at least
Copy code
'"body":"'||NEW.last_message_body||'","hello...
I'm sure there are better ways to do this formatting.
t
Yes tried it, now I get "Token \"hello\" is invalid."
g
You might want to just use sql editor and test your formatting so you can see results, or you can use
Copy code
raise log 'function message' || var
and look in database logs.
You might want to start a new question in the SQL section on what you are trying to format as you are past the question here, and not likely to get other eyes on it.
t
Okay I'll ask in the SQL section. thanks for your time