Answers for "serverless and connections mongodb atlas"

0

serverless and connections mongodb atlas

const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
let isConnected;

module.exports = connectToDatabase = () => {
  if (isConnected) {
    console.log('=> using existing database connection');
    return Promise.resolve();
  }

  console.log('=> using new database connection');
  return mongoose.connect(process.env.DB)
    .then(db => { 
      isConnected = db.connections[0].readyState;
    });
};
Posted by: Guest on June-13-2020

Code answers related to "serverless and connections mongodb atlas"

Code answers related to "Javascript"

Browse Popular Code Answers by Language