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-wrap

v1.2.2

Published

Thin wrapper around native node mongoDB driver. Provides persistent authenticated connection and genericf routines useful to building simple REST interfaces.

Downloads

5

Readme

#MongoWrap Wraps native node mongoDB driver, to provide persistent authenticated connection and routines useful to the construction of RESTful interfaces. MongoWrap's primary goal is to provide a wrapper for the more generic calls & persisted connection, but not block any native "db" functionality (i.e., best of both worlds).

###MongoWrap API ######Basics

######Generic Methods:

######Utility Methods:


###Install #####Install from npm registry

    
    npm install mongo-wrap --save    

#####Clone from github

    
    git clone https://github.com/gmilligan/mongowrap.git    

###Basic Usage #####Use mongoDb "db" directly (passed in from "connect" method)

  // for calls requiring additional functionality 
  // than generic wrapper interface provides
  dbWrap.connect(function(err, db){
    db.collection('name')
      .find({},{},{})
      .toArray(...
  });
  
  // run an "aggregate" off "db" collection  

  dbWrap.connect(function(err, db){
    if(err) return cb(err);
    db.collection('name')
      .aggregate({$group: {_id: '$field'}}, 
      function(err, results){
      if(err) return cb(err);
      cb(null, results);
    });
  });

#####or, use MongoWrap's convenience methods for generic REST type calls

  var opts = {
    collection: @collection,
    id: @id
  }
  dbWrap.findById(opts, function(err, result){
    if(err) return cb(err);
    if(result) cb(null, result);
  });
    

###Instantiate & Configure MongoWrap


// create wrapper by passing in connect configuration
// this is likely stored in a json file or environment variables  

var config = {  
  "username": "username",  
  "password": "password",  
  "database": "database",  
  "host"    : "localhost", 
  "port"    : "27017",     
}
var dbWrap = new MongoWrap(config)

// - or inline - 
 
var dbWrap = new MongoWrap({
    username: "username",  
    password: "password",  
    database: "database",  
    host    : "localhost", 
    port    : "27017",     
});

###Create Connection (Express example)

// instantiate MongoWrap
var dbWrap = require('./server/tools/mongowrap/mongo-wrap')(cfg.mongo)

// share instance of MongoWrap to modules requiring db interaction
reportProvider = require('./server/data-providers/report-provider')(dbWrap) 

// start server after db is connected
dbWrap.connect(function(err, db) {
  if(err) throw err;

  app.listen(cfg.express.port);
  console.log('Started Local Server, Port:' + cfg.express.port);
});

###Generic Query & Manipulation Methods ####findAll() Returns array of documents, using user defined "query" & "sort"

Syntax:

    var query = {
      collection: @collection,
      where: {},
      sort: {}
    }
    dbWrap.findAll(query, function(err, results){
      if(err) return cb(err);
      if(results){...}
    });

==== ####findById() Returns a single document, located using item "id"

Syntax:

    var opts = {
      collection: @collection,
      id: @id
    }
    dbWrap.findById(opts, function(err, result){
      if(err) return cb(err);
      if(result) cb(null, result);
    });

==== ####insert() Insert a new document

Syntax:

    var opts = {
      collection: collectionName,
      data: {}
    }
    dbWrap.insert(opts, function(err, result){
      if(err) return cb(err);
      if(result} cb(null, result);
    })

==== ####updateById() Update existing document, using item "id"

Syntax:

   var opts = {
     collection: collectionName,
     id: @id,
     data: {}
   }
   dbWrap.updateById(opts, function(err, code){
     if(err) return cb(err);
     if(code.success===true) {...};
   });

==== ####removeById() Delete existing document, using item "id"

Syntax:

    var opts = {
      collection: @collection,
      id: @id
    }
    dbWrap.removeById(opts, function(err, code){
      if(err) return cb(err);
      if(code.success===true){...};
    });

###Utility Methods
####show() Log out a colored tree view of any JavaScript object. Handy to check results. Basically logs a util.inspect(data, true, 10, true) on the object. It is handy.

Syntax:

   dbWrap.show(doc);

####todayAsISODate() Return today's date as in ISO format

Syntax:

   var ISODate = dbWrap.todayAsISODate();
   

####dateAsISODate() Return a date in ISO format, if no date is passed in, it return today's date in ISO format.

Syntax:

   var ISODate = dbWrap.todayAsISODate(data);
   

####Thoughts/TODO:

  • As every db call requires a collection name in the options parameter, it seems more intuitive to pass the collection name as a first parameter, or at least allow this as an option.
  • As I have decided to share this library, I think may be time to move to a revealing module pattern and hide the internal _connect method.