What methods/mixins sequelize adds to the models when an association is made?

前端 未结 2 556
傲寒
傲寒 2021-02-04 17:43

While going through the sequelize docs, more specifically the documentations about associations, I see that the guide casually shows the reader methods such as setTasks()<

2条回答
  •  忘掉有多难
    2021-02-04 18:29

    To get a listing of the added methods try:

        const model = %yourSequelizeModel%
        for (let assoc of Object.keys(model.associations)) {
          for (let accessor of Object.keys(model.associations[assoc].accessors)) {
            console.log(model.name + '.' + model.associations[assoc].accessors[accessor]+'()');
          }
        }
    

    Credit goes to https://gist.github.com/Ivan-Feofanov/eefe489a2131f3ec43cfa3c7feb36490

    To adjust the association names use "as" option:

    Model.hasOne(models.series_promotions, { as: 'seriesPromotions' });
    

    which changed the association method name from:

    series.getSeries_promotion()
    series.setSeries_promotion()
    series.createSeries_promotion()
    

    to

    series.getSeriesPromotions()
    series.setSeriesPromotions()
    series.createSeriesPromotions()
    

    based on the snippet above.

提交回复
热议问题