Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const config = require('../config/index');
- const Logger = require('./logger.service');
- const ObjectId = require('mongodb').ObjectId;
- var dbConn = null;
- var prmConn = null;
- function connectToMongo() {
- if (prmConn) return prmConn;
- // Reuse existing connection if exist
- if (dbConn) return Promise.resolve(dbConn);
- const MongoClient = require('mongodb').MongoClient;
- const url = config.dbURL;
- prmConn = MongoClient.connect(url, { useNewUrlParser: true }).then((client) => {
- Logger.info('[MONGO SERVICE] new connection to db');
- prmConn = null;
- // If we get disconnected (e.g. db is down)
- client.on('close', () => {
- Logger.info('[MONGO SERVICE] MongoDB Diconnected!');
- dbConn = null;
- client.close();
- });
- dbConn = client.db();
- return dbConn;
- });
- return prmConn;
- }
- module.exports = {
- connect: connectToMongo,
- ObjectId,
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement