始终从Bookshelf.js中的相关模型中获取

Est*_*ask 10 node.js bookshelf.js knex.js

我希望baffle.where({id: 1}).fetch()始终将typeName属性作为baffle模型的一部分,而不是baffleType每次都明确地获取它.

以下适合我,但似乎withRelated如果baffle直接获取模型,而不是关系,将加载关系:

let baffle = bookshelf.Model.extend({
    constructor: function() {
        bookshelf.Model.apply(this, arguments);

        this.on('fetching', function(model, attrs, options) {
            options.withRelated = options.withRelated || [];
            options.withRelated.push('type');           
        });
    },

    virtuals: {
        typeName: {
            get: function () {
                return this.related('type').attributes.typeName;
            }
        }
    },
    type: function () {
        return this.belongsTo(baffleType, 'type_id');
    }
});

let baffleType = bookshelf.Model.extend({});
Run Code Online (Sandbox Code Playgroud)

这样做的正确方法是什么?

Sat*_*eet 8

回购问题与Fetched事件有关,但Fetching事件正常(v0.9.2).

所以,例如,如果你有第三个模型

var Test = Bookshelf.model.extend({
   tableName : 'test',
   baffleField : function(){
       return this.belongsTo(baffle)
   } 
})
Run Code Online (Sandbox Code Playgroud)

然后做Test.forge().fetch({ withRelated : ['baffleField']}),fetching挡板上的事件会开火.但是ORM不包括type(子相关模型),除非您明确告诉它这样做

Test.forge().fetch({ withRelated : ['baffleField.type']})
Run Code Online (Sandbox Code Playgroud)

但是,try to avoid如果它正在制作N Query,我会这样做N records.

更新1

我在谈论你在fetching事件上做的同样的事情

fetch: function fetch(options) {
   var options = options || {}
   options.withRelated = options.withRelated || [];
   options.withRelated.push('type');    
    // Fetch uses all set attributes.
   return this._doFetch(this.attributes, options);
}
Run Code Online (Sandbox Code Playgroud)

model.extend.但是正如您所看到的,这可能会在version更改时失败.


Jon*_*onG 4

这个问题太老了,但我还是回答一下。

我通过添加一个新函数 解决了这个问题,fetchFull这让事情变得非常干燥。

let MyBaseModel = bookshelf.Model.extend({
  fetchFull: function() {
    let args;
    if (this.constructor.withRelated) {
      args = {withRelated: this.constructor.withRelated};
    }
    return this.fetch(args);
  },
};

let MyModel = MyBaseModel.extend({
    tableName: 'whatever',
  }, {
    withRelated: [
      'relation1',
      'relation1.related2'
    ]
  }
);
Run Code Online (Sandbox Code Playgroud)

然后,每当您进行查询时,您都可以调用Model.fetchFull()来加载所有内容,或者在您不想降低性能的情况下,您仍然可以诉诸Model.fetch().