Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

nodejs mongodb native reuse single connection

require('dotenv').config();
const { MongoClient } = require('mongodb');
const config = require('../../config/index');

const username = encodeURIComponent(config.mongo_db1.user);
const password = encodeURIComponent(config.mongo_db1.pass);
const dbHost = config.mongo_db1.host;
const authMechanism = 'DEFAULT';
const qString = `retryWrites=true&w=majority&authMechanism=${authMechanism}`;

const uri = `mongodb+srv://${username}:${password}@${dbHost}/?${qString}`;

const mongoOptions = {
  poolSize: 100,
  wtimeout: 2500,
  useNewUrlParser: true,
  useUnifiedTopology: true,
};

const client = new MongoClient(uri, mongoOptions);

let _db;

client.on('serverClosed', (event) => {
  // eslint-disable-next-line no-console
  console.log('received serverClosed');
  // eslint-disable-next-line no-console
  console.log(JSON.stringify(event, null, 2));

  // should i call mongoDBConnection() here if connection lost while app running?
});

const mongoDBConnection = async (app) => {
  try {
    if (client.isConnected()) {
      _db = client.db(config.mongo_db1.dbName);
      return client.db(config.mongo_db1.dbName);
    }

    await client.connect();
    if (app) app.use(passport.initialize());
    _db = client.db(config.mongo_db1.dbName);
    return client.db(config.mongo_db1.dbName);
  } catch (error) {
    return Promise.reject(error);
  }
};

const dbObj = () => _db;

module.exports = {
  mongoDBConnection,
  dbObj,
};
Comment

PREVIOUS NEXT
Code Example
Javascript :: filter text js 
Javascript :: react three fiber cannon collision 
Javascript :: getauth firebase admin node.js 
Javascript :: get product 
Javascript :: nestjs TS2339 
Javascript :: Return characters in a string in alphabetic order 
Javascript :: cargar un select con javascript dependiendo de otro select 
Javascript :: Self Invoking Function ($()) That Can Be Reused 
Javascript :: reduce function javascript 
Javascript :: javascript prompts user to input 
Javascript :: how to delete an element from an array in javascript 
Javascript :: select and select based on value in jquery 
Javascript :: comentar en javascript 
Javascript :: Javascript Scrape content from a website source code 
Javascript :: Remove escape characters from JSON Data 
Javascript :: console log like a pro 
Javascript :: react native scrollview item bottom 
Javascript :: how to make a delete button in javascript 
Javascript :: mongoose id validator 
Javascript :: discord.js purge 
Javascript :: date range picker jquery 
Javascript :: javascript pass array by value 
Javascript :: variable name as a string in Javascript function 
Javascript :: hide checkbox jquery 
Javascript :: how to display message in javascript 
Javascript :: random password generator javascript 
Javascript :: if page is loading then show loader in js 
Javascript :: sort array by field 
Javascript :: how to interrupt scroll with jquery 
Javascript :: Passing Boolean values as Props in react 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =