[][src]Crate tide_sqlx

A Tide 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.

Example

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("")
});

Why you may want to use this

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.

Structs

PostgresConnectionMiddleware

This middleware 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.

Traits

PostgresRequestExt

An extension trait for tide::Request which does proper unwrapping of the connection from req.ext().