Stop 08 · ORMs & Query BuildersTopics · 3/10

Sequelize

Part of the Backend Developer Roadmap

In short

Sequelize is one of the oldest and most widely used Node.js ORMs. You'll find it in lots of existing codebases, so it's worth knowing how its models and queries read.

A long-standing, mature ORM for SQL databases in Node.js, using a model-based API and supporting PostgreSQL, MySQL, SQLite and more.

The blob in a chef hat stirs several big pots of different dishes at once.

Think of it like this

The caterer who cooks every cuisine

A seasoned wedding caterer can do a Punjabi, South Indian or Gujarati menu with the same team. Sequelize is like that: one ORM for Postgres, MySQL, MariaDB, SQLite and SQL Server. It's been around for years, so most problems already have an answer online.

Watch it happen

A model talking to MySQL

User model
Sequelize
MySQL

findAll({ where })

1/6Your code asks the User model for students in year 2.

The big ideas

What you need to know

Models

A model describes a table with define() or a class with init(): column names, types and rules like allowNull.

Associations

hasMany, belongsTo and belongsToMany set up relations, and include loads them together.

Validations

Add rules like isEmail on a field. Sequelize checks them before sending any SQL.

Typing takes extra work

It works with TypeScript, but types aren't generated from a schema the way Prisma does it.

See it in code

Walk through the code

js
1import { Sequelize, DataTypes } from "sequelize";2 3const sequelize = new Sequelize("mysql://root:dev@localhost:3306/college");4 5const User = sequelize.define("User", {6  name: { type: DataTypes.STRING, allowNull: false },7  email: { type: DataTypes.STRING, unique: true, validate: { isEmail: true } },8  year: { type: DataTypes.INTEGER, defaultValue: 1 },9});10 11await sequelize.sync(); // dev only: creates the table if missing12 13const seconds = await User.findAll({ where: { year: 2 }, order: [["name", "ASC"]] });14await User.update({ year: 3 }, { where: { year: 2 } });

1/4Connect with a URL. The mysql2 package must be installed for MySQL.

The table name defaults to the plural, Users. For real projects, use sequelize-cli migrations instead of sync().

Save yourself the pain

Common mistakes

Oops

Running sequelize.sync({ force: true }) against a real database.

Do this instead

force drops and recreates tables, deleting data. Use migrations with sequelize-cli instead.

Oops

Loading associations in a loop, one query per user.

Do this instead

Use include: [Post] so Sequelize fetches users and their posts together.

Your mission

Sequelize starter

~25 min

Quick check

What does sequelize.sync({ force: true }) do?

Go deeper

  1. 1Define a model and perform basic CRUD operations with Sequelize.
  2. 2Compare its syntax and developer experience to Prisma.
  3. 3Learn Sequelize's association methods (`hasMany`, `belongsTo`) for relationships.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge