Skip to content

rtasalem/busgres

Repository files navigation

Busgres

Busgres is a Node.js package that will receieve a message from an Azure Service Bus queue and save it into a PostgreSQL database. It utilises the @azure/service-bus package for Service Bus integration and the pg (node-postgres) package for PostgreSQL connectivity.

Installation

This package can be installed using NPM:

npm i busgres

Usage

BusgresClient set-up & configuration:

const { BusgresClient } = require('busgres`)

const sbConnectionString = process.env.CONNECTION_STRING 

const sbEntity = process.env.QUEUE 

const pgClient = {
  user: process.env.USERNAME,
  database: process.env.DATABASE,
  host: process.env.HOST,
  port: process.env.PORT
}

const bgClient = new BusgresClient(sbConnectionString, sbEntity, pgClient)

Connecting to BusgresClient:

bgClient
  .connect()
  .then(async () => {
    console.log(
      `You are now connected to the ${process.env.DATABASE} database in PostgreSQL.`
    )

    const query = 'SELECT * FROM TABLE_NAME'
    const result = await bgClient.pgClient.query(query)
    console.log('All messages in the database:')

    result.rows.forEach((row, index) => {
      console.log(`Row ${index + 1}:`, row)
    })
  })
  .catch((error) => {
    console.error(
      `There has been an error connecting to your PostgreSQL database: ${error}`
    )
  })

The above example will confirm a connection has been established via the BusgresClient and will select all content from any table in the PostgreSQL database to then log it into the console (not necessary, just further confirms the PostgreSQL connection is active).

Receiving and saving messages via receiveMessage:

const tableName = 'TABLE_NAME'
const columnNames = ['COLUMN_NAME']

bgClient.receiveMessage(tableName, columnNames)

tableName and columnNames must be defined so that they can be passed into the receieveMessage function.

Disconnecting from BusgresClient:

bgClient.disconnect()

When called the connection to PostgreSQL will be terminated and will also close the Service Bus client & queue. Messages can no longer be received from Service Bus nor saved to the PostgreSQL database.

Configuration

BusgresClient - Client that contains the configuration for both Azure Service Bus & PostgreSQL. Arguments passed into the constructor of the BusgresClient include the Service Bus connection string (sbConnectionString), Service Bus entity (sbEntity) i.e. name of the queue, and the PostgreSQL client configuration (pgClient).

connect - When called will establish connection to the PostgreSQL database.

saveMessage & receiveMessage - Logic for inserting messages received from a Service Bus queue into a table within a PostgreSQL database.

disconnect - When called will terminate the connection to PostgreSQL and close the Service Bus client and entity.

Demo

A simple demo Node.js application called busgres-demo was created to test the functionality of this package during its development and to provide further example of usage.

License

This package is licensed under the MIT License. Refer to the LICENSE file for more details.

Feedback

Feel free to reach out if you have any suggestions for improvement.

Dependencies

This package has a total of 2 dependencies on the following:

Author

Rana Salem

About

Busgres is a Node.js package that will receieve a message from an Azure Service Bus queue and save it into a PostgreSQL database.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published