-
There's an example in the axum GitHub repo of using diesel_async's struct DatabaseConnection(
bb8::PooledConnection<'static, AsyncDieselConnectionManager<AsyncPgConnection>>,
);
impl<S> FromRequestParts<S> for DatabaseConnection
where
S: Send + Sync,
Pool: FromRef<S>,
{
type Rejection = (StatusCode, String);
async fn from_request_parts(_parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
let pool = Pool::from_ref(state);
let conn = pool.get_owned().await.map_err(internal_error)?;
Ok(Self(conn))
}
} I'm trying to modify this example to use:
but I'm not having much luck. I tried variations of: use axum::{
extract::{FromRef, FromRequestParts},
http::{request::Parts, StatusCode},
};
use diesel::sqlite::SqliteConnection;
use diesel_async::pooled_connection::deadpool;
use diesel_async::sync_connection_wrapper::SyncConnectionWrapper;
use crate::utils::internal_error;
type Pool = deadpool::Pool<SyncConnectionWrapper<SqliteConnection>>;
#[derive(Clone)]
pub struct AppState {
pub pool: Pool,
}
impl FromRef<AppState> for Pool {
fn from_ref(app_state: &AppState) -> Pool {
app_state.pool.clone()
}
}
pub struct DatabaseConnection(SyncConnectionWrapper<SqliteConnection>);
impl<S> FromRequestParts<S> for DatabaseConnection
where
S: Send + Sync,
Pool: FromRef<S>,
{
type Rejection = (StatusCode, String);
async fn from_request_parts(_parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
let pool = Pool::from_ref(state);
let conn = pool.get().await.map_err(internal_error)?;
Ok(Self(conn))
}
} I get two errors:
Can this be made to work? What's the right type of value to hold inside The usage in axum of the connection looks like: pub async fn show(Path(id): Path<i32>, DatabaseConnection(conn): DatabaseConnection<???>) -> Result<impl IntoResponse> {
use crate::schema::posts::dsl::posts;
let post = posts
.find(id)
.select(Post::as_select())
.first(conn)
.optional()
.await
.map_err(internal_error)?
.ok_or(StatusCode::NOT_FOUND)?;
Ok(...)
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
So let's break down what's in the example type first:
Now you want to do two things:
That gives you exactly the type that's already mentioned in your error message:
|
Beta Was this translation helpful? Give feedback.
-
Here's a final, working version: use async_trait::async_trait;
use axum::extract::{FromRef, FromRequestParts};
use axum::http::request::Parts;
use deadpool::managed::Object;
use diesel::sqlite::SqliteConnection;
use diesel_async::pooled_connection::deadpool::Pool;
use diesel_async::pooled_connection::deadpool::{BuildError, Hook, HookError};
use diesel_async::pooled_connection::AsyncDieselConnectionManager;
use diesel_async::sync_connection_wrapper::SyncConnectionWrapper;
use diesel_async::SimpleAsyncConnection;
use crate::error_handling::AppError;
pub type PoolState = Pool<SyncConnectionWrapper<SqliteConnection>>;
#[derive(Clone)]
pub struct AppState {
pub pool: PoolState,
}
impl FromRef<AppState> for PoolState {
fn from_ref(app_state: &AppState) -> PoolState {
app_state.pool.clone()
}
}
pub struct DbConnection(
pub Object<AsyncDieselConnectionManager<SyncConnectionWrapper<SqliteConnection>>>,
);
#[async_trait]
impl<S> FromRequestParts<S> for DbConnection
where
PoolState: FromRef<S>,
S: Send + Sync,
{
type Rejection = AppError;
async fn from_request_parts(_parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
let pool = PoolState::from_ref(state);
let conn = pool.get().await?;
Ok(Self(conn))
}
} |
Beta Was this translation helpful? Give feedback.
Here's a final, working version: