pub struct Tx<DB: Marker, E = Error> { /* private fields */ }Expand description
An axum extractor for a database transaction.
&mut Tx implements sqlx::Executor so it can be used directly with sqlx::query()
(and sqlx::query_as(), the corresponding macros, etc.):
use axum_sqlx_tx::Tx;
use sqlx::Sqlite;
async fn handler(mut tx: Tx<Sqlite>) -> Result<(), sqlx::Error> {
sqlx::query("...").execute(&mut tx).await?;
/* ... */
}It also implements Deref<Target = sqlx::Transaction> and DerefMut, so you can call
methods from Transaction and its traits:
use axum_sqlx_tx::Tx;
use sqlx::{Acquire as _, Sqlite};
async fn handler(mut tx: Tx<Sqlite>) -> Result<(), sqlx::Error> {
let inner = tx.begin().await?;
/* ... */
}The E generic parameter controls the error type returned when the extractor fails. This can be
used to configure the error response returned when the extractor fails:
use axum::response::IntoResponse;
use axum_sqlx_tx::Tx;
use sqlx::Sqlite;
struct MyError(axum_sqlx_tx::Error);
// The error type must implement From<axum_sqlx_tx::Error>
impl From<axum_sqlx_tx::Error> for MyError {
fn from(error: axum_sqlx_tx::Error) -> Self {
Self(error)
}
}
// The error type must implement IntoResponse
impl IntoResponse for MyError {
fn into_response(self) -> axum::response::Response {
(http::StatusCode::INTERNAL_SERVER_ERROR, "internal server error").into_response()
}
}
async fn handler(tx: Tx<Sqlite, MyError>) {
/* ... */
}Implementations§
Source§impl<DB: Marker, E> Tx<DB, E>
impl<DB: Marker, E> Tx<DB, E>
Sourcepub fn config(pool: Pool<DB::Driver>) -> Config<DB, Error>
pub fn config(pool: Pool<DB::Driver>) -> Config<DB, Error>
Configure extractor behaviour.
See the Config API for available options.
This is convenient to use from a type alias, e.g.
type Tx = axum_sqlx_tx::Tx<sqlx::Sqlite>;
let config = Tx::config(pool);Sourcepub async fn commit(self) -> Result<(), Error>
pub async fn commit(self) -> Result<(), Error>
Explicitly commit the transaction.
By default, the transaction will be committed when a successful response is returned
(specifically, when the Service middleware intercepts an HTTP 2XX or
3XX response). This method allows the transaction to be committed explicitly.
Note: trying to use the Tx extractor again after calling commit will currently
generate Error::OverlappingExtractors errors. This may change in future.
Trait Implementations§
Source§impl<DB: Marker, E> AsMut<Transaction<'static, <DB as Marker>::Driver>> for Tx<DB, E>
impl<DB: Marker, E> AsMut<Transaction<'static, <DB as Marker>::Driver>> for Tx<DB, E>
Source§fn as_mut(&mut self) -> &mut Transaction<'static, DB::Driver>
fn as_mut(&mut self) -> &mut Transaction<'static, DB::Driver>
Source§impl<DB: Marker, E> AsRef<Transaction<'static, <DB as Marker>::Driver>> for Tx<DB, E>
impl<DB: Marker, E> AsRef<Transaction<'static, <DB as Marker>::Driver>> for Tx<DB, E>
Source§fn as_ref(&self) -> &Transaction<'static, DB::Driver>
fn as_ref(&self) -> &Transaction<'static, DB::Driver>
Source§impl<'c, DB, E> Executor<'c> for &'c mut Tx<DB, E>
impl<'c, DB, E> Executor<'c> for &'c mut Tx<DB, E>
type Database = <DB as Marker>::Driver
Source§fn fetch_many<'e, 'q: 'e, Q>(
self,
query: Q,
) -> BoxStream<'e, Result<Either<<Self::Database as Database>::QueryResult, <Self::Database as Database>::Row>, Error>>
fn fetch_many<'e, 'q: 'e, Q>( self, query: Q, ) -> BoxStream<'e, Result<Either<<Self::Database as Database>::QueryResult, <Self::Database as Database>::Row>, Error>>
Source§fn fetch_optional<'e, 'q: 'e, Q>(
self,
query: Q,
) -> BoxFuture<'e, Result<Option<<Self::Database as Database>::Row>, Error>>
fn fetch_optional<'e, 'q: 'e, Q>( self, query: Q, ) -> BoxFuture<'e, Result<Option<<Self::Database as Database>::Row>, Error>>
Source§fn prepare_with<'e, 'q: 'e>(
self,
sql: &'q str,
parameters: &'e [<Self::Database as Database>::TypeInfo],
) -> BoxFuture<'e, Result<<Self::Database as Database>::Statement<'q>, Error>>where
'c: 'e,
fn prepare_with<'e, 'q: 'e>(
self,
sql: &'q str,
parameters: &'e [<Self::Database as Database>::TypeInfo],
) -> BoxFuture<'e, Result<<Self::Database as Database>::Statement<'q>, Error>>where
'c: 'e,
Source§fn execute<'e, 'q, E>(
self,
query: E,
) -> Pin<Box<dyn Future<Output = Result<<Self::Database as Database>::QueryResult, Error>> + Send + 'e>>
fn execute<'e, 'q, E>( self, query: E, ) -> Pin<Box<dyn Future<Output = Result<<Self::Database as Database>::QueryResult, Error>> + Send + 'e>>
Source§fn execute_many<'e, 'q, E>(
self,
query: E,
) -> Pin<Box<dyn Stream<Item = Result<<Self::Database as Database>::QueryResult, Error>> + Send + 'e>>
fn execute_many<'e, 'q, E>( self, query: E, ) -> Pin<Box<dyn Stream<Item = Result<<Self::Database as Database>::QueryResult, Error>> + Send + 'e>>
Source§fn fetch<'e, 'q, E>(
self,
query: E,
) -> Pin<Box<dyn Stream<Item = Result<<Self::Database as Database>::Row, Error>> + Send + 'e>>
fn fetch<'e, 'q, E>( self, query: E, ) -> Pin<Box<dyn Stream<Item = Result<<Self::Database as Database>::Row, Error>> + Send + 'e>>
Source§fn fetch_all<'e, 'q, E>(
self,
query: E,
) -> Pin<Box<dyn Future<Output = Result<Vec<<Self::Database as Database>::Row>, Error>> + Send + 'e>>
fn fetch_all<'e, 'q, E>( self, query: E, ) -> Pin<Box<dyn Future<Output = Result<Vec<<Self::Database as Database>::Row>, Error>> + Send + 'e>>
Vec.Source§impl<DB: Marker, S, E> FromRequestParts<S> for Tx<DB, E>
impl<DB: Marker, S, E> FromRequestParts<S> for Tx<DB, E>
Auto Trait Implementations§
impl<DB, E> Freeze for Tx<DB, E>
impl<DB, E = Error> !RefUnwindSafe for Tx<DB, E>
impl<DB, E> Send for Tx<DB, E>where
E: Send,
impl<DB, E> Sync for Tx<DB, E>
impl<DB, E> Unpin for Tx<DB, E>where
E: Unpin,
impl<DB, E = Error> !UnwindSafe for Tx<DB, 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
Source§impl<S, T> FromRequest<S, ViaParts> for T
impl<S, T> FromRequest<S, ViaParts> for T
Source§type Rejection = <T as FromRequestParts<S>>::Rejection
type Rejection = <T as FromRequestParts<S>>::Rejection
Source§fn from_request(
req: Request<Body>,
state: &S,
) -> impl Future<Output = Result<T, <T as FromRequest<S, ViaParts>>::Rejection>>
fn from_request( req: Request<Body>, state: &S, ) -> impl Future<Output = Result<T, <T as FromRequest<S, ViaParts>>::Rejection>>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more