1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
use mongodb::{options::ClientOptions, Client, Collection as MongoCollection};
use std::time::Duration;
pub mod queries;

pub struct Mungos {
    pub client: Client,
}

pub struct Collection<T> {
    pub collection: MongoCollection<T>,
}

impl Mungos {
    pub async fn new(uri: &str, app_name: &str, timeout: Duration) -> Mungos {
        // Parse your connection string into an options struct
        let mut client_options = ClientOptions::parse(uri).await.unwrap();
        // Manually set an option
        client_options.app_name = Some(app_name.to_string());
        client_options.connect_timeout = Some(timeout);
        // Get a handle to the cluster
        let client = Client::with_options(client_options).unwrap();
        println!();
        println!("============================");
        println!("==== connected to mongo ====");
        println!("============================");
        println!();
        Mungos { client }
    }

    pub fn collection<T>(&self, db_name: &str, collection_name: &str) -> Collection<T> {
        Collection {
            collection: self.client.database(db_name).collection(collection_name),
        }
    }
}