{"id":20354352,"url":"https://github.com/hemerajs/mongo-memory","last_synced_at":"2025-04-12T02:33:13.069Z","repository":{"id":57301608,"uuid":"100023333","full_name":"hemerajs/mongo-memory","owner":"hemerajs","description":"👥 Write tests for mongodb in memory","archived":false,"fork":false,"pushed_at":"2017-12-02T13:23:24.000Z","size":49,"stargazers_count":3,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-12T02:32:30.299Z","etag":null,"topics":["mock","mongodb","nodejs","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/hemerajs.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":"2017-08-11T11:05:23.000Z","updated_at":"2018-08-02T19:43:03.000Z","dependencies_parsed_at":"2022-09-08T17:32:22.153Z","dependency_job_id":null,"html_url":"https://github.com/hemerajs/mongo-memory","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hemerajs%2Fmongo-memory","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hemerajs%2Fmongo-memory/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hemerajs%2Fmongo-memory/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hemerajs%2Fmongo-memory/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hemerajs","download_url":"https://codeload.github.com/hemerajs/mongo-memory/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248506933,"owners_count":21115510,"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":["mock","mongodb","nodejs","testing"],"created_at":"2024-11-14T23:08:19.911Z","updated_at":"2025-04-12T02:33:13.052Z","avatar_url":"https://github.com/hemerajs.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Mongo Memory\n[![Build Status](https://travis-ci.org/hemerajs/mongo-memory.svg?branch=master)](https://travis-ci.org/hemerajs/mongo-memory)\n[![NPM Downloads](https://img.shields.io/npm/dt/mongo-memory.svg?style=flat)](https://www.npmjs.com/package/mongo-memory)\n[![npm](https://img.shields.io/npm/v/mongo-memory.svg?maxAge=3600)](https://www.npmjs.com/package/mongo-memory)\n[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com)\n\nBootstrap programmatically Mongodb for testing or mocking during development.\n\nWorks on all platforms which is due to the awesome [mongodb-prebuilt](https://www.npmjs.com/package/mongodb-prebuilt) package.\n\n## Installation\n````\nnpm install mongo-memory\n````\n\n## Usage\n\n````javascript\nconst MongoInMemory = require('mongo-memory')\n\nconst port = 8000\nconst dbPath = \"./tempb/.data\" // Mongodb placed some metadata\nconst mongoServerInstance = new MongoMemory(port, dbPath)\n\nmongoServerInstance.start().then(server) =\u003e {\n\n    mongoServerInstance.getMongouri('myDatabaseName')\n    mongoServerInstance.getCollection('coll1')\n    mongoServerInstance.getDocumentById('myDatabaseName', 'coll1', \"\u003cid\u003e\")\n    mongoServerInstance.addDocument('myDatabaseName', 'coll1', { _id : \"foo\" })\n    mongoServerInstance.addDirectoryOfCollections('myDatabaseName', '\u003cpath\u003e')\n    mongoServerInstance.mongodb.ObjectId // Mongodb driver\n    mongoServerInstance.serialize // EJSON\n    mongoServerInstance.deserialize // EJSON\n\n})\n\nmongoServerInstance.stop()\n````\n\n### Run tests\n\n```\n$ npm run test\n```\n\n## Caveats\n\n- **PR Pending**: Newer mongodb versions aren't correctly detected as started https://github.com/winfinit/mongodb-prebuilt/pull/40\n- **PR Pending**: Due to a wrong protocol there are some issue to download the offical mongodb server https://github.com/winfinit/mongodb-download/pull/31\n\n## What about BSON Types ?\n\nYou can use the [EJSON](https://github.com/mongodb-js/extended-json) format to express BSON Types with JSON e.g **ObjectId**, **Date**.\n\n```\n{\n    \"_id\" : {\n        \"$oid\": \"ec939793b7d8fe8f9f2aa707\"\n    },\n    'last_seen_at': {\n        '$date': 1405266782008\n    }\n}\n```\n\n## Background \n\nMongodb storageEngine [ephemeralForTest](https://docs.mongodb.com/v3.4/release-notes/3.2/#ephemeralfortest-storage-engine) is used.\nThe connection is created by the official mongodb package.\n\n## Credits\n\nMost code was copied from [mongo-in-memory](https://github.com/giorgio-zamparelli/mongo-in-memory) but with significant improvements:\n\n### Changes:\n\n- Don't save metadata in node_modules\n- Don't generate random folders\n- Clean up of metadata must be handled by user\n- 100% Promise API\n- Support for BSON Types via EJSON\n- Update mongodb-prebuilt to the newest version\n- Update dependencies and tests\n- Gracefully shutdown Mongodb when execute stop\n- Create recursively databasePath\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhemerajs%2Fmongo-memory","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhemerajs%2Fmongo-memory","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhemerajs%2Fmongo-memory/lists"}