After reading the docs: https://github.com/brianc/node-pg-pool, I am slightly concerned about reusing the new Pool() method.
The docs suggests that I need to place the new Pool() before exports and return it like so
// db.js
const pool = new Pool();
module.exports = () => { return pool; }
This way I can reuse Pool until the idleTimeoutMillis or client.release(), by using require() from other files
Eg:
const connect = require('./db')
connect().query(' .... ');
If this is correct, how does it work? Does node.js caches the new Pool(), as it not inside module.exports?
Yes, it is effectively cached since you create it exactly once (and node caches modules) and you always refer to that same instance in your exported method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With