You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, it all works well in my project, but I still want to simply my definition like making custom DataTypes and doing some getter and setter by default.
So I would like to ask does it have any specification on how to extends DataTypes?
Thanks.
I can successfully extend the datatype for my own use, but I don't know it's correct or not.
here is a reference for others who want to do the same thing as me.
classIPextendsDataTypes.ABSTRACT{constructor(){this.key='IP'super()}toSql(){return'VARBINARY(16)'}// Todovalidate(value){// const Validator = require('./utils/validator-extras').validator;// if (!Validator.isDate(String(value))) {// throw new sequelizeErrors.ValidationError(util.format('%j is not a valid date', value));// }returntrue;}_sanitize(value){returnnewIP(value);};_isChanged(value,originalValue){return!value.toBuffer().equals(originalValue.toBuffer());};_stringify(datetime){returndatetime.toBuffer();};}
DataTypes.IP = IP
Reacted by CallumHogg, Tim Cheng, Roman Galochkin, Oleg Tsymbal and Victor Hugo
After spending a week, studying all datetypes related source code, thanks god, I can extend my custom data types now.
Here is my code:
import{DataTypes}from'sequelize'import{IP,DateTime,Decimal}from'./types'import_from'lodash'importutilfrom'util'// Extends DATE Data TypedeleteDataTypes.DATE.prototype._applyTimezoneDataTypes.DATE.prototype._sanitize=function_sanitize(value,options){options.raw=falseif((!options||options&&!options.raw)&&!(valueinstanceofDateTime)&&!!value){returnnewDateTime(value)}returnvalue}DataTypes.DATE.prototype._isChanged=function_isChanged(value,originalValue){if(originalValue&&!!value&&(value===originalValue||valueinstanceofDateTime&&originalValueinstanceofDateTime&&value.toTimestamp()===originalValue.toTimestamp())){returnfalse}// not changed when set to same empty valueif(!originalValue&&!value&&originalValue===value){returnfalse}returntrue}DataTypes.DATE.prototype._stringify=function_stringify(date,options){returndate.toMysqlFormat()}// Extends DATETIME Data TypefunctionDATETIMETYPE(length){constoptions=typeoflength==='object'&&length||{length}if(!(thisinstanceofDATETIMETYPE))returnnewDATETIMETYPE(options)DataTypes.DATE.call(this,options);}util.inherits(DATETIMETYPE,DataTypes.DATE)DATETIMETYPE.prototype.key=DATETIMETYPE.key='DATETIME'DATETIMETYPE.prototype.toSql=functiontoSql(){return'DATETIME'}DataTypes.DATETIME=DATETIMETYPE// Extends TIMESTAMP Data TypefunctionTIMESTAMPTYPE(length){constoptions=typeoflength==='object'&&length||{length}if(!(thisinstanceofDATETIMETYPE))returnnewDATETIMETYPE(options)DataTypes.DATE.call(this,options);}util.inherits(TIMESTAMPTYPE,DataTypes.DATE)TIMESTAMPTYPE.prototype.key=TIMESTAMPTYPE.key='TIMESTAMP'TIMESTAMPTYPE.prototype.toSql=functiontoSql(){return'TIMESTAMP'}DataTypes.TIMESTAMP=TIMESTAMPTYPE// Extends IP Data TypefunctionIPTYPE(){if(!(thisinstanceofIPTYPE))returnnewIPTYPE()}util.inherits(IPTYPE,DataTypes.ABSTRACT)IPTYPE.prototype.key=IPTYPE.key='IPTYPE'IPTYPE.prototype.toSql=functiontoSql(){return'VARBINARY(16)'}IPTYPE.prototype._sanitize=function_sanitize(value,options){options.raw=falseif((!options||options&&!options.raw)&&!(valueinstanceofIP)&&!!value){returnnewIP(value)}returnvalue}IPTYPE.prototype._isChanged=function_isChanged(value,originalValue){if(originalValue&&!!value&&(value===originalValue||valueinstanceofIP&&originalValueinstanceofIP&&value.toBuffer().equals(originalValue.toBuffer()))){returnfalse}// not changed when set to same empty valueif(!originalValue&&!value&&originalValue===value){returnfalse}returntrue}IPTYPE.prototype._stringify=function_stringify(ip,options){returnip.toBuffer()}DataTypes.IP=IPTYPE// Extends DATE Data TypeDataTypes.DECIMAL._sanitize=function(value){options.raw=falseif((!options||options&&!options.raw)&&!(valueinstanceofDecimal)&&!!value){returnnewDecimal(value)}returnvalue}DataTypes.DECIMAL._isChanged=function(value,originalValue){if(originalValue&&!!value&&(value===originalValue||valueinstanceofIP&&originalValueinstanceofIP&&value.equals(originalValue))){returnfalse}// not changed when set to same empty valueif(!originalValue&&!value&&originalValue===value){returnfalse}returntrue}DataTypes.DECIMAL._stringify=function(decimal){returndecimal.toString()}export{DataTypes}
Reacted by diegoavidal, Log-of-e, Victor Hugo and Charlie Schliesser
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂
Activity
shtse8 commentedon Oct 29, 2017
I can successfully extend the datatype for my own use, but I don't know it's correct or not.
here is a reference for others who want to do the same thing as me.
shtse8 commentedon Oct 29, 2017
After spending a week, studying all datetypes related source code, thanks god, I can extend my custom data types now.
Here is my code:
stale commentedon Jan 27, 2018
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂
mathpere commentedon Nov 28, 2018
Hello shtse8,
I can't figure out how the "validate" method is used. Sequelize doesn't seem to use it 🤔
The "validate" method in its own datatypes is never called (STRING, JSON, etc.).
yracnet commentedon Jun 28, 2022
I tested the IP example and other, but only create the column, but not calll to _isChanged, _sanitize and _stringify. :`( . I working with v6.21.0
ephys commentedon Jul 9, 2022
We're working on making it easier / more powerful to extend data types in #14505
h0gar commentedon Aug 3, 2023
wow, creating a custom data type on sequelize 6 was quite a frustrating adventure.
In case anyone struggles with this too, here is my example (none of the other example was working for me):