Full Stack JS

Chapter 13
0%

Chapter 13: Sequelize ORM

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 TypeSQL EquivalentWhat It StoresExample
STRINGVARCHAR(255)Short textName, email
STRING(1000)VARCHAR(1000)Custom length textBio, address
TEXTTEXTUnlimited textBlog post, description
INTEGERINTEGERWhole numbersAge, quantity
FLOATFLOATDecimal numbersPrice, rating
BOOLEANBOOLEANtrue/falseisActive, completed
DATETIMESTAMPDate + timecreatedAt
DATEONLYDATEJust dateBirthday, dueDate
ENUMENUMOne of set values'low', 'medium', 'high'
JSONJSONJSON dataSettings, 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?

DataTypes.STRING
DataTypes.TEXT
DataTypes.INTEGER
DataTypes.BOOLEAN

3. What are migrations used for?

Moving data between servers
Tracking and applying database structure changes
Migrating from JavaScript to TypeScript
Backing up the database

4. What does unique: true do on a model field?

Makes the field required
Encrypts the field value
Makes the field read-only
Prevents duplicate values in that column

5. What does User.hasMany(Task) mean?

One user can have multiple tasks
Users and tasks are the same table
Tasks can have multiple users
Users are stored in the tasks table
← Chapter 12: Express.js Chapter 14: REST API with Sequelize →