parse-database-url
Advanced tools
Comparing version 0.2.2 to 0.3.0
var url = require("url"); | ||
var querystring = require("querystring"); | ||
var mongodbUri = require("mongodb-uri"); | ||
/** | ||
@@ -20,4 +22,4 @@ * This is the exported function that parses database URLs. | ||
config.driver = (parsedUrl.protocol || "sqlite3:") | ||
// The protocol coming from url.parse() has a trailing : | ||
.replace(/\:$/, "") | ||
// The protocol coming from url.parse() has a trailing : | ||
.replace(/\:$/, ""); | ||
@@ -31,3 +33,3 @@ // Cloud Foundry will sometimes set a 'mysql2' scheme instead of 'mysql'. | ||
if (parsedUrl.auth) { | ||
var userPassword = parsedUrl.auth.split(':', 2); | ||
var userPassword = parsedUrl.auth.split(":", 2); | ||
config.user = userPassword[0]; | ||
@@ -53,6 +55,28 @@ if (userPassword.length > 1) { | ||
} else { | ||
// Some drivers (e.g., redis) don't have database names. | ||
if (parsedUrl.pathname) { | ||
config.database = | ||
parsedUrl.pathname.replace(/^\//, "").replace(/\/$/, ""); | ||
if (config.driver === "mongodb") { | ||
// MongoDB URLs can have multiple comma-separated host:port pairs. This | ||
// trips up the standard URL parser. | ||
var mongoParsedUrl = mongodbUri.parse(databaseUrl); | ||
parsedUrl = {}; | ||
if (mongoParsedUrl.hosts) { | ||
config.hosts = mongoParsedUrl.hosts; | ||
for (var i = 0; i < config.hosts.length; i += 1) { | ||
if (config.hosts[i].port) | ||
config.hosts[i].port = config.hosts[i].port.toString(); | ||
} | ||
if (config.hosts.length === 1) { | ||
if (config.hosts[0].host) | ||
config.host = config.hosts[0].host; | ||
if (config.hosts[0].port) | ||
config.port = config.hosts[0].port.toString(); | ||
} | ||
} | ||
if (mongoParsedUrl.database) | ||
config.database = mongoParsedUrl.database; | ||
} else { | ||
// Some drivers (e.g., redis) don't have database names. | ||
if (parsedUrl.pathname) { | ||
config.database = | ||
parsedUrl.pathname.replace(/^\//, "").replace(/\/$/, ""); | ||
} | ||
} | ||
@@ -59,0 +83,0 @@ |
{ | ||
"name": "parse-database-url", | ||
"version": "0.2.2", | ||
"version": "0.3.0", | ||
"description": "Database configuration URL parser for node.js", | ||
@@ -20,6 +20,7 @@ "keywords": ["database", "configuration", "url", "heroku", "database_url"], | ||
"dependencies": { | ||
"mongodb-uri": ">= 0.9.7" | ||
}, | ||
"devDependencies": { | ||
"chai": ">= 1.6.0", | ||
"mocha": ">= 1.10.0" | ||
"chai": ">= 3.4.1", | ||
"mocha": ">= 2.3.4" | ||
}, | ||
@@ -26,0 +27,0 @@ "main": "lib/parse-database-url.js", |
# DATABASE_URL parser for node.js | ||
[![Build Status](https://travis-ci.org/pwnall/node-parse-database-url.svg)](https://travis-ci.org/pwnall/node-parse-database-url) | ||
[![NPM Version](http://img.shields.io/npm/v/parse-database-url.svg)](https://www.npmjs.org/package/parse-database-url) | ||
This is an [npm](https://npmjs.org/) package that parses database | ||
@@ -4,0 +7,0 @@ configurations passed in as URLs. This is typically useful in Heroku |
@@ -92,3 +92,60 @@ [ | ||
} | ||
}, | ||
{ | ||
"desc": "MongoDB URL with username and host", | ||
"url": "mongodb://someuser@server.heroku.com/herokudb", | ||
"config": { | ||
"driver": "mongodb", | ||
"user": "someuser", | ||
"host": "server.heroku.com", | ||
"hosts": [ | ||
{ | ||
"host": "server.heroku.com" | ||
} | ||
], | ||
"database": "herokudb" | ||
} | ||
}, | ||
{ | ||
"desc": "MongoDB URL with username/password and host/port", | ||
"url": "mongodb://user:pass@server.heroku.com:1337/herokudb", | ||
"config": { | ||
"driver": "mongodb", | ||
"user": "user", | ||
"password": "pass", | ||
"host": "server.heroku.com", | ||
"port": "1337", | ||
"hosts": [ | ||
{ | ||
"host": "server.heroku.com", | ||
"port": "1337" | ||
} | ||
], | ||
"database": "herokudb" | ||
} | ||
}, | ||
{ | ||
"desc": "MongoDB URL with username/password and multiple hosts/ports (Issue #6)", | ||
"url": "mongodb://user:pass@server1.heroku.com:1337,server2.heroku.com:1338,server3.heroku.com:1339/herokudb", | ||
"config": { | ||
"driver": "mongodb", | ||
"user": "user", | ||
"password": "pass", | ||
"hosts": [ | ||
{ | ||
"host": "server1.heroku.com", | ||
"port": "1337" | ||
}, | ||
{ | ||
"host": "server2.heroku.com", | ||
"port": "1338" | ||
}, | ||
{ | ||
"host": "server3.heroku.com", | ||
"port": "1339" | ||
} | ||
], | ||
"database": "herokudb" | ||
} | ||
} | ||
] |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11290
9
247
74
1
+ Addedmongodb-uri@>= 0.9.7
+ Addedmongodb-uri@0.9.7(transitive)