https://github.com/hellivan/mongoose-model-migration
Library for up/downgrading and versioning mongoose models
https://github.com/hellivan/mongoose-model-migration
collection migration models mongodb mongoose nodejs typescript versioning
Last synced: about 1 year ago
JSON representation
Library for up/downgrading and versioning mongoose models
- Host: GitHub
- URL: https://github.com/hellivan/mongoose-model-migration
- Owner: hellivan
- License: mit
- Created: 2016-12-19T21:58:50.000Z (over 9 years ago)
- Default Branch: develop
- Last Pushed: 2023-01-07T04:37:46.000Z (over 3 years ago)
- Last Synced: 2025-04-29T23:35:54.005Z (about 1 year ago)
- Topics: collection, migration, models, mongodb, mongoose, nodejs, typescript, versioning
- Language: TypeScript
- Homepage:
- Size: 1.28 MB
- Stars: 1
- Watchers: 2
- Forks: 2
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# mongoose-model-migration
[](https://circleci.com/gh/hellivan/mongoose-model-migration)
[](https://codecov.io/gh/hellivan/mongoose-model-migration)
[](LICENSE)
[](https://github.com/semantic-release/semantic-release)
[](https://renovatebot.com/)
[](https://www.npmjs.com/package/mongoose-model-migration)
[](https://www.npmjs.com/package/mongoose-model-migration)
Utility library that provides some basic mechanisms for versioning and upgrading mongoose models in Node.js
## Installation
Using npm:
```
npm install --save mongoose-model-migration
```
Using yarn
```
yarn add mongoose-model-migration
```
## Usage
In ES6 / Typescript
```typescript
import { migrateModel, migrateCollection } from 'mongoose-model-migration';
```
### Basic Collection migration
```typescript
import { migrateCollection, CollectionMigrationHandler } from 'mongoose-model-migration';
const migrationHandler: CollectionMigrationHandler = {
up: async (db, collection, fromVersion, toVersion) => {
// ... call upgrate operations for collection
},
down: async (db, collection, fromVersion, toVersion) => {
// ... call downgrade operations for collection
}
};
await migrateCollection('users', 2, migrationHandler);
```
### Collection migration options
`migrateCollection` accepts an optional options object as 4th parameter:
| option | Description |
| --------------------- | ---------------------------------------------------------------------------------------------------------------------------------- |
| db | Optionally specify the mongodb database that should be used during migration. Defaults to the global mongoose `connection.db` |
| versionCollectionName | Optionally specify the collection name that should be used to store version information. Defaults to `collectionName + '.version'` |