/* eslint no-console:0 */
import { DB_HOST } from './utility'
import mongoose from 'mongoose'
import { logger } from './logger'

mongoose.Promise = global.Promise
mongoose.connect('mongodb://' + DB_HOST + '/test')
mongoose.set('debug', false)

export const connection = mongoose.connection
connection.on('error', function (err) {
  logger.error('DB connection error: ' + err)
  throw new Error('Unable to connect to database')
})
connection.once('open', function () {
  logger.info('Connected to Mongo Database')
  logger.info('  db host: ' + DB_HOST)
  logger.info('  database: ' + connection.db.databaseName)
  logger.info('  Mongoose v' + mongoose.version)
  var admin = mongoose.connection.db.admin()
  admin.serverStatus((err, info) => {
    if (err) logger.error(err)
    logger.info('  Mongo v' + info.version)
  })
})
