Update JSONB documents in tables

I have a schema:

schema "assets" do
  field :title, :string
  field :description, :string
  field :files, :map
end

I don’t want to use a embedded schema for :files, because there may be a large list of optional fields here. So is it good way to update my :files JSONB document?

Resources.update_asset(asset, %{files: Map.merge(asset.files, new_params)})

It works, but looks not elegant. Actually there is not even a PostgreSQL jsonb concatenate function || used, because my data is concatenated at the Elixir level.

This is what fragment is for, then you can do it all in-query without it ever needing to arrive locally if you so want. :slight_smile:

3 Likes