rs_mongo_stream/
lib.rs

1/// # MongoDB Change Stream Wrapper
2///
3/// This crate provides a convenient wrapper around MongoDB change streams,
4/// allowing for easy subscription to database events with customizable callbacks.
5///
6/// ## Features
7///
8/// - Easy subscription to MongoDB change events (inserts, updates, deletes)
9/// - Support for custom callbacks per event type
10/// - Automatic reconnection on errors
11/// - Proper resource management with explicit stream closing
12///
13/// ## Example
14///
15/// ```rust,ignore
16/// use mongodb::{Client, options::ClientOptions};
17/// use rs_mongo_stream::{MongoStream, Event};
18///
19/// async fn example() -> Result<(), Box<dyn std::error::Error>> {
20///     // Connect to MongoDB
21///     let client_options = ClientOptions::parse("mongodb://localhost:27017").await?;
22///     let client = Client::with_options(client_options)?;
23///     let db = client.database("test_db");
24///     
25///     // Create MongoStream instance
26///     let mut mongo_stream = MongoStream::new(db);
27///     
28///     // Add custom callback for insert events
29///     mongo_stream.add_callback("users", Event::Insert, |doc| {
30///         Box::pin(async move {
31///             println!("Insert event received: {:?}", doc);
32///             // Additional processing logic here
33///         })
34///     });
35///     
36///     // Start monitoring the collection in the background
37///     mongo_stream.start_stream("users").await?;
38///     
39///     // ... Application logic ...
40///     
41///     // When done with a specific stream
42///     mongo_stream.close_stream("users").await;
43///     
44///     // Or close all streams when shutting down
45///     mongo_stream.close_all_streams().await;
46///     
47///     Ok(())
48/// }
49/// ```
50mod error;
51mod event;
52mod stream;
53
54pub use error::MongoStreamError;
55pub use event::Event;
56pub use stream::{Callbacks, MongoStream};