Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a custom DataType in Sequelize ORM

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.

The Sequelize docs doesn't contain any information related to this topic. It would be really nice if someone can help me on this.

like image 684
PIKP Avatar asked Oct 30 '22 12:10

PIKP


1 Answers

https://github.com/sequelize/sequelize/blob/master/lib/data-types.js holds the sequelize data types.

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).

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.

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.

like image 54
Evan Lucas Avatar answered Nov 09 '22 09:11

Evan Lucas