Skip to content

Commit

Permalink
added function binding
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinay Pulim committed Aug 1, 2011
1 parent 3e4c390 commit 910556a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
20 changes: 17 additions & 3 deletions Readme.md
@@ -1,7 +1,7 @@
Introduction
============

Mongode is a very thin (~200 lines of code) wrapper around the [node-mongodb-native](https://github.com/christkv/node-mongodb-native) driver. It's main purpose is to reduce the amount of function call nesting that is usually needed with the native driver. It does this by buffering commands until the appropriate objects are available.
Mongode is a very thin (~200 lines of code) wrapper around the [node-mongodb-native](https://github.com/christkv/node-mongodb-native) driver. It's main purpose is to reduce the amount of function call nesting that is usually needed with the native driver. It does this by buffering commands until the appropriate objects are available.

Installation
============
Expand Down Expand Up @@ -29,9 +29,23 @@ node-mongodb-native:
mongode:

var mongode = require('mongode');
var db = new mongode.Database('test', 'mongodb://127.0.0.1');
var collection = db.collection('test_collection');
var test = mongode.connect('test', 'mongodb://127.0.0.1');
var collection = test.collection('test_collection');
collection.insert({hello: 'world'}, {safe:true}, function(err, objects) {
if (err) console.warn(err.message);
});

Binding
-------

You can bind databases and collections by name:

var mongode = require('mongode');
mongode.connect('test', 'mongodb://127.0.0.1');

var test = mongode.test;
test.collection('foo');
test.collection('bar');

test.foo.find().each(function(err, object) {});
test.bar.find().each(function(err, object) {});
13 changes: 12 additions & 1 deletion index.js
@@ -1 +1,12 @@
module.exports = require('./lib/db.js');
var db = require('./lib/db.js');

function connect(name, serverURIs, options) {
if (!exports[name]) {
exports[name] = new db.Database(name, serverURIs, options);
}

return exports[name];
}

exports.connect = connect;
exports.Database = db.Database;
6 changes: 6 additions & 0 deletions lib/db.js
Expand Up @@ -40,6 +40,10 @@ var Database = function(name, serverURIs, options) {
util.inherits(Database, EventEmitter);

Database.prototype.collection = function(name) {
if (this[name]) {
return this[name];
}

var self = this,
collection = new Collection(name);

Expand All @@ -51,6 +55,8 @@ Database.prototype.collection = function(name) {
})
}

this[name] = collection;

return collection;
}

Expand Down

0 comments on commit 910556a

Please sign in to comment.