2019-06-06 23:54:32 +02:00
|
|
|
'use strict';
|
2019-04-03 00:25:12 +02:00
|
|
|
|
2019-06-06 23:54:32 +02:00
|
|
|
const fs = require('fs');
|
|
|
|
|
const path = require('path');
|
|
|
|
|
const Sequelize = require('sequelize');
|
|
|
|
|
const basename = path.basename(__filename);
|
|
|
|
|
const config = require(__dirname + '/../../config.js').SECRET_CONF.db
|
|
|
|
|
const db = {};
|
|
|
|
|
|
|
|
|
|
let sequelize = new Sequelize(config);
|
2019-04-03 00:25:12 +02:00
|
|
|
|
|
|
|
|
fs
|
|
|
|
|
.readdirSync(__dirname)
|
|
|
|
|
.filter(file => {
|
2019-06-06 23:54:32 +02:00
|
|
|
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
|
2019-04-03 00:25:12 +02:00
|
|
|
})
|
|
|
|
|
.forEach(file => {
|
2019-06-06 23:54:32 +02:00
|
|
|
const model = sequelize['import'](path.join(__dirname, file));
|
2019-04-03 00:25:12 +02:00
|
|
|
db[model.name] = model;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Object.keys(db).forEach(modelName => {
|
|
|
|
|
if (db[modelName].associate) {
|
2019-06-06 23:54:32 +02:00
|
|
|
db[modelName].associate(db);
|
2019-04-03 00:25:12 +02:00
|
|
|
}
|
2019-06-06 23:54:32 +02:00
|
|
|
});
|
2019-04-03 00:25:12 +02:00
|
|
|
|
2019-06-06 23:54:32 +02:00
|
|
|
db.sequelize = sequelize;
|
|
|
|
|
db.Sequelize = Sequelize;
|
2019-04-03 00:25:12 +02:00
|
|
|
|
|
|
|
|
module.exports = db;
|