npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

loopback-mongodb-migrate

v0.3.4

Published

loopback mongodb migration extension

Downloads

19

Readme

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:[email protected]: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.