I am storing some locale based data in the following format in the CouchDB.
{
"_id": "a62f81b5afad1857c2f6399db500c73b",
"_rev": "3-923e5ed468e0f617f09057035b41051a",
"type": "CAT",
"origin_id": "1",
"locale": "ar",
"values_translation": {
"id": "ar",
"title": ""
}
}
I use Nano as the client in Node JS app and I need to insert if not exists / update if exists docs using the Update Handler. for that I have created the following document in the couchDB;
"_id": "_design/handler",
"_rev": "36-d2d08a0a822a762ac07faa4042eca5a0",
"updates": {
"handler": "function(doc, req){
if (doc.type === req.type && doc.origin_id === req.origin_id && doc.locale === req.locale)
{ return [doc, 'OK'];}
else { return [null, 'KO']; }
}"
},
"language": "javascript"
}
But When I run it using the following Nano Method,
translationsDB.atomic("handler", "handler", "_design/handler",
{ type, origin_id, locale, values_translation: value }).then((response) => {
console.log(response);
}).catch(err => {
console.log(err);
});
it always returns 'OK'
even if I pass invalid data.
For real data it returns OK
but it does not update the document.
Need to figure how to achieve this (update if the doc is existing or insert if it is a new doc) in right way.