| Crates.io | db-library |
| lib.rs | db-library |
| version | 0.1.2 |
| created_at | 2025-02-20 11:45:38.596562+00 |
| updated_at | 2025-02-27 12:12:29.52472+00 |
| description | A Rust library for listening to database changes and notifying connected backend services. |
| homepage | https://github.com/Azeem-0/db-library |
| repository | https://github.com/Azeem-0/db-library |
| max_upload_size | |
| id | 1562565 |
| size | 139,890 |
This Rust library provides an event-based mechanism to monitor changes in database tables or collections and notify backend services, eliminating the need for polling. It supports PostgreSQL and MongoDB, allowing developers to register listeners and receive real-time updates on specified tables, columns, or collections.
Add the following to your Cargo.toml:
[dependencies]
db-library = "latest-version"
To get started with the library, you can use the following example:
use db_library::{config::DBListenerError, DBConfig, DBListener, EventType, PgNotify,MongoNotify};
let database_config = DBConfig::Postgres {
url, // Database connection URL
table_name, // Name of the table to monitor
columns, // List of specific columns to monitor for updates
table_identifier, // Unique identifier for the table
};
let events = vec![EventType::UPDATE, EventType::INSERT, EventType::DELETE]; // Events to monitor: updates, inserts, and deletes
let db_listener = DBListener::new(database_config, events).await?; // Create a new database listener
db_listener
.listen(|notification| {
// Print the raw notification
info!("Received notification: {:?}", notification);
// Attempt to deserialize the notification into the `PgNotify` struct
match serde_json::from_str::<PgNotify>(¬ification.to_string()) {
Ok(pg_notify) => {
info!("--> Payload received in channel");
info!("Parsed Payload: {:#?}", pg_notify);
}
Err(err) => {
error!("Error deserializing notification: {:#?}", err);
}
}
})
.await?;
Here's how you can set up a listener for MongoDB:
let database_config = DBConfig::MongoDB {
url, // Database connection URL
database, // Name of the Database to monitor.
collection, // Name of the Collection to monitor.
};
let events = vec![EventType::INSERT, EventType::UPDATE, EventType::DELETE]; // Events to monitor: updates, inserts, and deletes
let db_listener = DBListener::new(database_config, events).await?; // Create a new database listener
db_listener
.listen(|notification| {
// Print the raw notification
info!("Received notification: {:?}", notification);
// Attempt to deserialize the notification into the `MongoNotify` struct
match serde_json::from_str::<MongoNotify>(¬ification.to_string()) {
Ok(mongo_notify) => {
info!("--> Payload received in channel");
info!("Parsed Payload: {:#?}", mongo_notify);
}
Err(err) => {
error!("Error deserializing notification: {:#?}", err);
}
}
})
.await?;
Users can specify the database type, connection URL, table name or collection name, and columns when initializing a listener instance. The library provides a flexible configuration to accommodate various use cases.
This project is licensed under the MIT License.