rocket_db_pools: get DB connection to pass to other functions

144 views Asked by At

I am running a Rocket server and using rocket_db_pools for postgres connectivity. I would like to execute multiple queries in the same database within the context of an API call - like so.

#[get("/fetch_data_by_ids?<data_ids>")]
async fn fetch_data_by_ids(data_ids: Vec<&str>, mut db: Connection<DatabaseConnection>) -> Json<Vec<ProcessedDocument>> {
    let connection_one = db.clone();
    let connection_two = db.clone();
    let data_obj_one = do_a_database_process_one(connection_one).await;
    let data_obj_two = do_a_database_process_two(connection_two).await;
}

Is it possible to request multiple connections within one route handler?

1

There are 1 answers

0
Thomas Murphy On BEST ANSWER

It's possible to get multiple connections to the same DB by

  1. Creating multiple unit structs with configs pointing at the same DB
  2. Make multiple connection string entries in Rocket.toml corresponding to step 1
  3. Pass those multiple connections as args to Rocket annotated methods and attach() multiple connections at Rocket launch.

Kind of hack-y but the only solution I have currently.