/* eslint-disable no-unused-vars */
const mongoose = require('mongoose')
// mongodb database connection string. change it as per your needs. here "mydb" is the name of the database. You don't need to create DB from mongodb terminal. mongoose create the database automatically.
const inHostUrl = 'mongodb://root:KPTt76tImNBBMm4Kor8QA9gB@hotaka.liara.cloud:33794/asanserv_database?authSource=admin'
const outHostUrl = 'mongodb://root:KPTt76tImNBBMm4Kor8QA9gB@hotaka.liara.cloud:33794/asanserv_database?authSource=admin'
const init = ()=>{
  try {
    mongoose.connect(inHostUrl, {
      useNewUrlParser: true,
      useUnifiedTopology: true,
      useFindAndModify: false,
      useCreateIndex: true,
      connectTimeoutMS: 30000,
      socketTimeoutMS: 45000,
    })
  } catch (error) {
    init()
  }
}

init()
mongoose.plugin(schema => {
  schema.set('toObject', { getters: true })
  schema.set('toJSON', { getters: true })
  schema.set('timestamps', {
    createdAt: 'created_at',
    updatedAt: 'updated_at'
  })
})
const database = mongoose.connection
database.on('error', console.error.bind(console, 'connection error:'))
database.once('open', function callback() {
  console.log('🟠 ~ MongoDB Connected...')
})

module.exports = database
