Sling Academy
Home/Node.js/Data types in Sequelize: A cheat sheet

Data types in Sequelize: A cheat sheet

Last updated: December 29, 2023

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.

Next Article: Using beforeCreate and beforeUpdate hooks in Sequelize.js

Previous Article: Sequelize: How to Migrate Data from One Database to Another (3 Ways)

Series: Sequelize.js Tutorials

Node.js

You May Also Like

  • NestJS: How to create cursor-based pagination (2 examples)
  • Cursor-Based Pagination in SequelizeJS: Practical Examples
  • MongooseJS: Cursor-Based Pagination Examples
  • Node.js: How to get location from IP address (3 approaches)
  • SequelizeJS: How to reset auto-increment ID after deleting records
  • SequelizeJS: Grouping Results by Multiple Columns
  • NestJS: Using Faker.js to populate database (for testing)
  • NodeJS: Search and download images by keyword from Unsplash API
  • NestJS: Generate N random users using Faker.js
  • Sequelize Upsert: How to insert or update a record in one query
  • NodeJS: Declaring types when using dotenv with TypeScript
  • Using ExpressJS and Multer with TypeScript
  • NodeJS: Link to static assets (JS, CSS) in Pug templates
  • NodeJS: How to use mixins in Pug templates
  • NodeJS: Displaying images and links in Pug templates
  • ExpressJS + Pug: How to use loops to render array data
  • ExpressJS: Using MORGAN to Log HTTP Requests
  • NodeJS: Using express-fileupload to simply upload files
  • ExpressJS: How to render JSON in Pug templates