I am creating a rust backend using prisma and axum. I am trying to figure out the best way(or any working way) to pass my connection into my rust routes. I have created a new client in main which I am passing to the routes mod and then passing to each route. I think the problem is that I am not putting my connection string into my client, but I am not sure if this is the problem. I also see in this prisma-client-rust example that they created a db.rs file, but db.rs is ignored so I cannot see how the file is set up. I am getting the code to successfully compile but it is returning an empty vector.
Below is my main where I am creating the connection, however I do not think I am putting the connection string in and I cannot find a way to pass my connection string in. I do have a .env file with the connection string.
#[tokio::main]
async fn main() {
let client = Arc::new(PrismaClient::_builder().build().await.unwrap());
//create_routes is in mod.rs below
let app = create_routes(client);
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
.serve(app.into_make_service())
.await
.unwrap();
}
I am trying to passing client into mod, then finally my route. Here is my mod:
use axum::{Router, routing::get, Extension};
use hello_world::hello_world;
use handle_grades_get::handle_grades_get;
use std::sync::Arc;
use crate::PrismaClient;
pub fn create_routes(client: Arc<PrismaClient>) -> Router<> {
Router::new().route("/", get(hello_world))
.route("/getgrades", get(handle_grades_get))
.layer(Extension(client))
}
And here is my route:
use crate::{prisma::l_2_l_3_chemresult_02, PrismaClient};
use axum::{Extension, Json};
use std::sync::Arc;
pub async fn handle_grades_get(
Extension(client): Extension<Arc<PrismaClient>>,
) -> Json<Vec<l_2_l_3_chemresult_02::Data>> {
let samples = client
.l_2_l_3_chemresult_02()
.find_many(vec![l_2_l_3_chemresult_02::heatid::equals(Some(
"80".to_string(),
))])
.exec()
.await
.unwrap();
return Json(samples);
}