Closed moh-abk closed 5 years ago
Hey @moh-abk. Thanks for submitting this issue. So, the wither::Model
methods all expect the standard mongo driver's DB connection type which comes from the client type.
I looked through the docs in r2d2-mongodb and the associated crates, and I can not seem to find a type called connection::DbConn
(referenced in the error message you posted). Where does that come from? Once I can see the docs on that type, I can help with a path forward on better integration.
connection code below @thedodd
use r2d2;
use r2d2::ManageConnection;
use r2d2_mongodb::{ConnectionOptions, MongodbConnectionManager};
use rocket::http::Status;
use rocket::request::{self, FromRequest};
use rocket::{Outcome, Request, State};
use std::env;
use std::ops::Deref;
type Pool = r2d2::Pool<MongodbConnectionManager>;
pub fn init_pool() -> Pool {
let mongo_addr = env::var("MONGO_ADDR").expect("MONGO_ADDR must be set");
let mongo_port = env::var("MONGO_PORT").expect("MONGO_PORT must be set");
let db_name = env::var("MONGO_DB_NAME").expect("MONGO_DB_NAME env var must be set");
let db_username = env::var("MONGO_DB_USER").expect("MONGO_DB_USER env var must be set");
let db_password = env::var("MONGO_DB_PASSWORD").expect("MONGO_DB_PASSWORD env var must be set");
let manager = MongodbConnectionManager::new(
ConnectionOptions::builder()
.with_host(&mongo_addr, mongo_port.parse::<u16>().unwrap())
.with_db(&db_name)
.with_auth(&db_username, &db_password)
.build(),
);
match Pool::builder().max_size(64).build(manager) {
Ok(pool) => pool,
Err(e) => panic!("Error: failed to create mongodb pool {}", e),
}
}
pub struct DbConn(pub r2d2::PooledConnection<MongodbConnectionManager>);
impl<'a, 'r> FromRequest<'a, 'r> for DbConn {
type Error = ();
fn from_request(request: &'a Request<'r>) -> request::Outcome<DbConn, Self::Error> {
let pool = request.guard::<State<Pool>>()?;
match pool.get() {
Ok(conn) => Outcome::Success(DbConn(conn)),
Err(_) => Outcome::Failure((Status::ServiceUnavailable, ())),
}
}
}
impl Deref for DbConn {
type Target = <r2d2_mongodb::MongodbConnectionManager as ManageConnection>::Connection;
fn deref(&self) -> &Self::Target {
&self.0
}
}
@moh-abk ah, I see that. So, based on the docs here, it looks like the <r2d2_mongodb::MongodbConnectionManager as ManageConnection>::Connection
is the mongodb::Database type, which is what we need. So here is a way to bypass your issue:
self.0
, it should clone the inner value and return it.mongodb::db::Database
type, which is an Arc
wrapping another type from the mongodb crate. It is that Arc
type that the wither::Model
methods are expecting. Not a reference to an Arc
.Let me know if that works for you, then we can close this issue out.
Thanks @thedodd
this worked - RestaurantCity::find((*connection).to_owned(), None, None)
Just started to use the crate and can't get it to work as I'm using r2d2-mongodb to create a pooled connection. This works with the pure rust driver.
this works
this doesn't work
error