2011-11-08 by sourpoi in misc, tagged: couchdb couchapp

Tips are running lists of the latest hints that helped.

Design docs and validate_doc_update

I thought that design impositions required the _design/<name>/ prefix to be run. Evidently this is not so.

Using two design documents in the same database. The first one defines validate_doc_update like this:

function (newDoc, oldDoc, userCtx) { }

All by itself, this doesn't stop anything.

Push a second design doc from a new couchapp directory that defines validate_doc_update like this:

function (newDoc, oldDoc, userCtx) {
  throw({forbidden:'no way'}); }

Now the database won't accept updates to normal documents. Evidently the validation functions in all of a database's design documents are applied.

Design document logs

Use log() to capture object as JSON in /var/log/couchdb/couch.log. Here it's in a validate_doc_update definition:

function (newDoc, oldDoc, userCtx) {

PUT, POST and _rev

Use the _rev from a GET to figure out what to POST:

curl -X GET http://localhost:5984/testxportr/xxx

curl -vX POST -d

You can't POST to the same URL that you GET. But you can PUT a new document directly to an _id so long as it doesn't already exist.

Couchapp and EOL

When using couchapp, use vim -b and set noeol to eliminate end-of-line characters. For example, in a language file containing the string javascript, if you edit it normally, the end-of-line character shows up in the design document after you push it using couchapp.

It's not clear whether these extra characters mess up anything besides my fragile sense of aesthetics. I was assuming that for something like language it might mess up a validation check someplace. But for a validate_doc_update it didn't seem to hurt anything. Go fish.