https://github.com/hemerajs/mongo-memory
👥 Write tests for mongodb in memory
https://github.com/hemerajs/mongo-memory
mock mongodb nodejs testing
Last synced: about 1 year ago
JSON representation
👥 Write tests for mongodb in memory
- Host: GitHub
- URL: https://github.com/hemerajs/mongo-memory
- Owner: hemerajs
- License: mit
- Created: 2017-08-11T11:05:23.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-12-02T13:23:24.000Z (over 8 years ago)
- Last Synced: 2025-04-12T02:32:30.299Z (about 1 year ago)
- Topics: mock, mongodb, nodejs, testing
- Language: JavaScript
- Homepage:
- Size: 47.9 KB
- Stars: 3
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Mongo Memory
[](https://travis-ci.org/hemerajs/mongo-memory)
[](https://www.npmjs.com/package/mongo-memory)
[](https://www.npmjs.com/package/mongo-memory)
[](http://standardjs.com)
Bootstrap programmatically Mongodb for testing or mocking during development.
Works on all platforms which is due to the awesome [mongodb-prebuilt](https://www.npmjs.com/package/mongodb-prebuilt) package.
## Installation
````
npm install mongo-memory
````
## Usage
````javascript
const MongoInMemory = require('mongo-memory')
const port = 8000
const dbPath = "./tempb/.data" // Mongodb placed some metadata
const mongoServerInstance = new MongoMemory(port, dbPath)
mongoServerInstance.start().then(server) => {
mongoServerInstance.getMongouri('myDatabaseName')
mongoServerInstance.getCollection('coll1')
mongoServerInstance.getDocumentById('myDatabaseName', 'coll1', "")
mongoServerInstance.addDocument('myDatabaseName', 'coll1', { _id : "foo" })
mongoServerInstance.addDirectoryOfCollections('myDatabaseName', '')
mongoServerInstance.mongodb.ObjectId // Mongodb driver
mongoServerInstance.serialize // EJSON
mongoServerInstance.deserialize // EJSON
})
mongoServerInstance.stop()
````
### Run tests
```
$ npm run test
```
## Caveats
- **PR Pending**: Newer mongodb versions aren't correctly detected as started https://github.com/winfinit/mongodb-prebuilt/pull/40
- **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
## What about BSON Types ?
You can use the [EJSON](https://github.com/mongodb-js/extended-json) format to express BSON Types with JSON e.g **ObjectId**, **Date**.
```
{
"_id" : {
"$oid": "ec939793b7d8fe8f9f2aa707"
},
'last_seen_at': {
'$date': 1405266782008
}
}
```
## Background
Mongodb storageEngine [ephemeralForTest](https://docs.mongodb.com/v3.4/release-notes/3.2/#ephemeralfortest-storage-engine) is used.
The connection is created by the official mongodb package.
## Credits
Most code was copied from [mongo-in-memory](https://github.com/giorgio-zamparelli/mongo-in-memory) but with significant improvements:
### Changes:
- Don't save metadata in node_modules
- Don't generate random folders
- Clean up of metadata must be handled by user
- 100% Promise API
- Support for BSON Types via EJSON
- Update mongodb-prebuilt to the newest version
- Update dependencies and tests
- Gracefully shutdown Mongodb when execute stop
- Create recursively databasePath