weiznich / diesel_async

Diesel async connection implementation
Apache License 2.0
659 stars 82 forks source link

Unable to inject async DB connection via web::Dta<Pool<<>>>> #175

Closed ag039st closed 4 months ago

ag039st commented 4 months ago

Setup

use actix_web::web::Data; use actix_web::{web, HttpResponse, Responder}; use diesel_async::pooled_connection::deadpool::Pool; use diesel_async::pooled_connection::AsyncDieselConnectionManager; use diesel_async::AsyncPgConnection;

use crate::models::models::UserDto;

pub async fn create_user( user: web::Json, pool: Data<Pool<AsyncDieselConnectionManager>>, ) -> impl Responder { // let mut connection: &mut Pool<AsyncDieselConnectionManager>; // con.clone_into(connection); let mut conn = pool.get().await.unwrap(); // create_user_and_profile(, &user.user, &user.profile).await HttpResponse::Ok().body("Ok") }

use crate::controllers::user_controller::create_user; use actix_web::dev::Server; use actix_web::{middleware, web, App, HttpServer}; use diesel_async::pooled_connection::deadpool::Pool; use diesel_async::pooled_connection::AsyncDieselConnectionManager; use log::{error, info}; use std::env; use std::env::var; use std::error::Error; use std::net::TcpListener;

/// Starts a web server on the specified listener. /// /// # Arguments /// /// listener - A TcpListener instance used to listen for incoming connections. /// /// # Returns /// /// Result<Server, Box<dyn Error>> - A Result indicating success or failure. If successful, returns a Server instance representing the running web server. pub async fn start(listener: TcpListener) -> Result<Server, Box> { let port = listener.local_addr().unwrap().port(); let database_url = var("DATABASE_URL").unwrap_or_else(|e| panic!("an error occurred {e}")); let config = AsyncDieselConnectionManager::::new(database_url); let pool = Pool::builder(config).build()?;

// let db_pool = web::Data::new(pool);
info!(">>> Starting web server on port: {} >>>", port);
let server = HttpServer::new(|| {
    App::new()
        .route("/api/user", web::post().to(create_user))
        .app_data(pool.clone())
        .wrap(middleware::Logger::default())
})
.listen(listener)?
.run();
Ok(server)

}

Versions

diesel-async = {version = "0.5.0", features = ["postgres","deadpool"]} diesel = {version = "2.2.2", features = ["chrono", "postgres"]}

Feature Flags

Problem Description

error[E0277]: the trait bound AsyncDieselConnectionManager<AsyncPgConnection>: DerefMut is not satisfied --> src/controllers/user_controller.rs:11:16 11 pool: Data<Pool<AsyncDieselConnectionManager>>, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait DerefMut is not implemented for AsyncDieselConnectionManager<AsyncPgConnection>, which is required by AsyncDieselConnectionManager<AsyncDieselConnectionManager<AsyncPgConnection>>: deadpool::managed::Manager
= help: the trait `deadpool::managed::Manager` is implemented for `AsyncDieselConnectionManager<C>`
= note: required for `AsyncDieselConnectionManager<AsyncPgConnection>` to implement `AsyncConnection`

What are you trying to accomplish?

trying to inject Pool to app_Data() actix web

What is the expected output?

I expect the build to succeed and get connections from the pool

What is the actual output?

error[E0277]: the trait bound AsyncDieselConnectionManager<AsyncPgConnection>: DerefMut is not satisfied --> src/controllers/user_controller.rs:11:16 11 pool: Data<Pool<AsyncDieselConnectionManager>>, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait DerefMut is not implemented for AsyncDieselConnectionManager<AsyncPgConnection>, which is required by AsyncDieselConnectionManager<AsyncDieselConnectionManager<AsyncPgConnection>>: deadpool::managed::Manager
= help: the trait `deadpool::managed::Manager` is implemented for `AsyncDieselConnectionManager<C>`
= note: required for `AsyncDieselConnectionManager<AsyncPgConnection>` to implement `AsyncConnection`

Are you seeing any additional errors?

Steps to reproduce

use actix_web::web::Data; use actix_web::{web, HttpResponse, Responder}; use diesel_async::pooled_connection::deadpool::Pool; use diesel_async::pooled_connection::AsyncDieselConnectionManager; use diesel_async::AsyncPgConnection;

use crate::models::models::UserDto;

pub async fn create_user( user: web::Json, pool: Data<Pool<AsyncDieselConnectionManager>>, ) -> impl Responder { // let mut connection: &mut Pool<AsyncDieselConnectionManager>; // con.clone_into(connection); let mut conn = pool.get().await.unwrap(); // create_user_and_profile(, &user.user, &user.profile).await HttpResponse::Ok().body("Ok") }

Checklist