Expand description

This crate is a handy helper to share a MongoDB client within a process, for example, to share it among asynchronous request handlers.

Examples

// in main.rs
use std::sync::{Arc, Mutex};
use shared_mongodb::{ClientHolder, database};
use actix_web::{web, App, HttpServer};

let client_holder = Arc::new(Mutex::new(ClientHolder::new("mongodb+srv://...")));
HttpServer::new(move || {
    let app = App::new().app_data(client_holder.clone());
    return app;
})

// in handler.rs
pub async fn search(
    form: web::Query<FormData>,
    data: web::Data<Mutex<ClientHolder>>) -> Result<HttpResponse> {
    let db = database::get(&data).await;
    ...
}

Modules

Structs

Holds a MongoDB Client.