#mongo-db #database-client #process #share #handlers #among #handy

shared_mongodb

Handy functions to share a mongodb client within a process

8 releases

0.1.7 Jul 2, 2023
0.1.6 Feb 2, 2023
0.1.5 May 26, 2022

#2 in #among

Download history 10/week @ 2024-07-19 8/week @ 2024-07-26 28/week @ 2024-08-02 7/week @ 2024-08-09 15/week @ 2024-08-16 28/week @ 2024-08-23 56/week @ 2024-08-30 50/week @ 2024-09-06 77/week @ 2024-09-13 69/week @ 2024-09-20 31/week @ 2024-09-27 35/week @ 2024-10-04 81/week @ 2024-10-11 27/week @ 2024-10-18 37/week @ 2024-10-25 33/week @ 2024-11-01

186 downloads per month
Used in 4 crates (via debot-db)

Apache-2.0

7KB
71 lines

shared_mongodb

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


lib.rs:

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

Examples

use actix_web::{web, App, HttpServer};
use std::sync::{Arc, Mutex};
use shared_mongodb::{ClientHolder, database};
use mongodb::options::ClientOptions;

#[actix_rt::main]
async fn main() -> std::io::Result<()> {
    let client_options = ClientOptions::parse("mongodb://root:password@localhost:12345").await;
    let client_holder = web::Data::new(Mutex::new(ClientHolder::new(client_options.unwrap())));
    HttpServer::new(move || {
        let app = App::new().app_data(client_holder.clone());
        return app;
    });

    Ok(())
}

async fn handler(data: web::Data<Mutex<ClientHolder>>) -> std::io::Result<()> {
    let db = database::get(&data, "My_Company");
    database::disconnect(&data);

    let session = start_transaction(&data)?;
    commit_transaction(&mut session);

    Ok(())
}

Dependencies

~30–43MB
~812K SLoC