db.js 1.23 KB
const Sequelize = require('sequelize');

// Models
//const ProductFields = require('../app/models/product-fields');

let db = {

  init: function (dbOptions) {

    const options = dbOptions || {};

    // Connect to db
    const sequelize = new Sequelize(options.name, options.user, options.password, {
      host: options.host,
      dialect: 'mysql',
      pool: {
        max: 1,
        min: 0,
        idle: 10000
      }
    });

    db.sequelize = sequelize;
    db.models = {
      //'ProductFields': sequelize.define(ProductFields.name, ProductFields.model.schema, ProductFields.model.options)
    };

    /** Associations and Relations **/
    //db.models.Broker.hasMany(db.models.Product);
    //db.models.Product.belongsTo(db.models.InsuranceCompany);

    // Join tables
    /*db.models.Customer.belongsToMany(db.models.Product, {
      through: {
        model: db.models.Order,
        unique: false
      }
    });
    db.models.Product.belongsToMany(db.models.Customer, {
      through: {
        model: db.models.Order,
        unique: false
      }
    });*/

    // Sync models and config to db
    sequelize.sync().then(function () {

      console.log('Tables have been synced');
      return db;
    });
  }
};

module.exports = db;