Giter Site home page Giter Site logo

db-sync-package's Introduction

Database Sync Package

This package provides a solution for synchronizing data between a primary and a backup database. It allows you to listen for database events such as INSERT, UPDATE, and DELETE and replicate these changes to a backup database.

Installation

To install the package, run the following command:

Usage

import { Client } from "pg";
import { DatabaseSync } from "db-sync-package";
import { EventHandlers } from "db-sync-package";
import { PrimaryClient } from "db-sync-package";
import { BackupClient } from "db-sync-package";

const primaryClient = new PrimaryClient(
  "your_primary_user",
  "your_primary_host",
  "your_primary_database",
  "your_primary_password"
);

const backupClient = new BackupClient(
  "your_backup_user",
  "your_backup_host",
  "your_backup_database",
  "your_backup_password"
);

const eventHandlers = new EventHandlers();

async function main() {
  await primaryClient.connect();
  await backupClient.connect();

  const databaseSync = new DatabaseSync({
    primaryClient: primaryClient.client(),
    backupClient: backupClient.client(),
  });

  //Listen events with EventHandlers
  await eventHandlers.initEventListener(primaryClient.client());

  // Listen INSERT
  eventHandlers.onInsert("insert", async (event: any) => {
    const { table, data } = event;
    console.log(`INSERT event received for table ${table}:`, data);
    await databaseSync.syncTable(table, "INSERT", data);
  });

  // Listen DELETE
  eventHandlers.onInsert("delete", async (event: any) => {
    const { table, data } = event;
    console.log(`DELETE event received for table ${table}:`, data);
    await databaseSync.syncTable(table, "DELETE", data);
  });

  // Listen UPDATE
  eventHandlers.onInsert("update", async (event: any) => {
    const { table, data } = event;
    console.log(`UPDATE event received for table ${table}:`, data);
    await databaseSync.syncTable(table, "UPDATE", data);
  });
}

main().catch((error) => {
  console.error("An error occurred:", error);
});

Features

  • Listen for database events such as INSERT, UPDATE, and DELETE.
  • Replicate changes to a backup database.
  • Easy to use and integrate with existing projects.

db-sync-package's People

Contributors

tuzlu07x avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.