Dart: Throwing Darts from CouchDB

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.
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.
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().
 // 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;
  }
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&gt; 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.
About these ads

About financecoding
I enjoy coding in dart.

2 Responses to Dart: Throwing Darts from CouchDB

  1. Great job! We need more articles on how to uses databases from Dart.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: