https://github.com/baethon/mongoose-lazy-migration
Migrates on-fly fetched Mongoose documents.
https://github.com/baethon/mongoose-lazy-migration
mongodb mongoose mongoosejs npm
Last synced: about 1 month ago
JSON representation
Migrates on-fly fetched Mongoose documents.
- Host: GitHub
- URL: https://github.com/baethon/mongoose-lazy-migration
- Owner: baethon
- License: mit
- Created: 2019-12-30T20:30:00.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-05T03:55:38.000Z (over 3 years ago)
- Last Synced: 2025-05-13T19:29:35.545Z (about 1 year ago)
- Topics: mongodb, mongoose, mongoosejs, npm
- Language: JavaScript
- Homepage:
- Size: 1.1 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 🖤💛 mongoose-lazy-migration [](https://travis-ci.org/baethon/mongoose-lazy-migration)
Migrates on-fly fetched Mongoose documents.
## Installation
```
npm i @baethon/mongoose-lazy-migration
```
## Usage
Define model schema:
```js
const mongoose = require('mongoose')
const { Schema } = mongoose
const userSchema = new Schema({
fullname: String
})
```
Wrap it with `withMigrations()` helper:
```js
const { withMigrations, migration: m } = require('@baethon/mongoose-lazy-migration')
const migrations = [
m(1, (user) => ({
fullname: `${user.firstname} ${user.lastname}`
}))
]
const User = mongoose.model('User', withMigrations(userSchema, migrations))
```
Use `User` model as always.
## Under the hood
### Migrations
The `migrations` array contains list of objects that contain:
- schema version number
- migration callback that is applied to the document
`m()` is a convenience wrapper.
Schema versioning is not opinionated (it can be consecutive numbers, or date of writing), however, it's required to use an integer value.
Migration callback should be a pure function. Due to the structure of Mongoose hooks, the migration callback **has to be synchronous**.
### Schema
The `withMigrations()` function appends `schemaVersion` property to the model. It defines the _latest_ schema version used at the time of saving. The _latest_ version is taken from the last element of the `migrations` array.
By default `schemaVersion` is indexed. This, can be disabled by setting the `index: false` option.
```js
const User = mongoose.model('User', withMigrations(userSchema, migrations, { index: false }))
```
### Runtime
Whenever a document is fetched the library will compare its `schemaVersion` with the _latest_ schema version.
On mismatch it will apply the migration callbacks in following way:
- when `schemaVersion === NULL`:
- apply all of the migration callbacks
- otherwise:
- determine the index of the `schemaVersion` in the `migrations` array
- apply following migration callbacks
- update the `schemaVersion` property
The document **is not saved** after the migration.
## Testing
The package provides both unit tests and integration tests. The test suite requires access to MongoDB instance (see `ava.config.js`).
You can use `docker-compose` to setup local instance:
```
docker-compose up -d
```
To run the tests use:
```
yarn test
```