EctoPostgresEnum

Ecto helper library to use PostgreSQL enums

Installation

The package can be installed by adding ecto_postgres_enum to your list of dependencies in mix.exs:

def deps do
[
{:ecto_postgres_enum, "~> 1.0"}
]
end

Usage

Te define enum simply use EctoPostgresEnum module like:

defmodule MyEnum do
use EctoPostgresEnum, values: [:my, :enum]
# optionally add schema and type
# by default type is underscored last part of module name and therefore it's not required
# use EctoPostgresEnum, schema: :non_public, type: :my_enum, values: [:my, :enum]
end

and then add it to your schema like:

defmodule MySchema do
use Ecto.Schema
alias Ecto.Changeset
schema "my_table" do
field(:my_field, MyEnum)
end
def changeset(my_element, my_params) do
Changeset.cast(my_element, my_params, [:my_field])
end
end

Finally you will also need to create migration like:

defmodule Example.Repo.Migrations.MyEnum do
use Ecto.Migration
def up do
MyEnum.create_db_enum
alter table(:my_table) do
add :my_field, :my_enum
end
end
def down do
alter table(:my_table) do
remove :my_field
end
MyEnum.drop_db_enum
end
end