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();
})();
@yvele
Copy link

yvele commented Jun 7, 2015

Sample example with thunkify-mongodb:

// npm install mongodb thunkify-mongodb co
var MongoClient = require('thunkify-mongodb').MongoClient;
var mongodb = require('mongodb');
var co = require('co');

co(function*() {

    var mongoClient = new MongoClient(new mongodb.MongoClient());

    var db = yield mongoClient.connect('mongodb://127.0.0.1:27017/test');
    var items = yield db.collection('items');

    // cleanup
    yield items.remove();

    // insert some records
    var record1 = yield items.insert({lorem: "ipsum"});
    var record2 = yield items.insert({lorem: "dolor"});
    var record3 = yield items.insert({lorem: "sit amet"});

    // read it
    var count = yield items.count();

    var records = yield items.find().toArray();
    console.log(count, records);

    yield 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