You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Vivek Pathak <vp...@orgmeta.com> on 2013/11/20 07:26:43 UTC

simultaneously run update handler and update doc content

Hi

I have couchdb 1.2.1 - and wanted to update document content and also 
run a update handler using only one http call.

Is it possible to do this?

Thanks
Vivek


Re: simultaneously run update handler and update doc content

Posted by Vivek Pathak <vp...@orgmeta.com>.
On 11/20/2013 02:40 AM, Vivek Pathak wrote:
>
> I have an update handler.
> |{
>     "|_id|":|"_design/compnbd"|,
>     "|_rev|":|"36-3321da9588a5a7b3fef8789bdfefe18d"|,
>     "|updates|":|{
>         "|timestamp|":|"function(doc, req) {\n    if( doc.type == \"profile\" ) { \n        var d = new Date();\n        doc.serverISO8601time = d.toISOString(); \n        doc.serverTime = d.getTime() / 1000 ;\n    }\n    return [doc , ''] ;\n} \n"|
>     }|,
>     "|language|":|"javascript"|,
>     "|views|":|{
>        // removed ...
>     }
> }
> ||
>
> The function is copied here for convenience:
> function(doc, req) {
>     if( doc.type == "profile" ) {
>         var d = new Date();
>         doc.serverISO8601time = d.toISOString();
>         doc.serverTime = d.getTime() / 1000 ;
>     }
>     return [doc , ''] ;

The return is an array of doc and empty string  (empty string was messed 
up in copy paste to email) - but the same behavior happens if it were "OK"

> }
>
>
> I run the following commands:
>
>    curl -s 'http://localhost:5984/i7rmdb/testid' > /tmp/2.json
>
>    cat /tmp/2.json
> {"_id":"testid","_rev":"5-71ee77bc43fc698a935abc60dc33a82c","type":"profile","text":"hello  123","serverISO8601time":"2013-11-20T07:29:35.497Z","serverTime":1384932575.496999979}
>
> Now I edit the document.
>
>    cat /tmp/2.json
> {"_id":"testid","_rev":"5-71ee77bc43fc698a935abc60dc33a82c","type":"profile","text":"hello 
> 12345 !!!! text changed .... 
> ","serverISO8601time":"2013-11-20T07:29:35.497Z","serverTime":1384932575.496999979}
>
> And put it using the update handler.
>
>    curl -s -X PUT 
> 'http://localhost:5984/i7rmdb/_design/compnbd/_update/timestamp/testid' -d 
> @/tmp/2.json
>
> Notice the text did not change, just the time stamp changed.
>
>    curl -s 'http://localhost:5984/i7rmdb/testid'
> {"_id":"testid","_rev":"6-6726469319fc80710ef15d0df6dd6320","type":"profile","text":"hello123","serverISO8601time":"2013-11-20T07:31:58.351Z","serverTime":1384932718.3510000706}
>
>
> I was expecting both text and timestamp to change.  Is that correct?
>
> Thanks
>
>
> On 11/20/2013 01:38 AM, Stanley Iriele wrote:
>> Are you asking how to update a document with an update handler? Like...what
>> the  pattern looks like?
>> On Nov 19, 2013 10:31 PM, "Alexander Shorin"<kx...@gmail.com>  wrote:
>>
>>> The update handlers does document update already with single http
>>> request. To update document content or not is the question of function
>>> implementation.
>>> --
>>> ,,,^..^,,,
>>>
>>>
>>> On Wed, Nov 20, 2013 at 10:26 AM, Vivek Pathak<vp...@orgmeta.com>
>>> wrote:
>>>> Hi
>>>>
>>>> I have couchdb 1.2.1 - and wanted to update document content and also
>>> run a
>>>> update handler using only one http call.
>>>>
>>>> Is it possible to do this?
>>>>
>>>> Thanks
>>>> Vivek
>>>>
>


Re: simultaneously run update handler and update doc content

Posted by Vivek Pathak <vp...@orgmeta.com>.
Sure - I have several fields in the document and in principle, the 
client could send only what it changed - if it was also guaranteed to be 
the only writer (or I could send rev id to check).

But that requires my client to remember more state.  I am happy with 
sending entire document for now.

Thanks

On 11/20/2013 04:04 AM, Stanley Iriele wrote:
> Right...are you loading the entire json object out of the
> database?...messing with it..and then placing it back?
>
> Why not just send what you need?...in the http request?..that way you don't
> have to maintain the rev number...because the way you have it now if
> something updates the document out of place you get a 409 and you have to
> do a GET (by your model) instead of just sending the what you need
> changed...update handlers don't remove the possibility of conflicts but
> they do mamke it so you don't have to do the GET update PUT loop...or care
> about maintaining revisions
>
>
> On Wed, Nov 20, 2013 at 12:44 AM, Vivek Pathak <vp...@orgmeta.com> wrote:
>
>> I would like to do both update and run update handler - without having a
>> window of time between the client update and sever side update handler.
>>
>> What is wrong with this ?
>>
>>
>>
>> On 11/20/2013 03:36 AM, Stanley Iriele wrote:
>>
>>> Wait...I believe you''re fundamentally have the idea of the update handler
>>> wrong
>>>
>>> "curl -s -X PUT 'http://localhost:5984/i7rmdb/_design/compnbd/_update/
>>> timestamp/testid' -d @/tmp/2.json"
>>>
>>> you don't need to pass in that json file
>>>
>>> an update handler loads the latest version of that doc from the database
>>> using the id you pass in the url...in this case it is "testid"
>>> then couchdb places , a convenient version, of the http request you sent
>>> in
>>> in the second parameter
>>>
>>> your update function should look more like this:
>>>
>>> function(doc, req) {
>>>       if( doc.type == "profile" ) {
>>>
>>>           var d = new Date();
>>>           doc.serverISO8601time = d.toISOString();
>>>           doc.serverTime = d.getTime() / 1000 ;
>>>           log( toJSON(doc) ) ;
>>>           return [doc , "OK"] ;
>>>       }
>>> }
>>>
>>> and your curl command should look like this
>>> "curl -s -X PUT 'http://localhost:5984/i7rmdb/_design/compnbd/_update/
>>> timestamp/testid'
>>>
>>> side note....pipe it into python -mjson.tool for nice json formatting
>>>
>>>
>>>
>>>
>>>
>>> On Wed, Nov 20, 2013 at 12:16 AM, Vivek Pathak <vp...@orgmeta.com>
>>> wrote:
>>>
>>>   Ok - got it
>>>> I should have used
>>>>
>>>>      var newdoc = JSON.parse(req.body) ;
>>>>
>>>> I guess returning string as the first arg was somehow silently ignored.
>>>> Also the permissiveness of javascript allowed the rest of the code to run
>>>> without exception
>>>>
>>>> js> newdoc = "hello world" ;
>>>> "hello world"
>>>> js> var d = new Date();
>>>> js> newdoc.serverISO8601time = d.toISOString();
>>>> "2013-11-20T08:14:55.731Z"
>>>> js> newdoc.serverTime = d.getTime() / 1000 ;
>>>> 1384935295.731
>>>> js> newdoc
>>>> "hello world"
>>>>
>>>>
>>>> Thanks
>>>>
>>>>
>>>>
>>>> On 11/20/2013 03:10 AM, Vivek Pathak wrote:
>>>>
>>>>   Ok - so we are taking about the first problem only.
>>>>> If I try this, then I get not update to the data at all:
>>>>>
>>>>> function(doc, req) {
>>>>>       if( doc.type == "profile" ) {
>>>>>           var newdoc = req.body ;
>>>>>           var d = new Date();
>>>>>           newdoc.serverISO8601time = d.toISOString();
>>>>>           newdoc.serverTime = d.getTime() / 1000 ;
>>>>>           log( toJSON(newdoc) ) ;
>>>>>           return [newdoc , "OK"] ;
>>>>>       }
>>>>> }
>>>>>
>>>>> What is wrong here?
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> On 11/20/2013 03:04 AM, Alexander Shorin wrote:
>>>>>
>>>>>   On Wed, Nov 20, 2013 at 12:01 PM, Vivek Pathak <vp...@orgmeta.com>
>>>>>> wrote:
>>>>>>
>>>>>>   On 11/20/2013 02:48 AM, Alexander Shorin wrote:
>>>>>>>   First problem is that your update function works with `doc` argument
>>>>>>>> -
>>>>>>>> that is the stored within database document -, not that you had sent
>>>>>>>> with PUT request. To process the request payload you need to work
>>>>>>>> with
>>>>>>>> req.body data, but in your case this isn't need to.
>>>>>>>>
>>>>>>>> Second is that both text and timestamp have actually changed - take a
>>>>>>>> look on them one more time (;
>>>>>>>>
>>>>>>>>   I see "hello 123" in both the text fields.  Not sure what you see
>>>>>>> as the
>>>>>>> change.
>>>>>>>
>>>>>>>   Your update function changes only serverISO8601time and serverTime
>>>>>> fields, not any others.
>>>>>>
>>>>>> --
>>>>>> ,,,^..^,,,
>>>>>>
>>>>>>


Re: simultaneously run update handler and update doc content

Posted by Stanley Iriele <si...@gmail.com>.
Right...are you loading the entire json object out of the
database?...messing with it..and then placing it back?

Why not just send what you need?...in the http request?..that way you don't
have to maintain the rev number...because the way you have it now if
something updates the document out of place you get a 409 and you have to
do a GET (by your model) instead of just sending the what you need
changed...update handlers don't remove the possibility of conflicts but
they do mamke it so you don't have to do the GET update PUT loop...or care
about maintaining revisions


On Wed, Nov 20, 2013 at 12:44 AM, Vivek Pathak <vp...@orgmeta.com> wrote:

> I would like to do both update and run update handler - without having a
> window of time between the client update and sever side update handler.
>
> What is wrong with this ?
>
>
>
> On 11/20/2013 03:36 AM, Stanley Iriele wrote:
>
>> Wait...I believe you''re fundamentally have the idea of the update handler
>> wrong
>>
>> "curl -s -X PUT 'http://localhost:5984/i7rmdb/_design/compnbd/_update/
>> timestamp/testid' -d @/tmp/2.json"
>>
>> you don't need to pass in that json file
>>
>> an update handler loads the latest version of that doc from the database
>> using the id you pass in the url...in this case it is "testid"
>> then couchdb places , a convenient version, of the http request you sent
>> in
>> in the second parameter
>>
>> your update function should look more like this:
>>
>> function(doc, req) {
>>      if( doc.type == "profile" ) {
>>
>>          var d = new Date();
>>          doc.serverISO8601time = d.toISOString();
>>          doc.serverTime = d.getTime() / 1000 ;
>>          log( toJSON(doc) ) ;
>>          return [doc , "OK"] ;
>>      }
>> }
>>
>> and your curl command should look like this
>> "curl -s -X PUT 'http://localhost:5984/i7rmdb/_design/compnbd/_update/
>> timestamp/testid'
>>
>> side note....pipe it into python -mjson.tool for nice json formatting
>>
>>
>>
>>
>>
>> On Wed, Nov 20, 2013 at 12:16 AM, Vivek Pathak <vp...@orgmeta.com>
>> wrote:
>>
>>  Ok - got it
>>>
>>> I should have used
>>>
>>>     var newdoc = JSON.parse(req.body) ;
>>>
>>> I guess returning string as the first arg was somehow silently ignored.
>>> Also the permissiveness of javascript allowed the rest of the code to run
>>> without exception
>>>
>>> js> newdoc = "hello world" ;
>>> "hello world"
>>> js> var d = new Date();
>>> js> newdoc.serverISO8601time = d.toISOString();
>>> "2013-11-20T08:14:55.731Z"
>>> js> newdoc.serverTime = d.getTime() / 1000 ;
>>> 1384935295.731
>>> js> newdoc
>>> "hello world"
>>>
>>>
>>> Thanks
>>>
>>>
>>>
>>> On 11/20/2013 03:10 AM, Vivek Pathak wrote:
>>>
>>>  Ok - so we are taking about the first problem only.
>>>>
>>>> If I try this, then I get not update to the data at all:
>>>>
>>>> function(doc, req) {
>>>>      if( doc.type == "profile" ) {
>>>>          var newdoc = req.body ;
>>>>          var d = new Date();
>>>>          newdoc.serverISO8601time = d.toISOString();
>>>>          newdoc.serverTime = d.getTime() / 1000 ;
>>>>          log( toJSON(newdoc) ) ;
>>>>          return [newdoc , "OK"] ;
>>>>      }
>>>> }
>>>>
>>>> What is wrong here?
>>>>
>>>>
>>>>
>>>>
>>>> On 11/20/2013 03:04 AM, Alexander Shorin wrote:
>>>>
>>>>  On Wed, Nov 20, 2013 at 12:01 PM, Vivek Pathak <vp...@orgmeta.com>
>>>>> wrote:
>>>>>
>>>>>  On 11/20/2013 02:48 AM, Alexander Shorin wrote:
>>>>>>
>>>>>>  First problem is that your update function works with `doc` argument
>>>>>>> -
>>>>>>> that is the stored within database document -, not that you had sent
>>>>>>> with PUT request. To process the request payload you need to work
>>>>>>> with
>>>>>>> req.body data, but in your case this isn't need to.
>>>>>>>
>>>>>>> Second is that both text and timestamp have actually changed - take a
>>>>>>> look on them one more time (;
>>>>>>>
>>>>>>>  I see "hello 123" in both the text fields.  Not sure what you see
>>>>>> as the
>>>>>> change.
>>>>>>
>>>>>>  Your update function changes only serverISO8601time and serverTime
>>>>> fields, not any others.
>>>>>
>>>>> --
>>>>> ,,,^..^,,,
>>>>>
>>>>>
>>>>
>

Re: simultaneously run update handler and update doc content

Posted by Vivek Pathak <vp...@orgmeta.com>.
I would like to do both update and run update handler - without having a 
window of time between the client update and sever side update handler.

What is wrong with this ?


On 11/20/2013 03:36 AM, Stanley Iriele wrote:
> Wait...I believe you''re fundamentally have the idea of the update handler
> wrong
>
> "curl -s -X PUT 'http://localhost:5984/i7rmdb/_design/compnbd/_update/
> timestamp/testid' -d @/tmp/2.json"
>
> you don't need to pass in that json file
>
> an update handler loads the latest version of that doc from the database
> using the id you pass in the url...in this case it is "testid"
> then couchdb places , a convenient version, of the http request you sent in
> in the second parameter
>
> your update function should look more like this:
>
> function(doc, req) {
>      if( doc.type == "profile" ) {
>
>          var d = new Date();
>          doc.serverISO8601time = d.toISOString();
>          doc.serverTime = d.getTime() / 1000 ;
>          log( toJSON(doc) ) ;
>          return [doc , "OK"] ;
>      }
> }
>
> and your curl command should look like this
> "curl -s -X PUT 'http://localhost:5984/i7rmdb/_design/compnbd/_update/
> timestamp/testid'
>
> side note....pipe it into python -mjson.tool for nice json formatting
>
>
>
>
>
> On Wed, Nov 20, 2013 at 12:16 AM, Vivek Pathak <vp...@orgmeta.com> wrote:
>
>> Ok - got it
>>
>> I should have used
>>
>>     var newdoc = JSON.parse(req.body) ;
>>
>> I guess returning string as the first arg was somehow silently ignored.
>> Also the permissiveness of javascript allowed the rest of the code to run
>> without exception
>>
>> js> newdoc = "hello world" ;
>> "hello world"
>> js> var d = new Date();
>> js> newdoc.serverISO8601time = d.toISOString();
>> "2013-11-20T08:14:55.731Z"
>> js> newdoc.serverTime = d.getTime() / 1000 ;
>> 1384935295.731
>> js> newdoc
>> "hello world"
>>
>>
>> Thanks
>>
>>
>>
>> On 11/20/2013 03:10 AM, Vivek Pathak wrote:
>>
>>> Ok - so we are taking about the first problem only.
>>>
>>> If I try this, then I get not update to the data at all:
>>>
>>> function(doc, req) {
>>>      if( doc.type == "profile" ) {
>>>          var newdoc = req.body ;
>>>          var d = new Date();
>>>          newdoc.serverISO8601time = d.toISOString();
>>>          newdoc.serverTime = d.getTime() / 1000 ;
>>>          log( toJSON(newdoc) ) ;
>>>          return [newdoc , "OK"] ;
>>>      }
>>> }
>>>
>>> What is wrong here?
>>>
>>>
>>>
>>>
>>> On 11/20/2013 03:04 AM, Alexander Shorin wrote:
>>>
>>>> On Wed, Nov 20, 2013 at 12:01 PM, Vivek Pathak <vp...@orgmeta.com>
>>>> wrote:
>>>>
>>>>> On 11/20/2013 02:48 AM, Alexander Shorin wrote:
>>>>>
>>>>>> First problem is that your update function works with `doc` argument -
>>>>>> that is the stored within database document -, not that you had sent
>>>>>> with PUT request. To process the request payload you need to work with
>>>>>> req.body data, but in your case this isn't need to.
>>>>>>
>>>>>> Second is that both text and timestamp have actually changed - take a
>>>>>> look on them one more time (;
>>>>>>
>>>>> I see "hello 123" in both the text fields.  Not sure what you see as the
>>>>> change.
>>>>>
>>>> Your update function changes only serverISO8601time and serverTime
>>>> fields, not any others.
>>>>
>>>> --
>>>> ,,,^..^,,,
>>>>
>>>


Re: simultaneously run update handler and update doc content

Posted by Stanley Iriele <si...@gmail.com>.
Wait...I believe you''re fundamentally have the idea of the update handler
wrong

"curl -s -X PUT 'http://localhost:5984/i7rmdb/_design/compnbd/_update/
timestamp/testid' -d @/tmp/2.json"

you don't need to pass in that json file

an update handler loads the latest version of that doc from the database
using the id you pass in the url...in this case it is "testid"
then couchdb places , a convenient version, of the http request you sent in
in the second parameter

your update function should look more like this:

function(doc, req) {
    if( doc.type == "profile" ) {

        var d = new Date();
        doc.serverISO8601time = d.toISOString();
        doc.serverTime = d.getTime() / 1000 ;
        log( toJSON(doc) ) ;
        return [doc , "OK"] ;
    }
}

and your curl command should look like this
"curl -s -X PUT 'http://localhost:5984/i7rmdb/_design/compnbd/_update/
timestamp/testid'

side note....pipe it into python -mjson.tool for nice json formatting





On Wed, Nov 20, 2013 at 12:16 AM, Vivek Pathak <vp...@orgmeta.com> wrote:

> Ok - got it
>
> I should have used
>
>    var newdoc = JSON.parse(req.body) ;
>
> I guess returning string as the first arg was somehow silently ignored.
> Also the permissiveness of javascript allowed the rest of the code to run
> without exception
>
> js> newdoc = "hello world" ;
> "hello world"
> js> var d = new Date();
> js> newdoc.serverISO8601time = d.toISOString();
> "2013-11-20T08:14:55.731Z"
> js> newdoc.serverTime = d.getTime() / 1000 ;
> 1384935295.731
> js> newdoc
> "hello world"
>
>
> Thanks
>
>
>
> On 11/20/2013 03:10 AM, Vivek Pathak wrote:
>
>> Ok - so we are taking about the first problem only.
>>
>> If I try this, then I get not update to the data at all:
>>
>> function(doc, req) {
>>     if( doc.type == "profile" ) {
>>         var newdoc = req.body ;
>>         var d = new Date();
>>         newdoc.serverISO8601time = d.toISOString();
>>         newdoc.serverTime = d.getTime() / 1000 ;
>>         log( toJSON(newdoc) ) ;
>>         return [newdoc , "OK"] ;
>>     }
>> }
>>
>> What is wrong here?
>>
>>
>>
>>
>> On 11/20/2013 03:04 AM, Alexander Shorin wrote:
>>
>>> On Wed, Nov 20, 2013 at 12:01 PM, Vivek Pathak <vp...@orgmeta.com>
>>> wrote:
>>>
>>>> On 11/20/2013 02:48 AM, Alexander Shorin wrote:
>>>>
>>>>> First problem is that your update function works with `doc` argument -
>>>>> that is the stored within database document -, not that you had sent
>>>>> with PUT request. To process the request payload you need to work with
>>>>> req.body data, but in your case this isn't need to.
>>>>>
>>>>> Second is that both text and timestamp have actually changed - take a
>>>>> look on them one more time (;
>>>>>
>>>>
>>>> I see "hello 123" in both the text fields.  Not sure what you see as the
>>>> change.
>>>>
>>> Your update function changes only serverISO8601time and serverTime
>>> fields, not any others.
>>>
>>> --
>>> ,,,^..^,,,
>>>
>>
>>
>

Re: simultaneously run update handler and update doc content

Posted by Vivek Pathak <vp...@orgmeta.com>.
Ok - got it

I should have used

    var newdoc = JSON.parse(req.body) ;

I guess returning string as the first arg was somehow silently 
ignored.   Also the permissiveness of javascript allowed the rest of the 
code to run without exception

js> newdoc = "hello world" ;
"hello world"
js> var d = new Date();
js> newdoc.serverISO8601time = d.toISOString();
"2013-11-20T08:14:55.731Z"
js> newdoc.serverTime = d.getTime() / 1000 ;
1384935295.731
js> newdoc
"hello world"


Thanks


On 11/20/2013 03:10 AM, Vivek Pathak wrote:
> Ok - so we are taking about the first problem only.
>
> If I try this, then I get not update to the data at all:
>
> function(doc, req) {
>     if( doc.type == "profile" ) {
>         var newdoc = req.body ;
>         var d = new Date();
>         newdoc.serverISO8601time = d.toISOString();
>         newdoc.serverTime = d.getTime() / 1000 ;
>         log( toJSON(newdoc) ) ;
>         return [newdoc , "OK"] ;
>     }
> }
>
> What is wrong here?
>
>
>
>
> On 11/20/2013 03:04 AM, Alexander Shorin wrote:
>> On Wed, Nov 20, 2013 at 12:01 PM, Vivek Pathak <vp...@orgmeta.com> 
>> wrote:
>>> On 11/20/2013 02:48 AM, Alexander Shorin wrote:
>>>> First problem is that your update function works with `doc` argument -
>>>> that is the stored within database document -, not that you had sent
>>>> with PUT request. To process the request payload you need to work with
>>>> req.body data, but in your case this isn't need to.
>>>>
>>>> Second is that both text and timestamp have actually changed - take a
>>>> look on them one more time (;
>>>
>>> I see "hello 123" in both the text fields.  Not sure what you see as 
>>> the
>>> change.
>> Your update function changes only serverISO8601time and serverTime
>> fields, not any others.
>>
>> -- 
>> ,,,^..^,,,
>


Re: simultaneously run update handler and update doc content

Posted by Vivek Pathak <vp...@orgmeta.com>.
Ok - so we are taking about the first problem only.

If I try this, then I get not update to the data at all:

function(doc, req) {
     if( doc.type == "profile" ) {
         var newdoc = req.body ;
         var d = new Date();
         newdoc.serverISO8601time = d.toISOString();
         newdoc.serverTime = d.getTime() / 1000 ;
         log( toJSON(newdoc) ) ;
         return [newdoc , "OK"] ;
     }
}

What is wrong here?




On 11/20/2013 03:04 AM, Alexander Shorin wrote:
> On Wed, Nov 20, 2013 at 12:01 PM, Vivek Pathak <vp...@orgmeta.com> wrote:
>> On 11/20/2013 02:48 AM, Alexander Shorin wrote:
>>> First problem is that your update function works with `doc` argument -
>>> that is the stored within database document -, not that you had sent
>>> with PUT request. To process the request payload you need to work with
>>> req.body data, but in your case this isn't need to.
>>>
>>> Second is that both text and timestamp have actually changed - take a
>>> look on them one more time (;
>>
>> I see "hello 123" in both the text fields.  Not sure what you see as the
>> change.
> Your update function changes only serverISO8601time and serverTime
> fields, not any others.
>
> --
> ,,,^..^,,,


Re: simultaneously run update handler and update doc content

Posted by Alexander Shorin <kx...@gmail.com>.
On Wed, Nov 20, 2013 at 12:01 PM, Vivek Pathak <vp...@orgmeta.com> wrote:
> On 11/20/2013 02:48 AM, Alexander Shorin wrote:
>>
>> First problem is that your update function works with `doc` argument -
>> that is the stored within database document -, not that you had sent
>> with PUT request. To process the request payload you need to work with
>> req.body data, but in your case this isn't need to.
>>
>> Second is that both text and timestamp have actually changed - take a
>> look on them one more time (;
>
>
> I see "hello 123" in both the text fields.  Not sure what you see as the
> change.

Your update function changes only serverISO8601time and serverTime
fields, not any others.

--
,,,^..^,,,

Re: simultaneously run update handler and update doc content

Posted by Vivek Pathak <vp...@orgmeta.com>.
On 11/20/2013 02:48 AM, Alexander Shorin wrote:
> First problem is that your update function works with `doc` argument -
> that is the stored within database document -, not that you had sent
> with PUT request. To process the request payload you need to work with
> req.body data, but in your case this isn't need to.
>
> Second is that both text and timestamp have actually changed - take a
> look on them one more time (;

I see "hello 123" in both the text fields.  Not sure what you see as the 
change.

>
> Before:
>
> {"_id":"testid","_rev":"5-71ee77bc43fc698a935abc60dc33a82c","type":"profile","text":"hello
> 123",
> "serverISO8601time":"2013-11-20T07:29:35.497Z","serverTime":1384932575.496999979}
>
> After:
> {"_id":"testid","_rev":"6-6726469319fc80710ef15d0df6dd6320","type":"profile","text":"hello
> 123",
> "serverISO8601time":"2013-11-20T07:31:58.351Z","serverTime":1384932718.3510000706}
> --
> ,,,^..^,,,
>
>
> On Wed, Nov 20, 2013 at 11:40 AM, Vivek Pathak <vp...@orgmeta.com> wrote:
>> I have an update handler.
>>
>> |{
>>     "|_id|":|"_design/compnbd"|,
>>     "|_rev|":|"36-3321da9588a5a7b3fef8789bdfefe18d"|,
>>     "|updates|":|{
>>         "|timestamp|":|"function(doc, req) {\n    if( doc.type == \"profile\"
>> ) { \n        var d = new Date();\n        doc.serverISO8601time =
>> d.toISOString(); \n        doc.serverTime = d.getTime() / 1000 ;\n    }\n
>> return [doc , ''] ;\n} \n"|
>>     }|,
>>     "|language|":|"javascript"|,
>>     "|views|":|{
>>        // removed ...
>>     }
>> }
>> ||
>>
>>
>> The function is copied here for convenience:
>> function(doc, req) {
>>      if( doc.type == "profile" ) {
>>          var d = new Date();
>>          doc.serverISO8601time = d.toISOString();
>>          doc.serverTime = d.getTime() / 1000 ;
>>      }
>>      return [doc , ''] ;
>> }
>>
>>
>> I run the following commands:
>>
>>     curl -s 'http://localhost:5984/i7rmdb/testid' > /tmp/2.json
>>
>>     cat /tmp/2.json
>> {"_id":"testid","_rev":"5-71ee77bc43fc698a935abc60dc33a82c","type":"profile","text":"hello
>> 123","serverISO8601time":"2013-11-20T07:29:35.497Z","serverTime":1384932575.496999979}
>>
>> Now I edit the document.
>>
>>     cat /tmp/2.json
>> {"_id":"testid","_rev":"5-71ee77bc43fc698a935abc60dc33a82c","type":"profile","text":"hello
>> 12345 !!!! text changed ....
>> ","serverISO8601time":"2013-11-20T07:29:35.497Z","serverTime":1384932575.496999979}
>>
>> And put it using the update handler.
>>
>>     curl -s -X PUT
>> 'http://localhost:5984/i7rmdb/_design/compnbd/_update/timestamp/testid' -d
>> @/tmp/2.json
>>
>> Notice the text did not change, just the time stamp changed.
>>
>>     curl -s 'http://localhost:5984/i7rmdb/testid'
>> {"_id":"testid","_rev":"6-6726469319fc80710ef15d0df6dd6320","type":"profile","text":"hello
>> 123","serverISO8601time":"2013-11-20T07:31:58.351Z","serverTime":1384932718.3510000706}
>>
>>
>> I was expecting both text and timestamp to change.  Is that correct?
>>
>> Thanks
>>
>>
>>
>> On 11/20/2013 01:38 AM, Stanley Iriele wrote:
>>> Are you asking how to update a document with an update handler?
>>> Like...what
>>> the  pattern looks like?
>>> On Nov 19, 2013 10:31 PM, "Alexander Shorin" <kx...@gmail.com> wrote:
>>>
>>>> The update handlers does document update already with single http
>>>> request. To update document content or not is the question of function
>>>> implementation.
>>>> --
>>>> ,,,^..^,,,
>>>>
>>>>
>>>> On Wed, Nov 20, 2013 at 10:26 AM, Vivek Pathak <vp...@orgmeta.com>
>>>> wrote:
>>>>> Hi
>>>>>
>>>>> I have couchdb 1.2.1 - and wanted to update document content and also
>>>> run a
>>>>> update handler using only one http call.
>>>>>
>>>>> Is it possible to do this?
>>>>>
>>>>> Thanks
>>>>> Vivek
>>>>>


Re: simultaneously run update handler and update doc content

Posted by Alexander Shorin <kx...@gmail.com>.
First problem is that your update function works with `doc` argument -
that is the stored within database document -, not that you had sent
with PUT request. To process the request payload you need to work with
req.body data, but in your case this isn't need to.

Second is that both text and timestamp have actually changed - take a
look on them one more time (;

Before:

{"_id":"testid","_rev":"5-71ee77bc43fc698a935abc60dc33a82c","type":"profile","text":"hello
123",
"serverISO8601time":"2013-11-20T07:29:35.497Z","serverTime":1384932575.496999979}

After:
{"_id":"testid","_rev":"6-6726469319fc80710ef15d0df6dd6320","type":"profile","text":"hello
123",
"serverISO8601time":"2013-11-20T07:31:58.351Z","serverTime":1384932718.3510000706}
--
,,,^..^,,,


On Wed, Nov 20, 2013 at 11:40 AM, Vivek Pathak <vp...@orgmeta.com> wrote:
>
> I have an update handler.
>
> |{
>    "|_id|":|"_design/compnbd"|,
>    "|_rev|":|"36-3321da9588a5a7b3fef8789bdfefe18d"|,
>    "|updates|":|{
>        "|timestamp|":|"function(doc, req) {\n    if( doc.type == \"profile\"
> ) { \n        var d = new Date();\n        doc.serverISO8601time =
> d.toISOString(); \n        doc.serverTime = d.getTime() / 1000 ;\n    }\n
> return [doc , ''] ;\n} \n"|
>    }|,
>    "|language|":|"javascript"|,
>    "|views|":|{
>       // removed ...
>    }
> }
> ||
>
>
> The function is copied here for convenience:
> function(doc, req) {
>     if( doc.type == "profile" ) {
>         var d = new Date();
>         doc.serverISO8601time = d.toISOString();
>         doc.serverTime = d.getTime() / 1000 ;
>     }
>     return [doc , ''] ;
> }
>
>
> I run the following commands:
>
>    curl -s 'http://localhost:5984/i7rmdb/testid' > /tmp/2.json
>
>    cat /tmp/2.json
> {"_id":"testid","_rev":"5-71ee77bc43fc698a935abc60dc33a82c","type":"profile","text":"hello
> 123","serverISO8601time":"2013-11-20T07:29:35.497Z","serverTime":1384932575.496999979}
>
> Now I edit the document.
>
>    cat /tmp/2.json
> {"_id":"testid","_rev":"5-71ee77bc43fc698a935abc60dc33a82c","type":"profile","text":"hello
> 12345 !!!! text changed ....
> ","serverISO8601time":"2013-11-20T07:29:35.497Z","serverTime":1384932575.496999979}
>
> And put it using the update handler.
>
>    curl -s -X PUT
> 'http://localhost:5984/i7rmdb/_design/compnbd/_update/timestamp/testid' -d
> @/tmp/2.json
>
> Notice the text did not change, just the time stamp changed.
>
>    curl -s 'http://localhost:5984/i7rmdb/testid'
> {"_id":"testid","_rev":"6-6726469319fc80710ef15d0df6dd6320","type":"profile","text":"hello
> 123","serverISO8601time":"2013-11-20T07:31:58.351Z","serverTime":1384932718.3510000706}
>
>
> I was expecting both text and timestamp to change.  Is that correct?
>
> Thanks
>
>
>
> On 11/20/2013 01:38 AM, Stanley Iriele wrote:
>>
>> Are you asking how to update a document with an update handler?
>> Like...what
>> the  pattern looks like?
>> On Nov 19, 2013 10:31 PM, "Alexander Shorin" <kx...@gmail.com> wrote:
>>
>>> The update handlers does document update already with single http
>>> request. To update document content or not is the question of function
>>> implementation.
>>> --
>>> ,,,^..^,,,
>>>
>>>
>>> On Wed, Nov 20, 2013 at 10:26 AM, Vivek Pathak <vp...@orgmeta.com>
>>> wrote:
>>>>
>>>> Hi
>>>>
>>>> I have couchdb 1.2.1 - and wanted to update document content and also
>>>
>>> run a
>>>>
>>>> update handler using only one http call.
>>>>
>>>> Is it possible to do this?
>>>>
>>>> Thanks
>>>> Vivek
>>>>
>

Re: simultaneously run update handler and update doc content

Posted by Vivek Pathak <vp...@orgmeta.com>.
I have an update handler.

|{
    "|_id|":|"_design/compnbd"|,
    "|_rev|":|"36-3321da9588a5a7b3fef8789bdfefe18d"|,
    "|updates|":|{
        "|timestamp|":|"function(doc, req) {\n    if( doc.type == \"profile\" ) { \n        var d = new Date();\n        doc.serverISO8601time = d.toISOString(); \n        doc.serverTime = d.getTime() / 1000 ;\n    }\n    return [doc , ''] ;\n} \n"|
    }|,
    "|language|":|"javascript"|,
    "|views|":|{
       // removed ...
    }
}
||


The function is copied here for convenience:
function(doc, req) {
     if( doc.type == "profile" ) {
         var d = new Date();
         doc.serverISO8601time = d.toISOString();
         doc.serverTime = d.getTime() / 1000 ;
     }
     return [doc , ''] ;
}


I run the following commands:

    curl -s 'http://localhost:5984/i7rmdb/testid' > /tmp/2.json

    cat /tmp/2.json
{"_id":"testid","_rev":"5-71ee77bc43fc698a935abc60dc33a82c","type":"profile","text":"hello 
123","serverISO8601time":"2013-11-20T07:29:35.497Z","serverTime":1384932575.496999979}

Now I edit the document.

    cat /tmp/2.json
{"_id":"testid","_rev":"5-71ee77bc43fc698a935abc60dc33a82c","type":"profile","text":"hello 
12345 !!!! text changed .... 
","serverISO8601time":"2013-11-20T07:29:35.497Z","serverTime":1384932575.496999979}

And put it using the update handler.

    curl -s -X PUT 
'http://localhost:5984/i7rmdb/_design/compnbd/_update/timestamp/testid' 
-d @/tmp/2.json

Notice the text did not change, just the time stamp changed.

    curl -s 'http://localhost:5984/i7rmdb/testid'
{"_id":"testid","_rev":"6-6726469319fc80710ef15d0df6dd6320","type":"profile","text":"hello 
123","serverISO8601time":"2013-11-20T07:31:58.351Z","serverTime":1384932718.3510000706}


I was expecting both text and timestamp to change.  Is that correct?

Thanks


On 11/20/2013 01:38 AM, Stanley Iriele wrote:
> Are you asking how to update a document with an update handler? Like...what
> the  pattern looks like?
> On Nov 19, 2013 10:31 PM, "Alexander Shorin" <kx...@gmail.com> wrote:
>
>> The update handlers does document update already with single http
>> request. To update document content or not is the question of function
>> implementation.
>> --
>> ,,,^..^,,,
>>
>>
>> On Wed, Nov 20, 2013 at 10:26 AM, Vivek Pathak <vp...@orgmeta.com>
>> wrote:
>>> Hi
>>>
>>> I have couchdb 1.2.1 - and wanted to update document content and also
>> run a
>>> update handler using only one http call.
>>>
>>> Is it possible to do this?
>>>
>>> Thanks
>>> Vivek
>>>


Re: simultaneously run update handler and update doc content

Posted by Stanley Iriele <si...@gmail.com>.
Are you asking how to update a document with an update handler? Like...what
the  pattern looks like?
On Nov 19, 2013 10:31 PM, "Alexander Shorin" <kx...@gmail.com> wrote:

> The update handlers does document update already with single http
> request. To update document content or not is the question of function
> implementation.
> --
> ,,,^..^,,,
>
>
> On Wed, Nov 20, 2013 at 10:26 AM, Vivek Pathak <vp...@orgmeta.com>
> wrote:
> > Hi
> >
> > I have couchdb 1.2.1 - and wanted to update document content and also
> run a
> > update handler using only one http call.
> >
> > Is it possible to do this?
> >
> > Thanks
> > Vivek
> >
>

Re: simultaneously run update handler and update doc content

Posted by Alexander Shorin <kx...@gmail.com>.
The update handlers does document update already with single http
request. To update document content or not is the question of function
implementation.
--
,,,^..^,,,


On Wed, Nov 20, 2013 at 10:26 AM, Vivek Pathak <vp...@orgmeta.com> wrote:
> Hi
>
> I have couchdb 1.2.1 - and wanted to update document content and also run a
> update handler using only one http call.
>
> Is it possible to do this?
>
> Thanks
> Vivek
>