| Crates.io | rs-mongo-stream |
| lib.rs | rs-mongo-stream |
| version | 0.3.1 |
| created_at | 2025-03-06 23:33:00.648617+00 |
| updated_at | 2025-03-08 11:07:23.883946+00 |
| description | Wrapper on mongo to easier the way to handle mongo stream |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1582019 |
| size | 87,178 |
A MongoDB change stream library for Rust applications
rs-mongo-stream is a Rust library that makes it easy to work with MongoDB change streams. It provides a simple interface to monitor and react to changes in your MongoDB collections by registering callbacks for different database events (insertions, updates, and deletions).
Add the crate to your Cargo.toml:
[dependencies]
rs-mongo-stream = "0.3.1"
mongodb = "2.4.0"
tokio = { version = "1", features = ["full"] }
use mongodb::{Client, options::ClientOptions};
use rs_mongo_stream::{MongoStream, Event};
use tokio;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Connect to MongoDB
let client_options = ClientOptions::parse("mongodb://localhost:27017").await?;
let client = Client::with_options(client_options)?;
let db = client.database("my_database");
// Create the stream monitor
let mut stream = MongoStream::new(client.clone(), db);
// Register callbacks for a collection
stream.add_callback("users", Event::Insert, |doc| {
Box::pin(async move {
println!("New user inserted: {:?}", doc);
// Handle the insertion...
})
});
stream.add_callback("users", Event::Update, |doc| {
Box::pin(async move {
println!("User updated: {:?}", doc);
// Handle the update...
})
});
stream.add_callback("users", Event::Delete, |doc| {
Box::pin(async move {
println!("User deleted: {:?}", doc);
// Handle the deletion...
})
});
// Start monitoring the collection
stream.start_stream("users").await?;
Ok(())
}
The library provides a custom MongoStreamError type that wraps errors from the MongoDB driver and adds context when needed.
This project is licensed under the MIT License - see the LICENSE file for details.