Random posts about coding

Mostly blogging about dart.

Dart: Throwing Darts From CouchDB

| Comments

CouchDB provides an excellent NOSQL database with simple REST API. Continuing on with the odds and ends in Dart, I’d like to show how one could interface with CouchDB in Dart. Following the “Getting started with Python” I was able to stub out a simple wrapper implementation in no time. At some point a full wrapper should be done, I’ll possible look into it in the coming months.
CouchBase provides full standalone community build, best option if you don’t want to deal with building and deploying on your Mac. Pairing that up with XAMPP and you’ll be able to write the client side that communicates to the apache web server which then proxies to CouchDB. After installing XAMPP two files need to be edited to ease the local deployment.
[sourcecode] XAMPP/etc/http.conf - Add the ProxyPass configuration. XAMPP/etc/extra/httpd-userdir.conf - Add the user directory to point to the default dart folder location. [/sourcecode]
Httpconf
Fire up CouchDB and Apache, then open chrome http://127.0.0.1/couchdb and you should see the following.
Localhost
From here we can use XMLHttpRequest provided in the dart:html library to make requests to the Apache web server which proxy to CouchDB. One issue I ran into was DOMException INVALID_STATE_ERR when trying to set the headers. You must call open() before your able to call setRequestHeader().
[sourcecode lang=”java”] // Basic http methods getHttp(var uri) { XMLHttpRequest c = connect(); c.open(‘GET’, uri, false); c.setRequestHeader(‘Accept’, ‘application/json’); c.send(); return c.responseText; } postHttp(var uri, var body) { XMLHttpRequest c = connect(); c.open(‘POST’, uri, false); c.setRequestHeader(‘Content-type’, ‘application/json’); c.send(body); return c.responseText; } putHttp(var uri, [var body]) { XMLHttpRequest c = connect(); c.open(‘PUT’, uri, false); if (body is String && body.length > 0) { c.setRequestHeader(‘Content-type’, ‘application/json’); c.send(body); } else { c.send(); } return c.responseText; } deleteHttp(var uri) { XMLHttpRequest c = connect(); c.open(‘DELETE’, uri, false); c.send(); return c.responseText; } [/sourcecode]
Domexception
From there stubbing out a simple interface for dart with default implementation was easy.
Couchdb
The CouchDBWrapperImpl does wrapping for calling to the CouchDB using the default keyword helps to hide the implementation from the interface in a factory type way.
Couchdbimpl
Opening up chrome to http://127.0.0.1/<username> provides access into the users dart directory.
Fileview
Clicking on CouchDBExample.html should run the sample code.
Demo
The interfacing with CouchDB was simple and quick, a more elegant solution would be a full client implementation that lets you modify objects more indirectly.  Sample code on github.