最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Filter relation query using bookshelf.js - Stack Overflow

programmeradmin5浏览0评论

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 badges
Add a ment  | 

1 Answer 1

Reset to default 7

I 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.

发布评论

评论列表(0)

  1. 暂无评论