Append JSON object to existing JSON

Hi all,

I trying to implement a simple comment section where users can add comments to a particular entry in our database.

I've set up a column in my postgres table with the following settings. The postgres type is JSONB with a default value of {}:

image

Here is a simplified version of the query. It useS the spread operator as seen on this post.

The first time the query runs I end up with this in my table:
{"person": "person A", "comment": "this is a comment"}

(I would have expected { {} , {"person": "person A", "comment": "this is a comment"}})

But after this, whenever the query runs I end up with the same result:
{"person": "person A", "comment": "this is a comment"}

and not {{}, {"person": "person A", "comment": "this is a comment"}, {"person": "person A", "comment": "this is a comment"} }

I guess I'm overlooking something simple but haven't been able to work it out yet, could someone point me in the right direction? Any help is appreciated!

I haven't used postgres, but I built something similar in Retool using MongoDB. What I did was store the comments in an array. Something like:

{ comments: [{ comment: "String", commentedBy: current_user.id, commentedAt: moment() },{ comment: "String", commentedBy: current_user.id, commentedAt: moment() }] }

When someone adds a new comment, the query just pushes it into that array.

Thanks @benbarry , I'll check that out. Might be easier to work with an array to be able to use a push() method.