Skip to content

Instantly share code, notes, and snippets.

@hajovsky
Created March 26, 2014 07:12
Show Gist options
  • Save hajovsky/9778146 to your computer and use it in GitHub Desktop.
Save hajovsky/9778146 to your computer and use it in GitHub Desktop.
how to thunkify the mongodb native
// npm install mongodb thunkify-wrap co
var mongo = require('mongodb');
var t = require('thunkify-wrap');
var co = require('co');
co(function*(){
var db = yield t(mongo.MongoClient.connect)('mongodb://127.0.0.1:27017/test');
var items = db.collection('items');
// cleanup
yield t(items.remove, items)();
// insert some records
var record1 = yield t(items.insert, items)({lorem: "ipsum"});
var record2 = yield t(items.insert, items)({lorem: "dolor"});
var record3 = yield t(items.insert, items)({lorem: "sit amet"});
// read it
var count = yield t(items.count, items)();
var records = yield t(items.find().toArray, items)();
console.log(count, records);
db.close();
})();
@Globik
Copy link

Globik commented Nov 9, 2015

Thank you very much. Monk.js is very very old. And mongoose is shemalike.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment