Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add support for replica sets #15

Merged
merged 5 commits into from

2 participants

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 51 additions and 3 deletions.
  1. +18 −0 Readme.md
  2. +33 −3 lib/mongodb-wrapper.js
View
18 Readme.md
@@ -58,6 +58,24 @@ Remember the guiding principle: the syntax exactly matches the [command-line dri
db.collection('users')
db.users.count(cb)
+#### Replica Sets
+
+Replica sets are also supported with an alternate function signature:
+``` JavaScript
+var hostsArray = [
+ // opts is a hash of mongodb-native server opts: http://mongodb.github.com/node-mongodb-native/api-generated/server.html
+ // also optional
+ {host: "host1", port: 27017, opts: {}},
+ {host: "host1", port: 27018, opts: {}},
+ ...
+]
+// other replica set opts, such as read_secondary, are passed in here
+// these opts are also passed to each mongodb-native server object, so you can have defaults for your servers
+var opts = {rs_name: "myReplicaSet"}
+
+mongo.db(hostsArray, opts, dbname, [prefix], [username], [password])
+```
+
#### Authentication
View
36 lib/mongodb-wrapper.js
@@ -851,12 +851,42 @@ var Database = exports.Database = function(host, port, name, prefix, username, p
// A connection object! Stores some extra info on the mongoDatabase object
// It might be because I have all this stuff I'm referencing. Closure-style
// Try traditional inheritance
+//
+
+function merge(a, b) {
+ for (var att in b) {
+ a[att] = b[att]
+ }
+ return a
+}
+
+function makeReplServers(hosts, opts) {
+ // clean off RS options
+ delete opts.rs_name
+ delete opts.read_secondary
+ delete opts.socketOptions
+ var servers = []
+ for (var i = 0; i < hosts.length; i++) {
+ host = hosts[i]
+ // merge in hosts if they got em
+ if (hosts.opts) opts = merge(opts, hosts.opts)
+ servers.push(new mongodb.Server(host.host, host.port, opts))
+ }
+ return servers
+}
var Connection = exports.Connection = function(host, port, name) {
+ var server = null
- var server = new mongodb.Server(host, port, {
- native_parser: true
- })
+ if (Array.isArray(host)) {
+ var opts = port
+ if (typeof opts !== "object") throw new Error("you need to define opts for replica sets!")
+ server = new mongodb.ReplSetServers(makeReplServers(host, opts), opts)
+ } else {
+ server = new mongodb.Server(host, port, {
+ native_parser: true
+ })
+ }
Connection.__super__.constructor.call(this, name, server, {})
this.reused = 0
Something went wrong with that request. Please try again.