Advertisement
KoctrX

Untitled

Jun 13th, 2024
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. const fs = require('fs');
  4. const path = require('path');
  5. const Sequelize = require('sequelize');
  6. const process = require('process');
  7. const basename = path.basename(__filename);
  8. const env = process.env.NODE_ENV || 'development';
  9. const config = require(__dirname + '/../config/config.json')[env];
  10. const db = {};
  11.  
  12. let sequelize;
  13. if (config.use_env_variable) {
  14.   sequelize = new Sequelize(process.env[config.use_env_variable], config);
  15. } else {
  16.   sequelize = new Sequelize(config.database, config.username, config.password, config);
  17. }
  18.  
  19. fs
  20.   .readdirSync(__dirname)
  21.   .filter(file => {
  22.     return (
  23.       file.indexOf('.') !== 0 &&
  24.       file !== basename &&
  25.       file.slice(-3) === '.js' &&
  26.       file.indexOf('.test.js') === -1
  27.     );
  28.   })
  29.   .forEach(file => {
  30.     const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
  31.     db[model.name] = model;
  32.   });
  33.  
  34. Object.keys(db).forEach(modelName => {
  35.   if (db[modelName].associate) {
  36.     db[modelName].associate(db);
  37.   }
  38. });
  39.  
  40. db.sequelize = sequelize;
  41. db.Sequelize = Sequelize;
  42.  
  43. module.exports = db;
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement