You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by he...@gmx.net on 2010/05/29 04:14:56 UTC

Best practise handling request and creating new document?

Hello,

I'm trying to find out, how I can create new documents from a web formula request using just CouchDB.

I'm not going to submit a <form> using client side JavaScript and for example an AJAX HTTP PUT request. I'm going to send the form directly to CouchDB and let it handle the data and create new documents.

I'm new to CouchDB and currently I'm just seeing a solution in using shows:
http://books.couchdb.org/relax/design-documents/shows

This show "book" prints out the title of an existing document if its given. Shows a "New book" form if no existing document is given and prints out the title if the form is submitted.

{
  "book": "function(doc, req) {
     if( req.query.title ){
         return req.query.title;
     }
     else if( doc )
         return doc.title;
     else
         return '<form><input name=\"title\"><input type=\"submit\"></form>';
     
     }"
}

Instead of printing out the title I'm going to validate it and create a new document with it. How can I do that? Whats the best practise, should I perhaps using something else than a show?

I did not found the JS API documentation, exists one?

Thanks in advance!
Philipp

-- 
GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01

Re: Best practise handling request and creating new document?

Posted by Mirsal Ennaime <mi...@gmail.com>.
On Mon, May 31, 2010 at 11:10 PM,  <he...@gmx.net> wrote:
> Hello,
> I'm a CouchDB beginner and not fixed on shows or other methods. I'm going to know if there is any way to manipulate data or insert data using a HTML form / request and just CouchDB without client side JS or some kind of application layer?

You may want to read this:
http://wiki.apache.org/couchdb/Document_Update_Handlers

Cheers,
-- 
Mirsal

Re: Best practise handling request and creating new document?

Posted by Mirsal Ennaime <mi...@gmail.com>.
On Mon, May 31, 2010 at 11:10 PM,  <he...@gmx.net> wrote:
> Hello,
> I'm a CouchDB beginner and not fixed on shows or other methods. I'm going to know if there is any way to manipulate data or insert data using a HTML form / request and just CouchDB without client side JS or some kind of application layer?

You may want to read this:
http://wiki.apache.org/couchdb/Document_Update_Handlers

Cheers,
-- 
Mirsal

Re: Best practise handling request and creating new document?

Posted by he...@gmx.net.
Hello,

http://books.couchdb.org/relax/design-documents/shows#Side-Effect%20Free says:

> We’ve mentioned that a key constraint of show functions is that they are side
> effect free. This means that you can’t use them to update documents, kick off
> background processes, or trigger any other function.

> CouchDB takes a different tack: because we’re a database, not an
> application server, we think it’s more important to enforce best practices (and
> ensure that developers don’t write functions that adversely effect the database
> server) than offer absolute flexibility. Once you’re used to working within these
> constraints, they start to make a lot of sense.

I'm a CouchDB beginner and not fixed on shows or other methods. I'm going to know if there is any way to manipulate data or insert data using a HTML form / request and just CouchDB without client side JS or some kind of application layer?

Thanks so far!
Philipp

-------- Original-Nachricht --------
> Datum: Mon, 31 May 2010 02:27:22 +0200
> Von: hever@gmx.net
> An: user@couchdb.apache.org
> Betreff: Re: Best practise handling request and creating new document?

> 
> -------- Original-Nachricht --------
> > Datum: Sun, 30 May 2010 16:04:59 -0700
> > Von: Jason Benesch <ja...@realestatetomato.com>
> > An: user@couchdb.apache.org
> > Betreff: Re: Best practise handling request and creating new document?
> 
> > >
> > > "Instead of printing out the title I'm going to validate it and create
> a
> > > new document with it. How can I do that? Whats the best practise,
> should
> > I
> > > perhaps using something else than a show?"
> > 
> > 
> > The validation should be done using javascript on your ddoc (design
> > document).  The validate_doc_update function handles the validation.
> > Look here:
> > http://books.couchdb.org/relax/design-documents/validation-functions
> 
> Good hint, thank you!
> 
> > 
> > "I'm not going to submit a <form> using client side JavaScript and for
> > > example an AJAX HTTP PUT request. I'm going to send the form directly
> to
> > > CouchDB and let it handle the data and create new documents"
> > 
> > 
> > Why not use client side javascript? You have to send a HTTP request to
> > couch
> > one way or another to submit the document. You can either use javascript
> > or
> > a server side language.
> > 
> 
> I'm not going to use client side JS because I'm going to write a public
> web application. In my opinion it's really no good practice to write the
> application in a way, that it's just usable with JS.
> 
> Furthermore I'm not going to use a classic server side scripting language
> because I'm really enthused by the concept of having data and program in
> one storage with the ability to replicate both at once. It's nice to be able
> to share data and program at once with other people or let them
> replicate/download and use the "web application" locally.
> 
> I'm new to CouchDB but as I see it, CouchDB can do much more than a normal
> data storage would do. The ability to render HTML Templates or to handle
> Requests is nice but at least only really useful if it gives you the power
> to manipulate data. If this is not possible this bunch of functionallity is
> nice but not really complete or usable. In such a case it would be perhaps
> better to use another application layer and drop those things from CouchDB
> thinking of performance, security, complexity and so on...
> 
> http://localhost:5984/_utils/script/test/update_documents.js gives some
> ideas and the functionallity exists but as I'm new I can't really go on with
> this:
> > var db = new CouchDB("test_suite_db", {"X-Couch-Full-Commit":"false"});
> > db.deleteDb();
> > db.createDb();
> 
> > var doc = {"word":"plankton", "name":"Rusty"}
> > var resp = db.save(doc);
> 
> > xhr = CouchDB.request("PUT",
> "/test_suite_db/_design/update/_update/in-place/"+docid+'?field=title&value=test');
> 
> CouchDB seems not usable in this context...
> > new ReferenceError(\"CouchDB is not defined\", \"\", 4)
> 
> I belive there is some way doing it and I hope somebody will help me. 
> Thanks so far!
> 
> > 
> > On Fri, May 28, 2010 at 7:14 PM, <he...@gmx.net> wrote:
> > 
> > > Hello,
> > >
> > > I'm trying to find out, how I can create new documents from a web
> > formula
> > > request using just CouchDB.
> > >
> > > I'm not going to submit a <form> using client side JavaScript and for
> > > example an AJAX HTTP PUT request. I'm going to send the form directly
> to
> > > CouchDB and let it handle the data and create new documents.
> > >
> > > I'm new to CouchDB and currently I'm just seeing a solution in using
> > shows:
> > > http://books.couchdb.org/relax/design-documents/shows
> > >
> > > This show "book" prints out the title of an existing document if its
> > given.
> > > Shows a "New book" form if no existing document is given and prints
> out
> > the
> > > title if the form is submitted.
> > >
> > > {
> > >  "book": "function(doc, req) {
> > >     if( req.query.title ){
> > >         return req.query.title;
> > >     }
> > >     else if( doc )
> > >         return doc.title;
> > >     else
> > >         return '<form><input name=\"title\"><input
> > > type=\"submit\"></form>';
> > >
> > >     }"
> > > }
> > >
> > > Instead of printing out the title I'm going to validate it and create
> a
> > new
> > > document with it. How can I do that? Whats the best practise, should I
> > > perhaps using something else than a show?
> > >
> > > I did not found the JS API documentation, exists one?
> > >
> > > Thanks in advance!
> > > Philipp
> > >
> > > --
> > > GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
> > > Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01
> > >
> > 
> > 
> > 
> > -- 
> > Jason Benesch
> > 
> > We just launched www.TomatoUniversity.com - Join for free!
> > Technology Training for the Real Estate Industry.
> > 
> > Real Estate Tomato
> > Co-owner
> > www.realestatetomato.com
> > (619) 770-1950
> > jason@realestatetomato.com
> > 
> > ListingPress
> > Owner, Founder
> > www.listingpress.com
> > (619) 955-7465
> > jason@listingpress.com
> 
> -- 
> GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
> Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01

-- 
GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01

Re: Best practise handling request and creating new document?

Posted by he...@gmx.net.
Hello,

http://books.couchdb.org/relax/design-documents/shows#Side-Effect%20Free says:

> We’ve mentioned that a key constraint of show functions is that they are side
> effect free. This means that you can’t use them to update documents, kick off
> background processes, or trigger any other function.

> CouchDB takes a different tack: because we’re a database, not an
> application server, we think it’s more important to enforce best practices (and
> ensure that developers don’t write functions that adversely effect the database
> server) than offer absolute flexibility. Once you’re used to working within these
> constraints, they start to make a lot of sense.

I'm a CouchDB beginner and not fixed on shows or other methods. I'm going to know if there is any way to manipulate data or insert data using a HTML form / request and just CouchDB without client side JS or some kind of application layer?

Thanks so far!
Philipp

-------- Original-Nachricht --------
> Datum: Mon, 31 May 2010 02:27:22 +0200
> Von: hever@gmx.net
> An: user@couchdb.apache.org
> Betreff: Re: Best practise handling request and creating new document?

> 
> -------- Original-Nachricht --------
> > Datum: Sun, 30 May 2010 16:04:59 -0700
> > Von: Jason Benesch <ja...@realestatetomato.com>
> > An: user@couchdb.apache.org
> > Betreff: Re: Best practise handling request and creating new document?
> 
> > >
> > > "Instead of printing out the title I'm going to validate it and create
> a
> > > new document with it. How can I do that? Whats the best practise,
> should
> > I
> > > perhaps using something else than a show?"
> > 
> > 
> > The validation should be done using javascript on your ddoc (design
> > document).  The validate_doc_update function handles the validation.
> > Look here:
> > http://books.couchdb.org/relax/design-documents/validation-functions
> 
> Good hint, thank you!
> 
> > 
> > "I'm not going to submit a <form> using client side JavaScript and for
> > > example an AJAX HTTP PUT request. I'm going to send the form directly
> to
> > > CouchDB and let it handle the data and create new documents"
> > 
> > 
> > Why not use client side javascript? You have to send a HTTP request to
> > couch
> > one way or another to submit the document. You can either use javascript
> > or
> > a server side language.
> > 
> 
> I'm not going to use client side JS because I'm going to write a public
> web application. In my opinion it's really no good practice to write the
> application in a way, that it's just usable with JS.
> 
> Furthermore I'm not going to use a classic server side scripting language
> because I'm really enthused by the concept of having data and program in
> one storage with the ability to replicate both at once. It's nice to be able
> to share data and program at once with other people or let them
> replicate/download and use the "web application" locally.
> 
> I'm new to CouchDB but as I see it, CouchDB can do much more than a normal
> data storage would do. The ability to render HTML Templates or to handle
> Requests is nice but at least only really useful if it gives you the power
> to manipulate data. If this is not possible this bunch of functionallity is
> nice but not really complete or usable. In such a case it would be perhaps
> better to use another application layer and drop those things from CouchDB
> thinking of performance, security, complexity and so on...
> 
> http://localhost:5984/_utils/script/test/update_documents.js gives some
> ideas and the functionallity exists but as I'm new I can't really go on with
> this:
> > var db = new CouchDB("test_suite_db", {"X-Couch-Full-Commit":"false"});
> > db.deleteDb();
> > db.createDb();
> 
> > var doc = {"word":"plankton", "name":"Rusty"}
> > var resp = db.save(doc);
> 
> > xhr = CouchDB.request("PUT",
> "/test_suite_db/_design/update/_update/in-place/"+docid+'?field=title&value=test');
> 
> CouchDB seems not usable in this context...
> > new ReferenceError(\"CouchDB is not defined\", \"\", 4)
> 
> I belive there is some way doing it and I hope somebody will help me. 
> Thanks so far!
> 
> > 
> > On Fri, May 28, 2010 at 7:14 PM, <he...@gmx.net> wrote:
> > 
> > > Hello,
> > >
> > > I'm trying to find out, how I can create new documents from a web
> > formula
> > > request using just CouchDB.
> > >
> > > I'm not going to submit a <form> using client side JavaScript and for
> > > example an AJAX HTTP PUT request. I'm going to send the form directly
> to
> > > CouchDB and let it handle the data and create new documents.
> > >
> > > I'm new to CouchDB and currently I'm just seeing a solution in using
> > shows:
> > > http://books.couchdb.org/relax/design-documents/shows
> > >
> > > This show "book" prints out the title of an existing document if its
> > given.
> > > Shows a "New book" form if no existing document is given and prints
> out
> > the
> > > title if the form is submitted.
> > >
> > > {
> > >  "book": "function(doc, req) {
> > >     if( req.query.title ){
> > >         return req.query.title;
> > >     }
> > >     else if( doc )
> > >         return doc.title;
> > >     else
> > >         return '<form><input name=\"title\"><input
> > > type=\"submit\"></form>';
> > >
> > >     }"
> > > }
> > >
> > > Instead of printing out the title I'm going to validate it and create
> a
> > new
> > > document with it. How can I do that? Whats the best practise, should I
> > > perhaps using something else than a show?
> > >
> > > I did not found the JS API documentation, exists one?
> > >
> > > Thanks in advance!
> > > Philipp
> > >
> > > --
> > > GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
> > > Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01
> > >
> > 
> > 
> > 
> > -- 
> > Jason Benesch
> > 
> > We just launched www.TomatoUniversity.com - Join for free!
> > Technology Training for the Real Estate Industry.
> > 
> > Real Estate Tomato
> > Co-owner
> > www.realestatetomato.com
> > (619) 770-1950
> > jason@realestatetomato.com
> > 
> > ListingPress
> > Owner, Founder
> > www.listingpress.com
> > (619) 955-7465
> > jason@listingpress.com
> 
> -- 
> GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
> Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01

-- 
GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01

Re: Best practise handling request and creating new document?

Posted by he...@gmx.net.
-------- Original-Nachricht --------
> Datum: Sun, 30 May 2010 16:04:59 -0700
> Von: Jason Benesch <ja...@realestatetomato.com>
> An: user@couchdb.apache.org
> Betreff: Re: Best practise handling request and creating new document?

> >
> > "Instead of printing out the title I'm going to validate it and create a
> > new document with it. How can I do that? Whats the best practise, should
> I
> > perhaps using something else than a show?"
> 
> 
> The validation should be done using javascript on your ddoc (design
> document).  The validate_doc_update function handles the validation.
> Look here:
> http://books.couchdb.org/relax/design-documents/validation-functions

Good hint, thank you!

> 
> "I'm not going to submit a <form> using client side JavaScript and for
> > example an AJAX HTTP PUT request. I'm going to send the form directly to
> > CouchDB and let it handle the data and create new documents"
> 
> 
> Why not use client side javascript? You have to send a HTTP request to
> couch
> one way or another to submit the document. You can either use javascript
> or
> a server side language.
> 

I'm not going to use client side JS because I'm going to write a public web application. In my opinion it's really no good practice to write the application in a way, that it's just usable with JS.

Furthermore I'm not going to use a classic server side scripting language because I'm really enthused by the concept of having data and program in one storage with the ability to replicate both at once. It's nice to be able to share data and program at once with other people or let them replicate/download and use the "web application" locally.

I'm new to CouchDB but as I see it, CouchDB can do much more than a normal data storage would do. The ability to render HTML Templates or to handle Requests is nice but at least only really useful if it gives you the power to manipulate data. If this is not possible this bunch of functionallity is nice but not really complete or usable. In such a case it would be perhaps better to use another application layer and drop those things from CouchDB thinking of performance, security, complexity and so on...

http://localhost:5984/_utils/script/test/update_documents.js gives some ideas and the functionallity exists but as I'm new I can't really go on with this:
> var db = new CouchDB("test_suite_db", {"X-Couch-Full-Commit":"false"});
> db.deleteDb();
> db.createDb();

> var doc = {"word":"plankton", "name":"Rusty"}
> var resp = db.save(doc);

> xhr = CouchDB.request("PUT", "/test_suite_db/_design/update/_update/in-place/"+docid+'?field=title&value=test');

CouchDB seems not usable in this context...
> new ReferenceError(\"CouchDB is not defined\", \"\", 4)

I belive there is some way doing it and I hope somebody will help me. 
Thanks so far!

> 
> On Fri, May 28, 2010 at 7:14 PM, <he...@gmx.net> wrote:
> 
> > Hello,
> >
> > I'm trying to find out, how I can create new documents from a web
> formula
> > request using just CouchDB.
> >
> > I'm not going to submit a <form> using client side JavaScript and for
> > example an AJAX HTTP PUT request. I'm going to send the form directly to
> > CouchDB and let it handle the data and create new documents.
> >
> > I'm new to CouchDB and currently I'm just seeing a solution in using
> shows:
> > http://books.couchdb.org/relax/design-documents/shows
> >
> > This show "book" prints out the title of an existing document if its
> given.
> > Shows a "New book" form if no existing document is given and prints out
> the
> > title if the form is submitted.
> >
> > {
> >  "book": "function(doc, req) {
> >     if( req.query.title ){
> >         return req.query.title;
> >     }
> >     else if( doc )
> >         return doc.title;
> >     else
> >         return '<form><input name=\"title\"><input
> > type=\"submit\"></form>';
> >
> >     }"
> > }
> >
> > Instead of printing out the title I'm going to validate it and create a
> new
> > document with it. How can I do that? Whats the best practise, should I
> > perhaps using something else than a show?
> >
> > I did not found the JS API documentation, exists one?
> >
> > Thanks in advance!
> > Philipp
> >
> > --
> > GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
> > Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01
> >
> 
> 
> 
> -- 
> Jason Benesch
> 
> We just launched www.TomatoUniversity.com - Join for free!
> Technology Training for the Real Estate Industry.
> 
> Real Estate Tomato
> Co-owner
> www.realestatetomato.com
> (619) 770-1950
> jason@realestatetomato.com
> 
> ListingPress
> Owner, Founder
> www.listingpress.com
> (619) 955-7465
> jason@listingpress.com

-- 
GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01

Re: Best practise handling request and creating new document?

Posted by Jason Benesch <ja...@realestatetomato.com>.
>
> "Instead of printing out the title I'm going to validate it and create a
> new document with it. How can I do that? Whats the best practise, should I
> perhaps using something else than a show?"


The validation should be done using javascript on your ddoc (design
document).  The validate_doc_update function handles the validation.
Look here:
http://books.couchdb.org/relax/design-documents/validation-functions

"I'm not going to submit a <form> using client side JavaScript and for
> example an AJAX HTTP PUT request. I'm going to send the form directly to
> CouchDB and let it handle the data and create new documents"


Why not use client side javascript? You have to send a HTTP request to couch
one way or another to submit the document. You can either use javascript or
a server side language.


On Fri, May 28, 2010 at 7:14 PM, <he...@gmx.net> wrote:

> Hello,
>
> I'm trying to find out, how I can create new documents from a web formula
> request using just CouchDB.
>
> I'm not going to submit a <form> using client side JavaScript and for
> example an AJAX HTTP PUT request. I'm going to send the form directly to
> CouchDB and let it handle the data and create new documents.
>
> I'm new to CouchDB and currently I'm just seeing a solution in using shows:
> http://books.couchdb.org/relax/design-documents/shows
>
> This show "book" prints out the title of an existing document if its given.
> Shows a "New book" form if no existing document is given and prints out the
> title if the form is submitted.
>
> {
>  "book": "function(doc, req) {
>     if( req.query.title ){
>         return req.query.title;
>     }
>     else if( doc )
>         return doc.title;
>     else
>         return '<form><input name=\"title\"><input
> type=\"submit\"></form>';
>
>     }"
> }
>
> Instead of printing out the title I'm going to validate it and create a new
> document with it. How can I do that? Whats the best practise, should I
> perhaps using something else than a show?
>
> I did not found the JS API documentation, exists one?
>
> Thanks in advance!
> Philipp
>
> --
> GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
> Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01
>



-- 
Jason Benesch

We just launched www.TomatoUniversity.com - Join for free!
Technology Training for the Real Estate Industry.

Real Estate Tomato
Co-owner
www.realestatetomato.com
(619) 770-1950
jason@realestatetomato.com

ListingPress
Owner, Founder
www.listingpress.com
(619) 955-7465
jason@listingpress.com