0
0
NestJSframework~3 mins

Why Migrations in NestJS? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if your database could update itself perfectly every time you change your app?

The Scenario

Imagine you have a growing app and you need to change your database structure by hand every time you add a new feature.

You open your database tool and manually add or remove columns, tables, or indexes for each update.

The Problem

Manually changing the database is slow and risky.

You might forget a step, make a typo, or apply changes in the wrong order.

This can break your app or cause data loss, especially when working with a team.

The Solution

Migrations automate database changes in a safe, repeatable way.

They keep track of what changes were made and apply them in order.

This means your database stays in sync with your app code without manual errors.

Before vs After
Before
ALTER TABLE users ADD COLUMN age INT;
After
await queryRunner.addColumn('users', new TableColumn({ name: 'age', type: 'int' }));
What It Enables

Migrations let you evolve your database confidently as your app grows, even with multiple developers.

Real Life Example

When your team adds a new feature that needs a new table, migrations let everyone update their databases with one command, avoiding confusion and errors.

Key Takeaways

Manual database changes are error-prone and hard to track.

Migrations automate and organize database updates safely.

This keeps your app and database in sync as you develop.