aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/initializers/database.js
blob: 632040b81f96b083b4b32b077db2b5c89631f00a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
'use strict'

const mongoose = require('mongoose')

const constants = require('../initializers/constants')
const logger = require('../helpers/logger')

// Bootstrap models
require('../models/application')
require('../models/oauth-token')
require('../models/user')
require('../models/oauth-client')
require('../models/video')
// Request model needs Video model
require('../models/pods')
// Request model needs Pod model
require('../models/request')

const database = {
  connect: connect
}

function connect () {
  mongoose.Promise = global.Promise
  mongoose.connect('mongodb://' + constants.CONFIG.DATABASE.HOST + ':' + constants.CONFIG.DATABASE.PORT + '/' + constants.CONFIG.DATABASE.DBNAME)
  mongoose.connection.on('error', function () {
    throw new Error('Mongodb connection error.')
  })

  mongoose.connection.on('open', function () {
    logger.info('Connected to mongodb.')
  })
}

// ---------------------------------------------------------------------------

module.exports = database