3

I'm switching from Nodejs with NestJs to Rust Rocket + juniper. Even if I start feeling confident in Rust i'm having difficulties with some simple thing.

I'm trying to create a mongdb pool to attach to my app but I'm having some issue. Here is my main function:

#![feature(proc_macro_hygiene, decl_macro)]

#[macro_use] extern crate rocket;
#[macro_use] extern crate rocket_contrib;

#[rocket_contrib::database("db")]
struct DatabaseConnection(rocket_contrib::databases::mongodb::db::Database);

// mod features;
#[rocket::main]
async fn main() {

    rocket::build()
            .attach(DatabaseConnection::fairing())
        .mount(
            "/",
            rocket::routes![],
        )
        .launch()
        .await
        .expect("server to launch");
}

And here is my cargo.toml file:

[package]
name = "rocket_graphql"
version = "0.1.0"
edition = "2018"



[dependencies]
rocket = "0.5.0-rc.1"
juniper = { git = "https://github.com/graphql-rust/juniper" }
juniper_rocket = "0.2.0"
mongodb = "2.0.0-beta.2"
bson = "1.2.1"
tokio = { version = "1.8.0", features = ["full"] }


[dependencies.rocket_contrib]
version = "0.4.10"
default-features = false
features = ["mongodb_pool"]
[global.databases]
db = {url = "my-mongo-atlas-instance-url"}

I'm getting an error on this line #[rocket_contrib::database("db")] it says: unresolved import 'rocket' no 'Outcome' in the root.

Can you please tell me what I'm doing wrong here?

Thank you very much

user1445685
  • 793
  • 1
  • 11
  • 25

1 Answers1

3

I'm using Rocket 0.5. According to the changelog, the rocket_contrib has been deprecated in favor for rocket_sync_db_pools which doesn't seem to cover mongodb yet. So what I did is to create a state struct to hold a reference to the database. So thanks to the manage method on rocket, the reference to the database can be passed to all the routes. Feel free to correct it if I did anything wrong

I ended up doing this:

#![feature(proc_macro_hygiene, decl_macro)]
use mongodb::{
    Client, Database,
};
use rocket::{self, get, State};

#[get("/")]
async fn index(mongo_db: &State<AppDataPool>) -> &'static str {
    for coll_name in mongo_db.mongo.list_collection_names(None).await {
        println!("collection: {:?}", coll_name);
    }
    "helloooo"
}

struct AppDataPool {
    mongo: Database,
}

#[rocket::main]
async fn main() {
    let client = Client::with_uri_str("----").await;

    let db = client.unwrap().database("my-db");
    for coll_name in db.list_collection_names(None).await {
        println!("collection: {:?}", coll_name);
    }

    rocket::build()
        .manage(AppDataPool { mongo: db })
        .mount("/", rocket::routes![index])
        .launch()
        .await
        .expect("server to launch");
}

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
user1445685
  • 793
  • 1
  • 11
  • 25
  • 1
    what is this ? what did you change ? why it's work now ? – Stargateur Jul 14 '21 at 15:57
  • So, I'm using Rocket 0.5. According to the changelog, the rocket_contrib has been deprecated in favor for rocket_sync_db_pools which doesn't seem to cover mongodb yet. So what I did is to create a state struct to hold a reference to the database. So thanks to the manage method on rocket, the reference to the database can be passed to all the routes. Feel free to correct it if I did anything wrong – user1445685 Jul 14 '21 at 16:46