{"id":15908039,"url":"https://github.com/xpepermint/mongodb-builder","last_synced_at":"2025-03-21T21:31:58.324Z","repository":{"id":57301774,"uuid":"114409628","full_name":"xpepermint/mongodb-builder","owner":"xpepermint","description":"MongoDB migration framework.","archived":false,"fork":false,"pushed_at":"2023-04-19T17:56:39.000Z","size":241,"stargazers_count":9,"open_issues_count":7,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-18T05:34:37.696Z","etag":null,"topics":["migration","mongodb","seed"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xpepermint.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-12-15T20:42:40.000Z","updated_at":"2024-06-10T01:33:33.000Z","dependencies_parsed_at":"2024-10-28T11:36:11.781Z","dependency_job_id":"fc391881-d511-4b19-be47-82627fef5ec4","html_url":"https://github.com/xpepermint/mongodb-builder","commit_stats":{"total_commits":13,"total_committers":1,"mean_commits":13.0,"dds":0.0,"last_synced_commit":"57498a4be9cbb6f5360eac5691567d65c348e800"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xpepermint%2Fmongodb-builder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xpepermint%2Fmongodb-builder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xpepermint%2Fmongodb-builder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xpepermint%2Fmongodb-builder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xpepermint","download_url":"https://codeload.github.com/xpepermint/mongodb-builder/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244874400,"owners_count":20524577,"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","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":["migration","mongodb","seed"],"created_at":"2024-10-06T14:09:00.307Z","updated_at":"2025-03-21T21:31:58.010Z","avatar_url":"https://github.com/xpepermint.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"![Build Status](https://travis-ci.org/xpepermint/mongodb-builder.svg?branch=master)\u0026nbsp;[![NPM Version](https://badge.fury.io/js/mongodb-builder.svg)](https://badge.fury.io/js/mongodb-builder)\u0026nbsp;[![Dependency Status](https://gemnasium.com/xpepermint/mongodb-builder.svg)](https://gemnasium.com/xpepermint/mongodb-builder)\n\n# mongodb-builder\n\n\u003e MongoDB migration framework.\n\nThis is a light weight open source package for NodeJS written with [TypeScript](https://www.typescriptlang.org). It's actively maintained, well tested and already used in production environments. The source code is available on [GitHub](https://github.com/xpepermint/mongodb-builder) where you can also find our [issue tracker](https://github.com/xpepermint/mongodb-builder/issues).\n\n## Installation\n\nRun the command below to install the package.\n\n```\n$ npm install --save mongodb-builder\n```\n\nThis package uses promises thus you need to use [Promise polyfill](https://github.com/taylorhakes/promise-polyfill) when promises are not supported.\n\n## Usage\n\nThe package provides two core classes. The `Migrator` class is used for running MongoDB migrations and the `Seeder` is used for performing seed operations.\n\n### Migrations\n\nMigrations are performed in a sequence based on the `index` parameter. If you pass a context object into `Migrator` class the object will be passed to each migration method. Methods `upgrade` and `downgrade` runs migration recipes and stores the last successfully performed migration `index` value in the database.\n\n```js\nimport { MongoClient } from 'mongodb';\nimport { Migrator } from 'mongodb-builder';\n\n// connecting to the database\nconst mongo = await MongoClient.connect('mongodb://localhost:27017');\n\n// initialize migration class\nconst migrator = new Migrator({\n  collection: mongo.db('test').collection('migrations'), // required\n  context: { foo: \"foo\" }, // optional\n});\n\n// register migrations (you could move this object into a separate file)\nmigrator.add({\n  upgrade: async (context) =\u003e { /* do something */ },\n  downgrade: async (context) =\u003e { /* do something */ },\n});\n\n// run `upgrade` migrations\nawait migrator.upgrade();\n```\n\n**Migrator({ db, context })**\n\n\u003e Performs MongoDB migration operations.\n\n| Option | Type | Required | Default | Description\n|--------|------|----------|---------|------------\n| collection | Collection | Yes | - | Instance of MongoDB collection.\n| context | Object | No | - | Object which is passed into each `up` and `down` method.\n\n**migrator.add({ upgrade, downgrade })**\n\n\u003e Registeres new migration recipe.\n\n| Option | Type | Required | Default | Description\n|--------|------|----------|---------|------------\n| upgrade | Function, Promise | Yes | - | Logic for upgrading the database.\n| downgrade | Function, Promise | Yes | - | Logic for downgrading the database.\n\n**migrator.addDir(path)**\n\n\u003e Registeres new migration recipes by loading files at `path`.\n\n| Option | Type | Required | Default | Description\n|--------|------|----------|---------|------------\n| path | String | Yes | - | Path to the directory with migration files.\n\n```js\nexport async function upgrade(context) {}\nexport async function downgrade(context) {}\n```\n\n**migrator.remove(index)**\n\n\u003e Unregisters migration recipe at index.\n\n| Option | Type | Required | Default | Description\n|--------|------|----------|---------|------------\n| index | Integer | Yes | - | Migration recipe index.\n\n**migrator.upgrade(index)**\n\n\u003e Runs a sequence of registered `upgrade` methods.\n\n| Option | Type | Required | Default | Description\n|--------|------|----------|---------|------------\n| index | Integer | No | - | Sequential number of the last performable method from the beginning.\n\n**migrator.downgrade(index)**\n\n\u003e Runs a sequence of registered `downgrade` methods.\n\n| Option | Type | Required | Default | Description\n|--------|------|----------|---------|------------\n| index | Integer | No | - | Sequential number of the last performable method from the beginning.\n\n**migrator.lastIndex()**\n\n\u003e Runs the last migration index that has already been performed.\n\n### Seeding\n\nSeed operations are similar to migrations. The difference is only that they can be performed multiple times.\n\n```js\nimport { MongoClient } from 'mongodb';\nimport { Seeder } from 'mongodb-builder';\n\n// connecting to the database\nconst mongo = await MongoClient.connect('mongodb://localhost:27017');\n\n// initialize migration class\nconst seeder = new Seeder(\n  collection: mongo.db('test').collection('migrations'), // required\n  context: { foo: \"foo\" }, // optional\n);\n\n// register migrations (you could move this object into a separate file)\nseeder.add({\n  perform: async (context) =\u003e { /* do something */ },\n});\n\n// run `perform` methods\nawait seeder.perform();\n```\n\n**Seeder({ db, context })**\n\n\u003e Performs MongoDB seed operations.\n\n| Option | Type | Required | Default | Description\n|--------|------|----------|---------|------------\n| collection | Collection | Yes | - | Instance of MongoDB collection.\n| context | Object | No | - | Object which is passed into each `perform` method.\n\n**seeder.add({ seed })**\n\n\u003e Registeres new seed recipe.\n\n| Option | Type | Required | Default | Description\n|--------|------|----------|---------|------------\n| perform | Function, Promise | Yes | - | Logic for seeding the database.\n\n**seeder.addDir(path)**\n\n\u003e Registeres new seed recipes by loading files at `path`.\n\n| Option | Type | Required | Default | Description\n|--------|------|----------|---------|------------\n| path | String | Yes | - | Path to the directory with seed files.\n\n```js\nexport async function perform(context) {}\n```\n\n**seeder.remove(index)**\n\n\u003e Unregisters seed recipe at index.\n\n| Option | Type | Required | Default | Description\n|--------|------|----------|---------|------------\n| index | Integer | Yes | - | Seed recipe index.\n\n**seeder.perform(index)**\n\n\u003e Runs a sequence of registered `upgrade` methods.\n\n| Option | Type | Required | Default | Description\n|--------|------|----------|---------|------------\n| index | Integer | No | - | Sequential number of the last performable method from the beginning.\n\n## License (MIT)\n\n```\nCopyright (c) 2017+ Kristijan Sedlak \u003cxpepermint@gmail.com\u003e\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated modelation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxpepermint%2Fmongodb-builder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxpepermint%2Fmongodb-builder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxpepermint%2Fmongodb-builder/lists"}