glimr/db/decode

Database Decoder Utilities

Custom decoders that handle differences between PostgreSQL and SQLite. SQLite lacks native boolean support and stores booleans as integers (0/1), while PostgreSQL has true booleans. Array columns come back as native Erlang lists in PostgreSQL but as JSON text in SQLite.

These decoders abstract over driver differences so application code can work with Gleam types regardless of the backend.

Values

pub fn bool() -> decode.Decoder(Bool)

Decodes a boolean value from either a native boolean (PostgreSQL) or an integer 0/1 (SQLite). Returns True for any non-zero integer, False for zero.

Example:

let decoder = {
  use active <- decode.field("active", db_decode.bool())
  decode.success(active)
}
pub fn list_of(
  inner: decode.Decoder(a),
) -> decode.Decoder(List(a))

PostgreSQL returns array columns as native Erlang lists, but SQLite doesn’t have arrays — it stores them as JSON text like ["a","b"]. Without this decoder, generated models would need separate code paths per database. Trying the native list first means Postgres takes the fast path, and the JSON fallback only kicks in for SQLite.

Search Document