A lightweight migration tool for Mongoose (MongoDB), inspired by Django's migration system.
npm i -g mangoose-migrate
# or local
npm i -D mangoose-migrateOr use directly with npx (recommended):
npx mangoose-migrate [command]
# pnpm dlx mangoose-migrate [command]Before you start make sure you setup .env file or mangoose.config.js config file so you don't need to provide cli arguments on each command.
To generate a mangoose.config.js config file in your project root with default options:
npx mangoose-migrate initOther CLI commands:
npx mangoose-migrate make <name> # creates a migration file
npx mangoose-migrate migrate # run pending migrationsexport MONGODB_URI="mongodb://user:pass@localhost:27017/mydb?authSource=admin"// mangoose.config.js
export default {
connectionUri: process.env.MONGODB_URI,
migrationsPath: "./migrations",
options: {
authSource: "admin",
retryWrites: true,
// ...
},
};npx mangoose-migrate migrate \\
--connection-uri "mongodb://localhost:27017/mydb" \\
--migrations-path "./db/migrations"| Key | Required | Default | Description |
|---|---|---|---|
connectionUri |
Yes | - | MongoDB connection uri |
migrationsPath |
No | "./migrations" |
Path to migration files |
options |
No | {} |
Mongoose connection options |
import { Migration } from 'mangoose-migrate/core';
import { CreateModel } from 'mangoose-migrate/operations';
export default class InitialMigration extends Migration {
constructor() {
super('initial');
}
async up(db) {
this.addOperation(
new CreateModel('User', {
name: { type: String, required: true },
}),
);
}
}CreateModel(modelName, schema)AddField(modelName, fieldName, definition)
MIT. See LICENSE for more information.