I have a table with some basic jsonb, for this example lets call it transactions
CREATE TABLE transactions (
transaction_id text NOT NULL,
body jsonb,
);
So inside the body I have some data we can ignore but it's an object something like {"price": 10} etc
I want to add a refund key, and default it to an empty array, or push into the array in one query.
The following accomplishes it in two queries:
UPDATE transactions
SET body = jsonb_set(body, '{refunds}', '[]'::jsonb, true)
WHERE transaction_id = 'abc'
UPDATE transactions
SET body = jsonb_insert(body, '{refunds,0}', '{"id":"any"}'::jsonb, true)
WHERE transaction_id = 'abc'
But I can't seem to instantiate the array if it doesn't exist in a single transaction/query. Tried the postgres jsonb docs as well of course.
Thanks in advance!