Browse files

allow mongo connection string URLs in config

  • Loading branch information...
1 parent 2084a5b commit 7cfb33bbe30926ac4207c2e204c9ea05e138c7a9 @RandomEtc RandomEtc committed Apr 1, 2013
Showing with 41 additions and 34 deletions.
  1. +41 −34 lib/cube/database.js
View
75 lib/cube/database.js
@@ -2,47 +2,54 @@ var mongodb = require("mongodb");
var database = module.exports = {};
-function createServer(options) {
- if (options["mongo-hosts"]) {
- var mongoServers = [];
- for (var i = 0; i < options["mongo-hosts"].length; i++) {
- var server_opt = options["mongo-hosts"][i];
- mongoServers.push(new mongodb.Server(server_opt[0], parseInt(server_opt[1],10), server_opt[2]));
- }
- return new mongodb.ReplSetServers(mongoServers, { read_secondary: true });
- }
- var server_options = options["mongo-server-options"] || { auto_reconnect: true };
- return new mongodb.Server(options["mongo-host"], options["mongo-port"], server_options);
-}
+// Opens MongoDB driver given connection URL and optional options:
+//
+// {
+// "mongo-url": "",
+// "mongo-options": {
+// "db": { "safe": false },
+// "server": { "auto_reconnect": true },
+// "replSet": { "read_secondary": true }
+// }
+// }
+//
+// See http://docs.mongodb.org/manual/reference/connection-string/ for details.
+// You can also specify a Replica Set this way.
+//
+database.open = function(config, callback) {
+ var url = config["mongo-url"] || database.config2url(config),
+ options = config["mongo-options"] || database.config2options(config);
+ return mongodb.Db.connect(url, options, callback);
+};
-// Open MongoDB driver given Cube options:
+//
+// For backwards-compatibility you can specify a connection to a single Mongo(s) as follows:
+//
// {
// "mongo-host": "localhost",
// "mongo-port": "27017",
+// "mongo-server-options": { "auto_reconnect": true },
// "mongo-database": "cube",
+// "mongo-database-options": { "safe": false },
// "mongo-username": null,
// "mongo-password": null,
// }
// (defaults are shown)
-database.open = function(options, callback) {
- var mongo = createServer(options),
- db_options = options["mongo-database-options"] || { safe: false },
- db = new mongodb.Db(options["mongo-database"] || "cube", mongo, db_options);
- db.open(function(error) {
- if (error) {
- return callback(error);
- }
- if (options["mongo-username"]) {
- db.authenticate(options["mongo-username"], options["mongo-password"], function(error, success) {
- if (error) {
- return callback(error);
- } else if (!success) {
- return callback(new Error("authentication failed"));
- }
- return callback(null, db);
- });
- } else {
- return callback(null, db);
- }
- });
+//
+database.config2url = function(config) {
+ var user = config["mongo-username"],
+ pass = config["mongo-password"],
+ host = config["mongo-host"] || "localhost",
+ port = config["mongo-port"] || 27017,
+ name = config["mongo-database"] || "cube",
+ auth = user ? user+":"+pass+"@" : "";
+ return "mongodb://"+auth+host+":"+port+"/"+name;
+};
+
+database.config2options = function(config) {
+ return {
+ db: config["mongo-database-options"] || { safe: false },
+ server: config["mongo-server-options"] || { auto_reconnect: true },
+ replSet: { read_secondary: true }
+ };
};

0 comments on commit 7cfb33b

Please sign in to comment.