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

vs-mongo

v0.0.6

Published

Thin wrapper around native MongoDB driver for NodeJS

Downloads

2

Readme

vs-mongo

Thin wrapper around native MongoDB driver for NodeJS

Installation

npm install vs-mongo

Quick Start

var mongo = require('vs-mongo');

var uri = 'mongodb://localhost/sandbox';
var config = {
}

var db = mongo.connect(uri, config);

db.disconnect();

Configuration

  • shouldDropSync - permission to drop collections and indexes
  • batchSize - batch size used for retrieving large sets of documents (default - 10)
  • isCapped - collection should be capped
  • maxSize - maximum size of collection in bytes
  • maxDocCount - maximum number of documents in collection
  • shouldAutoIndexId - auto-populate and index _id field
  • keys - fields and orders used for indexing
  • options - options used for indexing
<config> = {
  <collection name> : {
    dropSync          : <shouldDropSync>,
    batchSize         : <batchSize>,
    create            : {
      capped            : <isCapped>,
      size              : <maxSize>,
      max               : <maxDocCount>,
      autoIndexId       : <shouldAutoIndexId>
    },
    indexes           : [
      [ <keys>, <options> ]
    ]
  }
}

Collection

Collection objects match <collection name> and are available immediately.

Create

Create a single document

var mongo = require('vs-mongo');

var uri = 'mongodb://localhost/sandbox';
var config = { users: { dropSync: true } }

var db = mongo.connect(uri, config);

var names = [ 'Bill', 'Jill', 'Phil', 'Will' ];
var users = names.map(function ( name ) {
  return db.users.create({ name: name });
});

Save

Saving one or multiple documents

db.users.save(users, function ( error, value ) {
  if ( error ) console.log('error occurred while saving users: ' + error);
  else console.log('users have been successfully saved');
});

Refresh

Refreshing one or multiple documents

db.users.refresh(users, function ( error, value ) {
  if ( error ) console.log('error occurred while refreshing users: ' + error);
  else console.log('users have been successfully refreshed');
});

Remove

Removing one or multiple documents

db.users.remove(users, function ( error, value ) {
  if ( error ) console.log('error occurred while removing users: ' + error);
  else console.log('users have been successfully removed');
});

Find One

Find first matching document

var query = { name: /^.ill$/ }, options = { }

db.users.findOne(query, options, function ( error, value ) {
  if ( error ) console.log('error occurred while looking for user: ' + error);
  else console.log('successfully found user: ' + String(value));
});

Find All

Find all matching documents (the result is limited by batchSize)

var query = { name: /^.ill$/ }, options = { }

db.users.findAll(query, options, function ( error, value ) {
  if ( error ) console.log('error occurred while looking for users: ' + error);
  else {
    console.log('successfully found users:');
    
    for ( var i = 0; i < value.length; i += 1 ) {
      console.log('user' + i + ' - ' + String(value[i]));
    }
  }
});

Stream

Streaming is used for retrieving large amounts of documents in batches

var query = { name: /^.ill$/ }, options = { }

var onBatch = function onBatch ( users ) {
  console.log('successfully retrieved a batch of users');
}

var callback = function callback ( error, value ) {
  if ( error ) console.log('error occurred while retrieving users: ' + error);
  else console.log('successfully retrieved all user');
}

db.users.stream(query, options, onBatch, callback);

Count

Counting matching documents

var query = { name: /^.ill$/ }

db.users.count(query, function ( error, value ) {
  if ( error ) console.log('error occurred while counting users: ' + error);
  else console.log('users have been successfully counted: ' + String(value));
});

Remove One

Remove first matching document

var query = { name: /^.ill$/ }

db.users.removeOne(query, function ( error, value ) {
  if ( error ) console.log('error occurred while removing user: ' + error);
  else console.log('successfully removed user: ' + String(value));
});

Remove All

Remove all matching documents

var query = { name: /^.ill$/ }

db.users.removeAll(query, function ( error, value ) {
  if ( error ) console.log('error occurred while removing users: ' + error);
  else console.log('successfully removed users: ' + String(value));
});

Document

Objects return by collection methods are wrappers around raw documents retrieved from the database.

These wrappers provide additional functionality that should make developer's life easier

Save

Save can easily be chained to the end of document creation

var callback = function callback ( error, value ) {
  if ( error ) console.log('error occurred while saving user: ' + error);
  else console.log('user have been successfully saved');
}

var user = db.users.create({ name: 'Joe' }).save(callback);

Refresh

Refresh helps to make sure the underlying document is up-to-date

var callback = function callback ( error, value ) {
  if ( error ) console.log('error occurred while refreshing user: ' + error);
  else console.log('user have been successfully refreshed');
}

user.refresh(callback);

Remove

Remove can be called directly on the object

var callback = function callback ( error, value ) {
  if ( error ) console.log('error occurred while removing user: ' + error);
  else console.log('user have been successfully removed');
}

user.remove(callback);

Extend

When extending a class, collection functionality is transferred to the class, and all retrieved documents are passed to the class constructor

var mongo = require('vs-mongo');

var uri = 'mongodb://localhost/sandbox';
var config = { users: { dropSync: true } }

var db = mongo.connect(uri, config);

var User = function User ( doc ) {
  db.users.create(doc, this);
}

db.users.extend(User);

var names = [ 'Bill', 'Jill', 'Phil', 'Will' ];
var users = names.map(function ( name ) {
  return new User({ name: name });
});

User.save(users, function ( error, value ) {
  if ( error ) console.log('error occurred while saving users: ' + error);
  else console.log('users have been successfully saved');
});

Logging

The output to the console, generated by the module, can be turned on or off

var mongo = require('vs-mongo');

mongo.log.on();    // turn logging on
mongo.log.off();   // turn logging off

mongo.log(true);   // turn logging on
mongo.log(false);  // turn logging off

License

MIT