Skip to content

fullcube/loopback-db-migrate

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A library to add simple database migration support to loopback projects.

Dependencies Circle CI

Migrations that have been run will be stored in a table called 'Migrations'. The library will read the loopback datasources.json files based on the NODE_ENV environment variable just like loopback does. The usage is based on the node-db-migrate project.

Configuration

To initialize, add the following in server.js or a boot script:

var migrate = require('loopback-db-migrate');
var options = {
  dataSource: ds, // Data source for migrate data persistence (defaults to 'db'),
  migrationsDir: path.join(__dirname, 'migrations'), // Migrations directory.
  enableRest: true // Expose migrate and rollback methods via REST api.
};
migrate(
  app, // The app instance
  options // The options
);

Running Migrations

Migrations can be run by calling the static migrate method on the Migration model. If you do not specify a callback, a promise will be returned.

Run all pending migrations:

Migrate.migrate('up', function(err) {});

Run all pending migrations upto and including 0002-somechanges:

Migrate.migrate('up', '0002-somechanges', function(err) {});

Rollback all migrations:

Migrate.migrate('down', function(err) {});

Rollback migrations upto and including 0002-somechanges:

Migrate.migrate('down', '0002-somechanges', function(err) {});

Example migrations

module.exports = {
  up: function(app, next) {
    app.models.Users.create({ ... }, next);
  },
  down: function(app, next) {
    app.models.Users.destroyAll({ ... }, next);
  }
};
/* executing raw sql */
module.exports = {
  up: function(app, next) {
    app.dataSources.mysql.connector.query('CREATE TABLE `my_table` ...;', next);
  },
  down: function(app, next) {
   app.dataSources.mysql.connector.query('DROP TABLE `my_table`;', next);
  }
};

About

simple db migrations for loopback

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%