I am trying create an equivalent of create/update trigger used in traditional RDBMs. create_ts is being created fine, however update_ts part is not working for me.
"updates": {
"add_ts": "function(doc, req)
{ if(!doc){
var result=JSON.parse(req.body);
result.created_ts=new Date();
return [result, 'Created']
}
doc.update_ts=new Date();
return [doc,'Updated'];
}"
},
The document creates all right:
curl -X POST $COUCHDB_URL/mobile_gateway/_design/devicetokens/_update/add_ts -d ' {"_id":"aaaa", "boris":"Ioffe"} '
{
"_id": "aaaa",
"_rev": "7-70069ed48a5fa2a571b5ad83067010b9",
"boris": "Ioffe",
"created_ts": "2018-12-24T20:24:58.064Z"
}
curl -X PUT $COUCHDB_URL/mobile_gateway/_design/devicetokens/_update/add_ts -d ' {"_id":"aaaa", "boris":"Loffe"} '
{"error":"conflict","reason":"Document update conflict."}
I feel I am missing something fundamental in my understanding couchdb document updates.