From 4853c7470316a6dbfbfa0d9b73133c8bb85f3b88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=86=90=ED=83=9C=EC=8A=B9?= Date: Fri, 30 Sep 2016 14:18:34 +0900 Subject: [PATCH 1/2] config accept url directly Someone would like this. --- index.js | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/index.js b/index.js index 40064c1..d6a5db1 100644 --- a/index.js +++ b/index.js @@ -115,23 +115,31 @@ DbWrapper.prototype.close = function() { }; var setup = function(config) { - var url = 'mongodb://'; - if (config.username) url += config.username + ':' + config.password + '@'; - if (config.hosts) { - config.hosts.forEach(function(host, i) { - if (i != 0) url += ','; - url += host.name + ':' + (host.port || 27017); - }); + var url = ''; + if(config.uri || config.url) { + // if full url(or uri) string is given, use it. + url = config.uri || config.url; } else { - url += 'localhost'; + // construct url string manually. + url = 'mongodb://'; + if (config.username) url += config.username + ':' + config.password + '@'; + if (config.hosts) { + config.hosts.forEach(function(host, i) { + if (i != 0) url += ','; + url += host.name + ':' + (host.port || 27017); + }); + } else { + url += 'localhost'; + } + url += '/' + (config.database + ''); + if (config.options) { + Object.keys(config.options).forEach(function(option, i) { + url += i == 0 ? '?' : '&'; + url += option + '=' + config.options[option]; + }); + }; } - url += '/' + (config.database + ''); - if (config.options) { - Object.keys(config.options).forEach(function(option, i) { - url += i == 0 ? '?' : '&'; - url += option + '=' + config.options[option]; - }); - }; + wrapper.db = new DbWrapper(url, config); return wrapper.db; }; From b0145623162afe8f86d7d865f1110c25be32124a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=86=90=ED=83=9C=EC=8A=B9?= Date: Fri, 30 Sep 2016 14:19:47 +0900 Subject: [PATCH 2/2] Update readme.md added more description for `config` object. --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 5443ae2..1783d97 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,15 @@ db = mongo.setup(config); db.add('users'); ``` +Basically, wrapper `setup` method constructs connection url from `config` object, +according following format(if no host is given, `localhost` is used): + +``` +mongodb://:@:,:,(...)/?querystring() +``` + +However if you set `config.url` directly, `setup` just uses the url rather creating another url. + ### Query ```js