diff options
Diffstat (limited to 'server/initializers/database.js')
-rw-r--r-- | server/initializers/database.js | 90 |
1 files changed, 65 insertions, 25 deletions
diff --git a/server/initializers/database.js b/server/initializers/database.js index 0564e4e77..f8f68adeb 100644 --- a/server/initializers/database.js +++ b/server/initializers/database.js | |||
@@ -1,37 +1,77 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const mongoose = require('mongoose') | 3 | const fs = require('fs') |
4 | const path = require('path') | ||
5 | const Sequelize = require('sequelize') | ||
4 | 6 | ||
5 | const constants = require('../initializers/constants') | 7 | const constants = require('../initializers/constants') |
6 | const logger = require('../helpers/logger') | 8 | const logger = require('../helpers/logger') |
9 | const utils = require('../helpers/utils') | ||
7 | 10 | ||
8 | // Bootstrap models | 11 | const database = {} |
9 | require('../models/application') | ||
10 | require('../models/oauth-token') | ||
11 | require('../models/user') | ||
12 | require('../models/oauth-client') | ||
13 | require('../models/video') | ||
14 | // Request model needs Video model | ||
15 | require('../models/pods') | ||
16 | // Request model needs Pod model | ||
17 | require('../models/request') | ||
18 | |||
19 | const database = { | ||
20 | connect: connect | ||
21 | } | ||
22 | 12 | ||
23 | function connect () { | 13 | const dbname = constants.CONFIG.DATABASE.DBNAME |
24 | mongoose.Promise = global.Promise | 14 | const username = constants.CONFIG.DATABASE.USERNAME |
25 | mongoose.connect('mongodb://' + constants.CONFIG.DATABASE.HOSTNAME + ':' + constants.CONFIG.DATABASE.PORT + '/' + constants.CONFIG.DATABASE.DBNAME) | 15 | const password = constants.CONFIG.DATABASE.PASSWORD |
26 | mongoose.connection.on('error', function () { | ||
27 | throw new Error('Mongodb connection error.') | ||
28 | }) | ||
29 | 16 | ||
30 | mongoose.connection.on('open', function () { | 17 | const sequelize = new Sequelize(dbname, username, password, { |
31 | logger.info('Connected to mongodb.') | 18 | dialect: 'postgres', |
32 | }) | 19 | host: constants.CONFIG.DATABASE.HOSTNAME, |
33 | } | 20 | port: constants.CONFIG.DATABASE.PORT, |
21 | benchmark: utils.isTestInstance(), | ||
22 | |||
23 | logging: function (message, benchmark) { | ||
24 | let newMessage = message | ||
25 | if (benchmark !== undefined) { | ||
26 | newMessage += ' | ' + benchmark + 'ms' | ||
27 | } | ||
28 | |||
29 | logger.debug(newMessage) | ||
30 | } | ||
31 | }) | ||
32 | |||
33 | database.sequelize = sequelize | ||
34 | database.Sequelize = Sequelize | ||
35 | database.init = init | ||
34 | 36 | ||
35 | // --------------------------------------------------------------------------- | 37 | // --------------------------------------------------------------------------- |
36 | 38 | ||
37 | module.exports = database | 39 | module.exports = database |
40 | |||
41 | // --------------------------------------------------------------------------- | ||
42 | |||
43 | function init (silent, callback) { | ||
44 | if (!callback) { | ||
45 | callback = silent | ||
46 | silent = false | ||
47 | } | ||
48 | |||
49 | if (!callback) callback = function () {} | ||
50 | |||
51 | const modelDirectory = path.join(__dirname, '..', 'models') | ||
52 | fs.readdir(modelDirectory, function (err, files) { | ||
53 | if (err) throw err | ||
54 | |||
55 | files.filter(function (file) { | ||
56 | // For all models but not utils.js | ||
57 | if (file === 'utils.js') return false | ||
58 | |||
59 | return true | ||
60 | }) | ||
61 | .forEach(function (file) { | ||
62 | const model = sequelize.import(path.join(modelDirectory, file)) | ||
63 | |||
64 | database[model.name] = model | ||
65 | }) | ||
66 | |||
67 | Object.keys(database).forEach(function (modelName) { | ||
68 | if ('associate' in database[modelName]) { | ||
69 | database[modelName].associate(database) | ||
70 | } | ||
71 | }) | ||
72 | |||
73 | if (!silent) logger.info('Database is ready.') | ||
74 | |||
75 | return callback(null) | ||
76 | }) | ||
77 | } | ||