gpt4 book ai didi

postgresql - 带有 pg-promise 的 Postgres LISTEN/NOTIFY

转载 作者:行者123 更新时间:2023-11-29 13:18:41 24 4
gpt4 key购买 nike

我正在尝试在 PostgreSQL 中设置一个简单的 LISTEN/NOTIFY 功能并使用 pg-promise 通知 node.js 代码图书馆。

我搭建的pg代码是

CREATE OR REPLACE FUNCTION notify_trigger() RETURNS trigger AS $$
DECLARE
BEGIN
RAISE NOTICE '%', 'HI';
PERFORM pg_notify('watchers', TG_TABLE_NAME || ', tags:,' || NEW.tags );
RETURN new;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER watched_table_trigger BEFORE INSERT OR UPDATE ON assets
FOR EACH ROW EXECUTE PROCEDURE notify_trigger();

它确实在 psql 控制台上通知。

但是当我使用 pg-promise 时代码,它不会像我希望的那样输出控制台消息。相关的node.js代码:

const pgoptions = require('./pgoptions.config.js');
const connectObject = require('./pgconnect.config.js');
const db = require('pg-promise')(pgoptions)(connectObject);

// added listener code for pg listen / notify
db.connect({direct: true})
.then(function (sco) {
sco.client.on('assets', function (data) {
console.log('Received: ', data);
});
return sco.none('LISTEN assets');
})
.catch(function (error) {
console.error('Error:', error);
});

module.exports = resources;

直接从pg-promise中取出的nodejs代码的 Learn By Example文档 here .我确实查看了有关此主题的其他帖子,但没有找到 pg-promise基于解决方案。

最佳答案

不是

sco.client.on('assets', function (data) {

sco.client.on('notification', function (data) {

即您订阅的是通用 notification 消息,而不是 channel 名称。就是这样显示的the very example你引用了。

关于postgresql - 带有 pg-promise 的 Postgres LISTEN/NOTIFY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45427280/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com