We have a new wiki. The migration is not 100% complete. You can help out by moving pages across. This wiki will exist for as long as there are pages left.

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.

Javascript Pattern to Share Code Between View and Other Functions

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:

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

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:

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

The variable 'myUtils' can be used to access the definitions from the shared code.

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.