diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-12-11 21:50:51 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-12-19 21:22:28 +0100 |
commit | feb4bdfd9b46e87aadfa7c0d5338cde887d1f58c (patch) | |
tree | 2abc9fbc9569760e218fd52835850b757344b420 /server/initializers/database.js | |
parent | 108626609eda75e4ecc0a83a650a4d53c46220e0 (diff) | |
download | PeerTube-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.js | 58 |
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 | ||
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') |
7 | 9 | ||
8 | // Bootstrap models | 10 | const database = {} |
9 | require('../models/application') | 11 | |
10 | require('../models/oauth-token') | 12 | const sequelize = new Sequelize(constants.CONFIG.DATABASE.DBNAME, 'peertube', 'peertube', { |
11 | require('../models/user') | 13 | dialect: 'postgres', |
12 | require('../models/oauth-client') | 14 | host: constants.CONFIG.DATABASE.HOSTNAME, |
13 | require('../models/video') | 15 | port: constants.CONFIG.DATABASE.PORT |
14 | // Request model needs Video model | 16 | }) |
15 | require('../models/pods') | 17 | |
16 | // Request model needs Pod model | 18 | const modelDirectory = path.join(__dirname, '..', 'models') |
17 | require('../models/request') | 19 | fs.readdir(modelDirectory, function (err, files) { |
18 | 20 | if (err) throw err | |
19 | const database = { | 21 | |
20 | connect: connect | 22 | files.filter(function (file) { |
21 | } | 23 | if (file === 'utils.js') return false |
22 | 24 | ||
23 | function 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 | |||
42 | database.sequelize = sequelize | ||
43 | database.Sequelize = Sequelize | ||
34 | 44 | ||
35 | // --------------------------------------------------------------------------- | 45 | // --------------------------------------------------------------------------- |
36 | 46 | ||