postgleam/decode

Types

A decoder that extracts a typed value from a row (list of columns).

pub opaque type RowDecoder(a)

A decoder that extracts a typed value from a single cell.

pub type ValueDecoder(a) =
  fn(option.Option(value.Value)) -> Result(a, error.Error)

Values

pub fn bool(
  val: option.Option(value.Value),
) -> Result(Bool, error.Error)

Decode a boolean value.

pub fn bytea(
  val: option.Option(value.Value),
) -> Result(BitArray, error.Error)

Decode a bytea (binary data) value.

pub fn date(
  val: option.Option(value.Value),
) -> Result(Int, error.Error)

Decode a date value (days since 2000-01-01).

pub fn element(
  index: Int,
  decoder: fn(option.Option(value.Value)) -> Result(
    a,
    error.Error,
  ),
  next: fn(a) -> RowDecoder(b),
) -> RowDecoder(b)

Decode the element at a given column index using a value decoder. Designed for use with Gleam’s use syntax.

let decoder = {
  use id <- decode.element(0, decode.int)
  use name <- decode.element(1, decode.text)
  decode.success(#(id, name))
}
pub fn float(
  val: option.Option(value.Value),
) -> Result(Float, error.Error)

Decode a float value (float4, float8).

pub fn int(
  val: option.Option(value.Value),
) -> Result(Int, error.Error)

Decode an integer value (int2, int4, int8).

pub fn json(
  val: option.Option(value.Value),
) -> Result(String, error.Error)

Decode a JSON value (string).

pub fn jsonb(
  val: option.Option(value.Value),
) -> Result(String, error.Error)

Decode a JSONB value (string).

pub fn numeric(
  val: option.Option(value.Value),
) -> Result(String, error.Error)

Decode a numeric/decimal value (string representation).

pub fn optional(
  decoder: fn(option.Option(value.Value)) -> Result(
    a,
    error.Error,
  ),
) -> fn(option.Option(value.Value)) -> Result(
  option.Option(a),
  error.Error,
)

Make any value decoder nullable (NULL-safe). Returns Ok(None) for NULL, Ok(Some(val)) for non-NULL.

pub fn run(
  decoder: RowDecoder(a),
  row: List(option.Option(value.Value)),
) -> Result(a, error.Error)

Run a decoder on a row.

pub fn success(value: a) -> RowDecoder(a)

Finalize a decoder chain with a value.

pub fn text(
  val: option.Option(value.Value),
) -> Result(String, error.Error)

Decode a text/string value.

pub fn timestamp(
  val: option.Option(value.Value),
) -> Result(Int, error.Error)

Decode a timestamp value (microseconds since 2000-01-01 00:00:00).

pub fn timestamptz(
  val: option.Option(value.Value),
) -> Result(Int, error.Error)

Decode a timestamptz value (microseconds since 2000-01-01 00:00:00 UTC).

pub fn uuid(
  val: option.Option(value.Value),
) -> Result(BitArray, error.Error)

Decode a UUID value (16-byte binary).

pub fn uuid_string(
  val: option.Option(value.Value),
) -> Result(String, error.Error)

Decode a UUID value as a hyphenated string (e.g. “550e8400-e29b-41d4-a716-446655440000”).

Search Document