aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/initializers/database.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-01-12 15:20:03 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-01-12 15:20:03 +0100
commit99fe265a5fc077cb66c322e7f3d191ff7110aea0 (patch)
treec9e04ccfcc5496d2300d7c26db5833e494b4cdad /server/initializers/database.js
parentfcc5f77b95d330bfcb439c172b7fcc58f3162e4d (diff)
parent91cc839af88730ba55f84997c56b85ea100070a7 (diff)
downloadPeerTube-99fe265a5fc077cb66c322e7f3d191ff7110aea0.tar.gz
PeerTube-99fe265a5fc077cb66c322e7f3d191ff7110aea0.tar.zst
PeerTube-99fe265a5fc077cb66c322e7f3d191ff7110aea0.zip
Merge branch 'postgresql'
Diffstat (limited to 'server/initializers/database.js')
-rw-r--r--server/initializers/database.js90
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
3const mongoose = require('mongoose') 3const fs = require('fs')
4const path = require('path')
5const Sequelize = require('sequelize')
4 6
5const constants = require('../initializers/constants') 7const constants = require('../initializers/constants')
6const logger = require('../helpers/logger') 8const logger = require('../helpers/logger')
9const utils = require('../helpers/utils')
7 10
8// Bootstrap models 11const database = {}
9require('../models/application')
10require('../models/oauth-token')
11require('../models/user')
12require('../models/oauth-client')
13require('../models/video')
14// Request model needs Video model
15require('../models/pods')
16// Request model needs Pod model
17require('../models/request')
18
19const database = {
20 connect: connect
21}
22 12
23function connect () { 13const dbname = constants.CONFIG.DATABASE.DBNAME
24 mongoose.Promise = global.Promise 14const username = constants.CONFIG.DATABASE.USERNAME
25 mongoose.connect('mongodb://' + constants.CONFIG.DATABASE.HOSTNAME + ':' + constants.CONFIG.DATABASE.PORT + '/' + constants.CONFIG.DATABASE.DBNAME) 15const 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 () { 17const 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
33database.sequelize = sequelize
34database.Sequelize = Sequelize
35database.init = init
34 36
35// --------------------------------------------------------------------------- 37// ---------------------------------------------------------------------------
36 38
37module.exports = database 39module.exports = database
40
41// ---------------------------------------------------------------------------
42
43function 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}