shared_mongodb/lib.rs
1//! This crate is a handy helper to share a MongoDB client within a process, for example, to share it among asynchronous request handlers.
2//!
3//! # Examples
4//!
5//! ```
6//! use actix_web::{web, App, HttpServer};
7//! use std::sync::{Arc, Mutex};
8//! use shared_mongodb::{ClientHolder, database};
9//! use mongodb::options::ClientOptions;
10//!
11//! #[actix_rt::main]
12//! async fn main() -> std::io::Result<()> {
13//! let client_options = ClientOptions::parse("mongodb://root:password@localhost:12345").await;
14//! let client_holder = web::Data::new(Mutex::new(ClientHolder::new(client_options.unwrap())));
15//! HttpServer::new(move || {
16//! let app = App::new().app_data(client_holder.clone());
17//! return app;
18//! });
19//!
20//! Ok(())
21//! }
22//!
23//! async fn handler(data: web::Data<Mutex<ClientHolder>>) -> std::io::Result<()> {
24//! let db = database::get(&data, "My_Company");
25//! database::disconnect(&data);
26//!
27//! let session = start_transaction(&data)?;
28//! commit_transaction(&mut session);
29//!
30//! Ok(())
31//! }
32//! ```
33mod client;
34pub mod database;
35pub use client::ClientHolder;