使用 node.js 对查询进行序列化
Sequelize queries with node.js
我正在尝试为我的查询确定更好的方法。我有两张桌子,一张是食谱,另一张是评论。目前,我有两个单独的查询来查找食谱,然后查找与该食谱相关的评论。有没有更好的方法可以做到这一点?
配方控制器
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) {
});
},
如果您被允许使用 ES6 生成器,您可以从 npm 模块 co
申请 co.wrap
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){
}),
如果你在食谱和评论之间建立了关系,我相信你可以使用 Sequelize 的包含选项,如下所示:
db.recipe
.findById(req.params.id, {
include: [{
model: review
}]
})
.then(function(recipe) {
// your success code
})
.catch(function(err) {
// you error code
});
据我所知,include 选项用作左连接。此外,这应该会执行得更好,因为数据库上只有一个查询 运行。
我正在尝试为我的查询确定更好的方法。我有两张桌子,一张是食谱,另一张是评论。目前,我有两个单独的查询来查找食谱,然后查找与该食谱相关的评论。有没有更好的方法可以做到这一点?
配方控制器
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) {
});
},
如果您被允许使用 ES6 生成器,您可以从 npm 模块 co
申请 co.wrapmodule.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){
}),
如果你在食谱和评论之间建立了关系,我相信你可以使用 Sequelize 的包含选项,如下所示:
db.recipe
.findById(req.params.id, {
include: [{
model: review
}]
})
.then(function(recipe) {
// your success code
})
.catch(function(err) {
// you error code
});
据我所知,include 选项用作左连接。此外,这应该会执行得更好,因为数据库上只有一个查询 运行。