Skip to content

DesignDocument

Dominic Barnes edited this page May 21, 2014 · 2 revisions

DesignDocument(db, [id], [rev])

Represents a CouchDB design document. Inherits directly from Document so any methods specified here will override the default behavior of Document.

Since this API is meant for creating design documents, the helper methods are chainable so you can write things in a "fluent" fashion.

Note: the _design/ prefix is not required most of the time, as it has been handled by the DesignDocument#id() method in place of the Document#id() method. However, if you are specifying the body directly, you will need to remember to do this yourself.

var couchdb = require("couchdb-api");

var db = couchdb().db("mydb");

// creates a design document with a specified language and view (map function only)
var ddoc = db.ddoc("myddoc")
    .language("javascript")
    .view("my-view", function (doc) {
        emit(doc.type, doc);
    });

DesignDocument#id([id])

This helper method gets or sets the _id property on the body of this document.

Note: this method has special behavior for DesignDocument, in that it will prefix _id with _design/ when using as a setter.

// getter
var id = ddoc.id();

// setter (no _design/ prefix required)
ddoc.id("my-other-ddoc");

DesignDocument#language(language)

Sets the language property of this design document.

Note: this is only a setter method.

ddoc.language("javascript");

DesignDocument#options(options)

Sets the options property (for configuration) of views in the this design document.

Note: this is only a setter method.

ddoc.options({
    include_design: true
});

DesignDocument#filter(name, fn)

Adds a new filter to this design document using the specified name as it's key.

Note: this is only a setter method.

ddoc.filter("myfilterfn", function (doc, req) {
    return doc.type === "mail";
});

DesignDocument#list(name, fn)

Adds a new list to this design document using the specified name as it's key.

Note: this is only a setter method.

ddoc.list("mylistfn", function (head, req) {
    while (var row = getRow()) {
        send(row.id);
    }
});

DesignDocument#rewrite(rule)

Adds a new rewrite to this design document.

Note: this is only a setter method, and it simply appends the rule object to an Array on each call.

ddoc
    .rewrite({
        from: "/a",
        to: "/some"
    })
    .rewrite({
        from; "/a/:foo",
        to: "/some",
        query: {
            k: "foo"
        }
    });

DesignDocument#show(name, fn)

Adds a new show to this design document using the specified name as it's key.

Note: this is only a setter method.

ddoc.show("myshowfn", function (doc, req) {
    if (doc) {
        return "Hello from " + doc._id + "!";
    } else {
        return "Hello, world!";
    }
});

DesignDocument#update(name, fn)

Adds a new update handler to this design document using the specified name as it's key.

Note: this is only a setter method.

ddoc.show("myshowfn", function (doc, req) {
    if (doc) {
        return "Hello from " + doc._id + "!";
    } else {
        return "Hello, world!";
    }
});

DesignDocument#validate(fn)

Sets the validation handler for this design document. (there can only be 1 per design document)

Note: this is only a setter method.

ddoc.validate(function (newDoc, oldDoc, userCtx, secObj) {
    if (newDoc._deleted === true) {
        throw { forbidden: "You cannot delete documents" };
    }
});

DesignDocument#view(name, [map], [reduce])

If only name is provided, it will return a new View object.

If a map is also provided, it adds a new view to this design document using the specified name as it's key.

The reduce parameter is optional, but it can either be a String (representing a built-in reduce function) or a Function.

// no reduce
ddoc.view("myview", function (doc) {
    emit(doc.type, doc);
});

// built-in reduce
ddoc.view("myview", function (doc) {
    emit(doc.type, doc);
}, "_count");

// custom reduce
ddoc.view("myview", function (doc) {
    emit(doc.type, doc);
}, function(keys, values, rereduce) {
    if (rereduce) {
        return sum(values);
    } else {
        return values.length;
    }
});