Skip to content

Commit 427119b

Browse files
committed
docs: notification
1 parent 2bc777a commit 427119b

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

docs/API/PgDb.md

+25
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,28 @@ For example see the [transaction](/transaction) section.
191191
If the PgDb instance has dedicated connection mode and has transaction it will rolls back, otherwise do nothing.
192192
Returns with PgDb instance (with pool connections mode) where no transaction is taking place.
193193
For example see the [transaction](/transaction) section.
194+
195+
---
196+
## listen
197+
<span class="def"><span class="func">listen</span>(channel:<span class="type">string</span>, callback:<span class="type">(Notification)=&gt;void)</span>;</>
198+
199+
Creates a new dedicated connection for listeners (if it not exists), and sets a callback for the channel.
200+
It is possible to set multiple callbacks for one channel.
201+
If there will be a notification from the database, the callback will run.
202+
For example see the [notification](/notification) section.
203+
204+
---
205+
## unlisten
206+
<span class="def"><span class="func">unlisten</span>(channel:<span class="type">string</span>, callback?:<span class="type">(Notification)=&gt;void)</span>;</>
207+
208+
Removes a listener. If callback parameter is set, only the given callback will be removed.
209+
If callback parameter is not set, all callbacks will be removed from the channel.
210+
If it was the last channel, the dedicated connection for listeners will be released.
211+
For example see the [notification](/notification) section.
212+
213+
---
214+
## notify
215+
<span class="def"><span class="func">notify</span>(channel:<span class="type">string</span>, payload?:<span class="type">string</span>;</>
216+
217+
Send a notification via postgresql.
218+
For example see the [notification](/notification) section.

docs/notification.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
## Notification
2+
as simple as:
3+
```js
4+
let result = '';
5+
await db.listen('channel', (data) => { result += data.payload; });
6+
await db.listen('channel', () => { result += ',nextCallback'; });
7+
8+
await db.run(`NOTIFY channel, 'data'`);
9+
//same as: await db.notify('channel', 'data');
10+
//result will be: 'data,nextCallback'
11+
12+
await db.unlisten('channel');
13+
//dedicated listener connection now released.
14+
```
15+
16+
See [Postgresql documentation](https://www.postgresql.org/docs/current/sql-notify.html)
17+
18+
## Some comments
19+
Notification listeners uses a dedicated connection. When the postgresql server restarts, some notifications will not be received, but the connection and the listeners will be re-created.

mkdocs.yml

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pages:
1212
- Mapping database types : mappingDatabaseTypes.md
1313
- Transaction : transaction.md
1414
- Stream : streams.md
15+
- Notification : notification.md
1516
- FAQ : faq.md
1617
- Known pitfalls : pitfalls.md
1718
- API :

0 commit comments

Comments
 (0)