admin管理员组文章数量:1332197
How do I retrieve all records using through method but with a condition for the intermediate table, for ex: I want to retrieve all tracks for a channel where is_publish field from album (intermediate table)has value 1
my code so far looks as below:
new channelModel({'id': req.params.channel_id})
.fetch({withRelated: ['tracks']})
.then(function (channel) {
if (channel) {
res.json({error: false, status: 200, data: channel});
} else {
res.json({error: true, status: 404, data: 'channel does not exist'});
}
})
With this code I retrieve all tracks.. And the channel model has a function defined as below:
tracks: function () {
return this.hasMany('track').through('album');
}
my database looks smth like this:
channels: id,name,desc
albums:channel_id,name,descr,is_publish
tracks:album_id,name,descr
Any suggestion?
How do I retrieve all records using through method but with a condition for the intermediate table, for ex: I want to retrieve all tracks for a channel where is_publish field from album (intermediate table)has value 1
my code so far looks as below:
new channelModel({'id': req.params.channel_id})
.fetch({withRelated: ['tracks']})
.then(function (channel) {
if (channel) {
res.json({error: false, status: 200, data: channel});
} else {
res.json({error: true, status: 404, data: 'channel does not exist'});
}
})
With this code I retrieve all tracks.. And the channel model has a function defined as below:
tracks: function () {
return this.hasMany('track').through('album');
}
my database looks smth like this:
channels: id,name,desc
albums:channel_id,name,descr,is_publish
tracks:album_id,name,descr
Any suggestion?
Share Improve this question edited Feb 25, 2015 at 1:39 Rhys van der Waerden 3,8672 gold badges29 silver badges32 bronze badges asked Feb 24, 2015 at 20:13 LulzimLulzim 5474 gold badges9 silver badges22 bronze badges1 Answer
Reset to default 7I haven't tested this, but I believe you can do the following:
ChannelModel = bookshelf.BaseModel.extend({
tracks: function () {
return this.hasMany('track').through('album');
},
publishedTracks: function () {
return this.tracks().query('where', 'is_publish', true);
},
unpublishedTracks: function () {
return this.tracks().query('where', 'is_publish', false);
},
});
new ChannelModel({'id': req.params.channel_id})
.fetch({withRelated: ['pubishedTracks']})
.then(function (channel) {
if (channel) {
res.json({error: false, status: 200, data: channel.toJSON()});
} else {
res.json({error: true, status: 404, data: 'channel does not exist'});
}
});
Alternatively, you may wish to do this:
new ChannelModel({'id': req.params.channel_id})
.fetch()
.tap(function (channel) {
channel.tracks().query('where', 'is_publish', true).fetch()
})
.then(function(channel) {
if (channel) {
res.json({error: false, status: 200, data: channel.toJSON()});
} else {
res.json({error: true, status: 404, data: 'channel does not exist'});
}
});
Also, while we're at it, I might point out require: true
, which is a style I prefer for these situations.
new ChannelModel({'id': req.params.channel_id})
.fetch({ require: true })
.tap(function (channel) {
channel.tracks().query('where', 'is_publish', true).fetch()
})
.then(function(channel) {
res.json({error: false, status: 200, data: channel.toJSON()});
})
.catch(bookshelf.NotFoundError, function(error) {
res.json({error: true, status: 404, data: 'channel does not exist'});
});
Also note that you were leaving off the call to .toJSON()
in your response.
本文标签: javascriptFilter relation query using bookshelfjsStack Overflow
版权声明:本文标题:javascript - Filter relation query using bookshelf.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742246849a2440013.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论