// posts table
| id | content |
|----|------------------|
| 40 | jsonb data |
// content field data
{
"blocks": [
{
"type": "TYPE_TEST",
"content": {
"list": [
{
"id": "Ub4iAqhSi1UnRPHI",
"test": "2600/Ub4iAqhSi1UnRPHI"
},
{
"id": "zuTEwE9f3ZONgzEa",
"test": "2600/zuTEwE9f3ZONgzEa"
}
]
}
}
]
}
To push new object {"id":"some new id"}
to blocks[0].content.list
, run this query:
UPDATE posts
SET content = jsonb_insert(content, '{blocks,0,content,list,1}', '{"id":"some new id"}'::jsonb, true)
WHERE id=40;
const query = `
UPDATE posts
SET content = jsonb_insert(content, '{blocks,${blockIndex},content,list,${insertAfterIndex}}', '${JSON.stringify(newImage)}'::jsonb, true)
WHERE id=${postId};`
Result:
{
"blocks": [
{
"type": "TYPE_TEST",
"content": {
"list": [
{
"id": "Ub4iAqhSi1UnRPHI",
"test": "2600/Ub4iAqhSi1UnRPHI"
},
{
"id": "zuTEwE9f3ZONgzEa",
"test": "2600/zuTEwE9f3ZONgzEa"
},
{
"id":"some new id"
}
]
}
}
]
}