EctoRange.Timestamp (ecto_range v0.3.0)
View SourceA custom type for working with the Postgres tsrange type.
iex> range = {~N[2020-10-31 09:30:00], ~N[2020-11-02 10:00:00]}
iex> cs = TestApp.Table.changeset(%TestApp.Table{name: "EctoRange.Timestamp"}, %{tsrange: range})
iex> cs.changes
%{tsrange: %Postgrex.Range{lower: ~N[2020-10-31 09:30:00.000000], upper: ~N[2020-11-02 10:00:00.000000], lower_inclusive: true, upper_inclusive: true}}
tsrange
in Postgres is a continuous range, and does not have any equivalent Elixir struct.
Casting
EctoRange.Timestamp
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
{t:timestamp() | t:String.t/0, t:timestamp() | t:String.t/0}
can be used to express unbounded ranges, wherenil
represents an unbounded endpointPostgrex.Range.t/0
will be treated as a valid range representation
Loading
All data loaded from the database will be normalized into an inclusive range
to align with the semantics of Date.Range.t()
Summary
Functions
Callback implementation for Ecto.Type.embed_as/1
.
Callback implementation for Ecto.Type.equal?/2
.
Converts valid NaiveDateTime.t()
tuples into a Postgrex.Range.t()
Types
@type timestamp() :: nil | NaiveDateTime.t()
Functions
Callback implementation for Ecto.Type.embed_as/1
.
Callback implementation for Ecto.Type.equal?/2
.
@spec to_postgrex_range(Postgrex.Range.t() | {timestamp(), timestamp()}) :: Postgrex.Range.t()
Converts valid NaiveDateTime.t()
tuples into a Postgrex.Range.t()
iex> EctoRange.Date.to_postgrex_range({~N[2021-03-01 08:30:00], ~N[2023-03-30 10:30:00]})
%Postgrex.Range{lower: ~N[2021-03-01 08:30:00], upper: ~N[2023-03-30 10:30:00], lower_inclusive: true, upper_inclusive: true}