Pure helpers for moving values between Postgres and the UI.
Two concerns live here:
cast_expr/2— given a column'sinformation_schemadata_type(andudt_namefor arrays / user-defined types), returns the Postgres type expression a text parameter should be cast to on write, ornilwhen the column is already textual and needs no cast.display / edit formatting — Postgrex returns native Elixir terms (
Date,Decimal,NaiveDateTime, …).display/1renders them for the grid;edit_value/1renders them for an editable text input.
Summary
Functions
Returns the cast target for a column, or nil if no cast is needed.
Formats a value for a specific HTML control.
Renders a Postgrex value for read-only display.
Like display/1, but uses the column's Postgres type to render binaries
accurately. Pass the information_schema data_type (e.g. "uuid",
"bytea") as the second argument; without it, 16-byte binaries are
optimistically rendered as UUIDs (since a UUID PK is by far the more common
case to render in a grid).
Renders a value for an editable text input. NULL becomes an empty string.
Maps a Postgres data_type to the kind of form control to render.
Functions
Returns the cast target for a column, or nil if no cast is needed.
Examples
iex> Lantern.Coercion.cast_expr("integer", "int4")
"integer"
iex> Lantern.Coercion.cast_expr("text", "text")
nil
iex> Lantern.Coercion.cast_expr("ARRAY", "_int4")
"int4[]"
Formats a value for a specific HTML control.
Date/time controls need ISO shapes (YYYY-MM-DD, YYYY-MM-DDTHH:MM:SS,
HH:MM:SS); everything else falls back to edit_value/1.
Renders a Postgrex value for read-only display.
Returns :null for nil so callers can distinguish SQL NULL from an empty
string; every other value becomes a String.t().
Like display/1, but uses the column's Postgres type to render binaries
accurately. Pass the information_schema data_type (e.g. "uuid",
"bytea") as the second argument; without it, 16-byte binaries are
optimistically rendered as UUIDs (since a UUID PK is by far the more common
case to render in a grid).
Renders a value for an editable text input. NULL becomes an empty string.
Pass the column's Postgres type (e.g. "uuid", "bytea") as the second
argument so binary values are disambiguated. Without it, 16-byte binaries are
treated as UUIDs.
Maps a Postgres data_type to the kind of form control to render.
Examples
iex> Lantern.Coercion.input_type("boolean")
:boolean
iex> Lantern.Coercion.input_type("timestamp without time zone")
:datetime
iex> Lantern.Coercion.input_type("text")
:text