You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by John Le Brasseur <jo...@gmail.com> on 2017/08/10 11:12:10 UTC

Document update handlers: Examples

Hi.
I am trying to create a generic document update handler.

I am using:

function(doc, req) {var field = req.query.field;  var value =
req.query.value; var message = 'set '+field+' to '+value;  doc[field] =
value; return [doc, message];  }

This works ok with simple json but not with a nested object such as
"abc":{"ax":"one", "by":"two" ...}

my curl command is:

curl -X PUT '
http://127.0.0.1:5984/db/_design/updatehandler/_update/inplace/id?field=abc.ax&value=three
'

The result is a new field is created and the existing abc:{ax:one} is left
untouched.


With a simpler example:

if I have: "xyz":"five"

curl -X PUT '
http://127.0.0.1:5984/db/_design/updatehandler/_update/inplace/id?field=xyz&value=ten
'

... works correctly.

I have not yet tried the generic process on "pqr":[s, t, u] yet but I guess
this may require a different design modification as well.

Ideally one wants something that works in at least the abovementioned three
cases if possible, as long as it is not too complex for it not to be worth
the effort.

Could someone possibly kindly help here or refer me to some examples please.

Many thanks.


John

Re: Document update handlers: Examples

Posted by John Le Brasseur <jo...@gmail.com>.
Thank you for that Stefan.

On 10 August 2017 at 14:02, Stefan Klein <st...@gmail.com> wrote:

> Hi
>
> 2017-08-10 13:12 GMT+02:00 John Le Brasseur <jo...@gmail.com>:
> >
> > Hi.
> > I am trying to create a generic document update handler.
> >
> > I am using:
> >
> > function(doc, req) {var field = req.query.field;  var value =
> > req.query.value; var message = 'set '+field+' to '+value;  doc[field] =
> > value; return [doc, message];  }
> >
> > This works ok with simple json but not with a nested object such as
> > "abc":{"ax":"one", "by":"two" ...}
>
>
> We use a _very_ stripped down version of lodash as a commonjs module.
> lodash provides a set and get function which does what you need.
>
> regards,
> Stefan
>



-- 
Kind regards,

John Le Brasseur
+27  73 157 5605
+27 11 431 0624

Re: Document update handlers: Examples

Posted by Stefan Klein <st...@gmail.com>.
Hi

2017-08-10 13:12 GMT+02:00 John Le Brasseur <jo...@gmail.com>:
>
> Hi.
> I am trying to create a generic document update handler.
>
> I am using:
>
> function(doc, req) {var field = req.query.field;  var value =
> req.query.value; var message = 'set '+field+' to '+value;  doc[field] =
> value; return [doc, message];  }
>
> This works ok with simple json but not with a nested object such as
> "abc":{"ax":"one", "by":"two" ...}


We use a _very_ stripped down version of lodash as a commonjs module.
lodash provides a set and get function which does what you need.

regards,
Stefan