Class DataTypes

View code

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

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

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, meanthat 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 fordefining default values. For example, to get a uuid field with a default value generated following v1 of the UUID standard:

  1. sequelize.define('model', {
  2. uuid: {
  3. type: DataTypes.UUID,
  4. defaultValue: DataTypes.UUIDV1,
  5. primaryKey: true
  6. }
  7. })

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

  1. sequelize.define('model', {
  2. uuid: {
  3. type: DataTypes.UUID,
  4. defaultValue: function() {
  5. return generateMyId()
  6. },
  7. primaryKey: true
  8. }
  9. })

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 (un)limited length text column. Available lengths: tiny, medium, long


INTEGER()

View code

A 32 bit integer.

Available properties: UNSIGNED, ZEROFILL


BIGINT()

View code

A 64 bit integer.

Note: an attribute defined as BIGINT will be treated like a string due this feature from node-postgres to prevent precision loss. To have this attribute as a number, this is a possible workaround.

Available properties: UNSIGNED, ZEROFILL


FLOAT()

View code

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

Available properties: UNSIGNED, ZEROFILL


REAL()

View code

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

Available properties: UNSIGNED, ZEROFILL


DOUBLE()

View code

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

Available properties: UNSIGNED, ZEROFILL


DECIMAL()

View code

Decimal number. Accepts one or two arguments for precision

Available properties: UNSIGNED, ZEROFILL


BOOLEAN()

View code

A boolean / tinyint column, depending on dialect


TIME()

View code

A time column


DATE()

View code

A datetime column


DATEONLY()

View code

A date only column


HSTORE()

View code

A key / value column. Only available in postgres.


JSON()

View code

A JSON string column. Only available in postgres.


JSONB()

View code

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


NOW()

View code

A default value of the current timestamp


BLOB()

View code

Binary storage. Available lengths: tiny, medium, long


RANGE()

View code

Range types are data types representing a range of values of some element type (called the range's subtype).Only available in postgres.See {@link http://www.postgresql.org/docs/9.4/static/rangetypes.html|Postgres documentation} for more details


UUID()

View code

A column storing a unique universal 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 v4 standard


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:

  1. sequelize.define('user', {
  2. password_hash: DataTypes.STRING,
  3. password: {
  4. type: DataTypes.VIRTUAL,
  5. set: function (val) {
  6. this.setDataValue('password', val); // Remember to set the data value, otherwise it won't be validated
  7. this.setDataValue('password_hash', this.salt + val);
  8. },
  9. validate: {
  10. isLongEnough: function (val) {
  11. if (val.length < 7) {
  12. throw new Error("Please choose a longer password")
  13. }
  14. }
  15. }
  16. }
  17. })

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.

VIRTUAL also takes a return type and dependency fields as argumentsIf 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.

  1. {
  2. active: {
  3. type: new DataTypes.VIRTUAL(DataTypes.BOOLEAN, ['createdAt']),
  4. get: function() {
  5. return this.get('createdAt') > Date.now() - (7 * 24 * 60 * 60 * 1000)
  6. }
  7. }
  8. }

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.


GEOMETRY()

View code

A column storing Geometry information.

Only available in PostgreSQL (with PostGIS) or MySQL.In MySQL, allowable Geometry types are 'POINT', 'LINESTRING', 'POLYGON'.

When using, GeoJSON is accepted as input and returned as output.In PostGIS, the GeoJSON is parsed using the PostGIS function ST_GeomFromGeoJSON.In MySQL it is parsed using the function GeomFromText.Therefore, one can just follow the GeoJSON spec for handling geometry objects. See the following examples:

  1. // Create a new point:
  2. var point = { type: 'Point', coordinates: [39.807222,-76.984722]};
  3. User.create({username: 'username', geometry: point }).then(function(newUser) {
  4. ...
  5. });
  6. // Create a new linestring:
  7. var line = { type: 'LineString', 'coordinates': [ [100.0, 0.0], [101.0, 1.0] ] };
  8. User.create({username: 'username', geometry: line }).then(function(newUser) {
  9. ...
  10. });
  11. // Create a new polygon:
  12. var polygon = { type: 'Polygon', coordinates: [
  13. [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0],
  14. [100.0, 1.0], [100.0, 0.0] ]
  15. ]};
  16. User.create({username: 'username', geometry: polygon }).then(function(newUser) {
  17. ...
  18. });
  19. // Create a new point with a custom SRID:
  20. var point = {
  21. type: 'Point',
  22. coordinates: [39.807222,-76.984722],
  23. crs: { type: 'name', properties: { name: 'EPSG:4326'} }
  24. };
  25. User.create({username: 'username', geometry: point }).then(function(newUser) {
  26. ...
  27. });

GEOGRAPHY()

View code

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


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