I know there are a couple of similar questions out there (like this one for example: CouchDB - trigger code when creating or updating document) which refer to "update handlers" in CouchDB but I have not been able to see how they solve the issue;
- Each time a new document is created in my CouchDB database I want to add a timestamp
I've created an update handler in _design/updatehandlers and verified that it gets called:
"timestampadd" : "function(doc,req) { if (!doc) { if ( req.id) { return [{_id : req.id, timestamp : new Date().getTime()}]; } return [null,'added empty']; } doc.timestamp = new Date().getTime(); return [doc,'added at ' + doc.timestamp]; }"
My question is this; How do can I use this function when creating a new document in the database, i.e. one which I would normally create by doing a POST to
http://mycouchdb:5984/test_db
where I want couchdb to create an _id for me (see for example: http://docs.cloudant.com/tutorials/crud/index.html#create)?
If I follow the documentation to do it (in Python in this example) I would issue a Post like so;
requests.post("http://mycouchdb:5984/test_db/_design/updatehandlers/_update/timestampadd",headers={'Content-type':'application/json'},data='{"foo":"bar"})
but here the update function timestampadd takes the "doc==null && req.id==null path, obviously, since the document has not yet been created and I didn't specify an ID. This leads me to believe that the update function can only be invoked on an existing document, despite what the documentation seems to imply, or am I wrong?
I can invoke the update on an existing document and add the timestamp without any trouble, that works well, but the issue here was to add a timestamp as the document is created.
Is that even possible..?