Skip to content

stabldev/mangoose-migrate

Repository files navigation

🥭 mangoose-migrate

NPM Version GitHub License GitHub Actions Workflow Status

A lightweight migration tool for Mongoose (MongoDB), inspired by Django's migration system.

Installation

npm i -g mangoose-migrate
# or local
npm i -D mangoose-migrate

Or use directly with npx (recommended):

npx mangoose-migrate [command]
# pnpm dlx mangoose-migrate [command]

Getting started with the CLI

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 init

Other CLI commands:

npx mangoose-migrate make <name> # creates a migration file
npx mangoose-migrate migrate # run pending migrations

Configuration

Option 1: Environment Variables

export MONGODB_URI="mongodb://user:pass@localhost:27017/mydb?authSource=admin"

Option 2: Config File

// mangoose.config.js
export default {
  connectionUri: process.env.MONGODB_URI,
  migrationsPath: "./migrations",
  options: {
    authSource: "admin",
    retryWrites: true,
    // ...
  },
};

Option 3: CLI Arguments

npx mangoose-migrate migrate \\
  --connection-uri "mongodb://localhost:27017/mydb" \\
  --migrations-path "./db/migrations"

Options

Key Required Default Description
connectionUri Yes - MongoDB connection uri
migrationsPath No "./migrations" Path to migration files
options No {} Mongoose connection options

Migration Example

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 },
      }),
    );
  }
}

Operations

  • CreateModel(modelName, schema)
  • AddField(modelName, fieldName, definition)

License

MIT. See LICENSE for more information.

About

🥭 A lightweight migration tool for Mongoose (MongoDB), inspired by Django's migration system.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors