Hey! Anyone can help with a sample code to use in ...
# help
w
Hey! Anyone can help with a sample code to use in creating a supabase function for rpc?
s
Postgres functions can do pretty much anything inside Postgres, so if you have an idea of what you're trying to achieve, it'll be easier to put something together
w
I want to get the total of
netAmount
. Here's what i did:
Copy code
declare
   total integer;
begin
   select count(netAmount) 
   into total
   from donations
   where beneficiaryId = id;
   
   return total;
end;
Do i have to put a whole block of code like something similar to
get_film_count
in https://www.postgresqltutorial.com/postgresql-create-function/?
s
Sort of - for basic queries like this, you can usually copy what's there. For your specific use case, something like this should work:
Copy code
sql
CREATE OR REPLACE FUNCTION public.get_net_amount_total(in_id uuid)
RETURNS money
AS $BODY$
BEGIN
  RETURN QUERY
  SELECT count(netAmount) AS total
  FROM public.donations
  WHERE id = in_id;
END;
$BODY$ language 'plpgsql';
When you call it from Supabase, you'd call:
supabase.rpc("get_net_amount_total", {in_id: "some_uuid"})
I'm assuming that
id
is a UUID - if not, change the data type in the arguments of the function.
money
is the data type that's returned (https://www.postgresql.org/docs/9.1/datatype-money.html)
w
I see. Well
id
is a UUID. But the code i showed above kinda work but i get this error
column "netamount" does not exist
. I have a
netAmount
column in my table. Here's how i call it
const { data, error } = await supabase.rpc("get_balance", { id });
s
You have 2 options: - Put quotes around
netAmount
in the query (e.g.
"netAmount"
) - Rename the column to
net_amount
The first option is easier, but the second option would be my recommendation. Postgres is easier to work with if you use all-lowercase, or snake-case for column names
w
First option worked, had to do same for
beneficiaryId
. Now got this error:
column reference "id" is ambiguous
I think i will use the syntax you pasted and see how it goes
s
If you get issues about ambiguous columns, you can assign an alias to a table with
AS
, like this:
Copy code
sql
CREATE OR REPLACE FUNCTION public.get_net_amount_total(in_id uuid)
RETURNS money
AS $BODY$
BEGIN
  RETURN QUERY
  SELECT count(netAmount) AS total
  FROM public.donations AS donations
  WHERE donations.id = in_id;
END;
$BODY$ language 'plpgsql';
w
getting this error
switched to using your syntax
Full code
Copy code
sql
CREATE OR REPLACE FUNCTION public.get_balance(id uuid)
RETURNS money
AS $BODY$
BEGIN
  RETURN QUERY
  SELECT count("netAmount") AS total
  FROM public.donations as donations
  WHERE donations."beneficiaryId" = id;
END;
$BODY$ language 'plpgsql';
s
The one I wrote can be run from the SQL editor directly - you should then be able to see it in the functions in the dashboard
w
oh!
cannot use RETURN QUERY in a non-SETOF function
Modified your query to ==>
Copy code
sql
CREATE OR REPLACE FUNCTION public.get_balance(id uuid)
RETURNS integer
AS $BODY$
BEGIN
  SELECT SUM("netAmount") AS total
  FROM public.donations as donations
  WHERE donations."beneficiaryId" = id;
  RETURN total;
END;
$BODY$ language 'plpgsql';
Still getting this error:
column reference "id" is ambiguous
s
If you have an
id
column on the same table, it might be trying to match against that. Whenever you're defining arguments for a function, it's best to make sure they don't share the name with any column - prefixing them with
in_
is how I prevent that from happening.
w
Found about that right now. Here's my working code:
Copy code
sql
DECLARE
  total integer;

BEGIN
  SELECT SUM("netAmount")
  INTO total
  FROM public.donations as donations
  WHERE donations."beneficiaryId" = userid; //params is now `userid`
  RETURN total;
END;
Thanks for your help and time