0.3.4 • Published 8 months ago

loopback-mongodb-migrate v0.3.4

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

loopback-mongodb-migrate

LoopBack

Installation

Install MongoDBMigrateComponent using npm;

$ npm install loopback-mongodb-migrate

Add Component to Application

Configure and load MongoDBMigrateComponent in the application constructor as shown below. Note: change the variables <MONGODB_BACKUP_DIR> and <MONGODB_URL>

project/src/application.ts

// insert this code
// [start of code]
import { MongoDBMigrateComponent, MongoDBMigrateComponentBindings } from 'loopback-mongodb-migrate';
// [end of code]

// ...
export class MyApplication extends BootMixin(ServiceMixin(RepositoryMixin(RestApplication))) {
  constructor(options: ApplicationConfig = {}) {
    // insert this code
    // [start of code]
    this.configure(MongoDBMigrateComponentBindings.COMPONENT).to({
      DB_BACKUP_DIR: <MONGODB_BACKUP_DIR>,
      DATASOURCE_URL: <MONGODB_URL>,
    });
    this.component(MongoDBMigrateComponent);
    // [end of code]
  }
  // ...
}

Example: project/src/application.ts

import { MongoDBMigrateComponent, MongoDBMigrateComponentBindings } from 'loopback-mongodb-migrate';
// ...
export class MyApplication extends BootMixin(ServiceMixin(RepositoryMixin(RestApplication))) {
  constructor(options: ApplicationConfig = {}) {
    this.configure(MongoDBMigrateComponentBindings.COMPONENT).to({
      DB_BACKUP_DIR: '../mongodb-backup',
      DATASOURCE_URL: 'mongodb://127.0.0.1:27017/mydatabase',
    });
    this.component(MongoDBMigrateComponent);
    // ...
  }
  // ...
}

If you are using authentication, add the user and password in the url

Example: project/src/application.ts

import { MongoDBMigrateComponent, MongoDBMigrateComponentBindings } from 'loopback-mongodb-migrate';
// ...
export class MyApplication extends BootMixin(ServiceMixin(RepositoryMixin(RestApplication))) {
  constructor(options: ApplicationConfig = {}) {
    this.configure(MongoDBMigrateComponentBindings.COMPONENT).to({
      DB_BACKUP_DIR: '../mongodb-backup',
      DATASOURCE_URL: 'mongodb://myusername:mypassword@127.0.0.1:27017/mydatabase?authSource=admin',
    });
    this.component(MongoDBMigrateComponent);
    // ...
  }
  // ...
}

NOTE: Don't forget to change the myusername, mypassword and mydatabase

Update Migrate file

Configure in migrate file. Note: change the App from the ./application file to your application class name which extends the BootMixin.

project/src/migrate.ts

import {
    IMigrationService,
    RepositoryModules,
    MongoDBMigrateComponentBindings,
    Application,
} from 'loopback-mongodb-migrate';
import { App } from './application';
import * as Repositories from './repositories';

export async function migrate(args: string[]) {
    const app = new App();
    await app.boot();

    // get migration service
    const migrationService = app
        .getSync<IMigrationService>(MongoDBMigrateComponentBindings.MIGRATION_SERVICE);

    // get all repositories for migration access
    const repositories: RepositoryModules = await migrationService
        .getRepositories(app as Application, Repositories);

    // execute migration
    await migrationService
        .migrate(args, repositories);

    // Connectors usually keep a pool of opened connections,
    // this keeps the process running even after all work is done.
    // We need to exit explicitly.
    process.exit(0);
}

migrate(process.argv).catch(err => {
    console.error('Cannot migrate database schema', err);
    process.exit(1);
});

Migration Usage

Create Migration File

To create a migration file, type the following:

$ npm run migrate create <migration-name>

where the <migration-name> is the name of the migration file

Example: npm run migrate create initial-data

migration file will be generated inside project-folder/src/migrations/. Note that when running the command npm run migrate <cmds...> loopback automatically rebuilds the project. If in case it did not rebuild, you must rebuild it manually by running npm run rebuild.

Run Migration Up

To run a migration, type the following:

$ npm run migrate up

This will run all migrations that are not yet executed sorted from oldest to latest Note that this will run only whats inside the up method of the migration file

Run Migration Down

If you have created a query for reverting your previous migration, make sure to put it inside the down function. To revert a migration, type the following:

$ npm run migrate down <filename>

This will run some existing migrations that matches the filename. This only executes files that are already migrated. Note that this will run only whats inside the down method of the migration file

Run Migration in Test Mode

If you want to run a migration but don't want to mark it as migrated, you can use the test keyword

$ npm run migrate up test

This will run all migrations that are not yet executed sorted from oldest to latest Note that migrations will not be marked as migrated. Any changes to your database will be executed so be careful and test it to a test database instead.

Backup Database before running Migration

If you want to backup your database before running a migration, you can use the backup keyword

$ npm run migrate backup

This will backup your database.

Restore Backup Database

If you want to restore your database, you can use the restore <backup-dir> keyword

$ npm run migrate restore db_01-16-2023_103407

This will restore your database. No need to put the exact directory since you already set it in DB_BACKUP_DIR, just the exact folder name of the backup to restore.

0.3.4

8 months ago

0.3.3

8 months ago

0.3.0

1 year ago

0.3.2

1 year ago

0.3.1

1 year ago

0.2.1

2 years ago

0.2.2

2 years ago

0.2.0

2 years ago

0.1.5

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.0.2

2 years ago