mongo-dsn
Version:
A DSN Object for MongoDB
80 lines (62 loc) • 1.61 kB
Markdown
mongo-dsn
=========
[](https://travis-ci.org/vancarney/mongo-dsn)
[](https://www.npmjs.org/package/mongo-dsn)
DSN Utility for MongoDB
* Supports `NODE_ENV` for environment based configs
* Supports a flat config for generic config regardless of environment
Usage
-----------
*configs/db.json*
```
{
"development":{
"username":"devel",
"password":"password",
"protocol":"mongodb",
"host":"0.0.0.0",
"port": 27017,
"database":"develop"
},
"test":{
"username":"test",
"password":"password",
"protocol":"mongodb",
"host":"0.0.0.0",
"port": 27017,
"database":"testing"
},
"production":{
"username":"prod",
"password":"password",
"protocol":"mongodb",
"host":"mongohost.domain.com",
"port": 27017,
"database":"production"
}
}
```
*load the config file and connect*
```
DSN.loadConfig(""+__dirname+"/configs/db.json", function (e,dsn) {
dbConnector.invokeDBConnection( dsn.toDSN() );
});
```
Events
-----------
emitted if configuration fails validation
Methods
-----------
Constructs a new DSN Object from a String or JS Object
returns new DSN
Attempts to load and parse a Config file
invokes callback with 'error' and 'dsn' params containing error or new DSN respectively
returns the JS Object representation
returns the DSN String representation usable by a MongoDB connector
alias of `toDSN`