
// mongoose.<%- js %> - Mongoose adapter
<%- tplTsOnly([`import { App } from './app.interface'${sc}`, '']) -%>
<%- tplImports('mongoose', null, 'req') %>
<%- tplImports('logger', './logger') %>
<%- insertFragment('imports') %>
<%- insertFragment('init') %>

<%- tplExport('function (app) {', 'function (app: App) {') %>
  mongoose.Promise = global.Promise<%- sc %>
  mongoose.connect(app.get('mongodb'), { useNewUrlParser: true, useCreateIndex: true })
    .then(({ connection }<%- tplTsOnly([': any']) -%>) => {
      // <%- lintDisableNextLineNoConsole %>
      console.log(`connected to "${connection.name}" database at ${connection.host}:${connection.port}`)<%- sc %>
      return connection<%- sc %>
    })
    .catch(error => {
      // <%- lintDisableNextLineNoConsole %>
      console.log(error)<%- sc %>
      logger.error(error)<%- sc %>
      process.exit(1)<%- sc %>
    })<%- sc %>
  <%- insertFragment('func_init') %>

  app.set('mongooseClient', mongoose)<%- sc %>
  <%- insertFragment('more') %>
}<%- tplJsOnly([sc]) %>

<%- insertFragment('funcs') %>
<%- insertFragment('end') %>
