【问题标题】:How to get RAISE INFO/NOTICE in Knex from node js?如何从节点 js 获取 Knex 中的 RAISE INFO/NOTICE?
【发布时间】:2021-04-14 05:19:16
【问题描述】:

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

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

raise notice 'This is my Log';

【问题讨论】:

    标签: node.js postgresql knex.js


    【解决方案1】:

    我们需要在创建池后设置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);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-26
      • 1970-01-01
      • 2020-07-20
      • 2016-02-05
      • 2018-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多