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.
$ npm install thunkify-mongodb --save
Basic | Events | Bulk Operations
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();
}
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() {
});
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;
}
- All 2.X.X versions of MongoDB Native Driver are supported.
AggregationCursor, GridStore, Mongos, etc. coming soon. Feel free to PR.
thunkify-object is used as a thunk wrapper engine.
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
Thunkify-mongodb is freely distributable under the terms of the MIT license.