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

ots-mongo-wrapper

v0.4.0

Published

ots-mongo-wrapper =================

Downloads

4

Readme

ots-mongo-wrapper

Wraps the node MongoDB native driver in an event emitter that emits connected when the database connection is established.

npm install ots-mongo-wrapper

Connection Options

The constructor expects a connection object like this:

{
  "host": "localhost",
  "port": 27021,
  "server_options": {
    "auto_reconnect": false,
    "poolSize": 20
  },
  "database": "DBNAME"
}

For a replica set, a connection object would look like this:

{
  "servers": [
    {
      "host": "localhost",
      "port": 27017
    },
    {
      "host": "localhost",
      "port": 27018
    },
    {
      "host": "localhost",
      "port": 27019
    },
  ],
  "server_options": {
    "auto_reconnect": false,
    "poolSize": 20
  },
  "connection_options": "?replicaSet=REPLSETNAME",
  "database": "DBNAME"
}

The options object is formatted to a standard MongoDB URL connection string.

How to interact with it

var
dbs = require("./path-to-connection-file"),
env = process.env.NODE_ENV || null,
db  = require("ots-mongo-wrapper")(dbs[env]);

db.collection("collectionName").find().toArray(function( err, docs ) {
  // Do stuff with docs.
});

There's a race condition above where you can start receiving queries before a database connection is established. The module will simply log to stdout that a query to a collection was received before the database connection was ready. Once the database connection is established, you'll start getting results as normal. If you want to avoid the race condition, do the following:

var
dbs = require("./path-to-connection-file"),
env = process.env.NODE_ENV || null,
db  = require("ots-mongo-wrapper")( dbs[env] );

// Wait for connected event.
db.on( "connected", function( readyDb ) {
    readyDb.collection("collectionName").find().toArray(function( err, docs ) {
        // Do stuff with docs.
    });
});

Multiple Connections

You can connect to multiple databases. The module keeps a hash of connections. The hash key is made up of the options you pass when you require. If you need the database connection you already established in another module, just pass in the identical options and you will get that client instance back. A new instance will not be created.