README.md

# 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`:

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

## Usage

Te define enum simply use `EctoPostgresEnum` module like:

```elixir
defmodule MyEnum do
  use EctoPostgresEnum, type: :my_enum, values: [:my, :enum]

  # optionally add schema:

  # use EctoPostgresEnum, schema: :non_public, type: :my_enum, values: [:my, :enum]
end
```

and then add it to your schema like:

```elixir
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:

```elixir
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
```