Bemerkung
While every design function has access to all JavaScript objects, the table
below describes appropriate usage cases. For example, you may use
emit() in List functions, but getRow() is not permitted
during Map functions.
| JS Function | Reasonable to use in design doc functions |
|---|---|
emit() |
Map functions |
getRow() |
List functions |
JSON |
any |
isArray() |
any |
log() |
any |
provides() |
Show functions, List functions |
registerType() |
Show functions, List functions |
require() |
any, except Reduce and rereduce functions |
send() |
List functions |
start() |
List functions |
sum() |
any |
toJSON() |
any |
Each design function executes in a special context of predefined objects, modules and functions:
emit(key, value)¶Emits a key-value pair for further processing by CouchDB after the map function is done.
| Parameter: |
|
|---|
function(doc){
emit(doc._id, doc._rev);
}
getRow()¶Extracts the next row from a related view result.
| Rückgabe: | View result row |
|---|---|
| Rückgabetyp: | object |
function(head, req){
send('[');
row = getRow();
if (row){
send(toJSON(row));
while(row = getRow()){
send(',');
send(toJSON(row));
}
}
return ']';
}
isArray(obj)¶A helper function to check if the provided value is an Array.
| Parameter: |
|
|---|---|
| Rückgabe: |
|
| Rückgabetyp: | boolean |
log(message)¶Log a message to the CouchDB log (at the INFO level).
| Parameter: |
|
|---|
function(doc){
log('Procesing doc ' + doc['_id']);
emit(doc['_id'], null);
}
After the map function has run, the following line can be found in CouchDB logs (e.g. at /var/log/couchdb/couch.log):
[Sat, 03 Nov 2012 17:38:02 GMT] [info] [<0.7543.0>] OS Process #Port<0.3289> Log :: Processing doc 8d300b86622d67953d102165dbe99467
provides(key, func)¶Registers callable handler for specified MIME key.
| Parameter: |
|
|---|
registerType(key, *mimes)¶Registers list of MIME types by associated key.
| Parameter: |
|
|---|
Predefined mappings (key-array):
*/*text/plain; charset=utf-8, txttext/html; charset=utf-8application/xhtml+xml, xhtmlapplication/xml, text/xml, application/x-xmltext/javascript, application/javascript,
application/x-javascripttext/csstext/calendartext/csvapplication/rss+xmlapplication/atom+xmlapplication/x-yaml, text/yamlmultipart/form-dataapplication/x-www-form-urlencodedapplication/json, text/x-jsonrequire(path)¶Loads CommonJS module by a specified path. The path should not start with a slash.
| Parameter: |
|
|---|---|
| Rückgabe: | Exported statements |
send(chunk)¶Sends a single string chunk in response.
| Parameter: |
|
|---|
function(head, req){
send('Hello,');
send(' ');
send('Couch');
return !
}
start(init_resp)¶Initiates chunked response. As an option, a custom response object may be sent at this point. For list-functions only!
Bemerkung
list functions may set the HTTP response code and headers by calling
this function. This function must be called before send(),
getRow() or a return statement; otherwise, the query server will
implicitly call this function with the empty object ({}).
function(head, req){
start({
"code": 302,
"headers": {
"Location": "http://couchdb.apache.org"
}
});
return "Relax!";
}
sum(arr)¶Sum arr‘s items.
| Parameter: |
|
|---|---|
| Rückgabetyp: | number |
toJSON(obj)¶Encodes obj to JSON string. This is an alias for the JSON.stringify
method.
| Parameter: |
|
|---|---|
| Rückgabe: | JSON string |
Support for CommonJS Modules (introduced in CouchDB 0.11.0) allows you to create modular design functions without the need for duplication of functionality.
Here’s a CommonJS module that checks user permissions:
function user_context(userctx, secobj) {
var is_admin = function() {
return userctx.indexOf('_admin') != -1;
}
return {'is_admin': is_admin}
}
exports['user'] = user_context
Each module has access to additional global variables:
module.exports objectThe CommonJS module can be added to a design document, like so:
{
"views": {
"lib": {
"security": "function user_context(userctx, secobj) { ... }"
}
},
"validate_doc_update": "function(newdoc, olddoc, userctx, secobj) {
user = require('lib/security').user(userctx, secobj);
return user.is_admin();
}"
"_id": "_design/test"
}
Modules paths are relative to the design document’s views object, but
modules can only be loaded from the object referenced via lib. The
lib structure can still be used for view functions as well, by simply
storing view functions at e.g. views.lib.map, views.lib.reduce, etc.