Permalink
Browse files

support mongodb URI format for connect() funciton

  • Loading branch information...
1 parent 4a97fd7 commit c90246f7c99811091dc079f5206c4eb7b5ed5df5 @vpulim committed Feb 24, 2012
Showing with 43 additions and 12 deletions.
  1. +2 −2 Readme.md
  2. +6 −3 index.js
  3. +32 −4 lib/db.js
  4. +3 −3 package.json
View
@@ -29,7 +29,7 @@ node-mongodb-native:
mongode:
var mongode = require('mongode');
- var test = mongode.connect('test', '127.0.0.1');
+ var test = mongode.connect('mongo://127.0.0.1/test');
var collection = test.collection('test_collection');
collection.insert({hello: 'world'}, {safe:true}, function(err, objects) {
if (err) console.warn(err.message);
@@ -41,7 +41,7 @@ Binding
You can bind databases and collections by name:
var mongode = require('mongode');
- mongode.connect('test', '127.0.0.1');
+ mongode.connect('mongo://127.0.0.1/test');
var test = mongode.test;
test.collection('foo');
View
@@ -1,11 +1,14 @@
var db = require('./lib/db.js');
+var nameCache = {};
+
function connect(name, serverURIs, options) {
- if (!exports[name]) {
- exports[name] = new db.Database(name, serverURIs, options);
+ if (!nameCache[name]) {
+ var newDb = new db.Database(name, serverURIs, options);
+ exports[newDb.name] = nameCache[name] = newDb;
}
- return exports[name];
+ return nameCache[name];
}
exports.connect = connect;
View
@@ -10,11 +10,37 @@ var Database = function(name, servers, options) {
var self = this,
serverConfig;
+ // handle case where name is a mongodb uri
+ if (typeof servers === 'object') {
+ options = servers;
+ servers = null;
+ }
+
options = options || {};
+ self.name = name;
self._waiting = [];
self._isConnected = false;
+ self.bson_serializer = mongodb.BSONPure;
+ self.bson_deserializer = mongodb.BSONPure;
+
+ // servers is null if name is a mongodb uri
+ if (!servers) {
+ mongodb.Db.connect(name, options, function(err, db) {
+ self._db = db;
+ if (err) {
+ self.emit('error', err);
+ }
+ else {
+ self._connected();
+ }
+ })
+ self.name = nameFromUri(name);
+ return;
+ }
+
+
if (Array.isArray(servers)) {
servers = servers.map(function(server) {
return createServer(server, options);
@@ -35,10 +61,6 @@ var Database = function(name, servers, options) {
self._connected();
}
});
-
- // expose useful Db members
- self.bson_serializer = self._db.bson_serializer;
- self.bson_deserializer = self._db.bson_deserializer;
};
util.inherits(Database, EventEmitter);
@@ -106,5 +128,11 @@ var createServer = function(server, options) {
return new mongodb.Server(host, port, options);
};
+var nameFromUri = function(url) {
+ var urlRE = new RegExp('^mongo(?:db)?://(?:|([^@/]*)@)([^@/]*)(?:|/([^?]*)(?:|\\?([^?]*)))$');
+ var match = url.match(urlRE);
+ return match[3] || 'default';
+}
+
exports.Database = Database;
exports.ObjectID = mongodb.BSONPure.ObjectID;
View
@@ -1,10 +1,10 @@
{
"name":"mongode",
- "version":"0.1.2",
+ "version":"0.1.3",
"description":"A thin wrapper around node-mongodb-native with a simpler API",
- "engines":{"node":">=0.4.0"},
+ "engines":{"node":">=0.6.0"},
"author":"Vinay Pulim <v@pulim.com>",
- "dependencies":{"mongodb":"0.9.7-3-5"},
+ "dependencies":{"mongodb":"0.9.9-3"},
"repository":{"type":"git","url":"https://github.com/milewise/mongode.git"},
"main":"./index.js",
"directories":{"lib":"./lib"},

0 comments on commit c90246f

Please sign in to comment.