Upsert map in jsonb map array

There are Postgres operators to do this directly on the jsonb column in an atomic way. You will need to use a fragment and play with the arguments a little bit.

The list of operators is in the postgresql docs. You are looking for jsonb_set in particular.

The Postgres adapter in my Flippant library has an example of doing upsert with an array on a jsonb column:

https://github.com/sorentwo/flippant/blob/master/lib/flippant/adapters/postgres.ex#L79

It is a bit complicated but works perfectly and is entirely atomic.

3 Likes