1. What is an ORM?

ORM = Object Relational Mapper

It lets you work with SQL databases using JavaScript Objects instead of writing raw SQL queries.

Without ORM (Raw SQL)

INSERT INTO users (name, email) VALUES ('Abhishek', 'abhi@example.com');

With Sequelize

User.create({ name: "Abhishek", email: "abhi@example.com" });

Thinking model → table:

JS Object SQL Row
{ id, name, email, password } `id

So ORM maps JS objects to SQL tables.


2. Why do we use Sequelize?

Problem (Without ORM) Sequelize Solves By
Writing SQL manually Auto query builder
Writing migration tables manually Model synchronizing / migrations
Managing relationships manually .hasOne, .hasMany, .belongsTo
Protecting against SQL Injection Parameterized queries
Rewriting same CRUD code Common model-based API

But the real reason, especially for someone who used Mongo/Mongoose like you:

Sequelize lets you define schema in JS like you did in Mongoose, instead of learning SQL DDL syntax.

This is why Sequelize “feels familiar”.


3. Minimal Setup (Clean, Modern Pattern)

db.js – Create DB Connection Once

import { Sequelize } from "sequelize";
import "dotenv/config";

export const sequelize = new Sequelize(
  process.env.DB_DATABASE,
  process.env.DB_USERNAME,
  process.env.DB_PASSWORD,
  {
    host: process.env.DB_HOST,
    dialect: "postgres",
    logging: false,
  }
);

export const connectDB = async () => {
  try {
    await sequelize.authenticate();
    console.log("âś… Database Connected");
  } catch (err) {
    console.log("❌ DB Connection Failed:", err);
    process.exit(1);
  }
};