A Loopback.io tool for easy handly db migrations and seeders
npm install loopback-migration-toolloopback-migration-tool expose the lb-migration script that ables you easy
handle migrations and seeders for Loopback.io
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./seeds/*.js
Seed your models with files in .``
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 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]
You can use a .lb-migrationrc.json to save your common migration settings.
| Property | Description | Type | Values | Default | As argument |
|:--------: |:---------------------------------------------: |:------: |:------: |:--------------------: |:------------------------------------------------------------: |
| app | Path to your loopback applicationmain file. | String | | ./server/server.js | -a --app --loopback-app |
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 |
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 |
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 app.models
where you can find all the loopback app data, that includes the 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.
}
``