Permalink
Browse files

fix #26 replica sets support

  • Loading branch information...
1 parent 71fb40c commit 0d1466cc1c06ab70639e9694fbfdd5c5cfbad69c @guileen committed Jan 16, 2012
Showing with 64 additions and 18 deletions.
  1. +10 −0 examples/replset.js
  2. +51 −15 lib/mongoskin/index.js
  3. +3 −3 package.json
View
@@ -0,0 +1,10 @@
+var mongoskin = require('../lib/mongoskin/');
+
+var db = mongoskin.db(['127.0.0.1:27017'], {
+ database: 'test'
+});
+
+db.open(function(err, data) {
+ console.log(err && err.stack);
+ console.log(data);
+});
View
@@ -5,8 +5,7 @@ var url = require('url'),
SkinDb =require('./db').SkinDb,
Db = mongo.Db,
Server = mongo.Server,
- ServerCluster = mongo.ServerCluster,
- ServerPair = mongo.ServerPair,
+ ReplSetServers = mongo.ReplSetServers
BSONNative = mongo.BSONNative,
DEFAULT_PORT = 27017;
@@ -29,7 +28,9 @@ var parseUrl = function(serverUrl) {
config.host = uri.hostname;
config.port = Number(uri.port) || DEFAULT_PORT;
- config.database = uri.pathname.replace(/\//g, '');
+ if(uri.pathname) {
+ config.database = uri.pathname.replace(/\//g, '');
+ }
config.options = {};
config.options['auto_reconnect'] = toBool(serverOptions['auto_reconnect']);
config.options['poolSize'] = parseInt(serverOptions['poolSize'] || 1);
@@ -54,24 +55,59 @@ function MongoSkin() {
}
/**
- * constructor SkinDb from serverUrl
+ * constructor SkinDb from serverUrls
+ *
+ * repliSet: mongoskin.db(serverUrls, rs_options, db_options)
+ *
+ * mongoskin.db(['192.168.0.1:27017/', '192.168.0.2/?auto_reconnect', '192.168.0.3/?auto_reconnect'], {
+ * database: 'mydb'
+ * })
+ *
+ * single Server: mongoskin.db(dbUrl, db_options)
+ *
+ * mongoskin.db('192.168.0.1:27017/mydb')
+ *
*/
MongoSkin.prototype.db = function(serverUrl, options) {
- var config = parseUrl(serverUrl);
- if (!config.database) {
- throw new Error('Please provide a database to connect to.');
- }
- var server = new Server(config.host, config.port, config.options);
- var skinServer = new SkinServer(server);
-
if(!options) {
options = {};
}
- if(options.username === undefined) {
- options.username = config.username;
- options.password = config.password;
+
+ var server, database;
+
+ if(Array.isArray(serverUrl)) {
+ if(!options.database) {
+ throw new Error('Please provide a database in options to connect.');
+ }
+ database = options.database;
+
+ var len = serverUrl.length;
+ var servers = [];
+ for(var i = 0; i < len; i++) {
+ var config = parseUrl(serverUrl[i]);
+ if(config.database || config.username) {
+ console.log('MONGOSKIN:WARN: database or username found in RepliSet server URL, ' + serverUrl[i]);
+ }
+ servers.push( new Server(config.host, config.port, config.options) );
+ }
+ server = new ReplSetServers(servers);
+ } else {
+ var config = parseUrl(serverUrl);
+ if (!config.database) {
+ throw new Error('Please provide a database to connect to.');
+ }
+ database = config.database;
+
+ server = new Server(config.host, config.port, config.options);
+
+ if(options.username === undefined) {
+ options.username = config.username;
+ options.password = config.password;
+ }
}
- return skinServer.db(config.database, options);
+
+ var skinServer = new SkinServer(server);
+ return skinServer.db(database, options);
};
/**
View
@@ -1,7 +1,7 @@
{
"name": "mongoskin",
"description": "The future layer above node-mongodb-native",
- "version": "0.2.3",
+ "version": "0.3.0",
"author": "Jason Green <guileen@gmail.com>",
"repository": {
"type": "git",
@@ -16,10 +16,10 @@
"mongodb"
],
"engines": {
- "node": "*"
+ "node": ">=0.4.0"
},
"dependencies": {
- "mongodb": "*"
+ "mongodb": ">=0.9.7-3"
},
"devDependencies": {
"expresso": "0.9.2",

0 comments on commit 0d1466c

Please sign in to comment.