1const mongoose = require('mongoose');
2mongoose.Promise = global.Promise;
3let isConnected;
4
5module.exports = connectToDatabase = () => {
6 if (isConnected) {
7 console.log('=> using existing database connection');
8 return Promise.resolve();
9 }
10
11 console.log('=> using new database connection');
12 return mongoose.connect(process.env.DB)
13 .then(db => {
14 isConnected = db.connections[0].readyState;
15 });
16};