spaceship_db/driver

Types

A database driver whose operations may be asynchronous.

JavaScript database APIs such as Cloudflare D1 return Promises. Keep this separate from Driver so synchronous drivers remain easy to use.

pub type AsyncDriver {
  AsyncDriver(
    name: String,
    connect: fn(String) -> promise.Promise(
      Result(Connection, String),
    ),
    close: fn(Connection) -> promise.Promise(Result(Nil, String)),
    prepare: fn(Connection, String) -> promise.Promise(
      Result(Statement, String),
    ),
    exec: fn(Statement, List(value.Value)) -> promise.Promise(
      Result(List(dynamic.Dynamic), String),
    ),
    begin: fn(Connection) -> promise.Promise(
      Result(Transaction, String),
    ),
    commit: fn(Transaction) -> promise.Promise(
      Result(Nil, String),
    ),
    rollback: fn(Transaction) -> promise.Promise(
      Result(Nil, String),
    ),
    begin_transaction_exec: fn(
      dynamic.Dynamic,
      String,
      List(value.Value),
    ) -> promise.Promise(
      Result(#(List(dynamic.Dynamic), dynamic.Dynamic), String),
    ),
    begin_transaction_commit: fn(dynamic.Dynamic) -> promise.Promise(
      Result(Nil, String),
    ),
    begin_transaction_rollback: fn(dynamic.Dynamic) -> promise.Promise(
      Result(Nil, String),
    ),
  )
}

Constructors

Database connection handle.

Built-in FFI drivers pass their JavaScript handle directly, while pure Gleam drivers can use the constructor to store their connection state.

pub type Connection {
  Connection(url: String, api_token: String)
}

Constructors

  • Connection(url: String, api_token: String)

Driver trait — every backend implements this.

pub type Driver {
  Driver(
    name: String,
    connect: fn(String) -> Result(Connection, String),
    close: fn(Connection) -> Result(Nil, String),
    prepare: fn(Connection, String) -> Result(Statement, String),
    exec: fn(Statement, List(value.Value)) -> Result(
      List(dynamic.Dynamic),
      String,
    ),
    begin: fn(Connection) -> Result(Transaction, String),
    commit: fn(Transaction) -> Result(Nil, String),
    rollback: fn(Transaction) -> Result(Nil, String),
  )
}

Constructors

Prepared statement — reusable across executions.

The payload is driver-specific.

pub type Statement {
  Statement(connection: Connection, sql: String)
}

Constructors

Transaction context.

The payload is driver-specific. Turso stores its baton here.

pub type Transaction {
  Transaction(state: dynamic.Dynamic)
}

Constructors

Search Document