Talk to databases using JavaScript instead of SQL — Sequelize translates your code into database queries automatically.
What is an ORM?
Database Setup
Models
DataTypes & Validations
Migrations
Quiz
What is an ORM?
Imagine you want to talk to someone who speaks a different language. You could learn their language (SQL), or you could use a translator (ORM). An ORM (Object-Relational Mapping) lets you talk to the database using JavaScript instead of writing raw SQL queries. Sequelize is that translator for Node.js.
Here's the key idea: a database table = a JavaScript class (model)
Think of a database as an Excel spreadsheet:
Database = the entire Excel file Table = one sheet/tab (e.g., "Users", "Tasks") Column = a header in that sheet (name, email, age) Row = one entry/record in that sheet Model = a cookie cutter that defines the shape of each row
SQL vs Sequelize
Comparison — SQL vs Sequelize
Database Setup
Step 1: Install Packages
Terminal — Install Sequelize
Step 2: Create Database Connection
config/database.js — Database Connection
Step 3: Environment Variables
.env — Environment Variables
NEVER commit your .env file! It contains passwords and secrets. Always add .env to your .gitignore file. Share a .env.example file instead (with fake values).
Models
A model is like a cookie cutter. It defines the shape of your data — what fields a user should have, what type each field is, and what rules they must follow. Every user you create is a "cookie" made from this cutter.
Defining a User Model
models/User.js — User Model
Task Model with Associations
models/Task.js — Task Model with Association
Associations explained:User.hasMany(Task) means one user can have multiple tasks. Task.belongsTo(User) means each task is owned by exactly one user. Sequelize automatically adds a userId column to the tasks table.
DataTypes & Validations
Common DataTypes
Sequelize Type
SQL Equivalent
What It Stores
Example
STRING
VARCHAR(255)
Short text
Name, email
STRING(1000)
VARCHAR(1000)
Custom length text
Bio, address
TEXT
TEXT
Unlimited text
Blog post, description
INTEGER
INTEGER
Whole numbers
Age, quantity
FLOAT
FLOAT
Decimal numbers
Price, rating
BOOLEAN
BOOLEAN
true/false
isActive, completed
DATE
TIMESTAMP
Date + time
createdAt
DATEONLY
DATE
Just date
Birthday, dueDate
ENUM
ENUM
One of set values
'low', 'medium', 'high'
JSON
JSON
JSON data
Settings, metadata
Built-in Validations
Sequelize — Validation Examples
Migrations
Migrations are like a version history for your database. Just like Git tracks changes to your code, migrations track changes to your database structure. Need to add a new column? Create a migration. Need to rename a table? Migration. If something goes wrong, you can roll back to a previous version.
Setting Up Sequelize CLI
Terminal — Sequelize CLI Commands
Migration File Example
migrations/20260327-create-users-table.js
Never edit a migration that's already been run! If you need to change the database structure, create a NEW migration. Old migrations are history — like Git commits, you don't go back and edit them.
📝 Chapter 13 Quiz
1. What does ORM stand for?
Online Resource Manager
Object Request Model
Object-Relational Mapping
Ordered Record Method
2. Which DataType would you use for a user's name?