I’m using a synchronized database connection for my project and I’m wondering does it cause trouble? Let me explain, how i use this. When the application starts life it connects to the database and it is using one connection until application dies. I prefered this because I need to export database to other documents for db queries. This is my code:
const { MongoClient } = require('mongodb');
const uri = 'mongodb://localhost:27017';
function connectToDB() {
const client = new MongoClient(uri);
try {
client.connect();
setTimeout(() => {
console.log(`\x1b[32m[DONE]\x1b[0m db connection established.`)
}, 200);
return client.db('serverDatabase')
} catch (error) {
console.error('error:', error);
throw error;
}
}
module.exports = connectToDB();