/ #Sequelize #JavaScript 

Sequelize Data Types: a practical guide

Get the Sequelize ES6 cheat sheet

A cheat sheet to level up your Sequelize productivity.
If you've ever needed better Sequelize docs, this is it.

I want this

DataTypes are core to the Sequelize 4 library. This is a practical and complete guide to make up for the sparse Sequelize official documentation on the subject.

First of all, DataTypes contains both the types and generators for default values: eg. NOW, UUIDV1 and UUIDV4 are special default values.

The places where you’re likely to encounter DataTypes are in the model field definition and in the migration definitions. Note that for a given model and field, the data type should be the same in the model and in the migration.

Table of contents:

Text types

  • STRING
    • A variable length string.
    • Default length 255.
    • Supports BINARY
    • Usage: a 100 length binary string DataTypes.STRING(100).BINARY
  • CHAR
    • A fixed length string.
    • Default length 255.
    • Supports BINARY
    • Usage: a 100 length binary char DataTypes.CHAR(100).BINARY
  • TEXT: An unlimited length text column

Default values

  • NOW: A default value of the current timestamp
  • UUIDV1: A default unique universal identifier generated following the UUID v1 standard
  • UUIDV4: A default unique universal identifier generated following the UUID v2 standard

Number

All the following support these properties: UNSIGNED, ZEROFILL.

eg.

DataTypes.INTEGER.UNSIGNED.ZEROFILL
// or 
DataTypes.INTEGER.ZEROFILL.UNSIGNED 

The same can be done using BIGINT.UNSIGNED, FLOAT.UNSIGNED etc.

  • INTEGER: A 32 bit integer.
  • BIGINT: A 64 bit integer.
  • FLOAT: Floating point number (4-byte precision). Accepts one or two arguments for precision
  • REAL: Floating point number (4-byte precision). Accepts one or two arguments for precision
  • DOUBLE: Floating point number (8-byte precision). Accepts one or two arguments for precision
  • DECIMAL: Decimal number. Accepts one or two arguments for precision

This is an extract from the Sequelize ES6 Cheatsheet - the better Sequelize docs you wish you had (get it here).

Fancy primitive types

  • BOOLEAN: Boolean/tinyint column that gets coerced to a JavaScript Boolean.
  • UUID: A column storing a unique universal identifier, shape is validated, use with UUIDV1 or UUIDV4 default values

Date/Time

  • TIME: A time column
  • DATE: A datetime column
  • DATEONLY: A date only column

Fancy types

  • BLOB: Binary storage. Available lengths: tiny, medium, long eg. DataTypes.BLOG('tiny')
  • VIRTUAL
    • 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.
    • See the docs
  • ENUM
    • An enumeration.
    • DataTypes.ENUM('value', 'another value')
    • Ideally should be used with strings stored in constants js const FIRST_ENUM_VALUE = 'FIRST_ENUM_VALUE'; const OTHER_ENUM_VALUE = 'OTHER_ENUM_VALUE'; // In migration or model definition DataTypes.ENUM(FIRST_ENUM_VALUE, OTHER_ENUM_VALUE)

Postgres fancy types

  • HSTORE: A key/value column
  • JSON: A JSON string column.
  • JSONB: A pre-processed JSON data column.
  • RANGE: For Postgres 9.4+, range types are data types representing a range of values of some element type (called the range’s subtype).
  • ARRAY
    • An array of type, e.g. DataTypes.ARRAY(DataTypes.DECIMAL)

This is an extract from the Sequelize ES6 Cheatsheet - the better Sequelize docs you wish you had (get it here).

unsplash-logoMika Baumeister

Author

Hugo Di Francesco

A developer, working out of London writing CSS, JavaScript and Python.

Get the Sequelize ES6 cheat sheet

A cheat sheet to level up your Sequelize productivity. If you've ever needed better Sequelize docs, this is it.