Trait SQLxRequestExt

Source
pub trait SQLxRequestExt {
    // Required method
    fn sqlx_conn<'req, 'async_trait, DB>(
        &'req self,
    ) -> Pin<Box<dyn Future<Output = RwLockWriteGuard<'req, ConnectionWrapInner<DB>>> + Send + 'async_trait>>
       where DB: Database + 'async_trait,
             DB::Connection: Send + Sync + 'static,
             Self: 'async_trait,
             'req: 'async_trait;
}
Expand description

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

Required Methods§

Source

fn sqlx_conn<'req, 'async_trait, DB>( &'req self, ) -> Pin<Box<dyn Future<Output = RwLockWriteGuard<'req, ConnectionWrapInner<DB>>> + Send + 'async_trait>>
where DB: Database + 'async_trait, DB::Connection: Send + Sync + 'static, Self: 'async_trait, 'req: 'async_trait,

Get the SQLx connection for the current Request.

This will return a “write” guard from a read-write lock. Under the hood this will transparently be either a postgres transaction or a direct pooled connection.

This will panic with an expect message if the SQLxMiddleware has not been run.

§Example
use sqlx::Acquire; // Or sqlx::prelude::*;

use tide_sqlx::SQLxRequestExt;

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

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<T: Send + Sync + 'static> SQLxRequestExt for Request<T>

Source§

fn sqlx_conn<'req, 'async_trait, DB>( &'req self, ) -> Pin<Box<dyn Future<Output = RwLockWriteGuard<'req, ConnectionWrapInner<DB>>> + Send + 'async_trait>>
where DB: Database + 'async_trait, DB::Connection: Send + Sync + 'static, Self: 'async_trait, 'req: 'async_trait,

Implementors§