View Source EctoRange.Date (ecto_range v0.2.1)

A custom type for working with the Postgres daterange type.

iex> range = Date.range(~D[1989-09-22], ~D[2021-03-01])
iex> cs = TestApp.Table.changeset(%TestApp.Table{name: "EctoRange.Date"}, %{range: range})
iex> cs.changes
%{range: %Postgrex.Range{lower: ~D[1989-09-22], upper: ~D[2021-03-01], lower_inclusive: true, upper_inclusive: true}}

casting

Casting

EctoRange.Date provides a couple of conveniences when casting data. All valid data will be cast into a Postgrex.Range.t/0 struct. When supplied to an Ecto.Changeset, the following types are valid

  • Date.Range.t/0 will be treated as an inclusive range
  • {t:date() | t:String.t/0, t:date() | t:String.t/0} can be used to express unbounded ranges, where nil represents an unbounded endpoint

  • Postgrex.Range.t/0 will be treated as a valid range representation

loading

Loading

All data loaded from the database will be normalized into an inclusive range to align with the semantics of Date.Range.t()

Link to this section Summary

Functions

Callback implementation for Ecto.Type.embed_as/1.

Callback implementation for Ecto.Type.equal?/2.

Converts a Postgrex.Range.t() into a normalized form. For bounded ranges, it will make the lower and upper bounds inclusive to align with the semantics of Date.Range.t()

Converts valid Date.Range.t() or Date.t() tuples into a Postgrex.Range.t()

Link to this section Types

@type date() :: nil | Date.t()

Link to this section Functions

Callback implementation for Ecto.Type.embed_as/1.

Callback implementation for Ecto.Type.equal?/2.

Converts a Postgrex.Range.t() into a normalized form. For bounded ranges, it will make the lower and upper bounds inclusive to align with the semantics of Date.Range.t()

iex> range = %Postgrex.Range{lower: ~D[1989-09-22], upper: ~D[2021-03-02], lower_inclusive: true, upper_inclusive: false}
iex> EctoRange.Date.normalize_range(range)
%Postgrex.Range{lower: ~D[1989-09-22], upper: ~D[2021-03-01], lower_inclusive: true, upper_inclusive: true}

iex> range = %Postgrex.Range{lower: ~D[1989-09-21], upper: ~D[2021-03-01], lower_inclusive: false, upper_inclusive: true}
iex> EctoRange.Date.normalize_range(range)
%Postgrex.Range{lower: ~D[1989-09-22], upper: ~D[2021-03-01], lower_inclusive: true, upper_inclusive: true}
Link to this function

to_postgrex_range(range)

View Source
@spec to_postgrex_range(Date.Range.t() | Postgrex.Range.t() | {date(), date()}) ::
  Postgrex.Range.t()

Converts valid Date.Range.t() or Date.t() tuples into a Postgrex.Range.t()

iex> EctoRange.Date.to_postgrex_range(Date.range(~D[1989-09-22], ~D[2021-03-01]))
%Postgrex.Range{lower: ~D[1989-09-22], upper: ~D[2021-03-01], lower_inclusive: true, upper_inclusive: true}