{"id":23512771,"url":"https://github.com/baethon/mongoose-lazy-migration","last_synced_at":"2026-05-03T10:33:36.724Z","repository":{"id":44020366,"uuid":"230975839","full_name":"baethon/mongoose-lazy-migration","owner":"baethon","description":"Migrates on-fly fetched Mongoose documents.","archived":false,"fork":false,"pushed_at":"2023-01-05T03:55:38.000Z","size":1153,"stargazers_count":0,"open_issues_count":8,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-13T19:29:35.545Z","etag":null,"topics":["mongodb","mongoose","mongoosejs","npm"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/baethon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-12-30T20:30:00.000Z","updated_at":"2020-12-12T15:46:06.000Z","dependencies_parsed_at":"2023-02-03T09:47:23.345Z","dependency_job_id":null,"html_url":"https://github.com/baethon/mongoose-lazy-migration","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/baethon/mongoose-lazy-migration","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baethon%2Fmongoose-lazy-migration","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baethon%2Fmongoose-lazy-migration/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baethon%2Fmongoose-lazy-migration/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baethon%2Fmongoose-lazy-migration/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/baethon","download_url":"https://codeload.github.com/baethon/mongoose-lazy-migration/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baethon%2Fmongoose-lazy-migration/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":274390719,"owners_count":25276408,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-09T02:00:10.223Z","response_time":80,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mongodb","mongoose","mongoosejs","npm"],"created_at":"2024-12-25T13:19:25.037Z","updated_at":"2026-05-03T10:33:31.690Z","avatar_url":"https://github.com/baethon.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 🖤💛 mongoose-lazy-migration [![Build Status](https://travis-ci.org/baethon/mongoose-lazy-migration.svg?branch=master)](https://travis-ci.org/baethon/mongoose-lazy-migration)\n\nMigrates on-fly fetched Mongoose documents.\n\n## Installation\n\n```\nnpm i @baethon/mongoose-lazy-migration\n```\n\n## Usage\n\nDefine model schema:\n\n```js\nconst mongoose = require('mongoose')\nconst { Schema } = mongoose\n\nconst userSchema = new Schema({\n    fullname: String\n})\n```\n\nWrap it with `withMigrations()` helper:\n\n```js\nconst { withMigrations, migration: m } = require('@baethon/mongoose-lazy-migration')\n\nconst migrations = [\n    m(1, (user) =\u003e ({\n        fullname: `${user.firstname} ${user.lastname}`\n    }))\n]\n\nconst User = mongoose.model('User', withMigrations(userSchema, migrations))\n```\n\nUse `User` model as always.\n\n## Under the hood\n\n### Migrations\n\nThe `migrations` array contains list of objects that contain:\n\n- schema version number\n\n- migration callback that is applied to the document\n\n`m()` is a convenience wrapper.\n\nSchema versioning is not opinionated (it can be consecutive numbers, or date of writing), however, it's required to use an integer value.\n\nMigration callback should be a pure function. Due to the structure of Mongoose hooks, the migration callback **has to be synchronous**.\n\n### Schema\n\nThe `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.\n\nBy default `schemaVersion` is indexed. This, can be disabled by setting the `index: false` option.\n\n```js\nconst User = mongoose.model('User', withMigrations(userSchema, migrations, { index: false }))\n```\n\n### Runtime\n\nWhenever a document is fetched the library will compare its `schemaVersion` with the _latest_ schema version.\n\nOn mismatch it will apply the migration callbacks in following way:\n\n- when `schemaVersion === NULL`:\n  \n  - apply all of the migration callbacks\n\n- otherwise:\n  \n  - determine the index of the `schemaVersion` in the `migrations` array\n  \n  - apply following migration callbacks\n\n- update the `schemaVersion` property\n\nThe document **is not saved** after the migration.\n\n## Testing\n\nThe package provides both unit tests and integration tests. The test suite requires access to MongoDB instance (see `ava.config.js`).\n\nYou can use `docker-compose` to setup local instance:\n\n```\ndocker-compose up -d\n```\n\nTo run the tests use:\n\n```\nyarn test\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbaethon%2Fmongoose-lazy-migration","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbaethon%2Fmongoose-lazy-migration","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbaethon%2Fmongoose-lazy-migration/lists"}