I’ve been using the backbone couchdb connector by Jan and I’ve been impressed. The real showstopper is realtime updates using the changes feed from couchdb.
For complex geospatial apps, you can’t go past rails + postgis as a toolchain, but for simpler apps where you do all of the work on the front end and basically need a hash store, rails is overkill.
Instead of having to run
rake migrate everytime you want to add a column, you just write to the backbone model, call save and it’s serialized away into your document store.
It’s possible to use postgres in a schemaless way by either:
- Autogenerating columns as they are referred to
- Serializing all the columns into a blob
But it makes sense to investigate databases that are designed for the purpose. The two I have experience with are:
CouchDB also runs well without any middleware, your app talks directly to the database.
So say you have a list of
posts on the left side of your page, you bind your
render() (or redraw) function to the
Posts.add(new Post(content : "Hello world!"))
All the dom elements will be automatically created and the posts list redrawn.
You can also use the localStorage plugin to persist your models to the browser. The backbone-couchdb persists your models to the couchdb store, so whenever you call save, it posts your model to the server - and when you reload the page, it loads all your models from the server.
And because it’s couchdb, all you have to do is create the database record in futon (one click) and all the models are automatically saved away, the database doesn’t need to be aware of what you’re saving, it just saves.
The magic of realtime
The magic part of this is the
_changes feed from couchdb. A client can request the _changes feed by doing an ajax request. Couchdb will then block (ala long polling) until there are changes on the database, at which point it will send down json and close the connection. The client processes these changes (updating collections and models as needed) and then reopens the connection to the changes feed.
And because you are already re-rendering your UI whenever a collection changes, you can see in realtime what anyone else is doing on the same dataset that you are using.
I’m using couchone to host my couch databases. It’s a great service, the only downside is that you can’t set Access-Control-Allow-Origin headers, which would let you do cross-domain ajax calls (so you can host your app at github and store your database at couchone). I got around this by proxing through my linode box, but this kills performance. Hopefully the couchone guys will give you the option to change the headers (they could use varnish or similar).