Node - Postgres driver setup using node-postgres

I am trying to integrate a node-postgres driver and learn how to do simple CRUD operations. In mine, app.jsI am doing something like this:

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

Inside my file adapters/postgres.js, I have the following content:

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

With the code above, I keep getting this error: ReferenceError: conf is not definedso I added it as var conf = require('../config/conf');. This is not the right solution, as I would like to pass it from app.js. Further, even with this addition, I get the following error: TypeError: Client is not a constructor. Can anyone be guided by fixing both of these errors?

+4
source share
1 answer

factory, . 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;

factory 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);
});
+2

Source: https://habr.com/ru/post/1687142/


All Articles