ORM = Object Relational Mapper
It lets you work with SQL databases using JavaScript Objects instead of writing raw SQL queries.
INSERT INTO users (name, email) VALUES ('Abhishek', 'abhi@example.com');
User.create({ name: "Abhishek", email: "abhi@example.com" });
| JS Object | SQL Row |
|---|---|
{ id, name, email, password } |
`id |
So ORM maps JS objects to SQL tables.
| 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”.
db.js – Create DB Connection Onceimport { 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);
}
};