incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Vivek Pathak <vpat...@orgmeta.com>
Subject Re: simultaneously run update handler and update doc content
Date Wed, 20 Nov 2013 09:11:00 GMT
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 <vpathak@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 <vpathak@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 <vpathak@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.
>>>>>>
>>>>>> --
>>>>>> ,,,^..^,,,
>>>>>>
>>>>>>


Mime
View raw message