We have this postgresql type:
create type order_input as (
item text,
quantity integer);
And this postgresql function:
create or replace function insert_into_orders(order_input[])
returns void language plpgsql as $$
declare
inserted_id integer;
begin
insert into public.orders(orderdate)
values (now())
returning orderid into inserted_id;
insert into public.orderdetails(orderid, item, quantity)
select inserted_id, item, quantity
from unnest($1);
end $$;
To execute in pgadmin-4 we run:
select insert_into_orders(
array[
('Red Widget', 10),
('Blue Widget', 5)
]::order_input[]
);
I am trying to figure out how to execute the insert_into_orders function using the pg-promise javascript library. I've tried doing the following:
const pgp = require("pg-promise")();
const db = pgp(connectionObj);
await db.func("insert_into_orders", [{item:"Red Widget", quantity:10}, {item:"Blue Widget", quantity:5}]
but getting the following message:
{
"error": {
"message": "malformed array literal: \"{\"item\":\"Red Widget\", \"quantity\":10}\""
}
}
Would really appreciate if anyone knew how I had to structure my input for pg-promise, the original post is from here: Postgres Function to insert multiple records in two tables