Ecto.JSON
makes Ecto
able to use any type of data (Integer, String, List, Map, etc.) which is valid JSON, not only key-value maps.
Installation
The package is available in Hex, it can be installed
by adding :ecto_json
to your list of dependencies in mix.exs
:
def deps do
[
{:ecto_json, "~> 0.1.0"},
]
end
Usage
The package is available in Hex, it can be installed
by adding :ecto_json
to your list of dependencies in mix.exs
:
defmodule Property do
use Ecto.Schema
import Ecto.Changeset
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "properties" do
field :key, :string
field :value, Ecto.JSON
timestamps([
type: :utc_datetime,
])
end
def changeset(property, attrs) do
property
|> cast(attrs, [:key, :value])
|> validate_required([:key, :value])
end
end