如何在Bookshelf.js中创建与自定义字段名称的多对多关系?

时间:2015-07-11 16:23:18

标签: javascript node.js bookshelf.js

我想在Bookshelf.js中创建多对多关系,并希望自己指定FK列的名称。我也希望能够访问Bookshelf中的helper-table,类似于:

NSSet

我该怎么做?

1 个答案:

答案 0 :(得分:0)

考虑到上面的例子,可以这样做:

var Doctor = bookshelf.Model.extend({

  patients: function() {
    return this.belongsToMany(Patient).through(Appointment);
  }

});

var Appointment = bookshelf.Model.extend({

  patient: function() {
    return this.belongsTo(Patient);
  },

  doctor: function() {
    return this.belongsTo(Doctor);
  }

});

var Patient = bookshelf.Model.extend({

  doctors: function() {
    return this.belongsToMany(Doctor).through(Appointment);
  }

});