Registry /
database / loopback-component-migration
Install & Compatibility
Where this runs
No compatibility data collected yet for this library.
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
Migration model
✓ const Migration = app.models.Migration;
✗ const Migration = require('loopback-component-migration');
Model is auto-registered on the app; access via app.models.Migration.
Migration options
✓ // in component-config.json
{
"loopback-component-migration": {
"migrationsDir": "./path",
"dataSource": "myDB",
"enableRest": true
}
}
✗ // Trying to require component directly
const migration = require('loopback-component-migration');
Configuration done via component-config.json, not direct require.
Migration script
✓ module.exports = {
up: function(app, next) { /* ... */ next(); },
down: function(app, next) { /* ... */ next(); }
};
✗ module.exports = {
up: (app) => { /* ... */ },
down: (app) => { /* ... */ }
};
Must call next() callback to signal completion; asynchronous scripts require next().
Configure the migration component in component-config.json, define a migration script, and run all pending migrations.
// server/component-config.json
{
"loopback-component-migration": {
"migrationsDir": "./server/migrations",
"dataSource": "db",
"enableRest": true
}
}
// server/migrations/001-initial.js
module.exports = {
up: function(app, next) {
var ds = app.dataSources.db;
ds.automigrate('MyModel', function(err) {
if (err) return next(err);
ds.autoupdate('MyModel', function(err) {
next(err);
});
});
},
down: function(app, next) {
app.dataSources.db.automigrate('MyModel', function(err) {
next(err);
});
}
};
// Running migrations from boot script or manually
app.models.Migration.migrate('up', '', function(err) {
if (err) console.error(err);
else console.log('Migrations complete.');
});
Errors
Common errors & fixes
Cannot find module 'loopback-component-migration'
Package not installed or not listed in dependencies.
fixRun: npm install --save loopback-component-migration
TypeError: app.models.Migration.migrate is not a function
Migration model not registered yet; component-config.json might be misconfigured or not loaded.
fixEnsure component-config.json has valid JSON and the component object is correct: {"loopback-component-migration": {}} ENOENT: no such file or directory, open '.../migrations'
Default migrationsDir does not exist.
fixCreate the migrations directory or set a custom path in component-config.json with "migrationsDir":"path/to/migrations".
Error: Callback was already called
Migration script called next() multiple times in up/down function.
fixEnsure you call next() only once, typically at the end of the operation.
Audit
Dependencies
loopbackrequiredPeer dependency required for LoopBack 3.x integration