mongodb-uri
Advanced tools
Comparing version 0.9.0 to 0.9.1
@@ -29,3 +29,3 @@ /* | ||
* password: String, | ||
* hosts: [{ host: String, port: Number }], | ||
* hosts: [ { host: String, port: Number } ], | ||
* database: String, | ||
@@ -182,7 +182,36 @@ * options: !Object | ||
/** | ||
* Takes either a URI object or string in standard format and returns a Mongoose connection string. Specifically, | ||
* instead of listing all hosts and ports in a single URI, a Mongoose connection string contains a list of URIs each | ||
* with a single host and port pair. | ||
* | ||
* @param {!Object|String} uri | ||
* @return {String} | ||
*/ | ||
MongodbUriParser.prototype.formatMongoose = function formatMongoose(uri) { | ||
var parser = this; | ||
if (typeof uri === 'string') { | ||
uri = parser.parse(uri); | ||
} | ||
if (!uri) { | ||
return parser.format(uri); | ||
} | ||
var uriString = ''; | ||
uri.hosts.forEach(function (h, i) { | ||
if (i > 0) { | ||
uriString += ','; | ||
} | ||
// This trick is okay because format() never dynamically inspects the keys in its argument | ||
var singleUri = Object.create(uri); | ||
singleUri.hosts = [ h ]; | ||
uriString += parser.format(singleUri); | ||
}); | ||
return uriString; | ||
}; | ||
exports.MongodbUriParser = MongodbUriParser; | ||
var defaultParser = new MongodbUriParser(); | ||
['parse', 'format'].forEach(function (f) { | ||
['parse', 'format', 'formatMongoose'].forEach(function (f) { | ||
exports[f] = defaultParser[f].bind(defaultParser); | ||
}); |
{ | ||
"name": "mongodb-uri", | ||
"version": "0.9.0", | ||
"version": "0.9.1", | ||
"description": "A parser and formatter for MongoDB URIs.", | ||
"keywords": ["mongodb", "uri", "url", "parser", "formatter"], | ||
"keywords": [ "mongodb", "uri", "url", "parser", "formatter", "mongoose" ], | ||
"author": "MongoLab <support@mongolab.com>", | ||
@@ -7,0 +7,0 @@ "contributors": [ |
@@ -21,3 +21,27 @@ # mongodb-uri | ||
Takes a URI of the form: | ||
``` | ||
mongodb://[username[:password]@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/database][?options] | ||
``` | ||
and returns an object of the form: | ||
```javascript | ||
{ | ||
scheme: !String, | ||
username: String, | ||
password: String, | ||
hosts: [ { host: String, port: Number } ], | ||
database: String, | ||
options: !Object | ||
} | ||
``` | ||
`scheme` and `hosts` will always be present. Other fields will only be present in the result if they were present in the | ||
input. | ||
#### Example | ||
```javascript | ||
var mongodbUri = require('mongodb-uri'); | ||
@@ -48,2 +72,10 @@ var uriObject = mongodbUri.parse('mongodb://user%3An%40me:p%40ssword@host:1234/d%40tabase?authenticationDatabase=%40dmin'); | ||
Takes a URI object and returns a URI string of the form: | ||
``` | ||
mongodb://[username[:password]@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/database][?options] | ||
``` | ||
#### Example | ||
```javascript | ||
@@ -73,1 +105,19 @@ var mongodbUri = require('mongodb-uri'); | ||
``` | ||
### formatMongoose | ||
Takes either a URI object or string in standard format and returns a Mongoose connection string. Specifically, | ||
instead of listing all hosts and ports in a single URI, a Mongoose connection string contains a list of URIs each | ||
with a single host and port pair. | ||
#### Examples | ||
```javascript | ||
var mongodbUri = require('mongodb-uri'); | ||
var uri = mongodbUri.formatMongoose('mongodb://user%3An%40me:p%40ssword@host:1234,host:5678/d%40tabase?authenticationDatabase=%40dmin'); | ||
console.log(uri); | ||
``` | ||
``` | ||
mongodb://user%3An%40me:p%40ssword@host:1234/d%40tabase?authenticationDatabase=%40dmin,mongodb://user%3An%40me:p%40ssword@host:5678/d%40tabase?authenticationDatabase=%40dmin | ||
``` |
11778
191
121