Security News
The Push to Ban Ransom Payments Is Gaining Momentum
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
mongo-uri-builder
Advanced tools
Readme
A zero dependency Node.js module to easily create MongoDB connection strings using configuration objects
Most of the existing MongoDB libraries (e.g. the official MongoClient or Mongoose) uses connection strings to connect to your running mongo instances which most of the times is very quick and convenient. Anyway sometime you want to have your MongoDB connection details organized in a nice javascript object so that you can easily serialize it in a configuration file and be able to override some specific values in different environments (e.g. "development" or "production") without having to override the entire connection string.
With this module you can easily accomplish this goal and in general you will be able to easily generate a MongoDB connection string starting from an organized javascript object that you can manipulate as you wish.
var mongoUriBuilder = require('mongo-uri-builder');
var connectionString = mongoUriBuilder({
username: 'user', // or user: 'user'
password: 'pass',
host: 'host1',
port: 1111,
replicas: [
{host: 'host2', port: 2222},
{host: 'host3', port: 3333}
],
database: 'db',
options: {
w: 0,
readPreference: 'secondary'
}
});
console.log(connectionString);
// prints "mongodb://user:pass@host1:1111,host2:2222,host3:3333/db?w=0&readPreference=secondary"
As easy as running:
npm install --save mongo-uri-builder
Requires Node.js 8+
As shown in the previous example the module exposes just a function that accepts an optional structured object as parameter. If no parameter is given the default MongoDB://localhost
will be built.
All the options are optional and the following example describes all the available ones:
{
username: 'user', // the username
// user: 'user', // this is alternative of username
password: 'pass', // the password
host: 'host1', // the main host (default: "localhost")
port: 1111, // the main port
replicas: [ // an array of replica databases
{host: 'host2', port: 2222}, // every replica must define an host, the port is optional
{host: 'host3', port: 3333}
],
database: 'db', // the name of the database
options: { // an arbitrary object of connection options
w: 0,
readPreference: 'secondary'
}
}
Everyone is very welcome to contribute to this project. You can contribute just by submitting bugs or suggesting improvements by opening an issue on GitHub.
You can also submit PRs as long as you adhere with the code standards and write tests for the proposed changes.
XO conventions and tools are used as code standard. You can easily check if your edits conforms the standard by running:
npm run check-style
Tape is used as default test runner. You can easily run the tests by using:
npm run tests
Covert can be used to check tests coverage by running:
npm run coverage
Licensed under MIT License. © Luciano Mammino.
FAQs
A module to easily create mongodb connection strings using configuration objects
We found that mongo-uri-builder demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
Application Security
New SEC disclosure rules aim to enforce timely cyber incident reporting, but fear of job loss and inadequate resources lead to significant underreporting.
Security News
The Python Software Foundation has secured a 5-year sponsorship from Fastly that supports PSF's activities and events, most notably the security and reliability of the Python Package Index (PyPI).