Skip to content

samjoch/mongo-mock

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mongo-mock     Circle CI

This is an in-memory 'pretend' mongodb. The goal is to make the interface compatible with the real mongodb module so they are interchangeable.

There are a TON of features for mongo and I can't write them all myself- so pull requests are encouraged! My initial goal was to provide basic CRUD operations to enable this to work as a throw-something-together tool.

Why?

Maybe you don't want to (or can't) connect to a MongoDB instance for your tests?
Maybe you want to throw together a quick example app?

Demo code

var mongodb = require('mongo-mock');
mongodb.max_delay = 0;//you can choose to NOT pretend to be async (default is 400ms)
var MongoClient = mongodb.MongoClient;
MongoClient.persist="mongo.js";//persist the data to disk

// Connection URL
var url = 'mongodb:https://localhost:27017/myproject';
// Use connect method to connect to the Server
MongoClient.connect(url, {}, function(err, db) {
  // Get the documents collection
  var collection = db.collection('documents');
  // Insert some documents
  var docs = [ {a : 1}, {a : 2}, {a : 3}];
  collection.insert(docs, function(err, result) {
    console.log('inserted',result);

    collection.update({ a : 2 }, { $set: { b : 1 } }, function(err, result) {
      console.log('updated',result);

      collection.findOne({a:2}, {b:1}, function(err, doc) {
        console.log('foundOne', doc);

        collection.remove({ a : 3 }, function(err, result) {
          console.log('removed',result);

          collection.find({}, {_id:-1}).toArray(function(err, docs) {
            console.log('found',docs);

            setTimeout(db.close, 1000);
          });
        });
      });
    });
  });
});

Install

Well, you know.. the usual:

$ npm install mongo-mock

license

The MIT License (MIT)

Copyright (c) 2015 William Kapke

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Let's pretend we have a real MongoDB

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages

  • JavaScript 100.0%