loopback-migration-tool
v1.4.0
Published
A Loopback.io tool for easy handly db migrations and seeders
Downloads
61
Maintainers
Readme
loopback-migration-tool
loopback-migration-tool expose the lb-migration
script that ables you easy
handle migrations and seeders for Loopback.io
Usage
Install Global
Install with Yarn
yarn global add loopback-migration-tool
or install with NPM
npm i -g loopback-migration-tool
Migrate all your models on all datasources.
lb-migration migrate
Migrate all your models on specific datasources.
lb-migration migrate --ds=datasource1 datasource2
Migrate specific models on specific datasources.
lb-migration migrate --ds=datasource1 datasource2 --model=model1 model2 model3
Migrate specific models using autoupdate method.
lb-migration migrate --model=model1 model2 model3 --method=update
Seed your models with files in ./seeds/*.js
.
lb-migration seed
Seed your models with files in multiple locations.
lb-migration seed --src=path/to/files1/*.js path/to/files2/*.js path/to/files3/*.js
Install as dev dependency
Install with Yarn
yarn add loopback-migration-tool --dev
or install with NPM
npm i loopback-migration-tool --save-dev
In your project root.
node_modules/.bin/lb-migration migrate [options]
options
.lb-migrationrc.json
You can use a .lb-migrationrc.json
to save your common migration settings.
Global options
| Property | Description | Type | Values | Default | As argument | |:--------: |:---------------------------------------------: |:------: |:------: |:--------------------: |:------------------------------------------------------------: | | app | Path to your loopback applicationmain file. | String | | ./server/server.js | -a --app --loopback-app |
migrate
Important: Loopback's datasources uses two methods to migrate your models.
- Auto-migration: Drop existing schema objects if they exist, and re-create them based on model definitions. Existing data will be lost.
- Auto-update: Detect the difference between schema objects and model definitions, and alter the database schema objects. Keep existing data.
Be careful what method must you use.
| Property | Description | Type | Values | Default | As argument | |:----------: |:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |:---------------------: |:-----------------: |:-------: |:-----------------------------: | | ds |Datasources that will be migrated.If empty or not present, all datasources will be migrates. | String, [String] | | [] | -d --ds --datasource | | model |Models in the selected datasourcesthat will be migrated. If empty or not present,all models in all selected datasources willbe migrates. Selected Models not presents inselected datasources will be not migrated. | [String] | | [] | --mod --model | | ignored-model |Models in the selected datasourcesthat will be not migrated. | [String] | | [] | --imod --ignored-model | | method |Loopback migration method to use.Loopback uses automigrate and autoupdate methodsfor migrations. | String | update migrate | migrate | -m --method |
seed
Important: seed
command will destroy all existing models' data calling to destroyAll method before start to seed them.
| Property | Description | Type | Values | Default | As argument | |:--------: |:---------------------------------: |:-----------: |:------: |:----------------: |:-----------------------------: | | src | File glob to your seeders files. | String | | "./seeds/*.js" | -s --src --sources |
Seeder file
A seeder file is just a js module that exports a function where you fill your model's data. Seeder files are executed in lexicographic order.
The exported function receive a loopback-app
instance as argument
where you can find all the loopback app data, that includes the app.models
object.
You can find a example here
./seeds/index.js
module.exports = function (app, cb) {
let Travel = app.models.Travel
let promises = _.map(data, function (entry) {
let passengers = entry.passengers
delete entry.passengers
return Travel.create(entry)
.then((travel) => travel.passengers.create(passengers))
})
return Promise.all(promises) //Or you can use cb argument when all is done, but not both.
}