Code Monkey home page Code Monkey logo

mongomem's Introduction

MongoMem

This package provides an in-memory MongoDB Server. Designed with testing in mind, the server will allow you to connect your favourite ODM or client library to the MongoDB Server and run integration tests isolated from each other.

Note about AVA Test Runner The package was designed to run one server instance (max) at a time. As such it runs one server instance per test file in AVA (as a result of AVA using forked processes). This has no negative consequences but it does mean that you should call MongoDBServer.start() only once, at the top of an imported file or test file. Calling it in a helper method may result in either unexpected behaviour or multiple servers running per test file.

Usage

The example below uses async/await from ES7. If you wish to use the same syntax, you should transpile your code using babel-preset-es2017. Alternatively, if you are already using ES6 then you can convert it to the appropriate Promise syntax.

Available on NPM using: npm install mongomem --save-dev or yarn add mongomem --dev

See here for usage examples: https://coligo.io/javascript-async-await/

// helpers.js
import mongoose from 'mongoose'
import { MongoDBServer } from 'mongomem'

// MongoDBServer.debug = true;
let serverHasStarted = MongoDBServer.start();

export default {
  async getMongooseMock() {
      // 
      const mongooseInstance = new mongoose.Mongoose();
      await serverHasStarted;
    
      // MongoDBServer.getConnectionString() returns a new UUID4 on every call.
      // This allows you to get a unique database whilst utilising one in-memory server.
      let db = await MongoDBServer.getConnectionString();
      await mongooseInstance.connect(db, {promiseLibrary: Promise});
      
      // Re-assign original mongose models to new Mongoose connection
      Object.keys(mongoose.models).forEach(name => {
        const model = mongoose.models[name];
        mongooseInstance.model(name, model.schema);
      });
    
      return mongooseInstance;
  }
}

mongomem's People

Contributors

cimrie avatar

Stargazers

Burak avatar

Watchers

Armagan Amcalar avatar James Cloos avatar  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.