based

Types

pub type BasedAdapter(conf, conn, t) {
  BasedAdapter(
    with_connection: WithConnection(conf, conn, t),
    conf: conf,
    service: Service(conn),
  )
}

Constructors

  • BasedAdapter(
      with_connection: WithConnection(conf, conn, t),
      conf: conf,
      service: Service(conn),
    )
pub type BasedError {
  BasedError(code: String, name: String, message: String)
}

Constructors

  • BasedError(code: String, name: String, message: String)

DB

opaque </>
pub opaque type DB
pub type Message {
  Execute(
    reply_with: Subject(Result(List(Dynamic), BasedError)),
    query: Query,
  )
  Shutdown
}

Constructors

  • Execute(
      reply_with: Subject(Result(List(Dynamic), BasedError)),
      query: Query,
    )
  • Shutdown
pub type Query {
  Query(sql: String, values: List(Value))
}

Constructors

  • Query(sql: String, values: List(Value))
pub type Returned(a) {
  Returned(count: Int, rows: List(a))
}

Constructors

  • Returned(count: Int, rows: List(a))
pub type Service(conn) =
  fn(Query, conn) -> Result(List(Dynamic), BasedError)

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(conf, conn, t) =
  fn(conf, fn(conn) -> t) -> t

Functions

pub fn all(
  query: Query,
  db: DB,
  decoder: fn(Dynamic) -> Result(a, List(DecodeError)),
) -> Result(Returned(a), BasedError)

Applies the provided Decoder to all rows returned by the Query.

pub fn bool(value: Bool) -> Value

Converts a bool to a Value type

pub fn decode(
  rows: List(Dynamic),
  decoder: fn(Dynamic) -> Result(a, List(DecodeError)),
) -> Result(Returned(a), BasedError)

Decodes a list of Dynamic values with the provided Decoder.

pub fn execute(
  query: Query,
  db: DB,
) -> Result(List(Dynamic), BasedError)

Performs a query and returns a list of Dynamic values.

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

Returns a Query record with the provided SQL string and an empty list of values. This function can be used on its own for queries that don’t require values. Its return value may also be piped into with_values to be given the appropriate list of values required for the query.

pub fn null() -> Value

Returns a Null Value type

pub fn one(
  query: Query,
  db: DB,
  decoder: fn(Dynamic) -> Result(a, List(DecodeError)),
) -> Result(a, BasedError)

Returns the first row returned by the query after being decoded by the provided Decoder. Useful for queries where only one row should be returned. If more rows are returned by the query, only the first row will be decoded and returned from this function.

pub fn register(
  based_adapter: BasedAdapter(a, b, c),
  callback: fn(DB) -> c,
) -> c

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_values(query: Query, values: List(Value)) -> Query

Appends a list of values to the provided Query record.

Search Document