glimr/db/pool
Connection Pooling
Provides connection pooling for both PostgreSQL and SQLite. PostgreSQL uses pog’s built-in pooling. SQLite uses an ETS heir-based pool (ported from pgo) for crash-safe connection management.
Types
Values
pub fn checkin(pool: Pool, conn: connection.Connection) -> Nil
Checkin Connection
Returns a connection to the pool. Only needed if you used
checkout directly instead of get_connection.
pub fn checkout(
pool: Pool,
) -> Result(connection.Connection, connection.DbError)
Get Connection (Unsafe)
Gets a connection from the pool without automatic return.
You MUST call release when done. However, if your process
crashes without calling release, the connection will be
automatically reclaimed by the pool (crash-safe).
pub fn get_connection(
pool: Pool,
next: fn(connection.Connection) -> next,
) -> next
Get Connection
Borrows a connection from the pool, executes a function, and returns the connection to the pool. Can be used in controllers, actions, etc.
Example:
pub fn show(req: Request, ctx: Context) -> Response {
use conn <- pool.get_connection(ctx.db.pool)
use user <- user_repository.find(conn, 1)
wisp.html_response("Hello " <> user.name, 200)
}
pub fn get_connection_or(
pool: Pool,
f: fn(connection.Connection) -> Result(a, connection.DbError),
) -> Result(a, connection.DbError)
Get Connection (Result)
Borrows a connection from the pool, executes a function, and returns the connection to the pool. Returns a Result, useful for operations that need error handling like console commands that may need a connection.
Example:
use conn <- pool.get_connection_or(pool)
query.execute(conn, "SELECT 1", [])
pub fn start(
config: connection.Config,
) -> Result(Pool, connection.DbError)
Start Pool
Starts a connection pool with the given configuration. For Postgres, this creates a named connection pool. For SQLite, this creates a pool of connections managed by an Erlang process.
Example:
let config = connection.sqlite_config("data.db", pool_size: 15)
let assert Ok(pool) = pool.start(config)