A library to add simple database migration support to loopback projects.
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
- Install in you loopback project:
npm install --save loopback-component-migrate
-
Create a component-config.json file in your server folder (if you don't already have one)
-
Enable the component inside
component-config.json
.
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:
Migrate;
Run all pending migrations upto and including 0002-somechanges:
Migrate;
Rollback all migrations:
Migrate;
Rollback migrations upto and including 0002-somechanges:
Migrate;
Example migrations
moduleexports = { appmodelsUsers; } { appmodelsUsers; };
/* executing raw sql */moduleexports = { appdataSourcesmysqlconnector; } { appdataSourcesmysqlconnector; };