CouchDB: Pre-filled fields when adding new documents?

自作多情 提交于 2019-12-12 01:57:30

问题


In the CouchDB GUI, when I add a new document the _id field is the only field that appears by default. I must manually click "Add field" for each other field I want to add. This gets repetitive when I know every document in that particular database needs those fields. Is it possible to customize a database, so that when I create a new document in that database it has more than the _id field by default?


回答1:


I don't think it's possible to have predefined fields in CouchDB, I would probebly solve it by using a update function:

function(doc, req) {
  if (!doc) {
    return [null, JSON.stringify({
        status: 400,
        message: 'nodoc'
    })];
  }

  doc.foo = "";
  doc.bar = "";

  return [doc, JSON.stringify({
    doc: doc,
    status: 200,
    message: 'updated'
  })];
}

And call it with curl -X PUT http://127.0.0.1:5984/db/_design/foo/_update/bar/_id for all documents I create.



来源:https://stackoverflow.com/questions/32937655/couchdb-pre-filled-fields-when-adding-new-documents

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!