-
Notifications
You must be signed in to change notification settings - Fork 131
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
Diesel + Postgres + Rocket example #172
Comments
Sorry for the delay. I just came back from my vacation? Did you figure it out? |
@bikeshedder, thank you for response. I think it's related more for Rocket which uses own But i will glad if you can give me an example of |
You should be using the aliases provided by the It's not immediately obvious from the docs, but it actually uses Therefore you can't use it directly, but you need to call the let conn = pool.get().await.expect("Database connection error");
let result = conn.interact(|conn| {
users
.filter(login.eq("ynhhoJ"))
.select(id)
.first::<i32>(conn)
.expect("Error gettings user ID");
}).expect("Interaction with connection failed"); If you really don't want to use the For use within pub async fn create_pool() -> deadpool_diesel::postgres::Pool {
dotenv().ok();
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
let manager = Manager::new(database_url, Runtime::Tokio1);
let pool = Pool::builder(manager).max_size(8).build().unwrap();
pool
}
rocket::build().manage(create_pool()); And then later inside the routes simply retrieve it: #[get("/db-test")]
async fn count(pool: &State<Pool>) -> String {
let conn = pool.get().await.unwrap();
let result = conn.interact(|conn| {
todo!("Add your DB query here");
}).expect("DB interaction failed");
todo!("Implement the rest of this route");
} btw. I highly recommend creating your own error types and implementing the proper |
Thank you for your example and detailed answer!
Cargo.toml[dependencies]
argon2 = "0.3.1"
chrono = "0.4"
deadpool-diesel = { version = "0.3.1", features = ["postgres"] }
diesel = { version = "1.4.8", features = ["postgres", "chrono"] }
dotenv = "0.15.0"
jsonwebtoken = { version = "8.0.0-beta.6" }
lazy_static = "1.4.0"
rand_core = { version = "0.6.3", features = ["std"] }
rocket_contrib = "0.4.10"
rocket_cors = { git = "https://github.com/lawliet89/rocket_cors", branch = "master" }
[dependencies.rocket]
version = "0.5.0-rc.1"
features = ["json"]
[dependencies.rocket_sync_db_pools]
version = "0.1.0-rc.1"
features = ["diesel_postgres_pool"] main.rs#[macro_use]
extern crate rocket;
use rocket::{State, Error};
use deadpool_diesel::Runtime;
use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl};
use to_do::models::User;
use to_do::schema;
use deadpool_diesel::{
postgres::{Manager, Pool},
};
use dotenv::dotenv;
use std::env;
pub async fn create_pool() -> Pool {
dotenv().ok();
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
let manager = Manager::new(database_url, Runtime::Tokio1);
Pool::builder(manager).max_size(8).build().unwrap()
}
#[get("/db-test")]
async fn db_test(pool: &State<Pool>) -> String { // 25
use schema::users::dsl::*;
let conn = pool.get().await.unwrap();
let result = conn
.interact(|conn| {
users.filter(login.eq("ynhhoJ")).first::<User>(conn)
})
.await
.expect("DB interaction failed");
match result {
Ok(user) => {
format!("{}", user.login)
}
Err(e) => format!("error: {:?}", e),
}
}
#[rocket::main]
async fn main() -> Result<(), Error> {
rocket::build()
.manage(create_pool())
.mount(
"/",
routes![
db_test
],
)
.mount("/", rocket_cors::catch_all_options_routes())
.launch()
.await
} |
That's weird indeed. Maybe the rocket macros don't play well with the generics? You could try the Newtype pattern: https://rust-unofficial.github.io/patterns/patterns/behavioural/newtype.html That way you could also add some helper methods. e.g. an |
Where i can find an example where i can see how i can use pools for diesel as in code below?
Now i have this:
Tried this:
But it's doesn't work
The text was updated successfully, but these errors were encountered: