Skip to content

Commit

Permalink
Include an example in the readme
Browse files Browse the repository at this point in the history
  • Loading branch information
davydog187 committed Mar 26, 2023
1 parent a9c456c commit c8bd661
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,65 @@ while providing the full flexibility of Postgres Range types inside of Ecto.
| tstzrange | `EctoRange.TimestampTZ` |
| daterange | `EctoRange.Date` |

## Usage

`EctoRange` provides custom types, and can be used like any other Ecto type.

```elixir
defmodule MyApp.Repo.Migrations.AddRange do
use Ecto.Migration

def change do
create table(:my_table) do
add :name, :string
add :range, :daterange
end
end
end

defmodule MyApp.Table do
use Ecto.Schema

import Ecto.Changeset

schema "my_table" do
field(:name, :string)
field(:range, EctoRange.Date)
end

def changeset(table, params) do
table
|> cast(params, [:name, :range])
|> validate_required([:name, :range])
end
end

iex> alias MyApp.Table

iex> range = Date.range(~D[1989-09-22], ~D[2021-03-01])

iex> cs = Table.changeset(%Table{}, %{name: "table", range: range}}

iex> cs.changes
%Ecto.Changeset{
changes: %{
range: %Postgrex.Range{
lower: ~D[1989-09-22],
upper: ~D[2021-03-01],
lower_inclusive: true,
upper_inclusive: true
},
name: "name"
},
data: %MyApp.Table{id: nil, name: nil, range: nil},
params: %{
"range" => Date.range(~D[1989-09-22], ~D[2021-03-01]),
"name" => "name"
},
valid?: true
}
```

## Installation

If [available in Hex](https://hex.pm/docs/publish), the package can be installed
Expand Down

0 comments on commit c8bd661

Please sign in to comment.