{"size": 868, "ext": "js", "max_stars_repo_path": "database/connection.js", "max_stars_repo_name": "HRSArgyropoulos/pharma-server-zach", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "database/connection.js", "max_issues_repo_name": "HRSArgyropoulos/pharma-server-zach", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "database/connection.js", "max_forks_repo_name": "HRSArgyropoulos/pharma-server-zach", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 22.8421052632, "max_line_length": 75, "alphanum_fraction": 0.7050691244}
const mongoose = require('mongoose');

mongoose.connection.close();
// Setup mongoose connection
mongoose.connect(process.env.PHARMA_DB_URI, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
  useCreateIndex: true,
  useFindAndModify: false,
});

//Get the connection
const db = mongoose.connection;

db.once('open', () => {
  console.log('Database has been connected');
});

//Bind connection to error event (to get notification of connection errors)
db.on('error', () => {
  console.error.bind(
    console,
    'MongoDB connection error:'
  );
  db.close();
});

/* Check connection with db AFTER being already established*/
db.on('disconnected', () =>
  console.error('Lost connection with MongoDB')
);
/* Successfull reconnection after disconnection with db */
db.on('reconnected', () =>
  console.log('Reconnected to the database')
);

module.exports = db;
