Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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://<username>:<password>@<hosts[0].name>:<hosts[0].port>,<hosts[1].name>:<hosts[1].port>,(...)/<database>?querystring(<options>)
```

However if you set `config.url` directly, `setup` just uses the url rather creating another url.

### Query

```js
Expand Down
38 changes: 23 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down