Giter Club home page Giter Club logo

loopback-component-migrate's Introduction

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

Dependencies

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.

Installation

  1. Install in you loopback project:

npm install --save loopback-component-migrate

  1. Create a component-config.json file in your server folder (if you don't already have one)

  2. Enable the component inside component-config.json.

{
  "loopback-component-migrate": {
    "key": "value"
  }
}

Options:

  • log

    [String] : Name of the logging class to use for log messages. (default: 'console')

  • enableRest

    [Boolean] : A boolean indicating wether migrate/rollback REST api methods should be exposed on the Migration model. (default: false)

  • migrationsDir

    [Boolean] : Directory containing migration scripts. (default: server/migrations)

  • dataSource

    [Boolean] : Datasource to connect the Migration and MigrationMap models to. (default: db)

  • acls

    [Array] : ACLs to apply to Migration and MigrationMap models. (default: [])

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);
  }
};

loopback-component-migrate's People

Contributors

alexthewilde avatar elhoyos avatar listepo avatar slively avatar slivelygh avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.