Skip to content

Instantly share code, notes, and snippets.

@Raynos
Created September 9, 2011 11:39
Show Gist options
  • Save Raynos/1205995 to your computer and use it in GitHub Desktop.
Save Raynos/1205995 to your computer and use it in GitHub Desktop.
var db = new Db('node-mongo-blog', new Server(host, port, {}), {native_parser:true});
var blogstack = middleware(function(db, data, next) {
db.open(function(err, db) {
data.db = db;
next();
});
}, function(db, data, next) {
db.dropDatabase(function(err, result) {
next();
});
}, function(db, data, next) {
db.collection('authors', function(err, collection) {
data.collection = collection;
next();
});
}, function(db, data, next) {
data.authors = {};
collection.insert([{'name':'William Shakespeare', 'email':'[email protected]', 'age':587},
{'name':'Jorge Luis Borges', 'email':'[email protected]', 'age':123}], function(err, docs) {
docs.forEach(function(doc) {
data.authors[doc.name] = doc;
});
next();
});
}, function(db, data, next) {
data.collection.find({}, {'sort':[['age', 1]]}, function(err, cursor) {
data.cursor = cursor;
next();
});
}, function(db, data, next) {
data.cursor.each(function(err, author) {
authorStack.handle(author, {});
});
});
// var authorStack = ...
blogstack.handle(db, {});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment