0

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);
}

Avi4nFLu
  • 152
  • 8

1 Answers1

0

I believe you're looking for with_state.

pub fn create_routes(client: Arc<PrismaClient>) -> Router<> {
    Router::new().route("/", get(hello_world))
    .route("/getgrades", get(handle_grades_get))
    .with_state(client)
}

Haven't tested this, but it's a step in the right direction!

You can see an example with application state here: https://github.com/tokio-rs/axum/blob/main/examples/key-value-store/src/main.rs

SeedyROM
  • 2,203
  • 1
  • 18
  • 22
  • When I change this I get this error from the server: Missing request extension: Extension of type `alloc::sync::Arc` was not found. Perhaps you forgot to add it? See `axum::Extension`. return Json(samples); I tried to remove extension in the handler but I was getting several more errors. I am unsure how to make this work. – Avi4nFLu Mar 31 '23 at 12:56
  • Does the prisma client need to be mutable? You'd need an `Arc>` or `Arc>` and you also need to pattern match on `State` not `Extension` in your route. See [here](https://github.com/tokio-rs/axum/blob/main/examples/key-value-store/src/main.rs#LL94C5-L94C38) – SeedyROM Mar 31 '23 at 12:58