title: Create a nested return model with Knex.js
body: <p>I'm using Knex.js to query a MySQL database in a Hapi.js route. The following code works but requires a nested query:</p>
  
  <pre><code>{
      path: '/recipes',
      method: 'GET',
      handler: (req, res) =&gt; {
          const getOperation = Knex.from('recipes')
          // .innerJoin('ingredients', 'recipes.guid', 'ingredients.recipe')
          .select()
          .orderBy('rating', 'desc')
          .limit(10)
          .then((recipes) =&gt; {
              if (!recipes || recipes.length === 0) {
                  res({
                      error: true,
                      errMessage: 'no recipes found'
                  });
              }
  
              const recipeGuids = recipes.map(recipe =&gt; recipe.guid);
              recipes.forEach(r =&gt; r.ingredients = []);
              const getOperation2 = Knex.from('ingredients')
                  .whereIn('recipe', recipeGuids)
                  .select()
                  .then((ingredients) =&gt; {
                      recipes.forEach(r =&gt; {
                          ingredients.forEach(i =&gt; {
                              if (i.recipe === r.guid) {
                                  r.ingredients.push(i);
                              }
                          });
                      });
                      res({
                          count: recipes.length,
                          data: recipes
                      });
                  });
          });
      }
  }
  </code></pre>
  
  <p>Is there a way to create a return model with Knex.js that has nested objects that match the parent's id/guid so that I don't have nested promises?</p>