Struct axum_sqlx_tx::Layer

source ·
pub struct Layer<DB: Database, E = Error> { /* private fields */ }
Expand description

A tower_layer::Layer that enables the Tx extractor.

This layer adds a lazily-initialised transaction to the request extensions. The first time the Tx extractor is used on a request, a connection is acquired from the configured [sqlx::Pool] and a transaction is started on it. The same transaction will be returned for subsequent uses of Tx on the same request. The inner service is then called as normal. Once the inner service responds, the transaction is committed or rolled back depending on the status code of the response.

Implementations§

source§

impl<DB: Database> Layer<DB>

source

pub fn new(pool: Pool<DB>) -> Self

Construct a new layer with the given pool.

A connection will be obtained from the pool the first time a Tx is extracted from a request.

If you want to access the pool outside of a transaction, you should add it also with axum::Extension.

To use a different type than Error to convert commit errors into responses, see new_with_error.

source

pub fn new_with_error<E>(pool: Pool<DB>) -> Layer<DB, E>

Construct a new layer with a specific error type.

See Layer::new for more information.

Trait Implementations§

source§

impl<DB: Database, E> Clone for Layer<DB, E>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<DB: Database, S, E> Layer<S> for Layer<DB, E>

§

type Service = Service<DB, S, E>

The wrapped service
source§

fn layer(&self, inner: S) -> Self::Service

Wrap the given service with the middleware, returning a new service that has been decorated with the middleware.

Auto Trait Implementations§

§

impl<DB, E = Error> !RefUnwindSafe for Layer<DB, E>

§

impl<DB, E> Send for Layer<DB, E>where
E: Send,

§

impl<DB, E> Sync for Layer<DB, E>where
E: Sync,

§

impl<DB, E> Unpin for Layer<DB, E>where
E: Unpin,

§

impl<DB, E = Error> !UnwindSafe for Layer<DB, E>

Blanket Implementations§

source§

impl<T> Any for Twhere
T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere
T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere
T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> FromRef<T> for Twhere
T: Clone,

source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
source§

impl<T, U> Into<U> for Twhere
U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for Twhere
T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere
U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere
U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere
V: MultiLane<T>,

§

fn vzip(self) -> V