@fetchq/pg-pubsub

中文文档
0.9.0发布于 2024/07/12MITCommonJS近 30 天下载 1.2万

A Publish/Subscribe implementation on top of PostgreSQL NOTIFY/LISTEN

原始使用文档
文档目录10 节
  1. Installation
  2. Requirements
  3. Usage
  4. Options
  5. Methods
  6. Examples
  7. Description
  8. Lint / Test
  9. Automate via Make/Docker
  10. Manual Execution

PG PubSub

A Publish/Subscribe implementation on top of PostgreSQL NOTIFY/LISTEN

Linting Build Status Node CI Build Status Coverage Status dependencies Status Known Vulnerabilities js-semistandard-style

Installation

npm install pg-pubsub --save

Requirements

Node.js >= 10.x Postgres >= 9.4

Usage

const PGPubsub = require('pg-pubsub');
const pubsubInstance = new PGPubsub(uri[, options]);

Options

{
  [log]: Function // default: silent when NODE_ENV=production, otherwise defaults to console.log(...)
}

Methods

  • addChannel(channelName[, eventListener]) – starts listening on a channel and optionally adds an event listener for that event. As PGPubsub inherits from EventEmitter one also add it oneself.
  • removeChannel(channelName[, eventListener]) – either removes all event listeners and stops listeneing on the channel or removes the specified event listener and stops listening on the channel if that was the last listener attached.
  • publish(channelName, data) – publishes the specified data JSON-encoded to the specified channel. It may be better to do this by sending the NOTIFY channelName, '{"hello":"world"}' query yourself using your ordinary Postgres pool, rather than relying on the single connection of this module. Returns a Promise that will become rejected or resolved depending on the success of the Postgres call.
  • close(): Promise – closes down the database connection and removes all listeners. Useful for graceful shutdowns.
  • All EventEmitter methods are inherited from EventEmitter

Examples

Simple

const pubsubInstance = new PGPubsub('postgres://username@localhost/database');

pubsubInstance.addChannel('channelName', function (channelPayload) {
  // Process the payload – if it was JSON that JSON has been parsed into an object for you
});

pubsubInstance.publish('channelName', { hello: "world" });

The above sends NOTIFY channelName, '{"hello":"world"}' to PostgreSQL, which will trigger the above listener with the parsed JSON in channelPayload.

Advanced

const pubsubInstance = new PGPubsub('postgres://username@localhost/database');

pubsubInstance.addChannel('channelName');

// pubsubInstance is a full EventEmitter object that sends events on channel names
pubsubInstance.once('channelName', function (channelPayload) {
  // Process the payload
});

Description

Creating a PGPubsub instance will not do much up front. It will prepare itself to start a Postgres connection once the first channel is added and then it will keep a connection open until its shut down, reconnecting it if it gets lost, so that it can constantly listen for new notifications.

Lint / Test

Automate via Make/Docker

The easiest way to run tests is via Makefile on a Linux compatible environment:

make test

Manual Execution

In case you want to run the tests manually, you first need to be able to access a PostgreSQL instance. You can do that in 2 ways:

Via Docker

docker run --rm -it -p 5432:5432 -e \
POSTGRES_DB=pgpubsub_test -e \
POSTGRES_PASSWORD=postgres postgres

Via Environment File

You can create an environment file in test/.env:

DATABASE_TEST_URL=postgres://postgres:postgres@localhost:5432/postgres

DATABASE_TEST_URL_INVALID_USER=postgres://invalidUser:postgres@localhost:5432/postgres

DATABASE_TEST_URL_INVALID_PASSWORD=postgres://postgres:invalidPassword@localhost:5432/postgres

Once the PostgreSQL setup is done, you can run the test with your Node environment:

npm i
npm test

版本与发布记录

@fetchq/pg-pubsub 共发布 7 个版本,首次发布于 2020/08/06;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.9.0latest2024/07/122 年前
0.8.02021/06/035 年前
0.7.32021/02/225 年前
0.7.22021/02/225 年前
0.6.12020/09/276 年前
0.6.02020/08/066 年前

关于 @fetchq/pg-pubsub 的常见问题

@fetchq/pg-pubsub 是什么?

A Publish/Subscribe implementation on top of PostgreSQL NOTIFY/LISTEN

如何安装 @fetchq/pg-pubsub?

在项目目录运行 npm install @fetchq/pg-pubsub,或使用 pnpm add / yarn add / bun add。

@fetchq/pg-pubsub 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/fetchq__pg-pubsub 是否存在。

@fetchq/pg-pubsub 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@fetchq/pg-pubsub 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=10.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@fetchq/pg-pubsub 可以商用吗?

@fetchq/pg-pubsub 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@fetchq/pg-pubsub 的下载量有多少?

根据 npm 官方下载统计,@fetchq/pg-pubsub 在 2026/10/15 – 2026/11/13 这 30 天内被下载 12,150 次,日均约 405 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fetchq/pg-pubsub 还在维护吗?

最新版本 0.9.0 发布于 2024/07/12(2 年前);至今共发布 7 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/08/06。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。