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

5

我正在尝试整合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。有人可以指导如何修复这两个错误吗?

@brianc 任何帮助都将不胜感激! - JackSlayer94
一个好的例子:使用Node和Postgres设计RESTful API - vitaly-t
1
@vitaly-t 谢谢,我会以此为基础!只是为了理解,你能帮我看一下现有的代码吗? - JackSlayer94
1个回答

2

导出一个工厂函数,该函数接受配置对象并返回客户端实例。pg中的Client不是默认导出项,因此需要解构。

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);
});

使用上面发布的代码,我得到了以下错误: var pgClient = createPgClient(conf); ^ ReferenceError: createPgClient未定义 - JackSlayer94

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接