sqlx

SQLx

Async SQL toolkit with compile-time checking

Details

Language / Topic
rustRust
Category
Libraries

Rules

balanced
- Use `sqlx::query!()` and `sqlx::query_as!()` macros for compile-time SQL validation — they check queries against your actual database schema.
- Use `PgPool`/`SqlitePool` for connection pooling — create once at startup with `Pool::connect()` and share via `Arc` or framework state.
- Use `sqlx::migrate!()` macro to embed migrations in the binary — run `sqlx migrate add <name>` to create new migration files.
- Use `PgPool` (or `SqlitePool`, `MySqlPool`) for connection pooling. Pass as application state.
- Use `sqlx::query_as!()` to map rows directly to Rust structs.
- Use `sqlx::FromRow` derive for automatic row-to-struct mapping.
- Use transactions: `let mut tx = pool.begin().await?; ... tx.commit().await?`.