编写一个 CouchDB 设计文档

7

是否有人愿意提供一个完整的文档示例,以说明如何编写couchdb设计文档?

我从未找到过适当的文档。我能找到可用方法的列表,但不知道如何在设计文档中编写它们。 例如,这个页面上的couchdb文档解释了如何使用映射函数,但它没有解释这个函数是如何在设计文档中实现的:

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

这方面的信息在此页面上非常稀少,但对我来说似乎非常不完整。例如,它甚至没有提到结构中可能会有“validate_doc_update”。

我认为,在couchdb文档本身中,一个好的文档示例将非常有用。
它可以看起来像下面这样:

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

这个例子将展示所有设计文档方法的用法,包括(但不限于,如果我忘了一些):查看(map、reduce函数...)、显示、列表、更新、过滤、验证。

1
IBM Cloudant有一个很好的文档部分 https://console.bluemix.net/docs/services/Cloudant/api/design_documents.html#design-documents - xpqz
2
我不知道为什么这个问题被投票否决了。这是一个严肃的问题,文档中几乎没有可用的示例或者通过谷歌搜索来演示更新处理程序的实现。在CouchDB 2.2.0的Fauxton工具中甚至没有显示更新处理程序。 - Billbad
1个回答

8

Pouchdb文档提供了很好的文档元素来回答问题,IBM Cloudant也是如此,正如@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) { ... }"
}

但我认为这个答案仍然可以改进和完善。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接