Tide middleware for SQLx pooled connections & transactions.
A middleware which holds a pool of postgres connections, and automatically hands each tide::Request a connection, which may transparently be either a postgres transaction, or a direct pooled connection.
By default, transactions are used for all http methods other than GET and HEAD.
When using this, use the PostgresRequestExt
extenstion trait to get the connection.
```rust
async fn main() -> anyhow::Result<()> { use sqlx::Acquire; // Or sqlx::prelude::*;
use tide_sqlx::PostgresConnectionMiddleware;
use tide_sqlx::PostgresRequestExt;
let mut app = tide::new();
app.with(PostgresConnectionMiddleware::new("postgres://localhost/geolocality", 5).await?);
app.at("/").post(|req: tide::Request<()>| async move {
let mut pg_conn = req.postgres_conn().await;
pg_conn.acquire().await?; // Pass this to e.g. "fetch_optional()" from a sqlx::Query
Ok("")
});
Ok(())
} ```
Postgres transactions are very useful because they allow easy, assured rollback if something goes wrong. However, transactions incur extra runtime cost which is too expensive to justify for READ operations that do not need this behavior.
In order to allow transactions to be used seamlessly in endpoints, this middleware manages a transaction if one is deemed desirable.
Licensed under the BlueOak Model License 1.0.0 — Contributions via DCO 1.1