Sequelize.js : What's the difference between sequelize.define and model.init?

后端 未结 1 948
野性不改
野性不改 2021-02-07 15:34

The documentation doesn\'t explain the difference between sequelize.define and Model.init. All it says is Internally, sequelize.define calls Model.init

What

相关标签:
1条回答
  • 2021-02-07 16:09

    I was recently wondering the same thing when I came across this question. I did some further investigation and found some additional details explained in the documentation that may be useful here.

    TL:DR

    Overall, the differences are strictly related to syntax, and using sequelize.define or Model.init comes down to personal preference. I do not believe one is preferred over the other, especially since the Sequelize CLI tool generates model definitions using the sequelize.define method, and it is not explicitly stated anywhere.

    Models can be defined in two equivalent ways in Sequelize:

    1. Calling sequelize.define(modelName, attributes, options) API documentation on define

    2. Extending Model and calling init(attributes, options) API documentation on init

    After a model is defined, it is available within sequelize.models by its model name.

    As you mentioned and as stated in the documentation:

    Internally, sequelize.define calls Model.init, so both approaches are essentially equivalent.

    0 讨论(0)
提交回复
热议问题