glimr/db/driver

Database Connection Configuration

Provides connection types for configuring database connections in a type-safe way. Users define connections in their database_provider.gleam file which is loaded at runtime.

Types

Represents a named database connection configuration. Each connection has a name that identifies it and connection parameters specific to the database type.

Use PostgresUriConnection for PostgreSQL with a connection URL. Use PostgresConnection for PostgreSQL with individual parameters. Use SqliteConnection for SQLite databases.

pub type Connection {
  PostgresUriConnection(
    name: String,
    url: Result(String, String),
    pool_size: Result(Int, String),
  )
  PostgresConnection(
    name: String,
    host: Result(String, String),
    port: Result(Int, String),
    database: Result(String, String),
    username: Result(String, String),
    password: Result(String, String),
    pool_size: Result(Int, String),
  )
  SqliteConnection(
    name: String,
    database: Result(String, String),
    pool_size: Result(Int, String),
  )
}

Constructors

  • PostgresUriConnection(
      name: String,
      url: Result(String, String),
      pool_size: Result(Int, String),
    )
  • PostgresConnection(
      name: String,
      host: Result(String, String),
      port: Result(Int, String),
      database: Result(String, String),
      username: Result(String, String),
      password: Result(String, String),
      pool_size: Result(Int, String),
    )
  • SqliteConnection(
      name: String,
      database: Result(String, String),
      pool_size: Result(Int, String),
    )

Identifies the underlying database type for a connection. Currently supports Postgres and SQLite as the two available database driver implementations.

pub type DriverType {
  Postgres
  Sqlite
}

Constructors

  • Postgres
  • Sqlite

Values

pub fn connection_name(connection: Connection) -> String

Returns the name identifying this connection configuration. The name is used to look up specific connections when multiple database connections are configured in the app.

pub fn connection_type(connection: Connection) -> DriverType

Returns whether the connection is for Postgres or SQLite. Inspects the connection variant to determine the database type without needing to access individual fields.

pub fn find_by_name(
  name: String,
  connections: List(Connection),
) -> Connection

Searches through a list of connections to find one with the specified name. Returns Ok with the connection if found, or Error(Nil) if no connection matches the given name.

pub fn to_config(
  connection: Connection,
) -> pool_connection.Config

Converts a Connection to a pool_connection.Config. Panics with a helpful message if any required environment variables are missing.

pub fn to_pascal_case(name: String) -> String

Converts a snake_case or lowercase name to PascalCase. Used for generating type names from connection names. Splits on underscores and capitalizes each segment.

pub fn validate(connection: Connection) -> List(String)

Validates that all required parameters for a connection are present. Returns a list of missing parameter names, which will be empty if the connection is fully configured.

pub fn with_pool_size(
  connection: Connection,
  size: Int,
) -> Connection

Returns a new connection with the pool size overridden to the specified value. Useful for console commands that only need a single connection.

Search Document