{"id":13657228,"url":"https://github.com/cdimascio/node-mongodb-fixtures","last_synced_at":"2025-06-25T07:03:16.189Z","repository":{"id":22870065,"uuid":"97535498","full_name":"cdimascio/node-mongodb-fixtures","owner":"cdimascio","description":"🍏 Setup and tear down test fixtures with MongoDB.","archived":false,"fork":false,"pushed_at":"2023-08-30T23:43:00.000Z","size":692,"stargazers_count":93,"open_issues_count":10,"forks_count":11,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-05-19T23:04:42.054Z","etag":null,"topics":["fixture-loader","fixtures","hacktoberfest","mongodb","test-driven-development","testing"],"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/cdimascio.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null},"funding":{"custom":"https://buymeacoffee.com/m97tA5c"}},"created_at":"2017-07-18T00:48:30.000Z","updated_at":"2024-11-01T04:31:23.000Z","dependencies_parsed_at":"2024-01-15T20:47:44.150Z","dependency_job_id":"b54f2a15-7f4e-4522-9714-2f2d31f37f32","html_url":"https://github.com/cdimascio/node-mongodb-fixtures","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"purl":"pkg:github/cdimascio/node-mongodb-fixtures","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cdimascio%2Fnode-mongodb-fixtures","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cdimascio%2Fnode-mongodb-fixtures/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cdimascio%2Fnode-mongodb-fixtures/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cdimascio%2Fnode-mongodb-fixtures/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cdimascio","download_url":"https://codeload.github.com/cdimascio/node-mongodb-fixtures/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cdimascio%2Fnode-mongodb-fixtures/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261161765,"owners_count":23118205,"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":["fixture-loader","fixtures","hacktoberfest","mongodb","test-driven-development","testing"],"created_at":"2024-08-02T05:00:38.870Z","updated_at":"2025-06-25T07:03:16.163Z","avatar_url":"https://github.com/cdimascio.png","language":"JavaScript","funding_links":["https://buymeacoffee.com/m97tA5c","https://www.buymeacoffee.com/m97tA5c"],"categories":["JavaScript"],"sub_categories":[],"readme":"# node-mongodb-fixtures\n\n![](https://img.shields.io/badge/status-stable-green.svg) [![](https://img.shields.io/npm/v/node-mongodb-fixtures.svg)](https://www.npmjs.com/package/node-mongodb-fixtures) ![](https://img.shields.io/npm/dm/node-mongodb-fixtures.svg) [![](https://img.shields.io/gitter/room/cdimascio-oss/community?color=%23eb205a)](https://gitter.im/cdimascio-oss/community) \n![](https://img.shields.io/badge/license-MIT-blue.svg)\n\n**Setup and tear down test fixtures with MongoDB.**\n\nUse custom scripts to create indexes and more!\n\n![](https://raw.githubusercontent.com/cdimascio/node-mongodb-fixtures/master/assets/mongodb-fixtures-logo.png)\n\n[![GitHub stars](https://img.shields.io/github/stars/cdimascio/node-mongodb-fixtures.svg?style=social\u0026label=Star\u0026maxAge=2592000)](https://GitHub.com/cdimascio/node-mongodb-fixtures/stargazers/) [![Twitter URL](https://img.shields.io/twitter/url/https/github.com/cdimascio/node-mongodb-fixtures.svg?style=social)](https://twitter.com/intent/tweet?text=Check%20out%20node-mongodb-fixtures%20by%20%40CarmineDiMascio%20https%3A%2F%2Fgithub.com%2Fcdimascio%2Fnode-mongodb-fixtures%20%F0%9F%91%8D)\n\n## Install\n\n```shell\nnpm install node-mongodb-fixtures\n```\n\n### CLI\n\nFor CLI use, it can be useful to install globally:\n\n```shell\nnpm install node-mongodb-fixtures -g\n```\n\n## Usage\n\n### Programmatic\n\n```javascript\nconst Fixtures = require('node-mongodb-fixtures');\nconst fixtures = new Fixtures();\n\nfixtures\n  .connect('mongodb://localhost:27017/mydb')\n  .then(() =\u003e fixtures.unload())\n  .then(() =\u003e fixtures.load())\n  .then(() =\u003e fixtures.disconnect());\n```\n\n[See detailed programmatic usage below](#programmatic-usage)\n\n### CLI\n\n```shell\n❯ mongodb-fixtures load -u mongodb://localhost:27017/mydb\n```\n\n[See detailed cli usage below](#cli-usage)\n\n---\n\n## Try the Example\n\nThe following example will load the example fixtures into a locally running MongoDB. To use another DB modify the the connection url accordingly (in step 2)\n\n### Run the example\n\n1. Clone the repo\n\n```shell\ngit clone https://github.com/cdimascio/node-mongodb-fixtures \u0026\u0026 cd node-mongodb-fixtures \u0026\u0026 npm install\n```\n\n2. Load the fixtures into MongoDb\n\n```shell\nnode bin/mongodb-fixtures load -u mongodb://localhost:27017/mydb --path ./examples/fixtures\n```\n\n---\n\n## Create fixtures\n\n#### How\n\n1.  Choose a directory for your fixtures e.g. `./fixtures`\n2.  Create any mix of JSON (`.json`), JavaScript (`.js`) files. (see file rules below)\n3.  Each filename defines a MongoDB collection\n\n**JSON Files**\n\nThe name of the JSON file is/will be the collection name. Each JSON file **_must_** contain a 'JSON Array of JSON objects'. Each JSON object is loaded as a document in the collection.\n\nJSON files are useful when you can represent all of your documents as JSON.\n\n```json\n[\n  { \"name\": \"Paul\", \"age\": 36 }, \n  { \"name\": \"Phoebe\", \"age\": 26 }\n]\n```\n\n**JavaScript Files**\n\nThe name of the JSON file is/will be the collection name. Each `.js` file must return a 'JSON Array of JSON objects'. Each JSON object is loaded as a document in the collection.\n\nJavaScript files are useful when you require code to represent your documents.\n\n```javascript\nconst { ObjectID: ObjectId } = require('mongodb');\n\nmodule.exports = [\n  { _id: ObjectId(), name: 'Paul', 'age': 36 },\n  { _id: ObjectId(), name: 'Phoebe', 'age': 26 },\n];\n```\n\n#### Example Structure\n\n```\nfixtures/\n|-- people.js\n|-- places.json\n```\n\nSee `./examples/fixtures`\n\n## Collection Scripts: Indexes and more...\n\n\"Collection scripts\" enable you to inject your own custom logic in the fixture creation lifecycle. Each custom script is passed a reference to a MongoDB collection. You may use this reference to modify the collection however you like. For example, you can add indexes and more.\n\n### How\n\n1.  Create a new JavaScript file with an underscore `_` suffix. e.g. `people_.js`.\n2.  The `_` denotes a script. The text preceding it, `people`, is the collection name.\n3.  Each script is passed a single argument, the collection.\n4.  Each must return a `function` that takes a `collection` and returns a `Promise`.\n\n#### Example\n\n```javascript\n// people_.js\nmodule.exports = function(collection) {\n  // Write your custom logic and return a promise\n  return collection.createIndex( { \"address.city\": 1 }, { unique: false } );\n}\n```\n\n```\nfixtures/\n|-- people_.js\n|-- people.js\n|-- places.json\n```\n\n**Note:** Custom scripts run after all fixtures have completed.\n\n## Programmatic Usage\n\n### Init\n\nuse the default fixtures directory,`./fixtures`\n\n```javascript\nconst Fixtures = require('node-mongodb-fixtures');\nconst fixtures = new Fixtures();\n```\n\nor specifiy the fixtures directory\n\n```javascript\nconst Fixtures = require('node-mongodb-fixtures');\nconst fixtures = new Fixtures({\n  dir: 'examples/fixtures',\n  mute: false, // do not mute the log output\n});\n```\n\nor filter the fixtures present in the directory with a Regex pattern\n\n```javascript\nconst Fixtures = require('node-mongodb-fixtures');\nconst fixtures = new Fixtures({\n  dir: 'examples/fixtures',\n  filter: 'people.*',\n});\n```\n\n### Connect\n\nUse the standard MongoDB [URI connection scheme](https://docs.mongodb.com/manual/reference/connection-string/)\n\n```javascript\nfixtures.connect('mongodb://localhost:27017/mydb'); // returns a promise\n```\n\n**connect(uri, options, dbName)**\n\n| arg       | type              | description                                                                                                                |\n| --------- | ----------------- | -------------------------------------------------------------------------------------------------------------------------- |\n| `uri`     | string (required) | [MongoDB connection string](https://docs.mongodb.com/manual/reference/connection-string/)                                  |\n| `options` | object (optional) | [MongoDB connection options](http://mongodb.github.io/node-mongodb-native/2.2/api/MongoClient.html#connect)                |\n| `dbName`  | string (optional) | identifies a database to switch to. Useful when the db in the connection string differs from the db you want to connect to |\n\nSee: `./examples/ex1.js`\n\n### Load\n\n```javascript\nfixtures.load(); // returns a promise\n```\n\n### Unload\n\n```javascript\nfixtures.unload(); // returns a promise\n```\n\n### Disconnect\n\n```javascript\nfixtures.disconnect(); // returns a promise\n```\n\n## Example\n\nThe following example does the following:\n\n- connects to mongo\n- then unloads all fixtures\n- then loads all fixtures\n- then disconnects\n\n```javascript\nconst Fixtures = require('node-mongodb-fixtures');\nconst uri = 'mongodb://localhost/mydb';\nconst options = null;\n\nconst fixtures = new Fixtures({\n  dir: 'examples/fixtures',\n  filter: '.*',\n});\n\nfixtures\n  .connect('mongodb://localhost:27017/mydb')\n  .then(() =\u003e fixtures.unload())\n  .then(() =\u003e fixtures.load())\n  .catch(e =\u003e console.error(e))\n  .finally(() =\u003e fixtures.disconnect());\n```\n\n## CLI Usage\n\n```shell\n❯ mongodb-fixtures\n\n  Usage: mongodb-fixtures [options] [command]\n\n\n  Options:\n\n    -V, --version         output the version number\n    -u --url \u003curl\u003e        mongo connection string\n    -s --ssl              use SSL\n    -d --db_name \u003cname\u003e   database name\n    -n --ssl_novalidate   use SSL with no verification\n    -c --ssl_ca \u003c/path/to/cert\u003e  path to cert\n    -p --path \u003cpath\u003e      resource path. Default ./fixtures\n    -f --filter \u003cpattern\u003e regex pattern to filter fixture names\n    -b --verbose          verbose logs\n    -h, --help            output usage information\n\n\n  Commands:\n\n    load\n    unload\n    rebuild\n```\n\n## Example Output\n\n```shell\n[info ] Using fixtures directory: /Users/dimascio/git/node-mongodb-fixtures/examples/fixtures\n[info ] Using database mydb\n[info ] No filtering in use\n[start] load people\n[start] load places\n[done ] load people\n[done ] load places\n[done ] *load all\n[start] script people_.js\n[done ] script people_.js\n[done ] *script all\n```\n\n## Contributors\n\nContributors are welcome!\n\nSpecial thanks to those who have contributed:\n\n- [cdimascio](https://www.github.com/cdimascio)\n- [Mykolas-Zenitech](https://github.com/Mykolas-Zenitech)\n- [westyside](https://github.com/westyside)\n- [mushketyk](https://github.com/mushketyk)\n- [leeandher](https://github.com/leeandher)\n\n## License\n\n[MIT](https://opensource.org/licenses/MIT)\n\n\u003ca href=\"https://www.buymeacoffee.com/m97tA5c\" target=\"_blank\"\u003e\u003cimg src=\"https://bmc-cdn.nyc3.digitaloceanspaces.com/BMC-button-images/custom_images/orange_img.png\" alt=\"Buy Me A Coffee\" style=\"height: auto !important;width: auto !important;\" \u003e\u003c/a\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcdimascio%2Fnode-mongodb-fixtures","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcdimascio%2Fnode-mongodb-fixtures","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcdimascio%2Fnode-mongodb-fixtures/lists"}