postgleam

Types

An opaque connection handle backed by an OTP actor.

pub opaque type Connection

A query parameter value (nullable).

pub type Param =
  option.Option(value.Value)

Result from a decoded query.

pub type Response(a) {
  Response(rows: List(a), count: Int, tag: String)
}

Constructors

  • Response(rows: List(a), count: Int, tag: String)

Values

pub fn bool(val: Bool) -> option.Option(value.Value)

Create a boolean parameter.

pub fn bytea(val: BitArray) -> option.Option(value.Value)

Create a bytea (binary data) parameter.

pub fn close(
  conn: Connection,
  name: String,
) -> Result(Nil, error.Error)

Close a prepared statement.

pub fn connect(
  config: config.Config,
) -> Result(Connection, error.Error)

Connect to PostgreSQL and return a connection handle. The connection is managed by an OTP actor process.

pub fn date(val: Int) -> option.Option(value.Value)

Create a date parameter (days since 2000-01-01).

pub fn disconnect(conn: Connection) -> Nil

Disconnect from PostgreSQL and stop the actor.

pub fn execute(
  conn: Connection,
  prepared: connection.PreparedStatement,
  params: List(option.Option(value.Value)),
) -> Result(connection.ExtendedQueryResult, error.Error)

Execute a previously prepared statement.

pub fn float(val: Float) -> option.Option(value.Value)

Create a float parameter (float4, float8).

pub fn int(val: Int) -> option.Option(value.Value)

Create an integer parameter (int2, int4, int8).

pub fn json(val: String) -> option.Option(value.Value)

Create a JSON parameter.

pub fn jsonb(val: String) -> option.Option(value.Value)

Create a JSONB parameter.

pub fn null() -> option.Option(value.Value)

Create a NULL parameter.

pub fn nullable(
  val: option.Option(a),
  to_param: fn(a) -> option.Option(value.Value),
) -> option.Option(value.Value)

Create a nullable parameter from an Option value.

postgleam.nullable(Some("hello"), postgleam.text)
// => Some(value.Text("hello"))

postgleam.nullable(None, postgleam.text)
// => None
pub fn numeric(val: String) -> option.Option(value.Value)

Create a numeric/decimal parameter (string representation).

pub fn prepare(
  conn: Connection,
  name: String,
  sql: String,
) -> Result(connection.PreparedStatement, error.Error)

Prepare a named statement for later execution.

pub fn query(
  conn: Connection,
  sql: String,
  params: List(option.Option(value.Value)),
) -> Result(connection.ExtendedQueryResult, error.Error)

Execute a parameterized query using the extended protocol (binary format). Returns typed values decoded through the codec registry.

Panics if the actor does not respond within the configured timeout.

pub fn query_error(message: String) -> error.Error

Create an error value for use in transactions or other contexts.

pub fn query_one(
  conn: Connection,
  sql: String,
  params: List(option.Option(value.Value)),
  decoder: decode.RowDecoder(a),
) -> Result(a, error.Error)

Execute a query and return the first decoded row, or error if no rows.

pub fn query_with(
  conn: Connection,
  sql: String,
  params: List(option.Option(value.Value)),
  decoder: decode.RowDecoder(a),
) -> Result(Response(a), error.Error)

Execute a parameterized query and decode each row using the provided decoder.

pub fn simple_query(
  conn: Connection,
  sql: String,
) -> Result(List(connection.SimpleQueryResult), error.Error)

Execute a simple text query (no parameters). Returns text-formatted results.

pub fn text(val: String) -> option.Option(value.Value)

Create a text/string parameter.

pub fn timestamp(val: Int) -> option.Option(value.Value)

Create a timestamp parameter (microseconds since 2000-01-01 00:00:00).

pub fn timestamptz(val: Int) -> option.Option(value.Value)

Create a timestamptz parameter (microseconds since 2000-01-01 00:00:00 UTC).

pub fn transaction(
  conn: Connection,
  f: fn(Connection) -> Result(a, error.Error),
) -> Result(a, error.Error)

Execute a function within a transaction. Automatically BEGINs, and COMMITs on Ok or ROLLBACKs on Error.

pub fn uuid(val: BitArray) -> option.Option(value.Value)

Create a UUID parameter (16-byte binary).

Search Document