Skip to content

Latest commit

 

History

History
62 lines (44 loc) · 1.25 KB

File metadata and controls

62 lines (44 loc) · 1.25 KB

Database

Connecting

let db = Database::connect("postgres://...").await?;

Customize sqlx pool options when needed:

let db = Database::connect_with_options(
    "postgres://...",
    dbkit::PgPoolOptions::new().max_connections(20),
)
.await?;

Migrations

Migrations are optional and use sqlx:

# Cargo.toml
dbkit = { version = "0.3", features = ["migrations"] }
use dbkit::{Database, migrate::Migrator};

static MIGRATOR: Migrator = dbkit::sqlx::migrate!("./migrations");

let db = Database::connect("postgres://...").await?;
db.migrate(&MIGRATOR).await?;

dbkit keeps migration execution thin and delegates migration file parsing/running to sqlx.

Transactions

let tx = db.begin().await?;
let users = User::query().all(&tx).await?;
tx.commit().await?;

Transaction-Local Postgres Settings

let tx = db.begin().await?;
tx.set_local("statement_timeout", "5s").await?;

let users = User::query()
    .filter(User::email.like("%@example.com"))
    .all(&tx)
    .await?;

tx.commit().await?;

set_local uses PostgreSQL set_config(..., true), so the setting is scoped to the current transaction instead of leaking across pooled connection reuse.