based

Types

pub type Adapter(a, c) =
  fn(Query(a), c) -> Result(Returned(a), Nil)
pub type DB(a, c) {
  DB(conn: c, execute: Adapter(a, c))
}

Constructors

  • DB(conn: c, execute: Adapter(a, c))
pub type Query(a) {
  Query(
    sql: String,
    args: List(Value),
    decoder: Option(dynamic.Decoder(a)),
  )
}

Constructors

  • Query(
      sql: String,
      args: List(Value),
      decoder: Option(dynamic.Decoder(a)),
    )
pub type Returned(a) {
  Returned(count: Int, rows: List(a))
}

Constructors

  • Returned(count: Int, rows: List(a))

Callers will interact with these Value types when building queries. Their chosen backend is responsible for converting these Value types to the appropriate type.

pub type Value {
  String(String)
  Int(Int)
  Float(Float)
  Bool(Bool)
  Null
}

Constructors

  • String(String)
  • Int(Int)
  • Float(Float)
  • Bool(Bool)
  • Null

Defines a valid with_connection function

pub type WithConnection(a, b, c) =
  fn(b, fn(DB(a, c)) -> Result(Returned(a), Nil)) ->
    Result(Returned(a), Nil)

Functions

pub fn all(
  query: Query(a),
  db: DB(a, b),
) -> Result(Returned(a), Nil)

The same as exec, but explicitly tells a reader that all queried rows are expected.

pub fn bool(value: Bool) -> Value

Converts a bool to a Value type

pub fn exec(
  query: Query(a),
  db: DB(a, b),
) -> Result(Returned(a), Nil)

Performs the query against the provided db

pub fn float(value: Float) -> Value

Converts a float to a Value type

pub fn int(value: Int) -> Value

Converts an int to a Value type

pub fn new_query(sql: String) -> Query(a)
pub fn null() -> Value

Returns a Null Value type

pub fn one(query: Query(a), db: DB(a, b)) -> Result(a, Nil)

Returns one queried row

pub fn register(
  with_connection: fn(a, fn(DB(b, c)) -> Result(Returned(b), Nil)) ->
    Result(Returned(b), Nil),
  b: a,
  callback: fn(DB(b, c)) -> Result(Returned(b), Nil),
) -> Result(Returned(b), Nil)

Expects a with_connection function and its first required argument. For a library implementing a with_connection function, the required argument will likely be its configuration data. In the case of based/testing.with_connection, the required argument is the expected return data.

pub fn string(value: String) -> Value

Converts a string to a Value type

pub fn with_args(query: Query(a), args: List(Value)) -> Query(a)
pub fn with_decoder(
  query: Query(a),
  decoder: fn(Dynamic) -> Result(a, List(DecodeError)),
) -> Query(a)
Search Document