DailyJS

Node Roundup: mongo-select, asynctrace

Alex R. Young

Subscribe

@dailyjs

Facebook

Google+

testing node modules npm mongodb

Node Roundup: mongo-select, asynctrace

Posted by Alex R. Young on .
Featured

testing node modules npm mongodb

Node Roundup: mongo-select, asynctrace

Posted by Alex R. Young on .

mongo-select

mongo-select (GitHub: dschenkelman / mongo-select, License: MIT, npm: mongo-select) is a module for creating projections on results from mongodb.

You can specify the fields to include using select.include:

var select = require('mongo-select').select();  
var projection = select.include(['name', 'email', 'children.name']).make();

console.log(projection); // { 'name': false, 'email': false, 'children.name': false };  

There are also methods for excluding fields (exclude) and ignoring the _id field (noId). These methods can be chained:

select.noId()._exclude(['name', 'email', 'children.name']);  

Using it with MongoDB queries looks like this:

var select = require('mongo-select').select();  
var mongodb = require('mongodb');

var MongoClient = mongodb.MongoClient;

MongoClient.connect('mongodb://127.0.0.1:27017/test', function(err, db) {  
  if (err) throw err;

  var users = db.collection('users');

  users.find(
    {},
    select.include(['name', 'email']),
    function(err, result) {
      // code here, access to only result[i]._id, result[i].name and result[i].email
    }
  );
});

asynctrace

asynctrace (GitHub: Empeeric / asynctrace, License: MIT, npm: asynctrace) from Empeeric is a stack trace API based on AsyncListener. By requiring asynctrace, you'll get stack traces that include details on asynchronous boundaries.

It's built using the shimmer module, which is meant to help safely replace functions by wrapping them.

If you use Mocha then you can optionally use it by specifying it on the command line: mocha --require asynctrace. That means you don't need to modify your code to use the module during testing.