title: How to query many to many relationship sequelize?
body: <p>Tables have many to many relationship, junction by an order table in between. </p>
  
  <p>Outlet --> Order &lt;-- Product</p>
  
  <p>I want to get the list of Outlet for today Order. </p>
  
  <p>So here is a function to get all outlets:</p>
  
  <pre><code>db.Outlet.findAll({include: [
      {model:db.Product, attributes: ['id', 'name', 'nameKh']}
      ]}).then(function(outlets){
      return res.jsonp(outlets);
  })
  </code></pre>
  
  <p>I got this result:</p>
  
  <p><a href="https://i.stack.imgur.com/P6gx9.png" rel="noreferrer"><img src="https://i.stack.imgur.com/P6gx9.png" alt="enter image description here"></a></p>
  
  <p>I can only select with where Product Id by using this:</p>
  
  <pre><code>db.Outlet.findAll({include: [
      {model:db.Product, attributes: ['id', 'name', 'nameKh'], where: {id: 2}
      ]}).then(function(outlets){
      return res.jsonp(outlets);
  })
  </code></pre>
  
  <p>How can I query by specific order amount, or today order date? </p>
  
  <p>Here are my models:</p>
  
  <p>Outlet:</p>
  
  <pre><code>var Outlet = sequelize.define('Outlet', {
              outletCode: DataTypes.STRING,
              outletName: DataTypes.STRING,
              outletNameKh: DataTypes.STRING,
              outletSubtype: DataTypes.STRING,
              perfectStoreType: DataTypes.STRING,
              address: DataTypes.STRING
          },
          {
              associate: function(models){
                  Outlet.belongsToMany(models.Product, {through: models.Order});
                  Outlet.belongsTo(models.Distributor);
                  // Outlet.hasMany(models.Order);
              }
          }
      );
  </code></pre>
  
  <p>Product:</p>
  
  <pre><code>var Product = sequelize.define('Product', {
              inventoryCode: DataTypes.STRING,
              name: DataTypes.STRING,
              nameKh: DataTypes.STRING,
              monthlyCaseTarget: DataTypes.INTEGER,
              pieces: DataTypes.INTEGER,
              star: DataTypes.BOOLEAN,
              price: DataTypes.FLOAT,
              active: DataTypes.BOOLEAN
          },
          {
              associate: function(models){
                  Product.belongsToMany(models.Outlet, {through: models.Order});
                  Product.belongsTo(models.Category);
                  // Product.hasMany(models.Order);
              }
          }
      );
  </code></pre>
  
  <p>Order:</p>
  
  <pre><code>var Order = sequelize.define('Order', {
              id: {
                  type: DataTypes.INTEGER,
                  primaryKey: true,
                  autoIncrement: true
              },
              amount: DataTypes.INTEGER
          },
          {
              associate: function(models){
                  Order.belongsTo(models.Outlet);
                  Order.belongsTo(models.Product);
                  Order.belongsTo(models.User);
              }
          }
      );
  </code></pre>