Hello 👋 I'm building an RPC and accessing it fro...
# help
m
Hello 👋 I'm building an RPC and accessing it from a JS (SvelteKit). I'm trying to build the call from JS, but am getting errors and am unsure what exactly is causing it.
n
Hello @monsto! This thread has been automatically created from your message in #843999948717555735 a few seconds ago. We have already mentioned the @User role so that they can see your message and help you as soon as possible! 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.
m
js function
Copy code
; ({ data } = await supabase
                .rpc("gen_report_epr", {
                    daterange_start: dateFrom.toISOString(),
                    daterange_end: dateTo.toISOString(),
                })
            )
rpc sql
Copy code
create temporary table temp_ouput_epr as (select * from electronic_purchase_report) with no data;

    create or replace function gen_report_epr(daterange_start date, daterange_end date)
    returns setof temp_ouput_epr
    as $BODY$
      select * from electronic_purchase_report where created_at between daterange_start and daterange_end
    $BODY$ language sql;
first off, in the js call the
.rpc()
2nd arg, is it supposed to be a fully formed object, or is it something else?
2ndly, where's the full docs for this function? the supabase docs seem thin. https://supabase.com/docs/reference/javascript/rpc
g
https://postgrest.org/en/stable/api.html#custom-queries probably the best document, although it is in the url/body format, it shows postgres functions and the format of the parameters the .rpc(func,parms) passes in the body.
m
does my code look ok tho?
g
What is your error? The call and parameter format look correct.
m
I'm seeing just now that I'm getting a 404
1 sec
gen_epr_report is the name of the rpc as shown above.
I did however build this as a query and not using the functions gui.
the query does not show in the gui. do i need to save it somehow?
g
If you goto database gui and functions section you should see the function
m
I don't.
i built it in sql editor.
g
It still should show up. What do you get when you click run
m
run returns zero, which i get because no parameters.
g
So there is a first step that creates the function with sql and you use run to do that. Nothing to do with running the actual function
m
tried to paste the msg here but windows is being stupid.
g
Should return "Success. No rows returned"
m
"success. no rows returned."
can I still use the gui/builder and return a large set of data?
g
Not sure why not showing up....
m
did you do anything more than than just put in the query and it magically appeared in the functions list?
g
that is it
put it in and hit run
m
well shit.
💢
it shows on the log, with 404
g
that is your rpc call coming thru the api.
m
right so it's getting that far.
I see that you have
public.gen_report_epr
equivalent. does that matter?
g
Yeah but 404 means it has an issue, l think that the function name with matching parameters was not found... but not sure what else it could mean.
Did I miss you putting in another schema?
m
just to rule out other chars, I c/p the name gen_report_epr
I don't know what schema there could be. I haven't changed it.
g
I just removed public. and it still gets created in the public schema which is needed
m
wait your return type is different.
I'm creating a temp table, then returning that.
g
That should not matter on finding the function
m
it's why I used the query instead of the gui, because i didn't think any of the return types in the gui would give the 35k rows that need to come back.
well i'm saying that might keep it from being seen by the gui, which may cascade to not being published to the api.
g
You are correct on the create gui, I wonder if even the display does not show.
m
(being alpha feature and all)
g
run your sql again without the or replace you should get an error
m
"without the" what 😁
It looks like you out a word
g
create or replace function gen_report_epr(daterange_start drop "or replace"
just trying to get over the function existing...
m
success . . .
g
that is not good...
you can't create a function twice
m
success multiple times.
right
g
I've never done the temp table approach, versus table return
m
I couldn't get the syntax to return the table.
what would be the syntax to just return the resulting table?
g
I don't know the syntax for your approach, it is acting though like the create table is not running
m
original (for reference)
Copy code
create temporary table temp_ouput_epr as (select * from electronic_purchase_report) with no data;

create function gen_report_epr(daterange_start date, daterange_end date)
returns setof temp_ouput_epr
as $BODY$
  select * from electronic_purchase_report where created_at between daterange_start and daterange_end
$BODY$ language sql;
g
can't you do "returns setof electronic_purchase_report"
m
!! success
lemme try the app...
ok dude it shows in functions now.
AND I'm getting different errors at app level.
g
so your front sql syntax was causing the last part not to be run... odd
m
yeah the temp table part? It appears so.
app level I'm getting 502 now, which is a whole other problem going back "a while"
I appreciate your help hear this was great. If i need to chase down the other issue, I'll start a new thread.
➕ 1️⃣
g
@monsto you mentioned 35000 records. The api limit setting (default 1000) in API settings does apply to rpc record returns also.
m
yeah we've been wrestling with that. Should I start a new thread?
g
Probably. But you have 3 choices with rpc, paginate it with range just like a regular select, up the limit or return one large json object.
m
we have it set to 50k in api settings.
we were doing range pagination, multiple chunks, doing array.push({...data}) then returning the array.
but one large json object?
g
if they are small records I would think that would be an option.
m
the chunk query:
Copy code
; ({ data, error, count } = await supabase
                    .from('electronic_purchase_report')
                    .select('*', { count: 'exact' })
                    .gte('created_at', dateFrom.toISOString())
                    .lte('created_at', dateTo.toISOString())
                    .range(from, to)
                    .then(e => e)
                    .catch(error => console.log(`conlog: supabase catch`, error))
                )
do I need to do anything to that for it to come as json?
g
If you already have pagination method, I'd just stick with that. I don't have how to do it at my fingers, but you should be able to return from the postgres function a single json object and handle that in rpc.
m
I'm not sure how the data comes back. I thought it already was json.
hm ok.
g
postgrest is turning the records to json
m
oh ok so have the rpc turn it to json, then return that
g
it also sets the limit on records, if your function provides one json response it would return that.