Permalink
Browse files

Merge remote branch 'adamwiggins/master'

  • Loading branch information...
2 parents 6ae4f1e + 021b6a3 commit 4c17fbfddc8d8d6bce6bf06280a62c47a64ecbb5 Christian Amor Kvalheim committed Jun 15, 2010
Showing with 39 additions and 0 deletions.
  1. +16 −0 examples/url.js
  2. +23 −0 lib/mongodb/db.js
View
@@ -0,0 +1,16 @@
+GLOBAL.DEBUG = true;
+
+sys = require("sys");
+test = require("mjsunit");
+
+var mongo = require('../lib/mongodb');
+
+sys.puts('Connecting to ' + (process.env.MONGO_URL || mongo.Db.DEFAULT_URL));
+mongo.connect(process.env.MONGO_URL, function(err, db) {
+ db.dropDatabase(function(err, result) {
+ db.collection('test', function(err, collection) {
+ collection.insert({'a':1});
+ db.close();
+ });
+ });
+});
View
@@ -506,3 +506,26 @@ Db.prototype.executeCommand = function(db_command, callback) {
// Execute command
this.serverConfig.masterConnection.send(db_command);
};
+
+/**
+ Connect to URL
+**/
+Db.DEFAULT_URL = 'mongo://localhost:27017/default';
+
+exports.connect = function(url, callback) {
+ config = require('url').parse(url || Db.DEFAULT_URL);
+
+ if (!config['protocol'].match(/^mongo/))
+ throw Error("URL must be in the format mongo://user:pass@host:port/dbname");
+
+ var host = config['hostname'] || 'localhost';
+ var port = config['port'] || Connection.DEFAULT_PORT;
+ var dbname = config['pathname'].replace(/^\//, '');
+
+ var options = {};
+ if (config['auth'])
+ options['user'], options['password'] = config['auth'].split(':', 2)
+
+ var db = new Db(dbname, new Server(host, port, options), {});
+ db.open(callback);
+}

0 comments on commit 4c17fbf

Please sign in to comment.