Options
All
  • Public
  • Public/Protected
  • All
Menu

Namespace DataTypes

The datatypes are used when defining a new model using Model.init, like this:

class MyModel extends MyModel {}
MyModel.init({ column: DataTypes.INTEGER }, { sequelize });

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:

class MyModel extends Model {}
MyModel.init({
uuid: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV1,
primaryKey: true
}
}, { sequelize })

There may be times when you want to generate your own UUID conforming to some other algorithm. This is accomplised using the defaultValue property as well, but instead of specifying one of the supplied UUID types, you return a value from a function.

class MyModel extends Model {}
MyModel.init({
uuid: {
type: DataTypes.UUID,
defaultValue() {
return generateMyId()
},
primaryKey: true
}
}, { sequelize })

Index

Interfaces

Type aliases

Variables

Type aliases

BlobSize: "tiny" | "medium" | "long"
TextLength: "tiny" | "medium" | "long"

Variables

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

A 64 bit integer.

Available properties: UNSIGNED, ZEROFILL

Binary storage. Available lengths: tiny, medium, long

A boolean / tinyint column, depending on dialect

A fixed length string. Default length 255

Case-insensitive text

A datetime column

A date only column

Decimal number. Accepts one or two arguments for precision

Floating point number (8-byte precision). Accepts one or two arguments for precision

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

Floating point number (4-byte precision). Accepts one or two arguments for precision

A geography datatype represents two dimensional spacial objects in an elliptic coord system.

A geometry datatype represents two dimensional spacial objects.

A key / value column. Only available in postgres.

A 32 bit integer.

A JSON string column. Only available in postgres.

A pre-processed JSON data column. Only available in postgres.

A 24 bit integer.

A default value of the current timestamp

Range types are data types representing a range of values of some element type (called the range's subtype). Only available in postgres.

See Postgres documentation for more details

Floating point number (4-byte precision). Accepts one or two arguments for precision

A 16 bit integer.

A variable length string. Default length 255

An (un)limited length text column. Available lengths: tiny, medium, long

A time column

A 8 bit integer.

Full text search vector. Only available in postgres.

A column storing a unique universal identifier. Use with UUIDV1 or UUIDV4 for default values.

A default unique universal identifier generated following the UUID v1 standard

A default unique universal identifier generated following the UUID v4 standard

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:

class User extends Model {}
User.init({
password_hash: DataTypes.STRING,
password: {
type: DataTypes.VIRTUAL,
set (val) {
this.setDataValue('password', val); // Remember to set the data value, otherwise it won't be validated
this.setDataValue('password_hash', this.salt + val);
},
validate: {
isLongEnough (val) {
if (val.length < 7) {
throw new Error("Please choose a longer password")
}
}
}
}
}, { sequelize });

VIRTUAL also takes a return type and dependency fields as arguments If a virtual attribute is present in attributes it will automatically pull in the extra fields as well. Return type is mostly useful for setups that rely on types like GraphQL.

{
active: {
type: new DataTypes.VIRTUAL(DataTypes.BOOLEAN, ['createdAt']),
get() {
return this.get('createdAt') > Date.now() - (7 * 24 * 60 * 60 * 1000)
}
}
}

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.

Generated using TypeDoc