Registry / database / loopback-component-migration

loopback-component-migration

JSON →
library1.0.3jsnpmunverified

A database migration component for LoopBack 3.x projects. Version 1.0.3, last updated in 2019. Adds simple migration support similar to node-db-migrate, with REST endpoints for migrate/rollback. Tracks run migrations in a 'Migrations' table. Lightweight alternative to loopback-migration with fewer features but direct datasource access.

npm install loopback-component-migration
INSTALL
IMPORT
SIG · LOOPBACK-COMPONENT
L
loopback-component-migration
databasejavascriptv1.0.3
harness data pending
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.'); });
Debug
Known issues
breakingloopback-component-migration only supports LoopBack 3.x, not 2.x or 4.x.
fix
Use loopback >=3.1.1 as peer dependency.
affects: >=1.0.0
deprecatedThis package is not actively maintained; last release in 2019.
fix
Consider alternatives like loopback-migration or migrate directly with a database library.
affects: >=1.0.0
gotchaMigrate.down with empty string rolls back all migrations; the second parameter cannot be omitted.
fix
Always pass a string for the second argument: Migration.migrate('down', '', callback).
affects: >=1.0.0
gotchaMigration scripts must call next() to proceed; forgetting to call next() will hang the migration process.
fix
Ensure every 'up' and 'down' function invokes next(err) exactly once.
affects: >=1.0.0
deprecatedREST API for migrations (enableRest) is considered a security risk in production.
fix
Keep enableRest false in production; run migrations programmatically or from a CLI.
affects: >=1.0.0
Errors
Common errors & fixes
Cannot find module 'loopback-component-migration'
Package not installed or not listed in dependencies.
fix
Run: 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.
fix
Ensure 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.
fix
Create 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.
fix
Ensure you call next() only once, typically at the end of the operation.
Upgrade
Version history
1.0.3latest on npm
Audit
Dependencies
loopbackrequiredPeer dependency required for LoopBack 3.x integration
Agent activity
2 hits · last 30 days
node
2
Resources
loopback-component-migration — npm install loopback-component-migration · libregistry