SHARE
TWEET

Untitled

a guest Mar 19th, 2019 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require('fs');
  2. const path = require('path');
  3. const Sequelize = require('sequelize');
  4. const basename = path.basename(module.filename);
  5. const env = process.env.NODE_ENV || 'development';
  6. const config = require(`${__dirname}/../config/config.js`)[env];
  7. const db = {};
  8. const databases = Object.keys(config.databases);
  9.  
  10. /** Add Databases**/
  11. for(let i = 0; i < databases.length; ++i) {
  12.     let database = databases[i];
  13.     let dbPath = config.databases[database];
  14.     db[database] = new Sequelize( dbPath.database, dbPath.username, dbPath.password, dbPath );
  15. }
  16.  
  17. /**Add the Database Models**/
  18. //Add models from database1 folder
  19. fs
  20.     .readdirSync(__dirname + '/database1')
  21.     .filter(file =>
  22.         (file.indexOf('.') !== 0) &&
  23.         (file !== basename) &&
  24.         (file.slice(-3) === '.js'))
  25.     .forEach(file => {
  26.         const model = db.ebdb.import(path.join(__dirname + '/database1', file));
  27.         db[model.name] = model;
  28.     });
  29.  
  30.  
  31. // Add models from database2 folder
  32.  
  33. fs
  34.     .readdirSync(__dirname + '/database2')
  35.     .filter(file =>
  36.         (file.indexOf('.') !== 0) &&
  37.         (file !== basename) &&
  38.         (file.slice(-3) === '.js'))
  39.     .forEach(file => {
  40.         const model = db.easyconnect.import(path.join(__dirname + '/database2', file));
  41.         db[model.name] = model;
  42.     });
  43.  
  44. Object.keys(db).forEach(modelName => {
  45.     if (db[modelName].associate) {
  46.         db[modelName].associate(db);
  47.     }
  48. });
  49.  
  50.  
  51. module.exports = db;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top