What is a migration in the context of NestJS and databases?
A migration is a script that changes the database schema in a controlled way. It helps keep track of changes like creating or modifying tables, so the database structure matches the app's needs.
Click to reveal answer
beginner
Why should you use migrations instead of manually changing the database?
Migrations keep changes organized and repeatable. They let you update databases safely across different environments and help avoid mistakes or data loss.
Click to reveal answer
beginner
Which NestJS package is commonly used to handle migrations?
TypeORM is often used with NestJS for migrations. It provides commands to create, run, and revert migrations easily.
Click to reveal answer
intermediate
What command do you use to generate a new migration file with TypeORM CLI?
You use the command: typeorm migration:create -n MigrationName to create a new migration file with a given name.
Click to reveal answer
beginner
How do migrations help when working in a team?
Migrations let everyone apply the same database changes in the same order. This keeps the database consistent for all developers and avoids conflicts.
Click to reveal answer
What is the main purpose of a migration in NestJS projects?
ATo manage user authentication
BTo write frontend UI code
CTo deploy the app to a server
DTo update the database schema safely
✗ Incorrect
Migrations are used to update the database schema safely and keep track of changes.
Which tool is commonly used with NestJS for database migrations?
AReact
BTypeORM
CExpress
DWebpack
✗ Incorrect
TypeORM is a popular ORM that supports migrations and integrates well with NestJS.
What does the command typeorm migration:run do?
ARuns all pending migrations to update the database
BCreates a new migration file
CDeletes the database
DStarts the NestJS server
✗ Incorrect
This command applies all migrations that have not yet been run to update the database schema.
Why is it important to commit migration files to version control?
ASo all team members can apply the same database changes
BTo speed up the app
CTo improve frontend styling
DTo reduce server costs
✗ Incorrect
Committing migration files ensures everyone uses the same database structure changes.
If you want to undo the last migration, which command would you use?
Atypeorm migration:run
Btypeorm migration:create
Ctypeorm migration:revert
Dnpm start
✗ Incorrect
The revert command undoes the last applied migration to rollback database changes.
Explain what migrations are and why they are useful in NestJS projects.
Think about how you keep your database organized and consistent.
You got /4 concepts.
Describe the typical workflow for creating and running migrations using TypeORM in NestJS.
Consider the steps from writing to applying migrations.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of migrations in a NestJS project using TypeORM?
easy
A. To handle HTTP requests and responses
B. To safely update the database schema step-by-step
C. To write business logic for the application
D. To manage user authentication
Solution
Step 1: Understand what migrations do
Migrations help change the database structure safely without losing data.
Step 2: Identify the correct purpose in NestJS context
They are not for business logic or HTTP handling but for database schema updates.
Final Answer:
To safely update the database schema step-by-step -> Option B
2. Which command correctly runs all pending migrations in a NestJS project using TypeORM?
easy
A. npx typeorm migration:create
B. npx typeorm migration:revert
C. npx typeorm migration:generate
D. npx typeorm migration:run
Solution
Step 1: Recall commands for migrations
migration:run applies pending migrations to the database.
Step 2: Differentiate from other commands
migration:create creates a new migration file, migration:generate generates migration from schema changes, and migration:revert undoes the last migration.
Final Answer:
npx typeorm migration:run -> Option D
Quick Check:
Run migrations = migration:run [OK]
Hint: Run migrations with migration:run command [OK]
Common Mistakes:
Using migration:create to run migrations
Confusing migration:generate with migration:run
Using migration:revert to apply migrations
3. Given this migration class, what will happen when up() is run?
export class AddAgeColumn1678901234567 {
async up(queryRunner) {
await queryRunner.query(`ALTER TABLE users ADD COLUMN age int`);
}
async down(queryRunner) {
await queryRunner.query(`ALTER TABLE users DROP COLUMN age`);
}
}
medium
A. Creates a new table called 'age'
B. Removes the 'age' column from the 'users' table
C. Adds a new integer column 'age' to the 'users' table
D. Throws a syntax error because of missing semicolons
Solution
Step 1: Analyze the up() method
The up() method runs SQL to add a column named 'age' of type integer to the 'users' table.
Step 2: Understand the effect of the query
This modifies the existing 'users' table by adding the new column; it does not remove columns or create new tables.
Final Answer:
Adds a new integer column 'age' to the 'users' table -> Option C
Quick Check:
up() adds column 'age' [OK]
Hint: up() applies changes like adding columns [OK]
Common Mistakes:
Confusing up() with down() method
Thinking it creates a new table
Assuming syntax error due to semicolons
4. You wrote this migration but get an error when running npx typeorm migration:run. What is the likely problem?
A. The SQL syntax for dropping a column is incorrect
B. The migration class name is invalid
C. Missing async keyword in down() method
D. The migration file is not saved
Solution
Step 1: Check the SQL in up() method
The query ALTER TABLE users DROP email is incorrect SQL syntax for dropping a column.
Step 2: Correct SQL syntax for dropping a column
The correct syntax is ALTER TABLE users DROP COLUMN email. Missing 'COLUMN' causes the error.
Final Answer:
The SQL syntax for dropping a column is incorrect -> Option A
Quick Check:
DROP COLUMN keyword required in SQL [OK]
Hint: Use DROP COLUMN to remove columns in SQL [OK]
Common Mistakes:
Omitting COLUMN keyword in DROP statement
Assuming class name causes error
Forgetting async keyword (not the cause here)
5. You want to create a migration that renames a column 'username' to 'user_name' in the 'accounts' table and can be reverted. Which up() and down() methods are correct?