Exploring options for storing custom data in Ecto

Click for: original source

Ever wanted to store a blob of custom data on a database record? The data we want to store might be complex too, made up of multiple fields and even lists. When the data is stored using Ecto there are several ways we can do it. This article explores two paths and discusses some of the pros and cons for each approach. By Mark Ericksen.

Ecto is Elixir’s database wrapper. If you want to use a database with Elixir, you need to know Ecto.

defmodule Core.GameSaves.GameSave do
  use Ecto.Schema

  schema "game_saves" do
    # ...
    field :data, :map, default: %{}, required: true

    belongs_to :account, Core.Accounts.Account
    timestamps()
  end
end

The article main content covers:

  • Our custom data
  • Path one: Store data as a map
  • Path Two: Store data as an Erlang encoded binary
  • Security considerations
  • Version the data!

Together we’ve peered down the two paths a bit and hopefully now we can quickly determine which path we want next time we come to this fork in the road. Good read!

[Read More]

Tags elixir web-development functional-programming apis erlang