Tuesday 23 May 2017

Node.js: initiate pg.Pool()

As per example (db.js)

const pg = require('pg');

const client_config = {...};

const pool = new pg.Pool(client_config);

pool.on('error', function(err, client) {
  console.error('idle client error', err.mesae, err.stack);
});

module.exports.query = function(text, values, callback) {
  return pool.query(text, values, callback);
};

module.exports.connect = function(callback) {
  return pool.connect(callback;
};

and within an express (generated) application, do I have to initiate/require the Pool (db.js) in my app.js/on app start-up or do I simply require the db.js within my data models (respectively required in my routes)? Intuitively I would initiate the Pool on start-up rather than on each connection to the routes to avoid multiple initiations, but I am fairly new to Node.js.



via ThingumaBob

No comments:

Post a Comment