Comparing version 0.4.0 to 0.4.1
45
index.js
var mongo = require('mongodb'); | ||
var common = require('common'); | ||
var url = require('url'); | ||
@@ -16,17 +17,35 @@ var noop = function() {}; | ||
}(); | ||
var parse = function(options) { | ||
var parseURL = function(options) { | ||
if (typeof options === 'object') { | ||
options.host = options.host || '127.0.0.1'; | ||
options.port = parseInt(options.port || 27017, 10); | ||
return options; | ||
} | ||
if (/^[^:\/]+$/.test(options)) { | ||
options = 'mongodb://127.0.0.1:27017/'+options; | ||
} | ||
var parsed = url.parse('mongodb://'+options.replace('mongodb://', '')); | ||
var result = {}; | ||
var match = options.match(/(?:mongodb:\/\/)?(?:(.+):(.+)@)?(?:([^:]+)(?::(\d+))?\/)?(.+)/); | ||
result.username = match[1]; | ||
result.password = match[2]; | ||
result.host = match[3] || '127.0.0.1'; | ||
result.port = parseInt(match[4] || 27017,10); | ||
result.db = match[5]; | ||
result.username = parsed.auth && parsed.auth.split(':')[0]; | ||
result.password = parsed.auth && parsed.auth.split(':')[1]; | ||
result.db = (parsed.path || '').substr(1); | ||
result.host = parsed.hostname; | ||
result.port = parsed.port; | ||
return result; | ||
return parse(result); | ||
}; | ||
var parse = function(options) { | ||
options = parseURL(options); | ||
if (options.replSet) { | ||
if (!options.replSet.members) { | ||
throw new Error('replSet.members required'); | ||
} | ||
options.replSet.members = options.replSet.members.map(parseURL); | ||
} | ||
return options; | ||
}; | ||
var shouldExtend = function(that, proto, name) { | ||
@@ -165,2 +184,3 @@ if (name[0] === '_') return false; | ||
url = parse(url); | ||
collections = collections || url.collections; | ||
@@ -172,4 +192,11 @@ var that = {}; | ||
function(next) { | ||
var client = new mongo.Db(url.db, new mongo.Server(url.host, url.port, {auto_reconnect:true})); | ||
var replSet = url.replSet && new mongo.ReplSetServers(url.replSet.members.map(function(member) { | ||
return new mongo.Server(member.host, member.port, {auto_reconnect:true}); | ||
}), { | ||
read_secondary:url.replSet.slaveOk, | ||
rs_name:url.replSet.name | ||
}); | ||
var client = new mongo.Db(url.db, replSet || new mongo.Server(url.host, url.port, {auto_reconnect:true})); | ||
that.client = client; | ||
@@ -176,0 +203,0 @@ that.bson = { |
@@ -5,3 +5,3 @@ { | ||
"keywords": ["mongo", "db", "mongodb"], | ||
"version":"0.4.0", | ||
"version":"0.4.1", | ||
"homepage" : "https://github.com/gett/mongojs", | ||
@@ -8,0 +8,0 @@ "author": "Ge.tt <hello@ge.tt>", |
# mongojs | ||
A [node.js](http://nodejs.org) module for mongodb, that emulates the mongodb API as much as possible. It wraps [mongodb-native](https://github.com/christkv/node-mongodb-native/). | ||
A [node.js](http://nodejs.org) module for mongodb, that emulates the [official mongodb API](http://www.mongodb.org/display/DOCS/Home) as much as possible. It wraps [mongodb-native](https://github.com/christkv/node-mongodb-native/). | ||
It is available through npm: | ||
@@ -7,2 +7,4 @@ | ||
## Usage | ||
mongojs is very simple to use: | ||
@@ -64,1 +66,19 @@ | ||
For more detailed information about the different usages of update and quering see [the mongo docs](http://www.mongodb.org/display/DOCS/Manual) | ||
## Replication Sets | ||
Mongojs can also connect to a mongo replication set | ||
``` js | ||
var db = require('mongojs').connect({ | ||
db: 'mydb', // the name of our database | ||
collection: ['mycollection'], // we can pass the collections here also | ||
replSet: { | ||
name: 'myReplSetName', // the name of the replication set | ||
slaveOk: true, // is it to read from secondary? defaults to false | ||
members: ['myserver:myport', 'myotherserver', 'mythirdserver'] | ||
} | ||
}); | ||
``` | ||
For more detailed information about replica sets see [the mongo replication docs](http://www.mongodb.org/display/DOCS/Replica+Sets) |
10238
238
83