pub struct Layer<E> { /* private fields */ }Expand description
A tower_layer::Layer that adds request-scoped PostgreSQL transaction
management to an axum router.
Insert this layer into your router to enable the Tx
extractor. Each request will have its own transaction, which is committed
automatically when the response is sent.
§Examples
With bb8-postgres:
use axum::Router;
use bb8_postgres::{PostgresConnectionManager, tokio_postgres::NoTls};
async fn example() {
let manager = PostgresConnectionManager::new("host=localhost user=postgres", NoTls);
let pool = bb8_postgres::bb8::Pool::builder().build(manager).await.unwrap();
let router = Router::new().layer(axum_postgres_tx::layer::Layer::from(pool));
}With deadpool-postgres:
use axum::Router;
use deadpool_postgres::{Config, Runtime};
use tokio_postgres::NoTls;
async fn example() {
let mut cfg = Config::new();
cfg.dbname = Some("postgres".to_string());
let pool = cfg.create_pool(Some(Runtime::Tokio1), NoTls).unwrap();
let router = Router::new().layer(axum_postgres_tx::layer::Layer::from(pool));
}Trait Implementations§
Auto Trait Implementations§
impl<E> !RefUnwindSafe for Layer<E>
impl<E> !UnwindSafe for Layer<E>
impl<E> Freeze for Layer<E>
impl<E> Send for Layer<E>where
E: Send,
impl<E> Sync for Layer<E>where
E: Sync,
impl<E> Unpin for Layer<E>where
E: Unpin,
impl<E> UnsafeUnpin for Layer<E>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more