]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/initializers/database.js
3 const fs
= require('fs')
4 const path
= require('path')
5 const Sequelize
= require('sequelize')
7 const constants
= require('../initializers/constants')
8 const logger
= require('../helpers/logger')
9 const utils
= require('../helpers/utils')
13 const dbname
= constants
.CONFIG
.DATABASE
.DBNAME
14 const username
= constants
.CONFIG
.DATABASE
.USERNAME
15 const password
= constants
.CONFIG
.DATABASE
.PASSWORD
17 const sequelize
= new Sequelize(dbname
, username
, password
, {
19 host: constants
.CONFIG
.DATABASE
.HOSTNAME
,
20 port: constants
.CONFIG
.DATABASE
.PORT
,
21 benchmark: utils
.isTestInstance(),
23 logging: function (message
, benchmark
) {
24 let newMessage
= message
25 if (benchmark
!== undefined) {
26 newMessage
+= ' | ' + benchmark
+ 'ms'
29 logger
.debug(newMessage
)
33 database
.sequelize
= sequelize
34 database
.Sequelize
= Sequelize
37 // ---------------------------------------------------------------------------
39 module
.exports
= database
41 // ---------------------------------------------------------------------------
43 function init (silent
, callback
) {
49 if (!callback
) callback = function () {}
51 const modelDirectory
= path
.join(__dirname
, '..', 'models')
52 fs
.readdir(modelDirectory
, function (err
, files
) {
55 files
.filter(function (file
) {
56 // For all models but not utils.js
57 if (file
=== 'utils.js') return false
61 .forEach(function (file
) {
62 const model
= sequelize
.import(path
.join(modelDirectory
, file
))
64 database
[model
.name
] = model
67 Object
.keys(database
).forEach(function (modelName
) {
68 if ('associate' in database
[modelName
]) {
69 database
[modelName
].associate(database
)
73 if (!silent
) logger
.info('Database %s is ready.', dbname
)