Writing a couchdb design document
Asked Answered
I

1

7

Would anyone be willing to provide a full documentation example on how to write a couchdb design document?

I've never been able to find a proper documentation on that. I'm able to find a list of the available methods, but not how to write them in the design document. For example, the couchdb documentation in this page explains how to use a map function, but it doesn't explain that this function is implemented in the design document the following way:

{
  "views": {
    "someView": {
      "map": "function(doc){ emit(doc.name, doc) }"
    }
  }
}

There are very sparse information on that in this page but it seems very incomplete to me. For example it doesn't even mention there can be "validate_doc_update" in the structure.

I think a good documentation example would be very useful in the couchdb doc itself in fact.
It could look like the following:

{
  "_id": "_design/exampleDesignDocument",
  "_rev": "1-11111111111111111111111111",
  "views": {
    "someView": {
      "map": "function(doc){ emit(doc.name, doc) }",
      ...
    }
  },
  "lists": {
    "someList": "function(head, req){ send('<html>hello</html>') }"
  }
  ...
}

This example would display usages of all design documents methods including (but not limited to if I forgot some): view (map, reduce functions...), show, list, update, filter, validate.

Idealistic answered 16/1, 2018 at 12:45 Comment(2)
IBM Cloudant has a good documentation section console.bluemix.net/docs/services/Cloudant/api/…Defend
I don't know why this has been voted down. It is a serious question and there are very few examples available through the documentation or a Google search that demonstrate the implementation of update handlers. Update handlers do not even SHOW UP in the Fauxton utility of CouchDB 2.2.0.Jadeite
I
8

Pouchdb docs provide good documentation elements to answer, as well as IBM cloudant, as proposed by @xpqz.

{
  "_id": "_design/exampleDesignDocument",
  "_rev": "1-11111111111111111111111111",
  "views": {
    "someView": {
      "map": "function(doc){ emit(doc.name, doc) }",
      ...
    }
  },
  "shows": {
    "someShowFunction": "function (doc, req) { ... }"
  },
  "lists": {
    "someList": "function(head, req){ send('<html>hello</html>') }"
  },
  "updates": {
    "oneUpdateFunc": "function (doc, req) { ... }"
  },
  "filters": {
    "someFilter": "function(doc, req){ if (doc.owner === req.userCtx.name) return true; else return false }"
  },
  "validate_doc_update": "function(newDoc, oldDoc, userCtx, secObj) { ... }"
}

But this answer can still be improved and completed I think.

Idealistic answered 16/1, 2018 at 16:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.