12 releases (4 breaking)

0.5.0 Feb 23, 2021
0.4.1 Feb 22, 2021
0.4.0 Jan 29, 2021
0.3.1 Dec 4, 2020
0.1.0 Nov 18, 2020

#33 in HTTP server

Download history 98/week @ 2020-11-15 21/week @ 2020-11-22 55/week @ 2020-11-29 65/week @ 2020-12-06 39/week @ 2020-12-13 30/week @ 2020-12-20 10/week @ 2020-12-27 15/week @ 2021-01-03 42/week @ 2021-01-10 22/week @ 2021-01-17 45/week @ 2021-01-24 59/week @ 2021-01-31 52/week @ 2021-02-07 62/week @ 2021-02-14 56/week @ 2021-02-21 26/week @ 2021-02-28

178 downloads per month
Used in preroll

BlueOak-1.0.0

20KB
162 lines

tide-sqlx

Tide middleware for SQLx pooled connections & transactions.


A Tide middleware which holds a pool of SQLx database connections, and automatically hands each tide::Request a connection, which may transparently be either a database transaction, or a direct pooled database connection.

By default, transactions are used for all http methods other than GET and HEAD.

When using this, use the SQLxRequestExt extenstion trait to get the connection.

Examples

Basic

#[async_std::main]
async fn main() -> anyhow::Result<()> {
    use sqlx::Acquire; // Or sqlx::prelude::*;
    use sqlx::postgres::Postgres;

    use tide_sqlx::SQLxMiddleware;
    use tide_sqlx::SQLxRequestExt;

    let mut app = tide::new();
    app.with(SQLxMiddleware::<Postgres>::new("postgres://localhost/a_database").await?);

    app.at("/").post(|req: tide::Request<()>| async move {
        let mut pg_conn = req.sqlx_conn::<Postgres>().await;

        sqlx::query("SELECT * FROM users")
            .fetch_optional(pg_conn.acquire().await?)
            .await;

        Ok("")
    });
    Ok(())
}

From sqlx PoolOptions and with ConnectOptions

#[async_std::main]
async fn main() -> anyhow::Result<()> {
    use log::LevelFilter;
    use sqlx::{Acquire, ConnectOptions}; // Or sqlx::prelude::*;
    use sqlx::postgres::{PgConnectOptions, PgPoolOptions, Postgres};

    use tide_sqlx::SQLxMiddleware;
    use tide_sqlx::SQLxRequestExt;

    let mut connect_opts = PgConnectOptions::new();
    connect_opts.log_statements(LevelFilter::Debug);

    let pg_pool = PgPoolOptions::new()
        .max_connections(5)
        .connect_with(connect_opts)
        .await?;

    let mut app = tide::new();
    app.with(SQLxMiddleware::from(pg_pool));

    app.at("/").post(|req: tide::Request<()>| async move {
        let mut pg_conn = req.sqlx_conn::<Postgres>().await;

        sqlx::query("SELECT * FROM users")
            .fetch_optional(pg_conn.acquire().await?)
            .await;

        Ok("")
    });
    Ok(())
}

Why you may want to use this

Database 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.

License

Licensed under the BlueOak Model License 1.0.0Contributions via DCO 1.1

Dependencies

~13–17MB
~366K SLoC