🎯 Why Mongeese CLI?
Auto-Generate Migrations
Eliminate manual migration scripts by automatically detecting schema changes between your code and database.
Prevent Schema Drift
Track all changes between your Mongoose models and database to prevent silent schema inconsistencies.
NestJS Integration
Seamlessly works with NestJS and Mongoose projects with automatic model detection.
Safe Rollbacks
Generate both up and down migration commands for safe rollback operations.
CI/CD Ready
Version-controlled migration files perfect for deployment pipelines and team collaboration.
Framework Agnostic
Works great with NestJS but is framework-agnostic - use with any Node.js MongoDB project.
🚀 Quick Start
# Detect changes and generate migration npx mongeese-cli generate
# Apply pending migrations npx mongeese-cli migrate up
# Show migration status npx mongeese-cli migrate status
📦 Installation
Global Installation
Install globally to use the mongeese
command anywhere:
# Using yarn yarn global add mongeese-cli
Using npx (Recommended)
No installation required - run commands directly:
Requirements
- Node.js >= 16
- Mongoose >= 7
- Active MongoDB connection
Works great with NestJS but is framework-agnostic. Compatible with any Node.js project using Mongoose.
🛠 Commands
Command | Description | Options |
---|---|---|
mongeese init |
Initialize Mongeese in your project and create connection configuration | None |
mongeese generate |
Generate migration file by comparing schemas with database state |
-n, --name <name> Custom migration name |
mongeese migrate |
Show migration status (default) | None |
mongeese migrate up |
Apply all pending migrations |
-t, --target <target> Target migration filename |
mongeese migrate down |
Rollback last migration |
-t, --target <target> Target migration filename |
mongeese migrate status |
Show detailed migration status and history | None |
Command Examples
# Rollback to specific migration mongeese migrate down --target 20240501_120000_add_users
# Check what migrations are pending mongeese migrate status
🔧 Configuration
After running mongeese init
, you'll get a connection
file that you need to configure:
TypeScript Projects
import { MongoClient, Db } from "mongodb";
import * as dotenv from "dotenv";
dotenv?.config();
// DbWithClient type that extends Db with an attached client property
export interface DbWithClient extends Db {
client: MongoClient;
}
/** Factory function that returns a DbWithClient object with the client attached */
export async function getDbWithClient(dbName?: string): Promise<DbWithClient> {
const client = new MongoClient(process.env.MONGODB_URI);
await client.connect();
const db = client.db(dbName);
(db as DbWithClient).client = client;
return db as DbWithClient;
}
JavaScript Projects
import { MongoClient } from "mongodb";
import * as dotenv from "dotenv";
dotenv?.config();
/** Factory function that returns a DbWithClient object with the client attached */
export async function getDbWithClient(dbName) {
const client = new MongoClient(process.env.MONGODB_URI);
await client.connect();
const db = client.db(dbName);
db.client = client;
return db;
}
Environment Variables
Make sure to set your MongoDB connection URI:
MONGODB_URI=mongodb://localhost:27017/your-database
# or for MongoDB Atlas
MONGODB_URI=mongodb+srv://username:password@cluster.mongodb.net/database