CouchDB: Pre-filled fields when adding new documen

2019-09-02 22:44发布

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条回答
聊天终结者
2楼-- · 2019-09-02 23:00

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.

查看更多
登录 后发表回答