I have an application where I need to create a new database for a new company that subscribes to use this application. So I'm maintaining a master database that stores this companies list and their respective database names. Now when the new company needs to use this newly subscribed application I need to create a new Pool
for their database for usage. How to create it programmatically and export it in node.js
?
I tried a way. I thought the pool as a simple object, so I tried to create it after it's database is created and stored it in the master database and tried to use that pool object whenever required (one pool is enough for me and I don't want to disconnect the pool as it should run 24/7, that's why I stored it). But I failed with the following error statement when I tried to connect to it
TypeError: myPool.connect is not a function
because once I store it as a jsonb in masterdb it no longer is a pool.
Suggestions on how to approach this will be helpful.
// eg. poolBuilder
const { Pool } = require('pg')
const pools = new Map();
function getPool(companyName){
if(!pools.has(companyName){
pools.set(companyName, new Pool({
host: 'localhost',
user: 'database-user',
max: 20,
idleTimeoutMillis: 30000,
connectionTimeoutMillis: 2000,
}))
}
return pools.get(companyName);
}
module.exports.getPool = getPool;
const {getPool} = require("poolBuilder");
const pool = getPool();