All Projects → eaze → tide-sqlx

eaze / tide-sqlx

Licence: other
Tide middleware for SQLx pooled connections & transactions

Programming Languages

rust
11053 projects
shell
77523 projects

Projects that are alternatives of or similar to tide-sqlx

tide-basic-crud
Basic CRUD api using Rust / Tide / Sqlx / Postgresql
Stars: ✭ 27 (-32.5%)
Mutual labels:  tide, sqlx
surfer
The Blog built on pure Rust stack. Backend for graphql services, and frontend for web application.
Stars: ✭ 35 (-12.5%)
Mutual labels:  tide
wptide
🌊 Tide is a series of automated tests run against every WordPress.org theme and plugin
Stars: ✭ 77 (+92.5%)
Mutual labels:  tide
todo-app
🔥 REST API для приложения списков ToDo
Stars: ✭ 78 (+95%)
Mutual labels:  sqlx
golangRestfulAPISample
Sample Restful app with chi router (golang)
Stars: ✭ 116 (+190%)
Mutual labels:  sqlx
Go-gRPC-RabbitMQ-microservice
Go gRPC RabbitMQ email microservice
Stars: ✭ 107 (+167.5%)
Mutual labels:  sqlx
sqlx-adapter
Asynchronous casbin adapter for mysql, postgres, sqlite based on sqlx-rs
Stars: ✭ 27 (-32.5%)
Mutual labels:  sqlx
sea-query
🔱 A dynamic SQL query builder for MySQL, Postgres and SQLite
Stars: ✭ 595 (+1387.5%)
Mutual labels:  sqlx
actix sqlx mysql user crud
A user crud written in Rust, designed to connect to a MySQL database with full integration test coverage.
Stars: ✭ 78 (+95%)
Mutual labels:  sqlx
Go-Clean-Architecture-REST-API
Golang Clean Architecture REST API example
Stars: ✭ 376 (+840%)
Mutual labels:  sqlx

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].