aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/initializers/database.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-12-11 21:50:51 +0100
committerChocobozzz <florian.bigard@gmail.com>2016-12-19 21:22:28 +0100
commitfeb4bdfd9b46e87aadfa7c0d5338cde887d1f58c (patch)
tree2abc9fbc9569760e218fd52835850b757344b420 /server/initializers/database.js
parent108626609eda75e4ecc0a83a650a4d53c46220e0 (diff)
downloadPeerTube-feb4bdfd9b46e87aadfa7c0d5338cde887d1f58c.tar.gz
PeerTube-feb4bdfd9b46e87aadfa7c0d5338cde887d1f58c.tar.zst
PeerTube-feb4bdfd9b46e87aadfa7c0d5338cde887d1f58c.zip
First version with PostgreSQL
Diffstat (limited to 'server/initializers/database.js')
-rw-r--r--server/initializers/database.js58
1 files changed, 34 insertions, 24 deletions
diff --git a/server/initializers/database.js b/server/initializers/database.js
index 0564e4e77..cc6f59b63 100644
--- a/server/initializers/database.js
+++ b/server/initializers/database.js
@@ -1,36 +1,46 @@
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')
7 9
8// Bootstrap models 10const database = {}
9require('../models/application') 11
10require('../models/oauth-token') 12const sequelize = new Sequelize(constants.CONFIG.DATABASE.DBNAME, 'peertube', 'peertube', {
11require('../models/user') 13 dialect: 'postgres',
12require('../models/oauth-client') 14 host: constants.CONFIG.DATABASE.HOSTNAME,
13require('../models/video') 15 port: constants.CONFIG.DATABASE.PORT
14// Request model needs Video model 16})
15require('../models/pods') 17
16// Request model needs Pod model 18const modelDirectory = path.join(__dirname, '..', 'models')
17require('../models/request') 19fs.readdir(modelDirectory, function (err, files) {
18 20 if (err) throw err
19const database = { 21
20 connect: connect 22 files.filter(function (file) {
21} 23 if (file === 'utils.js') return false
22 24
23function connect () { 25 return true
24 mongoose.Promise = global.Promise
25 mongoose.connect('mongodb://' + constants.CONFIG.DATABASE.HOSTNAME + ':' + constants.CONFIG.DATABASE.PORT + '/' + constants.CONFIG.DATABASE.DBNAME)
26 mongoose.connection.on('error', function () {
27 throw new Error('Mongodb connection error.')
28 }) 26 })
27 .forEach(function (file) {
28 const model = sequelize.import(path.join(modelDirectory, file))
29 29
30 mongoose.connection.on('open', function () { 30 database[model.name] = model
31 logger.info('Connected to mongodb.')
32 }) 31 })
33} 32
33 Object.keys(database).forEach(function (modelName) {
34 if ('associate' in database[modelName]) {
35 database[modelName].associate(database)
36 }
37 })
38
39 logger.info('Database is ready.')
40})
41
42database.sequelize = sequelize
43database.Sequelize = Sequelize
34 44
35// --------------------------------------------------------------------------- 45// ---------------------------------------------------------------------------
36 46