https://github.com/stabldev/mangoose-migrate
🥠A lightweight migration tool for Mongoose (MongoDB), inspired by Django's migration system.
https://github.com/stabldev/mangoose-migrate
cli migrations mongodb mongoose schema
Last synced: about 1 year ago
JSON representation
🥠A lightweight migration tool for Mongoose (MongoDB), inspired by Django's migration system.
- Host: GitHub
- URL: https://github.com/stabldev/mangoose-migrate
- Owner: stabldev
- License: mit
- Created: 2025-05-25T01:59:34.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-06-09T20:27:38.000Z (about 1 year ago)
- Last Synced: 2025-06-16T01:41:33.983Z (about 1 year ago)
- Topics: cli, migrations, mongodb, mongoose, schema
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/mangoose-migrate
- Size: 276 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# 🥠mangoose-migrate



A lightweight migration tool for Mongoose (MongoDB), inspired by Django's migration system.
## Installation
```bash
npm i -g mangoose-migrate
# or local
npm i -D mangoose-migrate
```
Or use directly with npx (recommended):
```bash
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:
```bash
npx mangoose-migrate init
```
Other CLI commands:
```bash
npx mangoose-migrate make # creates a migration file
npx mangoose-migrate migrate # run pending migrations
```
## Configuration
### Option 1: Environment Variables
```bash
export MONGODB_URI="mongodb://user:pass@localhost:27017/mydb?authSource=admin"
```
### Option 2: Config File
```js
// mangoose.config.js
export default {
connectionUri: process.env.MONGODB_URI,
migrationsPath: "./migrations",
options: {
authSource: "admin",
retryWrites: true,
// ...
},
};
```
### Option 3: CLI Arguments
```bash
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
```js
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](LICENSE) for more information.