Permalink
Browse files

added function binding

  • Loading branch information...
1 parent 3e4c390 commit 910556a876565a9c5446ec7d10c352ac0520fa73 @vpulim committed Aug 1, 2011
Showing with 35 additions and 4 deletions.
  1. +17 −3 Readme.md
  2. +12 −1 index.js
  3. +6 −0 lib/db.js
View
20 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
============
@@ -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) {});
View
13 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;
View
6 lib/db.js
@@ -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);
@@ -51,6 +55,8 @@ Database.prototype.collection = function(name) {
})
}
+ this[name] = collection;
+
return collection;
}

0 comments on commit 910556a

Please sign in to comment.