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

qh-mongodb-uri

v0.9.14

Published

A parser and formatter for MongoDB URIs. forked

Downloads

2

Readme

mongodb-uri

Parse and format MongoDB URIs of the form:

mongodb://[username[:password]@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database]][?options]

Note that there are two minor differences between this format and the standard MongoDB connect string URI format:

  1. password is optional even when a username is supplied
  2. The slash before the database is not required when leaving out the database but specifying options

Neither of these differences should prevent this library from parsing any URI conforming to the standard format.

Usage

parse

Takes a URI string and returns a URI object of the form:

{
  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

var mongodbUri = require('mongodb-uri');
var uri = 'mongodb://user%3An%40me:p%40ssword@host:1234/d%40tabase?authSource=%40dmin';
var uriObject = mongodbUri.parse(uri);
console.log(JSON.stringify(uriObject, null, 2));
{
  "scheme": "mongodb",
  "hosts": [
    {
      "host": "host",
      "port": 1234
    }
  ],
  "username": "user:n@me",
  "password": "p@ssword",
  "options": {
    "authSource": "@dmin"
  },
  "database": "d@tabase"
}

format

Takes a URI object and returns a URI string.

Example

var mongodbUri = require('mongodb-uri');
var uri = mongodbUri.format(
        {
            username: 'user:n@me',
            password: 'p@ssword',
            hosts: [
                {
                    host: 'host',
                    port: 1234
                }
            ],
            database: 'd@tabase',
            options: {
                authSource: '@dmin'
            }
        }
);
console.log(uri);
mongodb://user%3An%40me:p%40ssword@host:1234/d%40tabase?authSource=%40dmin

formatMongoose

Takes either a URI object or string 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.

Useful in environments where a MongoDB URI environment variable is provided, but needs to be programmatically transformed into a string digestible by mongoose.connect()--for example, when deploying to a PaaS like Heroku using a MongoDB add-on like MongoLab.

Example

var mongoose = require('mongoose');
var mongodbUri = require('mongodb-uri');

// A MongoDB URI, not compatible with Mongoose because it lists multiple hosts in the address
// Could be pulled from an environment variable or config file
var uri = 'mongodb://username:password@host1:1234,host2:5678/database';

// Reformat to a Mongoose connect string and connect()
var mongooseConnectString = mongodbUri.formatMongoose(uri);
mongoose.connect(mongooseConnectString);

// Test for connection success
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'Connection error: '));
db.once('open', function callback () {
    console.log('Successfully connected to MongoDB');
});