

What the HTTP is CouchApp - vault_
http://wiki.couchapp.org/page/what-is-couchapp

======
risotto
CouchDB is really fun. The rules are very tight, but consistent, and it
quickly appears to be a shotgun to cause a lot of damage with. Especially with
good with Javascript on the client side. There is a native Javascript driver
and the protocol is RESTful (HTTP GET, PUT, DELETE).

Compare the the closest NoSQL alternative, MongoDB. I say close because they
are both document stores (fancy key/value) with indexing and map/reduce.

MongoDB is very lean and blazing fast. But there isn't anything close to an
HTTP layer in Mongo. It speaks in a binary JSON protocol. The format,
commands, and driver, while well done, are a whole new set of rules to learn.
Plus a Ruby middleware to talk HTTP.

My conclusions are from writing a simple app with both backends. Punchline:
After all this experimentation I realize I need a relational DB for my
project.

------
papaf
This is really cool, especially with companies now providing CouchDB hosting.

The article says that writes can be validated so only authorised users may
write. Does anybody know if its possible to limit read access to certain
users? If so, I may revive an abandoned project and try it as a CouchApp.

~~~
TomasSedovic
A jchrisa pointed out, you can't get more granular than per-database read
restriction using just CouchDB.

However, you can put the database behind a proxy and use lists and shows (that
get passed the user info) to restrict the read access.

~~~
limmeau
Great to see that feature... I only played with CouchDB in 0.7-days before
shows and lists, and found it too everything-is-public for my needs.

Let's suppose I want to build a centralized messaging application where users
send each other private messages (and let's say per-user couches aren't
available). Users may only see messages directed to them. Messages are
contained in documents, one document per message (sounds like a reasonable
granularity).

For the JavaScript front-end, I'd like to ask the server for a list of
messages for the current user, and later on, for the contents of one
particular message.

The list of messages for one user sounds like a simple map-reduce view. In
order to only show messages targeted to the current user, my guess is that I
should add a list function to the same design document to remove other users'
items from the view result. To fetch a message by ID, I'd install a show
function which either returns the original message as JSON text or an invalid
access message, depending on the current user. Is that how you'd use shows and
lists?

~~~
TomasSedovic
Yes that would work. You could go a bit further, though:

Set your view to emit keys this way:

    
    
      [user,message_id]
    

you can query the view using:

    
    
      msgview?startkey=[user]&endkey=[user,{}]
    

which will give you all the messages for the given user, or

    
    
      msgview?startkey=[user,message_id]&limit=1
    

for a specific message.

Then your list doesn't have to go through all the messages for all the users
-- just the ones that the view returned -- and only check that their user info
is correct (i.e. nobody's calling the list using an inapropriate view).

With this scheme you could have the same list + view return a specific message
as well.

Oh, and definitely go check CouchDB 1.0 -- it's changed quite a bit since 0.7.
You can get a hosted version to play with at <http://www.couch.io/get>.

~~~
limmeau
Thank you!

------
vosper
_In fact, the bad old days are still with us, as most applications still rely
on fragile custom code, running in an application server like Ruby on Rails,
Python's Django, or some kinda Java thing._

I'm pretty sure the fragility of the code is as much down to the person
writing it as it is to the platform. I don't think it's fair to say that
application servers are inherently fragile; run Python or Java apps on App
Engine and you're unlikely to ever have a genuine scaling problem...

BTW is that how we quote text here?

~~~
jchrisa
Last I checked it is still possible to send an email from inside a Rails
controller, or to update a bunch of database records in a tight loop. You
can't do these sorts of things at scale, which a lot of people are still
learning the hard way.

Part of an app server like Django et al being flexible is giving you the
ability to shoot yourself in the foot. It is much harder to screw something up
royally when you are working within the CouchApp constraints. That's what I
meant by fragile.

Google App Engine gets this mostly right by enforcing draconian timeout limits
and other constraints, so it is much harder to write an app that is gonna fall
over under a big wave of traffic, as the sandbox will complain right away.

CouchApp is even more picky. The programming model doesn't have operations
that could block on remote IO, so you can't even go down the bad path. Of
course, when you do need to send an email, you've got to rethink how to do it.
(Actually in a real app you'd do email from an asynchronous task as well.)

------
iamwil
If an app can be replicated to a user's machine, can the user then also
manipulate the couchapp's view and design documents using futon or otherwise,
even though validation functions are in place?

ie, would a user be able to alter the application themselves, and not just
their data?

~~~
cmurphycode
Couch's design documents are protected, even though they are replicated, as
you say. Unless I'm misunderstanding your question, your application will be
safe from modification, as it will be update-able only by the author.

~~~
jchrisa
Actually, whatever is on the user's machine (or in a Couch in the cloud
controlled by the user) is 100% under the control of the user. They are free
to alter the application, validations, etc, as they see fit.

However, when the user attempts to replicate changes to another server (maybe
the original source server, maybe another server not under their control) it
is the validation functions on the target server that control which writes are
allowed to proceed.

So if my <http://jchrisa.net> server only allows updates from me (for instance
for blog posts), you can still replicate my blog to your machine, and edit
posts there, but when you replicate the changes back to my server, they will
be rejected by my validation function.

~~~
cmurphycode
Thank you; that is what I meant to explain, but you were much more clear. I
guessed that wil was asking about the safety of his own application once
replicated by others, because I don't see the concern of having users
replicate their own copy, as long as the damage is contained.

~~~
iamwil
Right, I was asking about the safety of the application that I write.

So any app written on couchapp can be considered to be modified by the end
user if it's replicated to their machine. Then any application where you don't
want users fiddling around with your 'source' is out of the question.

That said, there's plenty of applications that you'd want to pass along with
the user's own data that you'd want the end users to fiddle with and improve.

With that in mind then, I'm guessing you can have validation functions that
allow you to selectively update the design documents that you want from
another person's changes, just as I can selectively replicate data from
another person's copy of the couchapp.

~~~
jchrisa
Validation funs give you a lot of control over the update logic. However, they
cannot be used to grant anonymous write access to application code. The design
document may only be updated by the database or server admin.

------
arturadib
I've been waiting for this for a long time. Case in point: Feedlooks.com.

I created the entire app logic in jQuery, which is amazing for rapid app
development by the way, and all that missing was an http data server.

Of course I had to build my own interface on the server side to mediate the
database transfer. It's really an unnecessary layer.

Glad someone had this idea.

~~~
jchrisa
That's a lot like my last web app before I started using CouchDB. Before I
made the switch I was storing JSON in text fields in Postgres. Views are much
better than the hoops I had to jump through to query that.

------
olalonde
What about user input validation? If I understand it correctly, all the
application code resides in the browser. This means that there is no server
side code to make sure that the data stored in the database is properly
sanitized. For example, how would you go about making sure an input doesn't
contain HTML code? If the verification is made on the client side, it is easy
to circumvent it.

~~~
3ds
If you read the page you will find a section on "Server Side Validations"

