#mongo-db #listener #notifications #postgresql #database-table #back-end

bin+lib db-library

A Rust library for listening to database changes and notifying connected backend services

3 releases

new 0.1.2 Feb 27, 2025
0.1.1 Feb 24, 2025
0.1.0 Feb 20, 2025

#379 in Database interfaces

Download history

128 downloads per month

MIT/Apache

65KB
1K SLoC

Database Change Listener

Overview

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.

Features

  • Database-Agnostic: Designed to support multiple databases, starting with PostgreSQL and MongoDB.
  • Event-Driven: Notifies backend services of table or collection changes via event-based channels.
  • Customizable: Users can specify the database type, connection URL, table or collection name, and columns to monitor.
  • Efficient: Eliminates the need for polling, reducing unnecessary database load.
  • Automatic Pool Management: The library handles connection pooling internally, so users don't need to manage database connections manually, ensuring efficient resource usage.

Installation

Add the following to your Cargo.toml:

[dependencies]
db-library = "latest-version"

Quick Start

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>(&notification.to_string()) {
            Ok(pg_notify) => {
                info!("--> Payload received in channel");
                info!("Parsed Payload: {:#?}", pg_notify);
            }
            Err(err) => {
                error!("Error deserializing notification: {:#?}", err);
            }
        }
    })
    .await?;

Example Usage for MongoDB

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>(&notification.to_string()) {
            Ok(mongo_notify) => {
                info!("--> Payload received in channel");
                info!("Parsed Payload: {:#?}", mongo_notify);
            }
            Err(err) => {
                error!("Error deserializing notification: {:#?}", err);
            }
        }
    })
    .await?;

Supported Databases

  • PostgreSQL
  • MongoDB

Configuration

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.

Use Cases

  • Real-time Data Synchronization: Keep your application data in sync with database changes.
  • Event-Driven Microservices: Build microservices that react to database events.
  • Change Tracking for Analytics: Monitor changes for analytics and reporting purposes.

License

This project is licensed under the MIT License.

Dependencies

~23–36MB
~599K SLoC