João Vitor
06/17/2022, 2:31 PMsql
BEGIN
IF NOT EXISTS(SELECT 1 FROM cart WHERE user_id = auth.uid()) THEN
INSERT INTO cart (user_id, total) VALUES (auth.uid(), 0);
INSERT INTO cart_item (cart_id, product_id, user_id, quantity)
VALUES (
new.id,
new_product_id,
auth.uid(),
new_quantity
);
ELSE
UPDATE cart SET total = old.total + new_quantity * (SELECT price FROM product WHERE id = new_product_id)
WHERE user_id = auth.uid();
UPDATE cart_item
SET quantity = old.quantity + new_quantity
WHERE product_id = new_product_id AND user_id = auth.uid();
END IF;
RETURN new;
END;
Needle
06/17/2022, 2:31 PMgaryaustin
06/17/2022, 2:32 PMJoão Vitor
06/17/2022, 2:34 PMgaryaustin
06/17/2022, 2:34 PMJoão Vitor
06/17/2022, 2:34 PMgaryaustin
06/17/2022, 2:37 PMJoão Vitor
06/17/2022, 2:37 PMJoão Vitor
06/17/2022, 2:38 PMjs
await supabase.rpc('insert_item_to_cart', {
product_id: 'some-uuid',
quantity: 3,
})
João Vitor
06/17/2022, 2:39 PMnew
and old
keywords work whenever there's a INSERT
UPDATE
or DELETE
João Vitor
06/17/2022, 2:40 PMgaryaustin
06/17/2022, 2:40 PMgaryaustin
06/17/2022, 2:40 PMJoão Vitor
06/17/2022, 2:44 PMJoão Vitor
06/17/2022, 2:45 PMJoão Vitor
06/17/2022, 2:47 PMgaryaustin
06/17/2022, 2:49 PMJoão Vitor
06/17/2022, 2:50 PMnew
was the inserted datagaryaustin
06/17/2022, 2:50 PMJoão Vitor
06/17/2022, 2:50 PMINSERT INTO cart (user_id, total) VALUES (auth.uid(), 0);
João Vitor
06/17/2022, 2:51 PMnew
here would be the new cart
record inserted in the tableJoão Vitor
06/17/2022, 2:51 PMnew.id
as the cart.id
garyaustin
06/17/2022, 2:51 PMJoão Vitor
06/17/2022, 2:52 PMJoão Vitor
06/17/2022, 2:53 PMgaryaustin
06/17/2022, 2:54 PMJoão Vitor
06/17/2022, 2:55 PMJoão Vitor
06/17/2022, 2:56 PMcart.id
after this line
```sql
INSERT INTO cart (user_id, total) VALUES (auth.uid(), 0);
```Without doing another select?garyaustin
06/17/2022, 3:01 PMJoão Vitor
06/17/2022, 3:01 PMJoão Vitor
06/17/2022, 3:02 PMsql
INSERT INTO table (name)
OUTPUT Inserted.ID
VALUES('bob');
João Vitor
06/17/2022, 3:03 PMSCOPE_IDENTITY()
garyaustin
06/17/2022, 3:03 PMJoão Vitor
06/17/2022, 3:05 PMJoão Vitor
06/17/2022, 3:05 PMJoão Vitor
06/17/2022, 3:12 PMsql
DECLARE new_cart_item cart_item;
DECLARE new_cart_id uuid;
BEGIN
IF NOT EXISTS(SELECT 1 FROM cart WHERE user_id = auth.uid()) THEN
INSERT INTO cart (user_id, total) VALUES (auth.uid(), 0)
RETURNING id INTO new_cart_id;
INSERT INTO cart_item (cart_id, product_id, user_id, quantity)
VALUES (
new_cart_id,
new_product_id,
auth.uid(),
new_quantity
) RETURNING * INTO new_cart_item;
ELSE
UPDATE cart SET total = total + new_quantity * (SELECT price FROM product WHERE id = new_product_id)
WHERE user_id = auth.uid();
UPDATE cart_item
SET quantity = quantity + new_quantity
WHERE product_id = new_product_id AND user_id = auth.uid()
RETURNING * INTO new_cart_item;
END IF;
RETURN new_cart_item;
END;
João Vitor
06/17/2022, 3:14 PMJoão Vitor
06/17/2022, 3:14 PMJoão Vitor
06/17/2022, 3:14 PMJoão Vitor
06/17/2022, 3:19 PMjs
var data = {
cart_id: "d41373ff-c802-42f8-9436-5e02899cb0fb"
created_at: "2022-06-17T15:13:38.484863+00:00"
id: "693ac372-3d1c-4b3c-8e5d-f53134c8a54e"
product_id: "a5e19558-650d-400e-86c9-a894a8e85ef0"
quantity: 9
updated_at: "2022-06-17T15:16:06.054431+00:00"
user_id: "eb85eb1c-f9fe-45d2-9727-2a5e9d5cc5b9"
}
And I tried to .rpc().select('*, product(*)')
but got this error
Verify that 'insert_item_to_cart' and 'product' exist in the schema 'public' and that there is a foreign key relationship between them. If a new relationship was created, try reloading the schema cache.João Vitor
06/17/2022, 3:21 PMgaryaustin
06/17/2022, 3:22 PMJoão Vitor
06/17/2022, 6:51 PMsupabase.from('cart').select('*, items:cart_item(*)')
?João Vitor
06/17/2022, 6:54 PMgaryaustin
06/17/2022, 6:57 PMJoão Vitor
06/17/2022, 6:57 PMgaryaustin
06/17/2022, 6:58 PMNeedle
06/20/2022, 5:16 PM