couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alux <alu...@googlemail.com>
Subject Re: Create document with update handler (without sending an id)
Date Wed, 28 Jul 2010 08:49:23 GMT
Ah, this is obviously some way of self documenting that I wasn't previously
aware of.

;-))

Thank you Martin & Greetings from Hamburg,

alux



On 28 July 2010 10:38, Martin Higham <martin@ocasta.co.uk> wrote:

> I was simply looking at the couchdb log (debug level) for some other
> information in the request object and saw "uuid".
>
> On 28 July 2010 09:29, Alux <aluxob@googlemail.com> wrote:
>
> > Ah, thats pretty!
> >
> > Many thanks Martin, that makes it feel much more couchy!
> >
> > How did you discover that? I'm very impressed!
> >
> > Thanks again, and nice day,
> >
> > alux
> >
> >
> > On 28 July 2010 10:21, Martin Higham <martin@ocasta.co.uk> wrote:
> >
> > > I had the same problem and was about to embark on a similar solution
> when
> > I
> > > discovered that the req object passed into the update handler contains
> a
> > > uuid for you to use. So all you need to do is
> > >
> > > function(doc, req) {
> > >  var newDoc = JSON.parse(req.body);
> > >  if (!doc){
> > >    newDoc._id = req.uuid;
> > > ...
> > >
> > >
> > > Martin
> > >
> > >
> > >
> > > On 28 July 2010 09:01, Alux <aluxob@googlemail.com> wrote:
> > >
> > > > Hello,
> > > >
> > > > after some funny result I now am able to create documents with an
> > update
> > > > handler. I want to use usual couchy ids, and didnt know how to get
> the
> > > (in
> > > > one step, that is), so I did created one in JavaScript as below. Is
> > this
> > > > sensible? I'm a newbee, so comments are very welcome.
> > > >
> > > > Kind regards, alux
> > > >
> > > > ------
> > > >
> > > > function(doc, req) {
> > > >            start({'headers': {'Content-Type': 'text/html'}});
> > > >            var maxStr = 'ffffffffff';
> > > >            var max = parseInt(maxStr, 16);
> > > >            var r1=Math.floor(Math.random()*max);
> > > >            var r2=Math.floor(Math.random()*max);
> > > >            var r3=Math.floor(Math.random()*max);
> > > >            var r4=Math.floor(Math.random()*max);
> > > >            var rstr = r1.toString(16) + r2.toString(16) +
> > r3.toString(16)
> > > +
> > > > r4.toString(16);
> > > >            var id = rstr.substr(0,32);
> > > >            doc = {_id: id};
> > > >            doc.type = 'note';
> > > >            var message = '<html><body><h3>Created new
document with
> > id='
> > > +
> > > > id + '</h3>' +
> > > >            '<a href=\'' +id+ '\'>Enter the new
> > > > document.</a></body></html>';
> > > >            return [doc, message];
> > > >        }
> > > >
> > > > Remarks:
> > > >
> > > > 1. This update handler shall be called from a browser with HTTP POST
> > from
> > > a
> > > > form, it returns HTML for the browser.
> > > > 2. The four steps of random generation are because Math.random
> > generates
> > > > not
> > > > very long numbers, and toString() removes leading zeros. So this
> should
> > > be
> > > > rather safe.
> > > >
> > >
> >
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message