Registry / database / mongo-migration-service

mongo-migration-service

JSON →
library0.1.2jsnpmunverified

A lightweight migration service for Node.js applications using MongoDB, version 0.1.2. It provides automatic migration execution and rollback support with TypeScript types. Key differentiators include simple migration file format (timestamp-name) and automatic tracking via a 'migrations' collection. It requires an existing Mongoose connection and supports sync/rollback operations.

npm install mongo-migration-service
INSTALL
IMPORT
SIG · MONGO-MIGRATION-SE
M
mongo-migration-service
databasejavascriptv0.1.2
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.

MongoMigrationService
import MongoMigrationService from 'mongo-migration-service'
const MongoMigrationService = require('mongo-migration-service')
Default export, not named. CommonJS require also works but TypeScript may need esModuleInterop.
ConnectionBase
import { ConnectionBase } from 'mongoose'
import { ConnectionBase } from 'mongo-migration-service'
ConnectionBase is a Mongoose type used in migration functions, not exported by this package.
run
export async function run(db: ConnectionBase) { ... }
export function run(db) { ... }
The run function must be a named export from migration files and accept a ConnectionBase argument.

Connects to MongoDB, creates a migration service with a migrations folder, runs all pending migrations, and disconnects.

import Mongoose from 'mongoose'; import MongoMigrationService from 'mongo-migration-service'; async function main() { await Mongoose.connect(process.env.MONGO_URI ?? 'mongodb://localhost:27017/test'); const migrationService = new MongoMigrationService('./migrations'); await migrationService.synchronize(); console.log('Migrations applied'); await Mongoose.disconnect(); } main().catch(console.error);
Debug
Known issues
gotchaMongoDB connection must be established before creating the service; no internal connection management.
fix
Call Mongoose.connect() before instantiating MongoMigrationService.
affects: >=0.1
gotchaMigration files must follow the format {timestamp}[-{name}].ts (e.g., 20230101000000-init.ts). Otherwise they are ignored.
fix
Rename migration files to match the required timestamp pattern.
affects: >=0.1
gotchaRollback only reverts the last applied migration batch, not arbitrary migrations.
fix
Call rollback() repeatedly to revert multiple batches, or implement custom logic.
affects: >=0.1
deprecatedThe package is in early development (v0.1.2) and may have breaking changes in minor versions.
fix
Pin to exact version or lock file, and test upgrades.
affects: >=0.1 <1.0
Errors
Common errors & fixes
TypeError: mongo_migration_service_1.default is not a constructor
Using named import instead of default import or wrong require style.
fix
Use: import MongoMigrationService from 'mongo-migration-service' (default import)
Error: No migrations directory found at path ...
Provided path to migrations folder does not exist or is incorrect.
fix
Ensure the directory exists and path is correct (absolute or relative to CWD).
MongooseError: The `uri` parameter to `openUri()` must be a string, got "undefined"
No MongoDB connection established before creating the service.
fix
Call Mongoose.connect() before new MongoMigrationService()
SyntaxError: Unexpected token 'export'
Migration file uses ES module syntax in a CommonJS project without proper configuration.
fix
Ensure 'type': 'module' in package.json or use .mjs extension, or transpile with TypeScript.
Upgrade
Version history
0.1.2latest on npm
Audit
Dependencies
mongooserequiredRequired for database connection and collection operations (ConnectionBase type)
Agent activity
9 hits · last 30 days
node
8
Resources
mongo-migration-service — npm install mongo-migration-service · libregistry