开发者

How do I upload a design document to CouchDB using cURL?

I'm trying to learn CouchDB and I can create views and such in Futon, but I want to write my design documents on the desktop and the upload them using cURL. The 'Definitive Guide' shows updating content documents with cURL but all the design documents are either Futon or CouchApp.

I'd like to download the current design doc to a local file, edit the fi开发者_JAVA技巧le, then send it back to CouchDB.

What are the cURL commands to download and upload CouchDB design documents?


Download the design file "task" in database "dev-task" to file "task.json" :

curl http://localhost:5984/dev-task/_design/task > task.json

Once the file is edited, you can put it back.

curl -X PUT http://localhost:5984/dev-task/_design/task -d @task.json

This works because task.json contains an appropriate revision number. If you want to change the file again, you need to re-download it first to get the latest revision number.


Despite the answer from Victor Nicollet is true, it is mostly advisable to have a look on CLI tools such as CouchApp. - Easy start and very effective. Also these tools are stable, have already came to maturity long time ago, and are constantly improving powered by community of contributors.

Why?

  1. Design document is a JSON objects, and these are known for the scufold they overload on their editor. For example - No comments, every string, including key attributes must be wrapped in strings, and so forth.
  2. JSON documents are not designed to contain functions. The Couch-DB solution is to require the functions to be stringified - what makes them hard to edit. An editor would be required to convert the function to a workable format, edit it, and stringify it again.
  3. The CouchDB requires that the udpated document will match with version the existing one (and regturns you the next verision when the update succeeds). This requires the editor to track the versions and use them every time he wishes to apply an update.
  4. Attachments - which are for example, elementary for code reuse between map functions, or often used for applications that are served directly from the CouchDB's web-server - are hard to manage, because every attachment is usually uploaded in separate, or with even greater scufold.

Solution to these problems are CLI utilities that allow the editor work on normal JavaScript files which he then can check in to an SCM like code files the same way he is used to, and upon command have the CLI tool convert them to a valid JSON for him, and upload it to the target CouchDB together with all whatsoever attachments you need to update, if any.

Such Solutions - would be :

  • CouchApp in it's 2 flavors (python and NodeJS)
  • Kanzo (that is in fact much more, and not for anyone, but most definitely worths mentioning)

I personally use couchapp over NodeJS. Have fun :)

UPDATED, ~2 years later

Here's another tool that worth mentioning, although I'll have to say I'm bios about it. It's called ddocs. It's based on same principals as node-couchapp, and it's directed to continuous integration flows as well as personal development. It uses a designated section in package.json to keep info about deployement of ddocs, and allowing the developer/build-server override any config using CLI args.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜