关系多的Bookshelf如何查询数据?

How do you query data with Bookshelf with a lot of relations?

所以,我有以下内容:

var Device = services.bookshelf.Model.extend({
    tableName: 'device',
    calendar: function() {
        return this.belongsToMany(Calendar, 'calendar_device', 'deviceId', 'calendarId');
    }
});

var Calendar = module.exports = services.bookshelf.Model.extend({
    tableName: 'calendar',
    device: function() {
        return this.belongsToMany(Device, 'calendar_device', 'calendarId', 'deviceId');
    },
    schedule: function() {
        return this.hasMany(Schedule);
    }
});

var Schedule = module.exports = services.bookshelf.Model.extend({
    tableName: 'schedule',
    calendar: function() {
        return this.belongsTo(Calendar);
    },
    channel: function() {
        return this.hasMany(Channel);
    }
});

var Channel = module.exports = services.bookshelf.Model.extend({
    tableName: 'channel',
    schedule: function() {
        return this.belongsTo(Schedule);
    },
    screenItem: function() {
        return this.hasMany(ScreenItem);
    }
});

关系继续发展...... 例如,我应该如何进行查询以获取设备的频道?我不知道我是否遗漏了什么,但是我没有找到太多关于这方面的信息...

Device.where('id', id).fetch({withRelated: ['calendar.schedule.channel']}).then(function(devices) {
    ...
})

我所做的是使用 'id' = id 获取设备,指定我想要 return 的模型关系(在模型声明中可以看到在我的问题中)。 所以,说:

  • {withRelated: ['calendar']} return 是一个带有日历的设备
  • {withRelated: ['calendar.schedule']} return 是一个带有日历和时间表的设备
  • {withRelated: ['calendar.schedule.channel']} return 是一个设备及其日历及其时间表及其频道

如有更多相关车型想获取,依此类推

响应类似于:

{
    id: 1,
    ...,
    calendars: [
        id: 1,
        ...,
        schedules: [
            ...
        ]
    ]
}