项目作者: fullcube

项目描述 :
Migration framework for loopback
高级语言: JavaScript
项目地址: git://github.com/fullcube/loopback-component-migrate.git
创建时间: 2015-10-27T10:40:14Z
项目社区:https://github.com/fullcube/loopback-component-migrate

开源协议:

下载


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

Greenkeeper badge

  1. Install in you loopback project:

    npm install --save loopback-component-migrate

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

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

    1. {
    2. "loopback-component-migrate": {
    3. "key": "value"
    4. }
    5. }

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

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

  • dataSource

    [String] : 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:

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

Run all pending migrations upto and including 0002-somechanges:

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

Rollback all migrations:

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

Rollback migrations upto and including 0002-somechanges:

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

Example migrations

  1. module.exports = {
  2. up: function(app, next) {
  3. app.models.Users.create({ ... }, next);
  4. },
  5. down: function(app, next) {
  6. app.models.Users.destroyAll({ ... }, next);
  7. }
  8. };
  1. /* executing raw sql */
  2. module.exports = {
  3. up: function(app, next) {
  4. app.dataSources.mysql.connector.query('CREATE TABLE `my_table` ...;', next);
  5. },
  6. down: function(app, next) {
  7. app.dataSources.mysql.connector.query('DROP TABLE `my_table`;', next);
  8. }
  9. };