Skip to content
This repository has been archived by the owner on May 13, 2019. It is now read-only.

Wrapper on MongoDB native driver 2.0 to provide thunk methods

License

Notifications You must be signed in to change notification settings

yvele/node-thunkify-mongodb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NPM version MIT License Build Status Test Coverage

thunkify-mongodb

Installation | Examples | Supported MongoDB versions | Roadmap | Under the hood | Running tests | License

Give MongoDB native driver 2.0 a sweet generator aftertaste 🍭

Wrapper on MongoDB native driver to provide thunk methods, useful for generator-based flow control such as co, Koa, etc.

Installation

$ npm install thunkify-mongodb --save

Examples

Basic | Events | Bulk Operations

Basic

var MongoClient = require('thunkify-mongodb').MongoClient;
var mongodb = require('mongodb');

function* insertSomething(url) {
  var mongoClient = new MongoClient(new mongodb.MongoClient());

  var db = yield mongoClient.connect(url);

  var collection = yield db.collection('Documents');
  yield collection.insertOne({ some: 'thing' });

  yield db.close();
}

Events

All EventEmitter methods are wrapped.

You can use on and once methods as generators:

yield db.on('close');

You can also use them with a callback:

db.on('close', function() {

});

Bulk Operations

var MongoClient = require('thunkify-mongodb').MongoClient;
var mongodb = require('mongodb');

function* doBulk(url) {
  var mongoClient = new MongoClient(new mongodb.MongoClient());

  var db = yield mongoClient.connect(url);

  var collection = yield db.collection('Documents');

  var batch = collection.initializeOrderedBulkOp();
  batch.insert({a:1});
  batch.find({a:1}).updateOne({$set: {b:1}});
  batch.find({a:2}).upsert().updateOne({$set: {b:2}});
  batch.insert({a:3});
  batch.find({a:3}).delete({a:3});

  // Execute the operations
  var result = yield batch.execute();

  yield db.close();

  return result;
}

Supported MongoDB versions

  • All 2.X.X versions of MongoDB Native Driver are supported.

Roadmap

AggregationCursor, GridStore, Mongos, etc. coming soon. Feel free to PR.

Under the hood

thunkify-object is used as a thunk wrapper engine.

Running tests

You need a running MongoDB instance on mongodb://localhost:27017/thunkify-mongodb-test to run all the tests.

$ make test

With code coverage.

$ make test-cov

License

Thunkify-mongodb is freely distributable under the terms of the MIT license.