diff options
Diffstat (limited to 'server/initializers/database.js')
-rw-r--r-- | server/initializers/database.js | 65 |
1 files changed, 42 insertions, 23 deletions
diff --git a/server/initializers/database.js b/server/initializers/database.js index 9642231b9..f8f68adeb 100644 --- a/server/initializers/database.js +++ b/server/initializers/database.js | |||
@@ -10,7 +10,11 @@ const utils = require('../helpers/utils') | |||
10 | 10 | ||
11 | const database = {} | 11 | const database = {} |
12 | 12 | ||
13 | const sequelize = new Sequelize(constants.CONFIG.DATABASE.DBNAME, 'peertube', 'peertube', { | 13 | const dbname = constants.CONFIG.DATABASE.DBNAME |
14 | const username = constants.CONFIG.DATABASE.USERNAME | ||
15 | const password = constants.CONFIG.DATABASE.PASSWORD | ||
16 | |||
17 | const sequelize = new Sequelize(dbname, username, password, { | ||
14 | dialect: 'postgres', | 18 | dialect: 'postgres', |
15 | host: constants.CONFIG.DATABASE.HOSTNAME, | 19 | host: constants.CONFIG.DATABASE.HOSTNAME, |
16 | port: constants.CONFIG.DATABASE.PORT, | 20 | port: constants.CONFIG.DATABASE.PORT, |
@@ -26,33 +30,48 @@ const sequelize = new Sequelize(constants.CONFIG.DATABASE.DBNAME, 'peertube', 'p | |||
26 | } | 30 | } |
27 | }) | 31 | }) |
28 | 32 | ||
29 | const modelDirectory = path.join(__dirname, '..', 'models') | 33 | database.sequelize = sequelize |
30 | fs.readdir(modelDirectory, function (err, files) { | 34 | database.Sequelize = Sequelize |
31 | if (err) throw err | 35 | database.init = init |
32 | 36 | ||
33 | files.filter(function (file) { | 37 | // --------------------------------------------------------------------------- |
34 | if (file === 'utils.js') return false | ||
35 | 38 | ||
36 | return true | 39 | module.exports = database |
37 | }) | ||
38 | .forEach(function (file) { | ||
39 | const model = sequelize.import(path.join(modelDirectory, file)) | ||
40 | 40 | ||
41 | database[model.name] = model | 41 | // --------------------------------------------------------------------------- |
42 | }) | ||
43 | 42 | ||
44 | Object.keys(database).forEach(function (modelName) { | 43 | function init (silent, callback) { |
45 | if ('associate' in database[modelName]) { | 44 | if (!callback) { |
46 | database[modelName].associate(database) | 45 | callback = silent |
47 | } | 46 | silent = false |
48 | }) | 47 | } |
49 | 48 | ||
50 | logger.info('Database is ready.') | 49 | if (!callback) callback = function () {} |
51 | }) | ||
52 | 50 | ||
53 | database.sequelize = sequelize | 51 | const modelDirectory = path.join(__dirname, '..', 'models') |
54 | database.Sequelize = Sequelize | 52 | fs.readdir(modelDirectory, function (err, files) { |
53 | if (err) throw err | ||
55 | 54 | ||
56 | // --------------------------------------------------------------------------- | 55 | files.filter(function (file) { |
56 | // For all models but not utils.js | ||
57 | if (file === 'utils.js') return false | ||
57 | 58 | ||
58 | module.exports = database | 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 is ready.') | ||
74 | |||
75 | return callback(null) | ||
76 | }) | ||
77 | } | ||