glimr/db/connection
Database Connection Abstraction
Provides a unified interface over PostgreSQL (pog) and SQLite (sqlight) database connections. This allows application code to work with either database without changes.
Types
Config Type
Configuration for establishing a database connection.
Use postgres_config or sqlite_config to create instances.
pub type Config {
PostgresConfig(url: String, pool_size: Int)
SqliteConfig(path: String, pool_size: Int)
}
Constructors
-
PostgresConfig(url: String, pool_size: Int) -
SqliteConfig(path: String, pool_size: Int)
Connection Type
A database connection that abstracts over the underlying
driver. Use with the query module functions.
pub opaque type Connection
DbError Type
Unified error type for database operations.
pub type DbError {
NotFound
ConstraintError(message: String, constraint: String)
QueryError(message: String)
ConnectionError(message: String)
TimeoutError
DecodeError(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
Driver Type
Identifies which database driver is being used.
pub type Driver {
Postgres
Sqlite
}
Constructors
-
Postgres -
Sqlite
QueryResult Type
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))
Values
pub fn bool(value: Bool) -> Value
Boolean Value
Creates a boolean parameter value. Note: SQLite stores booleans as integers (0/1).
pub fn convert_placeholders(
sql: String,
driver: Driver,
) -> String
Convert Postgres Placeholder to SQLite
Converts $1, $2, etc. placeholders to ? for SQLite. This allows using consistent Postgres-style placeholders in SQL files.
pub fn driver(conn: Connection) -> Driver
Get Driver
Returns the driver type for a connection.
pub fn from_pog(conn: pog.Connection) -> Connection
Wrap Postgres Connection
Wraps a raw pog connection for use with the glimr db module.
pub fn from_sqlight(conn: sqlight.Connection) -> Connection
Wrap SQLite Connection
Wraps a raw sqlight connection for use with the glimr db module.
pub fn nullable(
inner: fn(a) -> Value,
value: option.Option(a),
) -> Value
Nullable Value
Creates a parameter value from an Option, converting None to NULL.
pub fn postgres_config(
url: String,
pool_size pool_size: Int,
) -> Config
Create Postgres Config
Creates a PostgreSQL configuration from a connection URL.
URL format: postgresql://user:password@host:port/database
Example:
let config = postgres_config(
"postgresql://postgres:secret@localhost:5432/myapp",
pool_size: 10,
)
pub fn sqlite_config(
path: String,
pool_size pool_size: Int,
) -> Config
Create SQLite Config
Creates a SQLite configuration from a file path.
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 string(value: String) -> Value
String Value
Creates a string/text parameter value.
pub fn to_pog(conn: Connection) -> pog.Connection
Get Raw Postgres Connection
Extracts the underlying pog connection. Panics if not Postgres.
pub fn to_pog_value(value: Value) -> pog.Value
Convert to Pog Value
Converts a generic Value to a pog-specific Value for use with PostgreSQL queries. Panics if given a SQLite-specific value.
pub fn to_sqlight(conn: Connection) -> sqlight.Connection
Get Raw SQLite Connection
Extracts the underlying sqlight connection. Panics if not SQLite.
pub fn to_sqlight_value(value: Value) -> sqlight.Value
Convert to SQLite Value
Converts a generic Value to a sqlight-specific Value for use with SQLite queries. Panics if given a PostgreSQL-specific value.