During our annual team meeting last week, my colleague Jeremy Tammik, stumbled across an interesting cloud-oriented database technology called CouchDB. Basically it consists of a database that runs as a service and that can be interacted with using RESTful requests. Here are three things you should like about it: CouchDB is simple, it works great, it is free and open-source!
-
A more complete definition can be found on its dedicated website:
CouchDB is a database that completely embraces the web. Store your data with JSON documents. Access your documents with your web browser, via HTTP. Query, combine, and transform your documents with JavaScript. CouchDB works well with modern web and mobile apps. You can even serve web apps directly out of CouchDB. And you can distribute your data, or your apps, efficiently using CouchDB’s incremental replication. CouchDB supports master-master setups with automatic conflict detection.
After installing it on your Windows or Mac OSX machine, it is literally a matter of minutes before you start playing with it. The 15 minutes QuickStart guide will help you to get on track.
The first step will be to create a new database to start creating and accessing records in it:
1 .Browse to http://localhost:5984/_utils/
in order to open the admin console
2. Click "Create Database"
3. Enter "adndb" (database name supports lower case and digits only)
You can then create a new record by sending a REST request containing JSON formatted entry. Using a tool like Fiddler for example or the Firefox REST Client , I am sending the following request:
-
Header
Content-Type: application/json
-
URL
http://localhost:5984/adndb/postrecordid
-
Body
{ "Author" : "Phil", "Blog" : "Cloud and Mobile", "Post" : "CouchDB" }
-
As you can see in the screenshot of the admin console below, a new record gets created in my local database:
-
-
The next thing I wanted to do was obviously hosting my CouchDB on the cloud, so I could use it in a concrete cloud-based application. This is made very easy by IrisCouch which provide free hosting for CouchDB, at least when the amount of data you are transiting is low. All you need to do is signup, provide your custom CouchDB domain name and you can start accessing your cloud database like we did locally. The CouchDB technology also made db replication very straightforward, so you can all the setup locally, and move your data to the cloud once you are ready!
--
My CouchDB app
-
I was reusing the project layout from my previous post previous post and migrate that to rely on CouchDB, starting with the .Net console and clients. From the various SDKs you can find that help wrapping CouchDB requests in .Net, I chose one called DreamSeat.
-
In that previous post I was using the cool async/await feature of .Net 4.5 to perform my asynchronous non-blocking calls. Although support asynchronous programming, Dreamseat is designed for lower version of the .net framework and didn’t support the async by default. As an exercise, I wrote a couple of wrapper methods, as extension of Dreamseat classes, so I could use async/await in my own code.
-
Here is an example of what my extension method looks like:
-
public static class CouchClientExtensions
{
public static async Task<CouchDatabase> GetDatabaseAsync(
this CouchClient client,
string databaseName,
bool createIfNotExists)
{
return await Task<CouchDatabase>.Factory.StartNew(() =>
{
try
{
return client.GetDatabase(
databaseName,
createIfNotExists);
}
catch
{
return null;
}
});
}
}
-
This allows to write very compact and elegant asynchronous code, for example the snippet below will access the CouchDB database and refresh the list of all the models I have uploaded previously:
-
private async Task RefreshCloudModels()
{
_tvModels.Nodes.Clear();
_modelIdNodeMap.Clear();
TreeNode root = _tvModels.Nodes.Add(
"_cloudModelsKey", "Cloud Models", 0, 0);
try
{
if (_db == null)
_db = await _couchClient.GetDatabaseAsync(
CouchDbName, true);
var docs = await _db.GetAllDocumentsAsync();
foreach (var row in docs.Rows)
{
var doc = await _db.GetDocumentAsync<JDocument>(row.Id);
ModelInfo mi = new ModelInfo(doc);
if (!_modelIdNodeMap.ContainsKey(mi.ModelId))
{
TreeNode node = root.Nodes.Add("", mi.ModelId, 1, 1);
node.Tag = mi;
_modelIdNodeMap[mi.ModelId] = node;
}
}
root.Expand();
Cursor = Cursors.Default;
}
catch (Exception ex)
{
Cursor = Cursors.Default;
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
-
In the attached project, you will find a complete implementation of the .Net client and console allowing for upload and viewing of pictures.
-
Comments
You can follow this conversation by subscribing to the comment feed for this post.