PeerTube/server/initializers/database.js

48 lines
1.1 KiB
JavaScript
Raw Normal View History

'use strict'
2015-06-09 17:41:40 +02:00
2016-12-11 21:50:51 +01:00
const fs = require('fs')
const path = require('path')
const Sequelize = require('sequelize')
2015-06-09 17:41:40 +02:00
2016-08-19 21:34:51 +02:00
const constants = require('../initializers/constants')
2016-03-16 22:29:27 +01:00
const logger = require('../helpers/logger')
2015-06-09 17:41:40 +02:00
2016-12-11 21:50:51 +01:00
const database = {}
const sequelize = new Sequelize(constants.CONFIG.DATABASE.DBNAME, 'peertube', 'peertube', {
dialect: 'postgres',
host: constants.CONFIG.DATABASE.HOSTNAME,
port: constants.CONFIG.DATABASE.PORT
})
const modelDirectory = path.join(__dirname, '..', 'models')
fs.readdir(modelDirectory, function (err, files) {
if (err) throw err
files.filter(function (file) {
if (file === 'utils.js') return false
return true
})
2016-12-11 21:50:51 +01:00
.forEach(function (file) {
const model = sequelize.import(path.join(modelDirectory, file))
2016-01-31 11:23:52 +01:00
2016-12-11 21:50:51 +01:00
database[model.name] = model
})
2016-12-11 21:50:51 +01:00
Object.keys(database).forEach(function (modelName) {
if ('associate' in database[modelName]) {
database[modelName].associate(database)
}
})
logger.info('Database is ready.')
})
database.sequelize = sequelize
database.Sequelize = Sequelize
2016-01-31 11:23:52 +01:00
// ---------------------------------------------------------------------------
2015-06-09 17:41:40 +02:00
module.exports = database