Registry / database / loopback-component-migrate

loopback-component-migrate

JSON →
library0.4.0jsnpmunverified

A database migration framework for LoopBack projects, version 0.4.0. It provides simple up/down migration support, storing run migrations in a 'Migrations' table. Migrations are defined as modules with up/down functions receiving the app and a callback. The component reads LoopBack datasources configuration and supports raw SQL via the connector. Its key differentiator is tight integration with LoopBack's model and datasource system, unlike general migration tools like db-migrate. It exposes a REST API optionally and allows custom migration directories and ACLs. The package is maintained by fullcube with limited recent activity.

npm install loopback-component-migrate
INSTALL
IMPORT
SIG · LOOPBACK-COMPONENT
L
loopback-component-migrate
databasejavascriptv0.4.0
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.

Migrate
const Migrate = app.models.Migrate;
import { Migrate } from 'loopback-component-migrate';
The component adds a 'Migrate' model to the application; access it via app.models after boot.
component-config
// in component-config.json { "loopback-component-migrate": { "enableRest": true, "migrationsDir": "./server/migrations" } }
// Wrong: trying to import directly const migrate = require('loopback-component-migrate');
The package is configured via LoopBack's component-config.json, not imported as a module.
migration module
module.exports = { up: function(app, next) { /* ... */ }, down: function(app, next) { /* ... */ } };
// Wrong: using default export module.exports = { up: (app) => { /* sync? */ } };
Migrations must export an object with up and down functions, each receiving (app, next) and calling next().

Install, configure, create a migration script, and run migrations using the Migrate model.

// 1. Install // npm install --save loopback-component-migrate // 2. Enable in server/component-config.json // { // "loopback-component-migrate": { // "migrationsDir": "./server/migrations", // "dataSource": "db" // } // } // 3. Create a migration file server/migrations/001-example.js module.exports = { up: function(app, next) { app.models.Users.create([{name: 'Alice'}, {name: 'Bob'}], next); }, down: function(app, next) { app.models.Users.destroyAll({name: {inq: ['Alice', 'Bob']}}, next); } }; // 4. In a boot script or manually: const Migrate = app.models.Migrate; Migrate.migrate('up', function(err) { if (err) console.error(err); else console.log('Migrations ran successfully'); });
Debug
Known issues
gotchaThe Migrate model is only available after the LoopBack application boots. Attempting to access it before `app.start()` will fail.
fix
Access app.models.Migrate inside a boot script or after `app.start()` callback.
affects: all
gotchaMigrations run synchronously in sequence; if an error occurs, subsequent migrations are not executed and the error must be handled.
fix
Ensure each migration calls the next callback with an error or null.
affects: all
deprecatedThe package depends on LoopBack 2.x/3.x APIs. LoopBack 4 is not supported.
fix
Use loopback4-migration or another LoopBack 4 compatible migration tool.
affects: >=0.1.0 <1.0.0
Errors
Common errors & fixes
Error: Cannot find module 'loopback-component-migrate'
The package is not installed or the require path is incorrect.
fix
Run `npm install --save loopback-component-migrate` and ensure it is configured via component-config.json, not required directly.
TypeError: Migrate.migrate is not a function
The Migrate model is undefined or does not have a migrate method.
fix
Ensure that `loopback-component-migrate` is configured in component-config.json and that the app has booted before accessing app.models.Migrate.
Upgrade
Version history
0.4.0latest on npm
Audit
Dependencies
loopbackoptionalPeer dependency for LoopBack application context
Agent activity
2 hits · last 30 days
node
2
Resources
loopback-component-migrate — npm install loopback-component-migrate · libregistry