db-library 0.1.2

A Rust library for listening to database changes and notifying connected backend services.
Documentation
use std::env;

use db_library::{
    config::DBListenerError, database::mongodb::MongoNotify, DBConfig, DBListener, EventType,
};
use dotenv::dotenv;
use tracing::{error, info, Level};
use tracing_subscriber::FmtSubscriber;

fn set_up_tracer() {
    let subscriber = FmtSubscriber::builder()
        .with_max_level(Level::INFO)
        .finish();

    tracing::subscriber::set_global_default(subscriber).expect("setting dfeault subscriber failed");
}

#[tokio::main]
async fn main() -> Result<(), DBListenerError> {
    set_up_tracer();

    dotenv().ok();

    // Get the database URL from the environment variables
    let database_url = env::var("MONGO_DATABASE_URL").expect("MONGO_DATABASE_URL must be set");

    let database = "SathishLoginPage".to_string();
    let collection = "users".to_string();

    let database_config = DBConfig::MongoDB {
        url: database_url,
        database,
        collection,
    };

    let events = vec![EventType::INSERT, EventType::UPDATE, EventType::DELETE];

    let db_listener = DBListener::new(database_config, events).await?;

    db_listener
        .listen(|notification| {
            // we could only show the log for notification and also mention that user's can deserialize the value using the respective notify struct.

            let mongo_notify = serde_json::from_str::<MongoNotify>(&notification.to_string());

            if let Err(err) = mongo_notify {
                error!("Error deserializing notification : {:#?}", err);
                return;
            }

            let mongo_notify = mongo_notify.unwrap();

            info!("--> payload received in channel");

            info!("Payload : {:#?}", mongo_notify);
        })
        .await?;

    Ok(())
}