TypeORM lets you work with the database using TypeScript instead of SQL.
Client (Postman / Frontend)↓Controller (NestJS)↓Service (Business Logic)↓Repository (TypeORM)↓PostgreSQL Database
👉 This is your full backend flow.
TypeScript Class Database TableUser Entity → users tablePost Entity → posts table
Example:
User {idname}
↓
users table-----------------id | name | email
User (1) -------- (N) Postusers table posts table------------- ----------------------id idname titlecontentuserId ← foreign key
👉 One user → many posts 👉 Each post → belongs to one user
Save User↓Detect posts[]↓Insert posts↓Link with userId
Code:
repo.save({name: 'Daniel',posts: [{ title: 'Hello' }]});
👉 Everything saved automatically
All Data:[1,2,3,4,5,6,7,8,9,10]Page 1 (limit 3):[1,2,3]Page 2:[4,5,6]
Logic:
skip = (page - 1) * limittake = limit
Start Transaction↓Create User↓Create Posts↓Error? ─── YES → Rollback ❌↓NO → Commit ✅
👉 Ensures data consistency
Without index:
Search email → scan ALL rows 😩
With index:
Search email → jump directly ⚡
Change Entity↓Generate Migration↓Run Migration↓Database Updated
Example:
Add column "age"↓Migration file created↓DB updated safely
POST /users↓Controller receives request↓DTO validates input↓Service processes logic↓Repository saves data↓TypeORM maps to SQL↓PostgreSQL stores data
Entities → Repositories → Services → Controllers → Client↓Database (PostgreSQL)
With TypeORM + PostgreSQL, you get:
Happy coding 🚀