0

I am trying to determine a better way for my query. I have two tables, one is for recipes, and another is for reviews. Currently, I have two separate queries to find the recipe, and then find the reviews associated with the recipe. Is there a better way I could be doing this?

RecipeController

module.exports = {
viewRecipe: function(req, res) {
    var recipeId = parseInt(req.params.id, 10);
    var recipeM = {};
    var reviewM = {};
    db.recipe.find({
        where: {
            id: recipeId
        }
    }).then(function(recipe) {
        recipeM = recipe.dataValues;
        recipeM.ingredients = recipe.ingredients.replace(/\s+/g, '').split(",");
        recipeM.instructions = recipe.instructions.split("\n");
    }, function (e) {
    });
    db.review.findAll({
        where: {
            recipeId: recipeId
        }
    }).then(function(review) {
        console.log(review);
        res.render('viewRecipe', { recipe: recipeM, review: review, categories: categoriesMain, title: recipeM.title });
    }, function(e) {

    });

},
1
  • I suppose Sequelize.Promise could help you group these two parallel queries and wait for them to finish with your final query. Commented Mar 15, 2016 at 20:23

2 Answers 2

1

If you are permitted to use ES6 generator, you can apply co.wrap from npm module co

module.exports = {                                                                                                                        
viewRecipe: co.wrap(function*(req, res) {                                                                                                 
    var recipeId = parseInt(req.params.id, 10);                                                                                           
    var recipeM = {};                                                                                                                     
    var reviewM = {};                                                                                                                     
    var recipe = yield db.recipe.find({                                                                                                   
        where: {                                                                                                                          
            id: recipeId                                                                                                                  
        }                                                                                                                                 
    });                                                                                                                                   

    recipeM = recipe.dataValues;                                                                                                          
    recipeM.ingredients = recipe.ingredients.replace(/\s+/g, '').split(",");                                                              
    recipeM.instructions = recipe.instructions.split("\n");                                                                               

    var review = yield db.review.findAll({                                                                                                
        where: {                                                                                                                          
            recipeId: recipeId                                                                                                            
        }                                                                                                                                 
    });                                                                                                                                   

    console.log(review);                                                                                                                  
    res.render('viewRecipe', { recipe: recipeM, review: review, categories: categoriesMain, title: recipeM.title });                      
}).catch(function(e){                                                                                                                     

}),
Sign up to request clarification or add additional context in comments.

Comments

0

If you have a relationship set up between a recipe and its reviews, I believe you can use Sequelize's include option as follows:

db.recipe
  .findById(req.params.id, {
    include: [{
      model: review
    }]
  })
  .then(function(recipe) {
     // your success code
  })
  .catch(function(err) {
     // you error code
  });

As far as I know, the include option works as a left join. Also, this should perform better since only one query will be ran on the database.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.