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,
is_default: Bool,
url: Result(String, String),
pool_size: Result(Int, String),
)
PostgresConnection(
name: String,
is_default: Bool,
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,
is_default: Bool,
database: Result(String, String),
pool_size: Result(Int, String),
)
}
Constructors
-
PostgresUriConnection( name: String, is_default: Bool, url: Result(String, String), pool_size: Result(Int, String), ) -
PostgresConnection( name: String, is_default: Bool, 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, is_default: Bool, database: Result(String, String), pool_size: Result(Int, String), )
Identifies the underlying database type (Postgres or SQLite).
pub type DriverType {
Postgres
Sqlite
}
Constructors
-
Postgres -
Sqlite
Values
pub fn connection_name(connection: Connection) -> String
Returns the name identifying this connection configuration.
pub fn connection_type(connection: Connection) -> DriverType
Returns whether the connection is for Postgres or SQLite.
pub fn is_default(connection: Connection) -> Bool
Returns whether this connection is marked as the default for its driver type. Only one connection per driver should have is_default set to True.
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 driver names.
Example: “main” -> “Main”, “my_database” -> “MyDatabase”
pub fn validate(connection: Connection) -> List(String)
Validates that all required parameters for a connection are present. Returns a list of missing parameter names.
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.