pub trait PostgresRequestExt {
// Required method
fn pg_conn<'req, 'async_trait>(
&'req self,
) -> Pin<Box<dyn Future<Output = RwLockWriteGuard<'req, ConnectionWrapInner<Postgres>>> + Send + 'async_trait>>
where Self: 'async_trait,
'req: 'async_trait;
}
Available on crate feature
postgres
only.Expand description
An extension trait for tide::Request which does proper unwrapping of the connection from req.ext()
.
Specialized for Postgres connections.
Required Methods§
Sourcefn pg_conn<'req, 'async_trait>(
&'req self,
) -> Pin<Box<dyn Future<Output = RwLockWriteGuard<'req, ConnectionWrapInner<Postgres>>> + Send + 'async_trait>>where
Self: 'async_trait,
'req: 'async_trait,
fn pg_conn<'req, 'async_trait>(
&'req self,
) -> Pin<Box<dyn Future<Output = RwLockWriteGuard<'req, ConnectionWrapInner<Postgres>>> + Send + 'async_trait>>where
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::postgres::PostgresRequestExt;
app.at("/").post(|req: tide::Request<()>| async move {
let mut pg_conn = req.pg_conn().await;
sqlx::query("SELECT * FROM users")
.fetch_optional(pg_conn.acquire().await?)
.await;
Ok("")
});