2016-02-07 04:23:23 -06:00
|
|
|
'use strict'
|
2015-06-09 10:41:40 -05:00
|
|
|
|
2016-12-11 14:50:51 -06:00
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
|
|
|
const Sequelize = require('sequelize')
|
2015-06-09 10:41:40 -05:00
|
|
|
|
2016-08-19 14:34:51 -05:00
|
|
|
const constants = require('../initializers/constants')
|
2016-03-16 16:29:27 -05:00
|
|
|
const logger = require('../helpers/logger')
|
2015-06-09 10:41:40 -05:00
|
|
|
|
2016-12-11 14:50:51 -06: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-02-07 04:23:23 -06:00
|
|
|
})
|
2016-12-11 14:50:51 -06:00
|
|
|
.forEach(function (file) {
|
|
|
|
const model = sequelize.import(path.join(modelDirectory, file))
|
2016-01-31 04:23:52 -06:00
|
|
|
|
2016-12-11 14:50:51 -06:00
|
|
|
database[model.name] = model
|
2016-02-07 04:23:23 -06:00
|
|
|
})
|
2016-12-11 14:50:51 -06: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 04:23:52 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
// ---------------------------------------------------------------------------
|
2015-06-09 10:41:40 -05:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
module.exports = database
|