var Mongorito = require('mongorito');
Mongorito.connect('localhost/blog');

var Post = Mongorito.Model.extend({
	collection: 'posts'
});

var post = new Post({
	title: 'Node.js with --harmony rocks!'
});

yield post.save();

var posts = yield Post.limit(5).find();
// NO CALLBACKS!

Spread the word!

Features

No callbacks

Mongorito is based on ES6 generators, so you will not need to write a single callback (or promise). Enjoy the comfort of writing synchronous code, while retaining the power of Node.js' async nature.

Common and established API

Mongorito does not re-invent the wheel. Take a look at the code example above, same principles as in ActiveRecord, Mongoose and others. Learning curve is so low, you'll forget when you opened the docs last time.

Middleware

Yes, like in Express and Koa. Put functions before or after CRUD methods and control the flow. Modify data, abort the operation and everything else I could not think of.

No Schema

That's right, no schema. One of the best MongoDB strengths is that it's totally schemaless. Mongorito does not want to take that feature away from you. Just use get and set methods and everything will go smooth.

Query population

Mongorito borrowed that idea from the awesome Mongoose. Population allows replacing ObjectID's in query response with actual documents from other collections.

Full test coverage

Every bit of Mongorito's functionality is tested. Just as main codebase, tests are easy to read and understand. If you don't belive it, check it yourself by running npm test after cloning the repository.

Getting Started

First, make sure you've got Node.js v0.11.x installed, because we will need to run node with --harmony option. If you still need previous versions of Node.js on the same machine, I recommend nvm or n utilities to manage Node.js versions. Also, be sure to wrap all your code with yield statements in co or a similar library. These will automatically run generator functions and do all the hard and dirty work for you. Now, let's install mongorito using npm:

$ npm install mongorito --save

In this short tutorial we are going to save and fetch blog posts to demonstrate core functionality of Mongorito. Next part, requiring Mongorito and connecting to the database. I also recommend to assign Mongorito.Model to a separate variable to create a shortcut.

var Mongorito = require('mongorito');
var Model = Mongorito.Model;

Mongorito.connect('localhost/blog');

To define a Post model, use Model.extend() method to inherit from Model. When extending, you must supply a collection name. You do not need to register a model or anything like that.

var Post = Model.extend({
	collection: 'posts'
});

To save a Post document, create a new instance of Post and execute save() method.

var post = new Post({
	title: 'Node.js with --harmony rocks!',
	body: 'Long post body',
	author: {
		name: 'John Doe'
	}
});

yield post.save();

Now we are going to find all posts in the database. The returned array contains documents wrapped into Post model.

var posts = yield Post.all();

To find all posts where body equals "Long post body" or posts where author's name equals "John Doe", use .where() method.

var posts;

// find posts where body equals "Long post body"
posts = yield Post.where('body', 'Long post body').find();

// find posts where author's name equals "John Doe"
posts = yield Post.where('author.name', 'John Doe').find();

// Bonus: find posts where title starts with "Node"
posts = yield Post.where('title', /^node/i).find();

When all the work is done, don't forget to close connection to MongoDB.

Mongorito.disconnect();

Documentation & Examples

Please refer to GitHub repository for documentation & examples for now.