Class DataTypes

View code A convenience class holding commonly used data types. The datatypes are used when definining a new model using Sequelize.define, like this:

sequelize.define('model', {
  column: DataTypes.INTEGER
})

When defining a model you can just as easily pass a string as type, but often using the types defined here is beneficial. For example, using DataTypes.BLOB, mean that that column will be returned as an instance of Buffer when being fetched by sequelize.

Some data types have special properties that can be accessed in order to change the data type. For example, to get an unsigned integer with zerofill you can do DataTypes.INTEGER.UNSIGNED.ZEROFILL. The order you access the properties in do not matter, so DataTypes.INTEGER.ZEROFILL.UNSIGNED is fine as well. The available properties are listed under each data type.

To provide a length for the data type, you can invoke it like a function: INTEGER(2)

Three of the values provided here (NOW, UUIDV1 and UUIDV4) are special default values, that should not be used to define types. Instead they are used as shorthands for defining default values. For example, to get a uuid field with a default value generated following v1 of the UUID standard:

sequelize.define('model', {
  uuid: {
    type: DataTypes.UUID,
    defaultValue: DataTypes.UUIDV1,
    primaryKey: true
  }
})

STRING

View code A variable length string. Default length 255

Available properties: BINARY


CHAR

View code A fixed length string. Default length 255

Available properties: BINARY


TEXT

View code An unlimited length text column


INTEGER

View code A 32 bit integer.

Available properties: UNSIGNED, ZEROFILL


BIGINT

View code A 64 bit integer.

Available properties: UNSIGNED, ZEROFILL


DATE

View code A datetime column


DATEONLY

View code A date only column


BOOLEAN

View code A boolean / tinyint column, depending on dialect


FLOAT

View code Floating point number. Accepts one or two arguments for precision

Available properties: UNSIGNED, ZEROFILL


NOW

View code A default value of the current timestamp


BLOB

View code Binary storage. Available lengths: tiny, medium, long


DECIMAL

View code Decimal number. Accepts one or two arguments for precision

Available properties: UNSIGNED, ZEROFILL


UUID

View code A column storing a unique univeral identifier. Use with UUIDV1 or UUIDV4 for default values.


UUIDV1

View code A default unique universal identifier generated following the UUID v1 standard


UUIDV4

View code A default unique universal identifier generated following the UUID v2 standard


HSTORE

View code A key / value column. Only available in postgres.


VIRTUAL

View code A virtual value that is not stored in the DB. This could for example be useful if you want to provide a default value in your model that is returned to the user but not stored in the DB.

You could also use it to validate a value before permuting and storing it. Checking password length before hashing it for example:

sequelize.define('user', {
  password_hash: DataTypes.STRING
  password: {
    type: DataTypes.VIRTUAL,
    set: function (val) {
       this.setDataValue('password', val);
       this.setDataValue('password_hash', this.salt + val);
     },
     validate: {
        isLongEnough: function (val) {
          if (val.length < 7) {
            throw new Error("Please choose a longer password")
         }
      }
    }
  }
})

In the above code the password is stored plainly in the password field so it can be validated, but is never stored in the DB. Aliases: NONE


ENUM

View code An enumeration. DataTypes.ENUM('value', 'another value').


ARRAY()

View code An array of type, e.g. DataTypes.ARRAY(DataTypes.DECIMAL). Only available in postgres.


This document is automatically generated based on source code comments. Please do not edit it directly, as your changes will be ignored. Please write on IRC, open an issue or a create a pull request if you feel something can be improved. For help on how to write source code documentation see JSDoc and dox