You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Alux <al...@googlemail.com> on 2010/07/28 10:01:36 UTC

Create document with update handler (without sending an id)

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.

Re: Create document with update handler (without sending an id)

Posted by Alux <al...@googlemail.com>.
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 <ma...@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 <al...@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 <ma...@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 <al...@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.
> > > >
> > >
> >
>

Re: Create document with update handler (without sending an id)

Posted by Martin Higham <ma...@ocasta.co.uk>.
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 <al...@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 <ma...@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 <al...@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.
> > >
> >
>

Re: Create document with update handler (without sending an id)

Posted by Alux <al...@googlemail.com>.
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 <ma...@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 <al...@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.
> >
>

Re: Create document with update handler (without sending an id)

Posted by Martin Higham <ma...@ocasta.co.uk>.
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 <al...@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.
>