SQL 将书架中的查询过滤为 nodejs 中的 Laravel
SQL filter query in bookshelf as Laravel in nodejs
我是 node.js 的新手,它是 ORM(bookshelf
)。我需要知道如何获得具有任何特定类型的用户。
1). 这里 users
有多个 genres
, 意思是 users
hasMany genres and genres
belongsToMany users
.
问题). 如何获取所有 genres
= 1
的用户
下面是我的代码试图实现这个但无法得到正确的答案。
User.forge()
.query(function(query){
query.whereExists(function(){
this.select('*').from('genres')
.join('genres_user', function(){
this.on('genres.id', '=', 'genres_user.genres_id');
})
.where('users.id', 'genres_user.user_id')
.where('genres_id', req.query.genres);
});
})
.fetchAll({
withRelated : ['profile', 'posts']
})
.then(function (collection) {
if(collection == ''){
res.json({error: true, message:'No user found!', data:{}});
}
res.json({error: false, data: collection.toJson()});
})
.catch(function (err) {
res.status(500).json({error: true, data: {message: err.message}});
});
实际上我在书架上需要 whereHas()
(如 Laravel),但 bookshelf
中没有。但是我通过如下所示的原始 SQL 查询对其进行了排序,并且按照我的要求工作得很好。
User.forge()
.query(function(query){
query.whereExists(function(){
this.select('*').from('genres')
.join('genres_user', function(){
this.on('genres.id', '=', 'genres_user.genres_id');
})
.whereRaw('users.id = genres_user.user_id')
.where('genres_id', req.query.genres);
});
})
.fetchAll({
withRelated : ['profile', 'posts']
})
.then(function (collection) {
if(collection == ''){
res.json({error: true, message:'No user found!', data:{}});
}
res.json({error: false, data: collection.toJson()});
})
.catch(function (err) {
res.status(500).json({error: true, data: {message: err.message}});
});
我是 node.js 的新手,它是 ORM(bookshelf
)。我需要知道如何获得具有任何特定类型的用户。
1). 这里 users
有多个 genres
, 意思是 users
hasMany genres and genres
belongsToMany users
.
问题). 如何获取所有 genres
= 1
下面是我的代码试图实现这个但无法得到正确的答案。
User.forge() .query(function(query){ query.whereExists(function(){ this.select('*').from('genres') .join('genres_user', function(){ this.on('genres.id', '=', 'genres_user.genres_id'); }) .where('users.id', 'genres_user.user_id') .where('genres_id', req.query.genres); }); }) .fetchAll({ withRelated : ['profile', 'posts'] }) .then(function (collection) { if(collection == ''){ res.json({error: true, message:'No user found!', data:{}}); } res.json({error: false, data: collection.toJson()}); }) .catch(function (err) { res.status(500).json({error: true, data: {message: err.message}}); });
实际上我在书架上需要 whereHas()
(如 Laravel),但 bookshelf
中没有。但是我通过如下所示的原始 SQL 查询对其进行了排序,并且按照我的要求工作得很好。
User.forge()
.query(function(query){
query.whereExists(function(){
this.select('*').from('genres')
.join('genres_user', function(){
this.on('genres.id', '=', 'genres_user.genres_id');
})
.whereRaw('users.id = genres_user.user_id')
.where('genres_id', req.query.genres);
});
})
.fetchAll({
withRelated : ['profile', 'posts']
})
.then(function (collection) {
if(collection == ''){
res.json({error: true, message:'No user found!', data:{}});
}
res.json({error: false, data: collection.toJson()});
})
.catch(function (err) {
res.status(500).json({error: true, data: {message: err.message}});
});