I am trying to make multiple connection pools and export them to other modules so that i can query in other modules as crm1.execute().[I have 15 databases so i have to create 15 pools] I have tried 2 methods (mentioned in code). If i try to export connection without promise i get undefined exports in app.js so after some digging i found that i will have to use promise to make sure the connections are exported when i use them in app.js. I have tried to implement promise in method 1 but still getting same thing.
pool.js
const config = require("../config/config");
const oracledb = require("oracledb");
//method 1
module.exports.crm1promise = new Promise((resolve, reject) => {
oracledb.createPool({
user: config.crm1.user,
password: config.crm1.password,
connectString: config.crm1.connectString,
poolAlias: config.crm1.poolAlias,
poolMin: 0,
poolMax: 10,
poolTimeout: 300
}, (error, pool) => {
if (error) {
reject(err);
}
module.exports.crm1db = pool.getConnection((err, connection) => {
if (err) {
reject(err);
}
resolve (module.exports.crm1db = connection);
});
});
});
//method 2
oracledb.createPool({
user: config.crm2.user,
password: config.crm2.password,
connectString: config.crm2.connectString,
poolAlias: config.crm2.poolAlias,
poolMin: 0,
poolMax: 10,
poolTimeout: 300,
}, (error, pool) => {
if (error) {
console.log(error);
}
module.exports.crm2db = oracledb.getConnection(config.crm2.poolAlias, (err, connection) => {
if (err) {
console.log(err);
}
console.log("Connection to CRM2 POOL created");
return connection;
});
});
app.js
const db = require("./db/pool");
const oracledb = require("oracledb");
console.log(db.crm1Promise);
console.log(db.exports);
cmd output
D:\Development\node\database>node app.js
undefined
undefined
Connection to CRM2 POOL created