const mongoose = require('mongoose'); const Promise = require('bluebird'); const MONGODB_URL = process.env.MONGODB_URI || 'mongodb://localhost:27017/bsu-fantom'; const { MONGODB_USER, MONGODB_PASSWORD } = process.env; let connection; const connectDb = () => new Promise ((resolve, reject) => { mongoose.Promise = Promise; mongoose.connect(MONGODB_URL, { user: MONGODB_USER, pass: MONGODB_PASSWORD, useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true, useFindAndModify: false, family: 4 // Use IPv4, skip trying IPv6 }); connection = mongoose.connection; connection.on('error', error => { console.log(error); reject(error); }); connection.once('open', () => { console.log('Connection to MongoDB successfull') resolve(); }); }); module.exports = connectDb;