Data types in Sequelize: A cheat sheet

Updated: December 29, 2023 By: Guest Contributor Post a comment

Introduction

Sequelize is a promise-based Node.js ORM for Postgres, MySQL, MariaDB, SQLite, and Microsoft SQL Server. It features solid transaction support, relations, eager and lazy loading, read replication and more. Data types in Sequelize correspond to the data types supported by the underlying database engine. This tutorial provides a comprehensive cheat sheet for these data types, along with example usage to enhance your Sequelize models.

Getting Started

Before diving into the data types, ensure you have Sequelize installed and a model to work with:

const Sequelize = require('sequelize');
const sequelize = new Sequelize('database', 'username', 'password', {
  dialect: 'mysql'
});

For demonstration, we will define a ‘User’ model:

const User = sequelize.define('user', {
  // Attributes will be defined here
});

String Data Types

Sequelize provides several string data types:

  • STRING – for variable-length strings
  • TEXT – for large text blocks
  • CHAR – for fixed-length strings
  • BINARY – for binary strings

Example:

User.init({
  username: { type: Sequelize.STRING },
  biography: { type: Sequelize.TEXT }
}, { sequelize, modelName: 'user' });

Numeric Data Types

Sequelize supports several numeric data types:

  • INTEGER, BIGINT – for integers
  • FLOAT, REAL, DOUBLE – for floating-point numbers
  • DECIMAL – for fixed precision decimals

Example:

User.init({
  age: { type: Sequelize.INTEGER },
  height: { type: Sequelize.FLOAT }
}, { sequelize, modelName: 'user' });

Boolean Data Types

For true/false values, Sequelize provides the BOOLEAN data type.

Example:

User.init({
  isActive: { type: Sequelize.BOOLEAN }
}, { sequelize, modelName: 'user' });

Temporal Data Types

Sequelize supports temporal data types like:

  • DATE – for date and time
  • DATEONLY – for date without time

Example:

User.init({
  birthday: { type: Sequelize.DATEONLY }
}, { sequelize, modelName: 'user' });

Other Data Types

Sequelize offers additional types for more complex scenarios:

  • ENUM – for a list of string values
  • ARRAY – for arrays (only PostgreSQL)
  • JSON, JSONB – for JSON data (PostgreSQL)

Example:

User.init({
  gender: { type: Sequelize.ENUM('male', 'female', 'other') }
}, { sequelize, modelName: 'user' });

Advanced Data Types

In addition to the basic types, Sequelize supports more advanced data types:

  • GEOGRAPHY, GEOMETRY – for spatial data
  • UUID – for unique identifiers
  • VIRTUAL – for virtual fields that are not stored in the DB

Example:

User.init({
  location: { type: Sequelize.GEOGRAPHY },
  uniqueId: { type: Sequelize.UUID, defaultValue: Sequelize.UUIDV4 }
}, { sequelize, modelName: 'user' });

Conclusion

This cheat sheet provides a quick overview of the various Sequelize data types you can use to define your model attributes. Understanding these data types is essential for creating efficient and accurate models in Sequelize. As you grow more familiar with Sequelize, you’ll find these data types integral in shaping your application’s data structure.