Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 46 additions & 68 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Busgres
Service BUS + PostGRES = Busgres

[Busgres](https://www.npmjs.com/package/busgres) is a Node.js package that will receieve a message from an Azure Service Bus queue or topic and save it into a PostgreSQL database. It abstracts the [`@azure/service-bus`](https://www.npmjs.com/package/@azure/service-bus) the [`pg` (node-postgres)](https://www.npmjs.com/package/pg) package for Service Bus and Postgres integration.
[Busgres](https://www.npmjs.com/package/busgres) is a Node.js package that will receive a message from an Azure Service Bus queue or topic and save it into a PostgreSQL database. It abstracts the [`@azure/service-bus`](https://www.npmjs.com/package/@azure/service-bus) and [`pg` (node-postgres)](https://www.npmjs.com/package/pg) packages for Service Bus and Postgres integration.

## Installation

Expand All @@ -15,95 +15,73 @@ npm i busgres

`BusgresClient` set-up & configuration:

```
const { BusgresClient } = require('busgres`)

const sbConnectionString = process.env.CONNECTION_STRING

const sbEntityName = process.env.QUEUE
const sbEntityType = 'queue'

const pgClient = {
user: process.env.USERNAME,
database: process.env.DATABASE,
host: process.env.HOST,
port: process.env.PORT
```javascript
import { BusgresClient } from 'busgres'
import 'dotenv/config'

const busgresClient = new BusgresClient({
serviceBus: {
connectionString: process.env.SB_CONNECTION_STRING,
entity: process.env.SB_ENTITY,
entityType: 'queue'
},
postgres: {
username: process.env.PG_USERNAME,
password: process.env.PG_PASSWORD,
database: process.env.PG_DATABASE,
host: process.env.PG_HOST,
port: process.env.PG_PORT
}
})

const tableName = 'busgres'
const columnNames = ['message']

export {
busgresClient,
tableName,
columnNames
}

const bgClient = new BusgresClient(sbConnectionString, sbEntityName, sbEntityType, pgClient)
```

NOTE: If using topics, provide the topic name for `sbEntityName` in place of a queue name. Additionally, ensure `sbEntityType` is set to `'topic'` and that a value for `sbEntitySubscription` is also provided.

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}`
)
})
```
NOTE: If using topics, provide the topic name for `sbEntityName` in place of a queue name. Additionally, ensure `sbEntityType` is set to `'topic'` and that a value for `sbEntitySubscription` is also provided.

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).<br><br>
Receiving and saving messages via `receiveMessage`:
Starting the `BusgresClient` connection:

```
const tableName = 'TABLE_NAME'
const columnNames = ['COLUMN_NAME']
```javascript
import {
busgresClient,
tableNames,
columnNames
} from './busgres-config.js'

bgClient.receiveMessage(tableName, columnNames)
```
await busgresClient.start(tableName, columnNames)

`tableName` and `columnNames` must be defined so that they can be passed into the `receieveMessage` function.<br><br>
Disconnecting from `BusgresClient`:
process.on('SIGINT', async () => {
await busgresClient.stop()
process.exit()
})

```
bgClient.disconnect()
```

When called the connection to PostgreSQL will be terminated and will also close the Service Bus client & receiver. 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 (`sbEntityName`) i.e. name of the queue or topic, Service Bus entity type (either `'queue'` or `'topic'`), Service Bus entity subscription (name of the subscription if using topic), and the PostgreSQL client configuration (`pgClient`).<br><br>
`connect` - When called will establish connection to the PostgreSQL database.<br><br>
`saveMessage` & `receiveMessage` - Logic for inserting messages received from a Service Bus entity into a table within a PostgreSQL database.<br><br>
`disconnect` - When called will terminate the connection to PostgreSQL and close the Service Bus client and entity.
With the above set-up and configuration a basic working `BusgresClient` connection can be established.

## Demo

A simple demo Node.js application called [busgres-demo](https://github.com/rtasalem/busgres-demo) was created to test the functionality of this package during its development and to provide further example of usage.
A simple demo Node.js application, [busgres-demo](https://github.com/rtasalem/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](https://github.com/rtasalem/busgres/blob/main/LICENSE) file for more details.

## Feedback

Feel free to reach out if you have any suggestions for improvement. A [contributor's guide](https://github.com/rtasalem/busgres/blob/main/CONTRIBUTORS-GUIDE.md) is also available.
Feel free to reach out if you have any suggestions for improvement or further development.

## Dependencies

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

- [`@azure/service-bus`](https://www.npmjs.com/package/@azure/service-bus)
- [`pg`](https://www.npmjs.com/package/pg)
This package has a total of 2 dependencies on the following:
[@azure/service-bus](https://www.npmjs.com/package/@azure/service-bus)
[pg](https://www.npmjs.com/package/pg)

## Author

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "busgres",
"version": "5.0.0",
"version": "5.0.2",
"description": "Busgres is an NPM package for receiving messages from Service Bus and saving them into a PostgreSQL database.",
"main": "index.js",
"type": "module",
Expand Down