The official documentation has moved to http://docs.couchdb.org — The transition is not 100% complete, but http://docs.couchdb.org should be seen as having the latest info. In some cases, the wiki still has some more or older info on certain topics inside CouchDB.

You need to be added to the ContributorsGroup to edit the wiki. But don't worry! Just email any Mailing List or grab us on IRC and let us know your user name.

Javascript Pattern to Share Code Between View and Other Functions

This works only if one is using CouchApp since this pattern is dependent on CouchApp directives.

CouchApp directives are explained here: Chris Strom's blog.

Shared Code

The following Javascript code fragment can be used as an example of a shared library between a View Map function and other functions (in this example, a validate_update_doc function). In the design document, create an entry under vendor.myComp.utils which is:

   1 var utils = {
   2 
   3    isArray: function(o) {
   4       if( typeof(o) !== 'object' ) return false;
   5       if( typeof(o.length) !== 'number' ) return false;
   6       if( typeof(o.push) !== 'function' ) return false;
   7       if( typeof(o.pop) !== 'function' ) return false;
   8       if( typeof(o.concat) !== 'function' ) return false;
   9       if( typeof(o.join) !== 'function' ) return false;
  10       if( typeof(o.slice) !== 'function' ) return false;
  11       if( typeof(o.reverse) !== 'function' ) return false;
  12       if( typeof(o.splice) !== 'function' ) return false;
  13       if( typeof(o.sort) !== 'function' ) return false;
  14 
  15       return true;
  16    }
  17 };
  18 
  19 // CommonJS bindings
  20 if( typeof(exports) === 'object' ) {
  21    exports.isArray = utils.isArray;
  22 };

Notes:

  1. In a couchApp (an application developed and deployed using CouchApp), the content above would be saved in a file named "vendor/myComp/utils.js".

  2. Files uploaded using CouchApp are stripped of their extensions since it would lead to confusion as the dot (period) is the natural Javascript separator. Therefore, the shared code is "known" by CouchApp as "vendor/myComp/utils.js" while it is referred internally to the design documents as "vendor.myComp.utils".

Inclusion using CommonJs

Using CommonJs, the library defined above (shared code) would be included in a "validate_update_doc" function as follows:

   1 function(newDoc, oldDoc, userCtxt) {
   2         
   3    var myUtils = require('vendor/myComp/utils');
   4    
   5    // Verify geometries
   6    if( newDoc.type === 'myDoc' ) {
   7 
   8       // Verify that list is an array
   9       if( !myUtils.isArray(newDoc.list) ) {
  10          throw( {forbidden: 'Invalid or missing list'} );
  11       }
  12    }
  13 }

Notes:

  1. The 'require' function in CommonJs exposes the 'exports' defined in the library and assigns them to the variable.

  2. The variable 'myUtils' can be used to access the definitions from the shared code.
  3. Note that the path uses slashes and that the file extension (if using CouchApp) is omitted.

Inclusion using CouchApp directives

Since View Map and View Reduce functions do not have access to CommonJs, the way one can include shared code within those functions is to use CouchApp directives. More specifically, in CouchApp, the 'code' directive instructs the tool to include the content of another file (verbatim) at the location of the directive before uploading the file to CouchDb.

An example of View Map function that shares the code defined above:

   1 function(doc) {
   2         
   3    // !code vendor/myComp/utils.js
   4    
   5    // Verify that list is an array
   6    if( utils.isArray(doc.list) ) {
   7       for(var i=0,e=doc.list.length; i<e; ++i) {
   8          emit(doc.list[i],doc);
   9       }
  10    }
  11 }

Since CouchApp includes the whole file at the point of the directive, the resulting content uploaded for this map function is:

   1 function(doc) {
   2         
   3    var utils = {
   4    
   5       isArray: function(o) {
   6          if( typeof(o) !== 'object' ) return false;
   7          if( typeof(o.length) !== 'number' ) return false;
   8          if( typeof(o.push) !== 'function' ) return false;
   9          if( typeof(o.pop) !== 'function' ) return false;
  10          if( typeof(o.concat) !== 'function' ) return false;
  11          if( typeof(o.join) !== 'function' ) return false;
  12          if( typeof(o.slice) !== 'function' ) return false;
  13          if( typeof(o.reverse) !== 'function' ) return false;
  14          if( typeof(o.splice) !== 'function' ) return false;
  15          if( typeof(o.sort) !== 'function' ) return false;
  16 
  17          return true;
  18       }
  19    };
  20 
  21    // CommonJS bindings
  22    if( typeof(exports) === 'object' ) {
  23       exports.isArray = utils.isArray;
  24    };
  25    
  26    // Verify that list is an array
  27    if( utils.isArray(doc.list) ) {
  28       for(var i=0,e=doc.list.length; i<e; ++i) {
  29          emit(doc.list[i],doc);
  30       }
  31    }
  32 }

Notes:

  1. In this case, the CommonJs bindings are ignored since 'exports' is not defined in a View Map function.

  2. The shared code is accessed via the variable named 'utils' defined in the external content file.
  3. The CouchApp directive requires the file extension.

JavascriptPatternViewCommonJs (last edited 2011-11-23 15:07:14 by JanLehnardt)