如何从节点 js 在 Knex 中获取 RAISE INFO/NOTICE?

How to get RAISE INFO/NOTICE in Knex from node js?

我正在使用 Knex 查询生成器和 Postgres 作为我的数据库。

我需要从 Postgres 函数或过程中获取 raise noticeraise info 的输出。

raise notice 'This is my Log';

我们需要在池创建后设置notice,为此我们需要使用afterCreate函数。 供大家参考

https://github.com/knex/knex/issues/4241

示例代码如下。

var pConn = {
        host: 'SERVER',
        port: 'PORT',
        user: 'USER',
        password: 'PASSWORD',
        database: 'DATABAENAME'
    };
    var query = "select * from pglog_test()";
    try {
    
        const knex = require('knex')({
            client: 'pg',
            connection: pConn,
            pool: {
                afterCreate: function (conn, done) {
                    conn.query('select 1 as result', function (err, result) {
                        conn.on('notice', function (msg) {
                            // here we can get the pg procedure logs (raise notice/raise info)
                            console.log('logs from postgress' + msg);
                        });
                        done(err, conn);
                    });
                }
            }
        });
        // procedure calling
        knex.raw(query).then(function (result) {
            console.log(result);
        }).catch(function (error) {
            console.log(error);
        });
    } catch (error) {
        console.log(error);
    }