WalEx.Casting.Types (WalEx v4.7.0)

View Source

Cast from Postgres to Elixir types

Implementation inspired by Cainophile, Supabase Realtime and Sequin

Summary

Functions

Casts a PostgreSQL string value to its appropriate Elixir type.

Functions

cast_record(record, type)

Casts a PostgreSQL string value to its appropriate Elixir type.

Examples

iex> cast_record("t", "bool")
true

iex> cast_record("123", "int4")
123

iex> cast_record("123.45", "numeric")
#Decimal<123.45>

iex> cast_record("{1,2,3}", "_int4")
[1, 2, 3]

iex> cast_record("2024-01-15T10:30:00Z", "timestamptz")
#DateTime<2024-01-15 10:30:00Z>

Special values like NaN and Infinity are handled:

iex> cast_record("NaN", "float8")
:nan

Returns the original value if casting fails.