Postgres' range types

What's the best approach for adding tstzrange type column to a resource?
6 Replies
kernel
kernel2y ago
add an ecto type for it, then create an Ash Type for it
tellesleandro
tellesleandroOP2y ago
How do I do that? Can you give me some guidance?
kernel
kernel2y ago
i.e:
defmodule KS.Type.TstzRange do
use Ash.Type

@impl Ash.Type
def storage_type, do: :tstzrange

@impl Ash.Type
def cast_input(value, _) do
PgRanges.TstzRange.cast(value)
end

@impl Ash.Type
def dump_to_embedded(value, constraints) do
cast_input(value, constraints)
end

@impl Ash.Type
def cast_stored(value, _constraints) do
PgRanges.TstzRange.load(value)
end

@impl Ash.Type
def dump_to_native(value, _) do
PgRanges.TstzRange.dump(value)
end
end

defimpl Phoenix.HTML.Safe, for: PgRanges.TstzRange do
def to_iodata(value) do
"#{value.lower},#{value.upper}"
end
end
defmodule KS.Type.TstzRange do
use Ash.Type

@impl Ash.Type
def storage_type, do: :tstzrange

@impl Ash.Type
def cast_input(value, _) do
PgRanges.TstzRange.cast(value)
end

@impl Ash.Type
def dump_to_embedded(value, constraints) do
cast_input(value, constraints)
end

@impl Ash.Type
def cast_stored(value, _constraints) do
PgRanges.TstzRange.load(value)
end

@impl Ash.Type
def dump_to_native(value, _) do
PgRanges.TstzRange.dump(value)
end
end

defimpl Phoenix.HTML.Safe, for: PgRanges.TstzRange do
def to_iodata(value) do
"#{value.lower},#{value.upper}"
end
end
along with using the pg_ranges library
tellesleandro
tellesleandroOP2y ago
I'll follow that. Thanks.
kernel
kernel2y ago
then use it like, attribute :range, KS.Type.TstzRange etc
tellesleandro
tellesleandroOP2y ago
Perfect. Thank you very much.

Did you find this page helpful?