aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/initializers/database.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-05-15 22:22:03 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-05-20 09:57:40 +0200
commit65fcc3119c334b75dd13bcfdebf186afdc580a8f (patch)
tree4f2158c61a9b7c3f47cfa233d01413b946ee53c0 /server/initializers/database.js
parentd5f345ed4cfac4e1fa84dcb4fce1cda4d32f9c73 (diff)
downloadPeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.gz
PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.zst
PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.zip
First typescript iteration
Diffstat (limited to 'server/initializers/database.js')
-rw-r--r--server/initializers/database.js77
1 files changed, 0 insertions, 77 deletions
diff --git a/server/initializers/database.js b/server/initializers/database.js
deleted file mode 100644
index 043152a0e..000000000
--- a/server/initializers/database.js
+++ /dev/null
@@ -1,77 +0,0 @@
1'use strict'
2
3const fs = require('fs')
4const path = require('path')
5const Sequelize = require('sequelize')
6
7const constants = require('../initializers/constants')
8const logger = require('../helpers/logger')
9const utils = require('../helpers/utils')
10
11const database = {}
12
13const dbname = constants.CONFIG.DATABASE.DBNAME
14const username = constants.CONFIG.DATABASE.USERNAME
15const password = constants.CONFIG.DATABASE.PASSWORD
16
17const sequelize = new Sequelize(dbname, username, password, {
18 dialect: 'postgres',
19 host: constants.CONFIG.DATABASE.HOSTNAME,
20 port: constants.CONFIG.DATABASE.PORT,
21 benchmark: utils.isTestInstance(),
22
23 logging: function (message, benchmark) {
24 let newMessage = message
25 if (benchmark !== undefined) {
26 newMessage += ' | ' + benchmark + 'ms'
27 }
28
29 logger.debug(newMessage)
30 }
31})
32
33database.sequelize = sequelize
34database.Sequelize = Sequelize
35database.init = init
36
37// ---------------------------------------------------------------------------
38
39module.exports = database
40
41// ---------------------------------------------------------------------------
42
43function init (silent, callback) {
44 if (!callback) {
45 callback = silent
46 silent = false
47 }
48
49 if (!callback) callback = function () {}
50
51 const modelDirectory = path.join(__dirname, '..', 'models')
52 fs.readdir(modelDirectory, function (err, files) {
53 if (err) throw err
54
55 files.filter(function (file) {
56 // For all models but not utils.js
57 if (file === 'utils.js') return false
58
59 return true
60 })
61 .forEach(function (file) {
62 const model = sequelize.import(path.join(modelDirectory, file))
63
64 database[model.name] = model
65 })
66
67 Object.keys(database).forEach(function (modelName) {
68 if ('associate' in database[modelName]) {
69 database[modelName].associate(database)
70 }
71 })
72
73 if (!silent) logger.info('Database %s is ready.', dbname)
74
75 return callback(null)
76 })
77}