swarmforest

coding stuff

How to use js libs (like underscorejs) in your CouchDB views

facebooktwittergoogle_plusredditpinterestlinkedinmail

Sometimes, in CouchDB you have to do some data manipulation in the map/reduce functions, that involves array manipulation, summing timestamps, grouping and so on.

But CouchDB doesn’t offer a native way to do that. So you have to code all by your bare hands, like this:

//SOME DATA
var array = ["Juan", "Marisa", "James", "Juan", "Marcos"];

//CODE FOR GROUPING
result = {};
//BASICALLY YOU ITERATES OVER AN ARRAY
for(a in array){
  //IF THE INDEX IS ALREADY IN THE RESULT OBJECT, JUST ADD ONE
  if(result[array[a]])result[array[a]]++;
  //ELSE , YOU ADD A NEW ENTRY IN THE RESULT OBJECT
  else result[array[a]]=1;
}

//AT THIS POINT YOU HAVE THE RESULT OBJECT FILLED WITH PAIRS
/*
{
  "Juan" : 2,
  "Marisa" : 1, 
  "James": 1
  "Marcos": 1
}
*/

But this is not usual nowadays, where you have so many production-ready js libraries like underscorejs to do all the hard work for you.

When I faced this problem at the first time I found only 1 post that have mentioned it (CommonJS Modules in CouchDB by Caolan McMahon) which explains how to use this feature (Since version 1.1, it offers a way to use CommonJS 1.0 modules) in a CouchDB Database.

I just simply  picked up the whole minified source of underscorejs and pasted in the design document/ views / lib / libname  field of the design document that I want.

It will be like this:

UnderscoreJS lib in CouchDB

UnderscoreJS lib in CouchDB

You can do this with any library you want, since it follows the CommonJS Modules Specification.

And the code above in the post will be:

function(doc) {
  //IMPORTING THE LIB
  var _ = require("views/lib/underscorejs");
  //MOCKING DATA
  var array = ["Juan", "Marisa", "James", "Juan", "Marcos"];

  //INSTEAD OF DOING MANUAL OPERATIONS I SIMPLY CALL:
  var grouped = _.countBy(array, function(i){
      return i;
  });

  //AT THIS POINT YOU HAVE THE RESULT OBJECT FILLED WITH PAIRS 
  /* 
     { 
       "Juan" : 2, 
       "Marisa" : 1, 
       "James": 1, 
       "Marcos": 1 } 
  */

  //THAT YOU CAN EMIT TO REDUCE LIKE THIS.
  emit(doc._id, grouped); 
}

A print showing the result:

A Map function with UnderscoreJS

A Map function with UnderscoreJS

Another example is the momentjs lib, it can be helpful when you need to do some date/time manipulation.

But there are 2 issues in this feature:

#1 –  You can’t import the libs in the reduce function;

#2 – Like Caolan said, there’s still no support for caching of evaluated modules, that means require a module will be evaluated every time (a bit inneficient);

That’s it for now, please leave comments if you know some related techniques or alternatives for importing libs in CouchDB.

 

Resources:

CouchDB CommonJS Modules

CommonJS Module Specification 10.0

Jira Ticket about the second issue

facebooktwittergoogle_plusredditpinterestlinkedinmail

Next

Simple way to access CouchDB views using SQL

1 Comment

  1. Really? I have to copy the entire moment.js code into my design doc to use it. Seems archiac. Of course I am new to this stuff, yet I am an Engineer.

    I am using Cloudant NOSQL database on Bluemix and simply trying to use some date math to get last 24 hours, last 7 days and last 30 days from the current moment in time. God help me trying to get this done. I have researched everywhere and posted on Stack Overflow. Moment.js has a very useful add and subtract methods, but I can’t figure out how to incorporate them for use.

Leave a Reply

Powered by WordPress & Theme by Anders Norén