Crate mongo_drop

Source
Expand description

§MongoDrop

A Rust library that provides an experimental AsyncDrop implementation for MongoDB change streams.

This library allows you to collect changes made to a MongoDB database and automatically undo them when the MongoDrop instance is dropped. It uses the async_drop feature to ensure that the undo operations are performed asynchronously.

§Features

  • tracing: Enables tracing for logging events.

§Usage

#[tokio::test]
async fn test_mongo_drop() {
    // Initialize MongoDB
    let client = Client::with_uri_str("mongodb://localhost:27017").await.unwrap();
    let database = client.database("test_db");
    // Create a MongoDrop guard
    let mongo_drop = MongoDrop::new(&database).await.unwrap();

    // Perform database operations within the guard
    let coll = database.collection("test_collection");
    coll.insert_one(doc! { "key": "value" }).await.unwrap();
    let record = coll.find_one(doc! {}).await.unwrap();

    assert_eq!(record, Some(doc! { "key": "value" }));
    // The changes will be rolled back automatically when the guard goes out of scope
}

Structs§

MongoDrop
A data type that collects database changes and automatically undoes them asynchronously when dropped within an async context using AsyncDrop. Requires nightly Rust and the async_drop feature.