Friday, January 15, 2010

Storing data online with CouchDB

I'm looking at using a remote CouchDB as a store for character data. With some sync code, characters should be transparently available on any device you're using.

CouchDB looks cool, but the documentation is horrible (at least for starting out using Javascript).

Something I didn't fully appreciate is that you can't call out to different a server from Javascript directly (this is "cross-site scripting", and there are workarounds). Essentially, it's assumed any connections will be performed on the server side and the client (browser) connects to a single server. The quick-and-dirty solution is to host the Player's Companion from my local CouchDB instance:
  1. Log in to Futon: http://localhost:5984/_utils/index.html

  2. Create a database (charmgr) to host the files

  3. Create a document (id: "charmgr")

  4. Upload all the files as attachments to the document

  5. Go to: http://localhost:5984/charmgr/charmgr/charmgr.html, et voila!


In the code, writing out the data is direct:

var db = new CouchDB(cookie_name.toLowerCase(), {"X-Couch-Full-Commit":"false"});
db.createDb();
db.save(data);


Thanks to Justin Walgran: http://mail-archives.apache.org/mod_mbox/couchdb-user/200812.mbox/%3Cf893e2810812262119k3163421ds372d4b7a2eac22c1@mail.gmail.com%3E

Useful: http://sitr.us/2009/06/30/database-queries-the-couchdb-way.html

No comments: