你如何使用 knexjs 查询连接表?
How do you Query Junction tables using knexjs?
我正在使用(NodeJS、ExpressJS、KnexJS 和 Postgres)建立一个基本博客 API。每个博客都有很多类别。因此,当我查询一个博客时,我也会得到每个博客的类别。同时每个类别可以有很多博客。我可能想按类别查询博客 post。所以我设置了 3 tables:
博客 TABLE () 列 {id, title, description, slug}
类别TABLE列{id,name,slug}
CatBlog TABLE(连接点 table)列 {cat_id, blog_id}
我查询博客的时候post我也想有一个分类数组
{
"id": 14,
"title": "Title for a recipe coming soon",
"description":"",
"slug": "title-for-a-recipe-coming-soon",
"categories": [
{
"id": 6,
"name": "Business",
"slug": "business"
},
{
"id": 7,
"name": "Marketing",
"slug": "marketing"
},
{
"id": 8,
"name": "Chef",
"slug": "chef"
}
]
}
我正在使用 KnexJS 来处理我的查询,但我没有看到任何关于如何处理联结的信息 table。
我如何使用 CatBlog table 从类别 table 添加类别以获取它们?
我不确定这是否是最佳答案,但我能够进行查询
//Run the select on the table I am attempting to query to get all the data I need.
knex.select('*').from('Blog')
.then(function(data){
//Once I have the data run a then with the array so that I can iterate over each item.
data.map(function(item){
//For each Item I am querying the CatBlog table and then if the item.id matches the blog_id I will grab that cat_id. Once I am inside the this query I also run a join to add the categories table on each cat_id that I find. and then lastly I can also select the columns I would like to display.
return knex('CatBlog').whereIn('blog_id',item.id).join('Categories', 'cat_id','=','Categories.id').select('id','name','slug')
//I take that data and pass a new key for each item called categories. Each key has a value of the query.
.then(function(data){
item['categories'] = data;
});
});
res.json(data);
})
所以我做的第一件事就是查询 table 我想显示的,所以如果我想要有很多类别的博客,我会查询这些博客。如果我想要包含很多博客的类别,我会查询类别。
接下来,我查询交汇点 table 以获取关系。因为我在博客 table 中,所以我与 blog_id 进行比较以找到我需要的类别 ID。最后,我 运行 加入了 id 和 select 我想显示的列。
不确定这段代码是否有效,但这是我的方法
我建议您使用 underscore/lodash 和一些 control-flow 模块,例如 asyncjs。
function loadBlogPosts(callback){
// fetching 10 blogpost
knex("blog").select("*").limit(10)
.then(function(posts){
callback(null,posts)
})
.catch(function(err){
callback(err);
})
}
function loadCategories(posts,callback){
// making an array from post ids
var postIds = _.pluck(posts,'id');
// join with blog_category table
knex("category").join("blog_category","category.id","blog_category.cat_id").whereIn({"blog_category.blog_id":postIds}).select("category.*","blog_category.blog_is as blog_id")
.then(function(categories){
callback(null,posts,categories)
})
.catch(function(err){
callback(err);
})
}
async.waterfall([loadBlogPosts,loadCategories],function(err,posts,categories){
var post_list = _.map(posts,function(post){
post.categories = _.find(categories,{blog_id: post.id});
})
})
在这个例子中,你只发送请求 2 查询(1 个用于帖子,1 个用于类别),这在性能上要好得多。
我正在使用(NodeJS、ExpressJS、KnexJS 和 Postgres)建立一个基本博客 API。每个博客都有很多类别。因此,当我查询一个博客时,我也会得到每个博客的类别。同时每个类别可以有很多博客。我可能想按类别查询博客 post。所以我设置了 3 tables:
博客 TABLE () 列 {id, title, description, slug}
类别TABLE列{id,name,slug}
CatBlog TABLE(连接点 table)列 {cat_id, blog_id}
我查询博客的时候post我也想有一个分类数组
{
"id": 14,
"title": "Title for a recipe coming soon",
"description":"",
"slug": "title-for-a-recipe-coming-soon",
"categories": [
{
"id": 6,
"name": "Business",
"slug": "business"
},
{
"id": 7,
"name": "Marketing",
"slug": "marketing"
},
{
"id": 8,
"name": "Chef",
"slug": "chef"
}
]
}
我正在使用 KnexJS 来处理我的查询,但我没有看到任何关于如何处理联结的信息 table。 我如何使用 CatBlog table 从类别 table 添加类别以获取它们?
我不确定这是否是最佳答案,但我能够进行查询
//Run the select on the table I am attempting to query to get all the data I need.
knex.select('*').from('Blog')
.then(function(data){
//Once I have the data run a then with the array so that I can iterate over each item.
data.map(function(item){
//For each Item I am querying the CatBlog table and then if the item.id matches the blog_id I will grab that cat_id. Once I am inside the this query I also run a join to add the categories table on each cat_id that I find. and then lastly I can also select the columns I would like to display.
return knex('CatBlog').whereIn('blog_id',item.id).join('Categories', 'cat_id','=','Categories.id').select('id','name','slug')
//I take that data and pass a new key for each item called categories. Each key has a value of the query.
.then(function(data){
item['categories'] = data;
});
});
res.json(data);
})
所以我做的第一件事就是查询 table 我想显示的,所以如果我想要有很多类别的博客,我会查询这些博客。如果我想要包含很多博客的类别,我会查询类别。
接下来,我查询交汇点 table 以获取关系。因为我在博客 table 中,所以我与 blog_id 进行比较以找到我需要的类别 ID。最后,我 运行 加入了 id 和 select 我想显示的列。
不确定这段代码是否有效,但这是我的方法
我建议您使用 underscore/lodash 和一些 control-flow 模块,例如 asyncjs。
function loadBlogPosts(callback){
// fetching 10 blogpost
knex("blog").select("*").limit(10)
.then(function(posts){
callback(null,posts)
})
.catch(function(err){
callback(err);
})
}
function loadCategories(posts,callback){
// making an array from post ids
var postIds = _.pluck(posts,'id');
// join with blog_category table
knex("category").join("blog_category","category.id","blog_category.cat_id").whereIn({"blog_category.blog_id":postIds}).select("category.*","blog_category.blog_is as blog_id")
.then(function(categories){
callback(null,posts,categories)
})
.catch(function(err){
callback(err);
})
}
async.waterfall([loadBlogPosts,loadCategories],function(err,posts,categories){
var post_list = _.map(posts,function(post){
post.categories = _.find(categories,{blog_id: post.id});
})
})
在这个例子中,你只发送请求 2 查询(1 个用于帖子,1 个用于类别),这在性能上要好得多。