https://github.com/becual/node-pg-notify
NodeJS PostgreSQL pg-notify subscription library
https://github.com/becual/node-pg-notify
nodejs notify pg-notify postgresql subscription
Last synced: 12 months ago
JSON representation
NodeJS PostgreSQL pg-notify subscription library
- Host: GitHub
- URL: https://github.com/becual/node-pg-notify
- Owner: becual
- Created: 2018-03-16T17:35:34.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-04-17T20:44:27.000Z (about 8 years ago)
- Last Synced: 2025-06-04T10:03:20.463Z (about 1 year ago)
- Topics: nodejs, notify, pg-notify, postgresql, subscription
- Language: JavaScript
- Size: 188 KB
- Stars: 10
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## pg-notify
Configure and subscribe to Postgres Notify automatically for a given set of tables.
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| client | object | | node-postgres Client instance |
| configObject | object | | Configuration object |
| configObject.schema | string | "public" | Name of the schema where the tables exists. |
| configObject.functionName | string | "notify_table_change" | Name of the function to use notify. |
| configObject.channelName | string | "notify_table_change_channel" | Name of the channel where the function will notify. |
* [pg-notify](#module_pg-notify)
* [~Subscribe to all database events of a given list of tables(tables)](#module_pg-notify..Subscribe to all database events of a given list of tables) ⇒ Promise
* [~Create the functions and triggers to configure pg-notify.(tables)](#module_pg-notify..Create the functions and triggers to configure pg-notify.) ⇒ Promise
### pg-notify~Subscribe to all database events of a given list of tables(tables) ⇒ Promise
**Kind**: inner method of [pg-notify](#module_pg-notify)
**Returns**: Promise - a promise wich resolves an NodeJS EventEmitter
| Param | Type | Description |
| --- | --- | --- |
| tables | Array.<string> | An array of table names to listen |
**Example**
```js
const { Client } = require('pg');
const pgNotify = require('@becual/pg-notify');
let eventHandler = evt => {
console.log(JSON.stringify(evt, null, 4));
};
(async () => {
// Use your connection string
const client = new Client({ connectionString: process.env.PG_CONNECTION_STRING });
// Choose your tables to listen
const tables = ['customer', 'order_detail'];
try {
// Connect client
await client.connect();
// By default schema is public
const sub = await pgNotify(client, {schema: 'mySchema'}).subscribe(tables);
// Listen for changes
sub.on('INSERT', eventHandler);
sub.on('UPDATE', eventHandler);
sub.on('DELETE', eventHandler);
}
catch(error) {
console.log(error.message);
await client.end();
}
})();
```
### pg-notify~Create the functions and triggers to configure pg-notify.(tables) ⇒ Promise
**Kind**: inner method of [pg-notify](#module_pg-notify)
**Returns**: Promise - A promise that will implement the pg-notify config.
| Param | Type | Description |
| --- | --- | --- |
| tables | Array.<string> | The list of tables to create pg notify configuration. |
**Example**
```js
const { Client } = require('pg');
const pgNotify = require('@becual/pg-notify');
(async () => {
const client = new Client({ connectionString: process.env.PG_CONNECTION_STRING });
const tableList = ['customer', 'order_detail'];
try {
// Try to generate configuration
await client.connect();
await pgNotify(client, {schema: 'mySchema'}).config(tableList);
}
catch(error) {
// Show errors
console.log(error.message);
}
finally {
// Close connection
await client.end();
}
})();
```