News, ideas and randomness

PhoneGap CouchDB Attachment Plugin

Posted: March 14th, 2011 | Author: Andrew Gleave | Filed under: couchdb, iphone, mobile | No Comments »

I’m working on a cool new project which makes use of a lot of exciting new tech: jQuery Mobile, PhoneGap, CouchDB (Couchbase) and Django, and I wanted to enable the iPhone and Android PhoneGap apps to push binary attachments directly to CouchDB, without having to proxy through Django.

All access to CouchDB is performed via its RESTful JSON¬†API which is awesome because it means you can to talk directly to the database using javascript! It’s also great because, in this case, there isn’t much going on in the middleware (Django) layer so implementing Python API endpoints to proxy to a standard database wouldn’t be worth it when I can get CouchDB to do the heavy lifting for free.

I had a look around and couldn’t find any existing plugins which could upload binary attachments to CouchDB (I could have used an XHR request to post the photo data, but I didn’t want to Base64-encode the photos as it can cause Out of Memory exceptions on devices with high-res cameras and increases the file size by ~1.4x), so I decided to write a quick Objective-C plugin. ¬†PhoneGap has a simple plugin system which enables native code plugins to be exposed to DOM which means I can call my upload method, pass it the photo’s file URI, and let the plugin handle uploading the content.

The plugin is up on GitHub. Feel free to fork and send pull requests.

Leave a Reply