Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement FromRef for State into sqlx pool #42

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@
//! It's not currently possible to use `Tx` for a dynamic number of databases. Feel free to open an
//! issue if you have a requirement for this.
//!
//! ## Accessing the pool
//!
//! Note that [`State`] implements [`FromRef`](axum_core::extract::FromRef) into the inner SQLx pool. Therefore,
//! if you still need to access the database pool at some handler, you can use axum's `State`
//! extractor normally.
//!
//! ```
//! use axum::extract::State;
//!
//! async fn this_still_works(State(pool): State<sqlx::SqlitePool>) {
//! /* ... */
//! }
//! ```
//!
//! # Examples
//!
//! See [`examples/`][examples] in the repo for more examples.
Expand Down
8 changes: 8 additions & 0 deletions src/state.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use axum_core::extract::FromRef;

use crate::Marker;

/// Application state that enables the [`Tx`] extractor.
Expand Down Expand Up @@ -34,3 +36,9 @@ impl<DB: Marker> Clone for State<DB> {
}
}
}

impl<DB: Marker> FromRef<State<DB>> for sqlx::Pool<DB::Driver> {
fn from_ref(input: &State<DB>) -> Self {
input.pool.clone()
}
}
29 changes: 29 additions & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,35 @@
assert!(response.status().is_success());
}

#[tokio::test]
async fn extract_pool_from_state() {
let pool = sqlx::SqlitePool::connect("sqlite::memory:").await.unwrap();

let (state, layer) = Tx::setup(pool);

let app = axum::Router::new()
.route(
"/",
axum::routing::get(
|axum::extract::State(_pool): axum::extract::State<sqlx::SqlitePool>| async move {},
),
)
.layer(layer)
.with_state(state);

let response = app
.oneshot(
http::Request::builder()
.uri("/")
.body(axum::body::Body::empty())
.unwrap(),
)
.await
.unwrap();

assert!(response.status().is_success());
}

#[tokio::test]
async fn missing_layer() {
let pool = sqlx::SqlitePool::connect("sqlite::memory:").await.unwrap();
Expand Down Expand Up @@ -457,7 +486,7 @@
(pool, Response { status, body })
}

struct MyExtractorError(axum_sqlx_tx::Error);

Check warning on line 489 in tests/lib.rs

View workflow job for this annotation

GitHub Actions / test

field `0` is never read

impl From<axum_sqlx_tx::Error> for MyExtractorError {
fn from(error: axum_sqlx_tx::Error) -> Self {
Expand All @@ -471,7 +500,7 @@
}
}

struct MyLayerError(sqlx::Error);

Check warning on line 503 in tests/lib.rs

View workflow job for this annotation

GitHub Actions / test

field `0` is never read

impl From<sqlx::Error> for MyLayerError {
fn from(error: sqlx::Error) -> Self {
Expand Down
Loading