npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongo-connection-string

v0.1.5

Published

Handle MongoDB connection strings with ease!

Downloads

419

Readme

mongo-connection-string

CircleCI codecov Greenkeeper badge GuardRails badge

Utilities to help with MongoDB connection strings and related tasks.

Parsing Connection Strings

Parse a connection string into ConnectionString object:

const { ConnectionString } = require('mongo-connection-string');
const connectionString = new ConnectionString('mongodb://user:p@ssw0rd@host1,host2:2700/db?w=majority');

The connection string object has the following fields:

{
  protocol: 'mongodb://',
  username: 'user',
  password: 'p@ssw0rd',
  hosts: [{ host: 'host1', port: null }, { host: 'host2', port: 2700 }],
  database: 'db',
  options: {
    w: 'majority'
  }
}

You can now write it as a MongoDB compatible URI, or a more human readable string:

connectionString.toURI();

//  Produces:
//  mongodb://user:p%40ssw0rd/db?readPreference=secondary

connectionString.toString();

//  Produces:
//  mongodb://user:********/db?readPreference=secondary
//  (a little bit safer to go into logs/error messages etc)!

Building Connection Strings

The ConnectionString constructor can also take the fields required to build a connection string:

const { ConnectionString } = require('mongo-connection-string');

const connectionString = ConnectionString({
  username: 'user',
  password: 'pwd',
  hosts: [{ host: 'host1' }],
  database: 'db',
  options: {
    readPreference: 'secondary'
  }
});

//  Write out the connection string.
console.log(connectionString.toURI());

Produces:

mongodb://user:pwd@host1/db?readPreference=secondary

Url Encoding

When parsing a connection string, encoded charecters are unencoded:

const { parse } = require('mongo-connection-string');
const connectionString = new ConnectionString('mongodb://%40dmin:P%40ssword%3C%3E%2F@host1,host2:2700/db?w=majority');

//  Write out the connection string.
console.log(connectionString.username);
console.log(connectionString.password);

Produces:

@dmin
P@ssword<>/

Similarly, connection string object usernames and passwords are encoded:

const { ConnectionString } = require('mongo-connection-string');

const connectionString = ConnectionString({
  username: '@dmin',
  password: 'P@ssword<>/',
  hosts: [{ host: 'localhost' }]
});

//  Write out the connection string.
console.log(connectionString.toURI());

Produces:

mongodb://%40dmin:P%40ssword%3C%3E%2F@localhost

This means you can actually use the library to clean up a non-url encoded connection string. This can be useful to allow connection strings to be input in a more readable way (mongo-monitor does this):

new ConnectionString('@dmin:P@ssword<>/@localhost').toURI();

Produces:

mongodb://%40dmin:P%40ssword%3C%3E%2F@localhost

Notes

  • If no protocol is specifed, the library will assume mongodb:// should be used.
  • If there is a % symbol in the username or password, the code will try to URI decode it. If this fails, it will assume the username or password is plain text with a % symbol as part of the password. This means that if your password is actually something like p%40ssword then this will be URI decoded to p@ssword.