Thursday 2011-10-27

CouchDB makes writing webapps way faster by virtue of providing JSON to disk. That quick cheap persistence makes a great complement to sites that run almost pure javascript.

Most couchdb tutorials seem to skip from curl invocations to design docs. A good warm-up intermediate step is to use jQuery's getJSON() and the putJSON() below to make a basic CRUD webapp, so that less n00bs are sitting around reading stuff like curl -X PUT http://localhost:5984/db/_design/app/_update/accumulate/my_doc and thinking "*that's* their idea of RESTful"?

$.putJSON = function (url, data, callback) {
	$.ajax({
		'url': url,
		'type': 'put',
		'processData': false,
		'data': JSON.stringify(data),
		contentType: 'application/json',
		success: callback,
	});
};

// an exemplary lame usenet punchline obfuscator
var url = "http://mycouch.atmyhouse.com:5984/couchdb/document";
var doc = "";
$.getJSON(url, function(d) { doc = d } );
doc.article = rot13(doc.article);
$.putJSON(url, doc, function() { console.log("happiness") } );