Node - 使用 node-postgres 设置 Postgres 驱动程序

Node - Postgres driver setup with node-postgres

我正在尝试集成 node-postgres 驱动程序并学习做一个简单的 CRUD 操作。在我的 app.js 中,我做了这样的事情:

...
var postgres = require('./adapters/postgres')
var postClient = new postgres(conf);
...
postClient.connect(function (dbconn) {
    app.dbconn = dbconn;
    app.conf = conf;

    console.log("************************************************************");
    console.log(new Date() + ' | CRUD Server Listening on ' + conf['web']['port']);
    console.log("************************************************************");

    server.listen(conf['web']['port']);

    var Routes = require('./routes/http-routes');
    new Routes(app);
});

在我的 adapters/postgres.js 文件中,我有以下内容:

const Client = require('pg');
const postClient = new Client(conf)({
    host: conf['postgres'].host,
    port: conf['postgres'].port,
    dbname: conf['postgres'].dbname,
    username: conf['postgres'].username,
    password: conf['postgres'].password,
    dbconn: null,
});
module.exports = postClient;
postClient.prototype.connect = function (cbk) {
    var self = this;
    client.connect(function (err, db) {
        console.log(new Date() + " | Postgres Server Connection Establised...");
        console.log(new Date() + " | Current database: ", db.databaseName);
        if (!err) {
            console.log(new Date() + " | Postgres Server Authenticated...");
            self.dbconn = db;
            cbk(db);
        } else {
            console.log(new Date() + " | Postgres Server Error in connection...");
            console.log(err);
            self.dbconn = db;
            cbk(db);
        }
    });
};

使用上面的代码,我不断收到此错误:ReferenceError: conf is not defined 所以我将其添加为 var conf = require('../config/conf');。这不是一个合适的解决方案,因为我想从 app.js 传递它。接下来,即使添加了这个,我也会收到以下错误:TypeError: Client is not a constructor。有人可以指导修复这两个错误吗?

导出采用配置对象和 returns 客户端实例的工厂函数。 Client 不是 pg 中的默认导出,因此需要对其进行解构。

const { Client } = require('pg');

const createPgClient = (conf) => {
    const pgClient = new Client(conf)({
        host: conf['postgres'].host,
        port: conf['postgres'].port,
        dbname: conf['postgres'].dbname,
        username: conf['postgres'].username,
        password: conf['postgres'].password,
        dbconn: null,
    });

    pgClient.prototype.connect = function (callback) {

        client.connect()
        .then(() => {
            console.log(new Date() + " | Postgres Server Authenticated...");
        })
        .catch((err) => {
            console.log(new Date() + " | Postgres Server Error in connection...");
            console.log(err);
        })
        .finally(()=> {
            self.dbconn = this;
            callback(this);
        });

    };
    return pgClient;
};
module.exports = createPgClient;

在您的 app.js 中使用此工厂函数创建客户端。

const createPgClient = require('./adapters/postgres')
cont pgClient = createPgClient(conf);

pg.connect(function (dbconn) {
    app.dbconn = dbconn;
    app.conf = conf;

    console.log("************************************************************");
    console.log(new Date() + ' | CRUD Server Listening on ' + conf['web']['port']);
    console.log("************************************************************");

    server.listen(conf['web']['port']);

    const Routes = require('./routes/http-routes');
    new Routes(app);
});