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

会有一股神秘感。 提交于 2020-12-29 03:38:06

问题


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

What things do I need to consider when choosing which one to use? What are the main differences between the two and what are the consequences for choosing one over the other? It seems like sequelize.init is the preferred method in the documentation - why is that?


回答1:


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.



来源:https://stackoverflow.com/questions/55876970/sequelize-js-whats-the-difference-between-sequelize-define-and-model-init

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!