Giter Site home page Giter Site logo

mongo-memory's Introduction

Mongo Memory

Build Status NPM Downloads npm js-standard-style

Bootstrap programmatically Mongodb for testing or mocking during development.

Works on all platforms which is due to the awesome mongodb-prebuilt package.

Installation

npm install mongo-memory

Usage

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', "<id>")
    mongoServerInstance.addDocument('myDatabaseName', 'coll1', { _id : "foo" })
    mongoServerInstance.addDirectoryOfCollections('myDatabaseName', '<path>')
    mongoServerInstance.mongodb.ObjectId // Mongodb driver
    mongoServerInstance.serialize // EJSON
    mongoServerInstance.deserialize // EJSON

})

mongoServerInstance.stop()

Run tests

$ npm run test

Caveats

What about BSON Types ?

You can use the EJSON format to express BSON Types with JSON e.g ObjectId, Date.

{
    "_id" : {
        "$oid": "ec939793b7d8fe8f9f2aa707"
    },
    'last_seen_at': {
        '$date': 1405266782008
    }
}

Background

Mongodb storageEngine ephemeralForTest is used. The connection is created by the official mongodb package.

Credits

Most code was copied from 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

mongo-memory's People

Contributors

starptech avatar

Watchers

James Cloos avatar Zhuoran (John) Li avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.