title: How to filter through 3 table joins using sequelize in expressjs api
body: <p>I have 3 tables named:</p>
  <pre class="lang-js prettyprint-override"><code>//restaurants
  columns ( id, name, restaurant_type_id(FK)
  //restaurant_branches 
  columns ( id, name, restaurant_id(FK)
  //restaurant_types
  columns ( id, restaurant_type_name('italian', 'french'...etc))
  </code></pre>
  <p>I would like to filter restaurant_branches by restaurant_type_id using query params in my restaurant_branches.findAll(); action in the controller as the following.</p>
  <pre class="lang-js prettyprint-override"><code>const findAll = async (req, res) =&gt; {
      let RestaurantTypeId= req.query.restaurantType ? parseInt(req.query.restaurantType): null ;
      var type = RestaurantTypeId ? {where:{ restaurantTypeId: RestaurantTypeId }} : null ;
      console.log(RestaurantTypeId); 
      
      await RestaurantBranch.findAll({
          order: [
              ['id', 'ASC']
          ],
  
          include: 
          [
               {
                    model: Restaurant, 
                    type,
                    include: [{
                       model: RestaurantType,
                   }
                   ]
               }
                  
              ]
      }).then((restaurantBranches) =&gt; {
          return res.status(200).send({
              message: &quot;restaurant branches returned&quot;, 
              data: restaurantBranches
          })
      })
      .catch((error) =&gt; {res.status(500).send(error.message);});
  
  }
  module.exports = {
      findAll
  }
  </code></pre>
  <pre><code>//Sequelize Associations
  db.RestaurantType.hasMany(db.Restaurant);
  db.Restaurant.belongsTo(db.RestaurantType);
  // Restaurant / Restaurant Branches
  db.RestaurantBranch.belongsTo(db.Restaurant);
  db.Restaurant.hasMany(db.RestaurantBranch);
  
  </code></pre>
  <pre><code>Sequelize log: 
  Executing (default): SELECT &quot;restaurant_branches&quot;.&quot;id&quot;, &quot;restaurant_branches&quot;.&quot;name&quot;, &quot;restaurant_branches&quot;.&quot;description&quot;, &quot;restaurant_branches&quot;.&quot;email&quot;, &quot;restaurant_branches&quot;.&quot;phoneNumber&quot;, &quot;restaurant_branches&quot;.&quot;address&quot;, &quot;restaurant_branches&quot;.&quot;country_code&quot;, &quot;restaurant_branches&quot;.&quot;image&quot;, &quot;restaurant_branches&quot;.&quot;latitude&quot;, &quot;restaurant_branches&quot;.&quot;longitude&quot;, &quot;restaurant_branches&quot;.&quot;workingHours&quot;, &quot;restaurant_branches&quot;.&quot;workingDays&quot;, &quot;restaurant_branches&quot;.&quot;offDays&quot;, &quot;restaurant_branches&quot;.&quot;locationAddress&quot;, &quot;restaurant_branches&quot;.&quot;locationCity&quot;, &quot;restaurant_branches&quot;.&quot;status&quot;, &quot;restaurant_branches&quot;.&quot;hasParking&quot;, &quot;restaurant_branches&quot;.&quot;instruction&quot;, &quot;restaurant_branches&quot;.&quot;isActive&quot;, &quot;restaurant_branches&quot;.&quot;isDeleted&quot;, &quot;restaurant_branches&quot;.&quot;createdAt&quot;, &quot;restaurant_branches&quot;.&quot;updatedAt&quot;, &quot;restaurant_branches&quot;.&quot;restaurantId&quot;, &quot;restaurant_branches&quot;.&quot;cityId&quot;, &quot;restaurant_branches&quot;.&quot;districtId&quot;, &quot;city&quot;.&quot;id&quot; AS &quot;city.id&quot;, &quot;city&quot;.&quot;name&quot; AS &quot;city.name&quot;, &quot;city&quot;.&quot;code&quot; AS &quot;city.code&quot;, &quot;city&quot;.&quot;status&quot; AS &quot;city.status&quot;, &quot;city&quot;.&quot;isDeleted&quot; AS &quot;city.isDeleted&quot;, &quot;city&quot;.&quot;createdAt&quot; AS &quot;city.createdAt&quot;, &quot;city&quot;.&quot;updatedAt&quot; AS &quot;city.updatedAt&quot;, &quot;city&quot;.&quot;countryId&quot; AS &quot;city.countryId&quot;, &quot;district&quot;.&quot;id&quot; AS &quot;district.id&quot;, &quot;district&quot;.&quot;name&quot; AS &quot;district.name&quot;, &quot;district&quot;.&quot;isDeleted&quot; AS &quot;district.isDeleted&quot;, &quot;district&quot;.&quot;createdAt&quot; AS &quot;district.createdAt&quot;, &quot;district&quot;.&quot;updatedAt&quot; AS &quot;district.updatedAt&quot;, &quot;district&quot;.&quot;cityId&quot; AS &quot;district.cityId&quot;, &quot;restaurant&quot;.&quot;id&quot; AS &quot;restaurant.id&quot;, &quot;restaurant&quot;.&quot;name&quot; AS &quot;restaurant.name&quot;, &quot;restaurant&quot;.&quot;aboutUs&quot; AS &quot;restaurant.aboutUs&quot;, &quot;restaurant&quot;.&quot;phoneNumber&quot; AS &quot;restaurant.phoneNumber&quot;, &quot;restaurant&quot;.&quot;address&quot; AS &quot;restaurant.address&quot;, &quot;restaurant&quot;.&quot;latitude&quot; AS &quot;restaurant.latitude&quot;, &quot;restaurant&quot;.&quot;longitude&quot; AS &quot;restaurant.longitude&quot;, &quot;restaurant&quot;.&quot;image&quot; AS &quot;restaurant.image&quot;, &quot;restaurant&quot;.&quot;countryCode&quot; AS &quot;restaurant.countryCode&quot;, &quot;restaurant&quot;.&quot;restaurantRegisterDocument&quot; AS &quot;restaurant.restaurantRegisterDocument&quot;, &quot;restaurant&quot;.&quot;isDeleted&quot; AS &quot;restaurant.isDeleted&quot;, &quot;restaurant&quot;.&quot;createdAt&quot; AS &quot;restaurant.createdAt&quot;, &quot;restaurant&quot;.&quot;updatedAt&quot; AS &quot;restaurant.updatedAt&quot;, &quot;restaurant&quot;.&quot;restaurantTypeId&quot; AS &quot;restaurant.restaurantTypeId&quot;, &quot;restaurant&quot;.&quot;categoryId&quot; AS &quot;restaurant.categoryId&quot;, &quot;restaurant&quot;.&quot;userId&quot; AS &quot;restaurant.userId&quot;, &quot;restaurant-&gt;restaurant_type&quot;.&quot;id&quot; AS &quot;restaurant.restaurant_type.id&quot;, &quot;restaurant-&gt;restaurant_type&quot;.&quot;name&quot; AS &quot;restaurant.restaurant_type.name&quot;, &quot;restaurant-&gt;restaurant_type&quot;.&quot;photo&quot; AS &quot;restaurant.restaurant_type.photo&quot;, &quot;restaurant-&gt;restaurant_type&quot;.&quot;createdAt&quot; AS &quot;restaurant.restaurant_type.createdAt&quot;, &quot;restaurant-&gt;restaurant_type&quot;.&quot;updatedAt&quot; AS &quot;restaurant.restaurant_type.updatedAt&quot; FROM &quot;restaurant_branches&quot; AS &quot;restaurant_branches&quot; LEFT OUTER JOIN &quot;cities&quot; AS &quot;city&quot; ON &quot;restaurant_branches&quot;.&quot;cityId&quot; = &quot;city&quot;.&quot;id&quot; LEFT OUTER JOIN &quot;districts&quot; AS &quot;district&quot; ON &quot;restaurant_branches&quot;.&quot;districtId&quot; = &quot;district&quot;.&quot;id&quot; LEFT OUTER JOIN &quot;restaurants&quot; AS &quot;restaurant&quot; ON &quot;restaurant_branches&quot;.&quot;restaurantId&quot; = &quot;restaurant&quot;.&quot;id&quot; LEFT OUTER JOIN &quot;restaurant_types&quot; AS &quot;restaurant-&gt;restaurant_type&quot; ON &quot;restaurant&quot;.&quot;restaurantTypeId&quot; = &quot;restaurant-&gt;restaurant_type&quot;.&quot;id&quot; ORDER BY &quot;restaurant_branches&quot;.&quot;id&quot; ASC;
  </code></pre>
  <p>so far I'm doing this, and I get All the restaurant branches if I GET request to this URL:</p>
  <p><code>{{URL}}/restaurant_branches?restaurantType=2</code></p>
  <p>what I'd like to be getting instead is all the restaurant branches whom their restaurants belong to the restaurant type with id 2</p>
  <p>Any help or guidance is highly appreciated.</p>