I’ve got an Ecto schema that has a column where I store some random data from a 3rd-party provider. This is mostly just for book-keeping purposes so we can search/parse it later if needed. In the database, this meta column is defined as a string. In my code, I end up with a Map which represents the data that I wish to store.
My question is: what’s the most elegant way to convert the Map to JSON on the way into the database and convert it back into a map on the way out? Could this accomplished via a custom Ecto.Type (I think yes)? Or should it be handled via some other means?
If you’re using postgres I’d recommend storing the field as jsonb in the database instead of a string, then if you have a field :metadata, :map ecto will handle the serialization for you (and you can even query and index directly on fields inside the jsonb column)
Just to be thorough for future visitors, here’s the full example:
From my migration:
# ...
# (The :map type gets mapped to :jsonb for PostGres from what I understand)
add :meta, :map, default: "{}" # <-- assuming I always want to store a JSON object
From my schema:
# ...
field :meta, :map, null: false, default: "{}"