如何在Sequelize ORM中添加自定义DataType [英] How to add a custom DataType in Sequelize ORM
问题描述
我想通过继承现有 DataType.Integer
的所有默认行为,在 Sequelize
中定义自定义数据类型。这里的基本思想是定义一个新类型并覆盖 valueOf
和 toString
方法。
I want to define a custom data type in Sequelize
by inheriting all default behaviours of existing DataType.Integer
. The base idea here is to define a new type and override valueOf
and toString
methods.
Sequelize文档不包含与此主题相关的任何信息。如果有人可以帮助我,那将是非常好的。
The Sequelize docs doesn't contain any information related to this topic. It would be really nice if someone can help me on this.
推荐答案
https://github.com/sequelize/sequelize/blob/master/lib/data-types.js 保存续集数据类型。
具体来说, https://github.com/sequelize/sequelize/blob/master/lib/data-types.js#L251-L273 显示了DataTypes.INTEGER如何继承自DataTypes.NUMBER使用 NUMBER.inherits(fn)
。
Specifically, https://github.com/sequelize/sequelize/blob/master/lib/data-types.js#L251-L273 shows how DataTypes.INTEGER inherits from DataTypes.NUMBER using NUMBER.inherits(fn)
.
那些继承自ABSTRACT。您可以为继承的数据类型覆盖 toString()
方法,如 https://github.com/sequelize/sequelize/blob/master/lib/data-types.js#L62-L64 。
Those inherit from ABSTRACT. You could override the toString()
method for your inherited data type like as seen at https://github.com/sequelize/sequelize/blob/master/lib/data-types.js#L62-L64.
免责声明:由于未公开记录,我不确定API的稳定程度,并且由于未来可能的变化而会保持谨慎。
Disclaimer: with it not being publicly documented, I am not sure how stable the APIs are and would be cautious due to possible future changes.
这篇关于如何在Sequelize ORM中添加自定义DataType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!