I need some help regarding pg npm.
I have read many write ups and examples and have got totally confused about using the pg pool in a right way. Many of the articles are old which I read.
I would like to show you some of my code and how I have structured everything around db. I have few concerns where I need your support.
This is how I have implemented:
I establish a connection with Postgres once at the time of server startup only.
let pg = require('pg')
const db_config = {
user : DB_USER,
password : DB_PWD,
database : DB_NAME,
host : DB_HOST,
max : 5, // max number of clients in the pool
connectionTimeoutMillis : 5000,
idleTimeoutMillis : 30000
};
conn = new pg.Pool(db_config);
conn.connect((err, client, done) => {
if (err) {
log.error(err.message);
log.error(`could not connect to database`);
} else {
conn.query('SELECT 1', (err, res) => {
done();
if (err) {
log.error(err)
} else {
log.info("connected to database");
}
});
}
});
Please note conn
object is global which I use every where in my program further.
Then I start my HTTP server. Every request coming to server has to run around 25 database queries on an average. I use same conn object for firing queries as below during the lifetime of a program. Every function in the code includes only following piece of the code for queries.
conn.query(query, function (err, docs) {
if (err) {
log.error(err);
} else {
// do something
}
});
I am using "pg": "^6.2.2"
——————————————————
My concerns :
Am I utilising a pool?
How do I properly utilise pool?
Do I need to connect to pg which means conn.connect every time before I execute any query in the code?
How do I handle if the database is not reachable, code just hangs up on read and then times out after very long time? Is there any way I can time out after desired time?