1. // myproject/lib/sequelize-additions.js
    2. module.exports = function sequelizeAdditions(Sequelize) {
    3. DataTypes = Sequelize.DataTypes
    4. /*
    5. * Create new types
    6. */
    7. class NEWTYPE extends DataTypes.ABSTRACT {
    8. // Mandatory, complete definition of the new type in the database
    9. toSql() {
    10. return 'INTEGER(11) UNSIGNED ZEROFILL'
    11. }
    12. // Optional, validator function
    13. validate(value, options) {
    14. }
    15. // Optional, sanitizer
    16. _sanitize(value) {
    17. // Force all numbers to be positive
    18. if (value < 0) {
    19. value = 0
    20. }
    21. return Math.round(value)
    22. }
    23. // Optional, value stringifier before sending to database
    24. _stringify(value) {
    25. return value.toString()
    26. }
    27. return Number.parseInt(value)
    28. }
    29. }
    30. DataTypes.NEWTYPE = NEWTYPE;
    31. // Mandatory, set key
    32. DataTypes.NEWTYPE.prototype.key = DataTypes.NEWTYPE.key = 'NEWTYPE'
    33. // Optional, disable escaping after stringifier. Not recommended.
    34. // Warning: disables Sequelize protection against SQL injections
    35. // DataTypes.NEWTYPE.escape = false
    36. // For convenience
    37. // `classToInvokable` allows you to use the datatype without `new`
    38. Sequelize.NEWTYPE = Sequelize.Utils.classToInvokable(DataTypes.NEWTYPE)