Sequelize.js:sequelize.define 和 model.init 有什么区别? [英] Sequelize.js : What's the difference between sequelize.define and model.init?

查看:185
本文介绍了Sequelize.js:sequelize.define 和 model.init 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档 没有解释 sequelize 之间的区别.define 和 Model.init.它所说的只是在内部,sequelize.define 调用 Model.init

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

在选择使用哪一种时,我需要考虑哪些事项?两者之间的主要区别是什么,选择一个而不是另一个的后果是什么?似乎 sequelize.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?

推荐答案

我最近在遇到这个问题时也在想同样的事情.我做了一些进一步的调查,并在 文档 在这里可能有用.

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

总的来说,差异与语法密切相关,使用sequelize.defineModel.init 归结为个人喜好.我不相信一个比另一个更受欢迎,特别是因为 Sequelize CLI 工具 使用 sequelize.define 方法,并且没有在任何地方明确说明.

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.

模型可以在 Sequelize 中以两种等效方式定义:

Models can be defined in two equivalent ways in Sequelize:

  1. 调用 sequelize.define(modelName, attributes, options) API 文档定义

扩展模型并调用 init(attributes, options) 关于 init 的 API 文档

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

定义模型后,它可以在 sequelize.models 中通过模型名称使用.

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

正如您在文档中提到的和所述:

As you mentioned and as stated in the documentation:

在内部,sequelize.define 调用 Model.init,因此两种方法本质上是等效的.

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

这篇关于Sequelize.js:sequelize.define 和 model.init 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆