index.js 990 B

123456789101112131415161718192021222324252627282930313233343536
  1. const
  2. fs = require('fs'),
  3. Sequelize = require('sequelize');
  4. // create Sequelize instance
  5. const sequelize = new Sequelize('w4aclem', 'clement.krebs', 'n4or2bgm', {
  6. host: 'localhost',
  7. port: 2080,
  8. dialect: 'mysql',
  9. dialectOptions: { decimalNumbers: true }
  10. });
  11. // this object will contain the model objects
  12. // each key being the model's name
  13. const db = {};
  14. // read the files of the current directory
  15. fs.readdirSync(__dirname)
  16. .filter((filename) => filename !== 'index.js') // avoid this file
  17. .forEach((filename) => {
  18. const model = require('./' + filename)(sequelize); // get the model definition
  19. db[model.name] = model; // add the entry in the db object
  20. });
  21. // go through each entry of the db object
  22. Object.keys(db).forEach((modelName) => {
  23. // call the "associate" function on the model object
  24. // and pass it the db object (so that it can have access to other models)
  25. db[modelName].associate(db);
  26. });
  27. // sync the DB
  28. sequelize.sync();
  29. // expose the db object
  30. module.exports = db;