glimr/db/pool_connection
Pool Connection Abstraction
Provides types and utilities for database connections. The actual connection handling is delegated to driver packages (glimr_sqlite, glimr_postgres) which register with the driver registry.
Types
Configuration for establishing a database connection. Use
postgres_config, postgres_params_config, or
sqlite_config to create instances.
pub type Config {
PostgresConfig(url: String, pool_size: Int)
PostgresParamsConfig(
host: String,
port: Int,
database: String,
username: String,
password: option.Option(String),
pool_size: Int,
)
SqliteConfig(path: String, pool_size: Int)
}
Constructors
-
PostgresConfig(url: String, pool_size: Int) -
PostgresParamsConfig( host: String, port: Int, database: String, username: String, password: option.Option(String), pool_size: Int, ) -
SqliteConfig(path: String, pool_size: Int)
Unified error type for database operations. This allows users to handle specific database errors any way they like.
pub type DbError {
NotFound
ConstraintError(message: String, constraint: String)
QueryError(message: String)
ConnectionError(message: String)
TimeoutError
DecodeError(message: String)
ConfigError(message: String)
}
Constructors
-
NotFoundThe requested row was not found (for single-row queries)
-
ConstraintError(message: String, constraint: String)A constraint was violated (unique, foreign key, etc.)
-
QueryError(message: String)A query syntax or execution error
-
ConnectionError(message: String)Connection to database failed or unavailable
-
TimeoutErrorQuery timed out
-
DecodeError(message: String)Result decoding failed
-
ConfigError(message: String)Connection configuration is invalid or missing required parameters
Identifies which database driver is being used. This allows you to seamlessly use multiple connections of different database drivers throughout your app.
pub type Driver {
Postgres
Sqlite
}
Constructors
-
Postgres -
Sqlite
A pooled database connection that wraps the underlying driver connection. The actual connection is stored as Dynamic and unwrapped by driver packages.
pub opaque type PoolConnection
The result of a database query, containing the number of affected rows and the returned data.
pub type QueryResult(t) {
QueryResult(count: Int, rows: List(t))
}
Constructors
-
QueryResult(count: Int, rows: List(t))
A parameter value that can be passed to a database query. Use the constructor functions to create values.
pub type Value {
IntValue(Int)
FloatValue(Float)
StringValue(String)
BoolValue(Bool)
NullValue
BlobValue(BitArray)
}
Constructors
-
IntValue(Int) -
FloatValue(Float) -
StringValue(String) -
BoolValue(Bool) -
NullValue -
BlobValue(BitArray)
Values
pub fn bool(value: Bool) -> Value
Creates a boolean parameter value. Note: SQLite stores booleans as integers (0/1).
pub fn convert_placeholders(
sql: String,
driver: Driver,
) -> String
Converts $1, $2, etc. placeholders to ? for SQLite. This allows using consistent Postgres-style placeholders in SQL files.
pub fn driver(connection: PoolConnection) -> Driver
Returns the driver type for the provided pool connection.
pub fn get_pool_ref(
connection: PoolConnection,
) -> dynamic.Dynamic
Extracts the pool reference from a pool connection. This is the reference returned by checkout that must be passed to checkin.
pub fn nullable(
inner: fn(a) -> Value,
value: option.Option(a),
) -> Value
Creates a parameter value from an Option, converting None to NULL.
pub fn postgres_config(
url: String,
pool_size pool_size: Int,
) -> Config
Creates a PostgreSQL configuration from a connection URL. This configuration will have its own pool of connections specific to this database.
URL format: postgresql://user:password@host:port/database
Example:
let config = postgres_config(
"postgresql://postgres:secret@localhost:5432/myapp",
pool_size: 10,
)
pub fn postgres_params_config(
host host: String,
port port: Int,
database database: String,
username username: String,
password password: option.Option(String),
pool_size pool_size: Int,
) -> Config
Creates a PostgreSQL configuration from individual
parameters. This is an alternative to postgres_config when
you have separate host, port, database, username, and
password values.
Example:
let config = postgres_params_config(
host: "localhost",
port: 5432,
database: "myapp",
username: "postgres",
password: Some("secret"),
pool_size: 10,
)
pub fn sqlite_config(
path: String,
pool_size pool_size: Int,
) -> Config
Creates a SQLite configuration from a file path. This configuration will have its own pool of connections specific to this database.
Use :memory: for an in-memory database.
Example:
let config = sqlite_config("data.db", pool_size: 5)
let memory_config = sqlite_config(":memory:", pool_size: 1)
pub fn unwrap_handle(
connection: PoolConnection,
) -> dynamic.Dynamic
Gets the raw connection handle. Used by driver packages to unwrap and cast to the native connection type.
pub fn wrap_connection(
driver: Driver,
handle: dynamic.Dynamic,
pool_ref: dynamic.Dynamic,
) -> PoolConnection
Creates a pool connection wrapper. Called by driver packages when checking out a connection from the pool.