pg_value

PostgreSQL values, along with their encoders and decoders. Can be used by PostgreSQL client libraries written in gleam. Currently used by pgl.

Types

The Value type represents PostgreSQL data types. Values can be encoded to PostgreSQL’s binary format. Values can be used when interacting with PostgreSQL databases through client libraries like pgl.

pub type Value {
  Null
  Bool(Bool)
  Int(Int)
  Float(Float)
  Text(String)
  Bytea(BitArray)
  Time(calendar.TimeOfDay)
  Date(calendar.Date)
  Timestamp(timestamp.Timestamp)
  Timestamptz(timestamp.Timestamp, duration.Duration)
  Interval(interval.Interval)
  Array(List(Value))
  Uuid(BitArray)
  Hstore(dict.Dict(String, option.Option(String)))
  Enum(String)
  Json(json.Json)
}

Constructors

Values

pub fn array(elements: List(a), of kind: fn(a) -> Value) -> Value

Returns an Array value. Each element is converted using the provided function.

pub fn bool(bool: Bool) -> Value

Returns a Bool value.

pub fn bytea(bytea: BitArray) -> Value

Returns a Bytea value.

pub fn date(date: calendar.Date) -> Value

Returns a Date value.

pub fn date_decoder() -> decode.Decoder(calendar.Date)

Returns a decoder for Date values.

pub fn decode(
  bits: BitArray,
  info: type_info.TypeInfo,
) -> Result(dynamic.Dynamic, String)

Decodes binary PostgreSQL data into a Dynamic value. Dynamic values can then be decoded using gleam/dynamic/decode.

pub fn encode(
  value: Value,
  info: type_info.TypeInfo,
) -> Result(BitArray, String)

Encodes a Value as a PostgreSQL data type

pub fn enum(label: String) -> Value

Returns an Enum value with the given label.

pub const false: Value
pub fn float(float: Float) -> Value

Returns a Float value.

pub fn hstore(
  hstore: dict.Dict(String, option.Option(String)),
) -> Value

Returns an Hstore value. Keys map to optional string values.

pub fn int(int: Int) -> Value

Returns an Int value.

pub fn interval(interval: interval.Interval) -> Value

Returns an Interval value.

pub fn json(json: json.Json) -> Value

Returns a Json value.

pub const null: Value
pub fn nullable(
  inner_type: fn(a) -> Value,
  optional: option.Option(a),
) -> Value

Checks if the provided value is option.Some or option.None. If None then the value returned is value.Null. If Some value is provided then it is passed to the inner_type function.

Example:

  let int = pg_value.nullable(pg_value.int, Some(10))

  let null = pg_value.nullable(pg_value.int, None)
pub fn text(text: String) -> Value

Returns a Text value.

pub fn time(time_of_day: calendar.TimeOfDay) -> Value

Returns a Time value.

pub fn time_decoder() -> decode.Decoder(calendar.TimeOfDay)

Returns a decoder for TimeOfDay values.

pub fn timestamp(timestamp: timestamp.Timestamp) -> Value

Returns a Timestamp value.

pub fn timestamp_decoder() -> decode.Decoder(timestamp.Timestamp)

Returns a decoder for Timestamp values.

pub fn timestamptz(
  timestamp: timestamp.Timestamp,
  offset: duration.Duration,
) -> Value

Returns a Timestamp value with a timezone offset.

pub fn to_string(value: Value) -> String

Converts a Value to a string formatted properly for PostgreSQL

pub const true: Value
pub fn uuid(uuid: BitArray) -> Value

Returns a UUID Value. Callers are responsible for ensuring the provided value is a valid UUID. If the value is not a valid UUID, encoding will fail.

Search Document