Installation
First, install the package:Basic Concepts
Migrations in Orion.js:- Run sequentially in the order they are defined
- Execute only once (tracked in the database)
- Can optionally run within MongoDB transactions
- Automatically run through a background job that checks for pending migrations
Configuration
To set up migrations in your project, you need to:- Create migration services
- Load them in your application
Setting Up Migrations
Create a configuration file for migrations, typically atapp/config/migrations/index.ts
:
Creating Migration Files
Migrations are defined as services using theMigrationService
decorator. Each migration must have:
- A unique name (typically including a version number)
- A
runMigration
method containing the migration logic
app/config/migrations/list/MigrationExample1/index.ts
):
Advanced Features
MongoDB Transactions
For operations that need to be atomic, you can use MongoDB transactions:Long-Running Migrations
For migrations that take a long time to complete, extend the lock time to prevent the job from timing out:Disabling Automatic Execution
If you want to manually control when migrations run, you can disable the automatic job:MigrationsService
:
How It Works
Migrations in Orion.js work using a background job that:- Checks for pending migrations every 30 seconds
- Runs the next unexecuted migration
- Marks the migration as complete upon successful execution
- Continues until all migrations are executed
orionjs.migrations
collection in MongoDB with their execution status.