Blog Categories
- Blogging
- Computorials
- News
Services Spotlight
01/27 : Architectural Modeling, Rendering, and Animation We provide quality rendering services using a variety of skills and digital tools. Whether interior or exterior, we can model from scratch any new or existing design and texture it… more>
From Our Gallery
Hand In Hand - Pencil Sketch: Rendering of a father’s and child’s hands. Original pencil sketch created by Avery DC and…
more>
AS4L Street Scene: Architectural rendering of a street scene to be used in an upcoming You Tube commercial…
more>
Expert Witness: Static HTML website created for the Expert Witness, a medical/ legal consulting business. Site created…
more>
Welcome to Shane's Blog
CouchDB and ExtJS-4, A Winning “Web 2.0 App” Combination
A "Computorials" post
Series Theme: How to build an example client side MVC (Model, View, Controller) CRUD (Create, Read, Update, Delete) Web 2.0 application that uses CouchDB as the back-end database server and ExtJS-4 as the client side Javascript application framework.
Intended Audience: ExtJS and CouchDB users. By extension, anyone with experience using Object Oriented Programming languages to create client/server applications.
Series Introduction: I do not personally believe there is such a thing as the “one-size-fits-all” or “silver bullet” database server, server side framework, and/or client side framework. Fortunately there are many, many choices available these days that empower a web application developer to leverage the strengths and weaknesses of various development tools against the actual needs of the application under development. The majority of these are Open Source, or have an Open Source licensing scheme, so they are readily available to learn and explore for the motivated programmer. Gone are the days in my little universe where my only perceived choice was a traditional LAMP stack running in the confines of one dedicated server. Now it is much easier to solve big problems, like distributing heavy processing and storage loads horizontally over multiple computers in multiple locations. All you have to do is research the many available choices out there and pick the architecture that best fits. So which one will it be?
I am not an advocate of any one solution over another, but I think I found a winning combination that will serve as my “default” Web 2.0 application development solution. I believe building an application around these tools will satisfy the requirements of the majority of use cases I will run in to as a web developer. These tools will also allow me to scale my applications up and down freely and flexibly without giving up relevant features and performance. I have been evaluating and testing CouchDB ( http://www.couchbase.com/ ) since August of 2010 and ExtJS-4 ( http://www.sencha.com/products/extjs/ ) since before its pre-release early in 2011. I am nothing short of amazed at these two innovations in web application development, and I am grateful to their creators and supporters. After benefiting from the many efforts of dedicated contributors who are much smarter than I am, I decided I could contribute something back to these communities. So, I present a series of screen-cast tutorials where I share what I have learned in hopes of benefiting any who may travel the same path as me.
I am a newbie to Javascript programming, but I have some experience with Object Oriented Programming using Lazarus/FPC ( http://wiki.lazarus.freepascal.org/ ) and I understand the basic principles of OOPs. However, I view myself as a “dummy” compared to most folks in the industry. So I honestly believe the cliche, “If I can do it, anyone can”.
Why CouchDB?
CouchDB is an implementation of the modern NoSQL DBMS movement referred to as a “de-normalized” or “document” database server. A good summary of why CouchDB may be a good solution for your application can be found here: http://www.couchbase.com/why-nosql/nosql-database and here: http://www.couchbase.com/products-and-services/couchbase-single-server .
The subject is vast, so I don’t want to attempt to be exhaustive here. Some of the high points that attracted me to CouchDB are as follows:
- Schemaless data repository
- Append only architecture (MVCC)
- Distributed architecture (auto-sharding, distributed queries, etc.)
- Powerful and easy to use replication/ filtered replication
- Drop-dead simple to implement and manage
- Provides full ACID transactions
- Multi-platform (Linux, Windows, Mac)
- RESTful interface and API
- Built-in server-side Javascript interpreter (Spider-monkey), web server, authentication support, and SSL support - all work harmoniously together
- Built-in Geo-spatial indexing
- Data is stored natively as JSON (great for Object Oriented Programming)
- Scaling down (mobile devices) is as flexible as scaling up (elastic “cloud” computing)
- Future proof horizontal scaling options (BigCouch, Cloudant, Lounge, future versions of Couchbase/Membase)
- The continuous changes feed
To become familiar with CouchDB, I found these online resources helpful:
- Videos/Webcasts: http://www.youtube.com/results?search_query=couchdb+oreilly&aq=f
- Apache Project Home: http://couchdb.apache.org/
- Couchbase community and documentaion: http://www.couchbase.org/
- Couchbase commercial site: http://www.couchbase.com/
- API Docs: http://www.couchbase.org/documentation
- Apache CouchDB Wiki (older): http://wiki.apache.org/couchdb/
Why ExtJS-4 from Sencha?
ExtJS-4 is a robust client side Javascript framework designed to create rich graphical Web 2.0 applications. A good summary of why ExtJS-4 may be a good solution for your application can be found here: http://www.sencha.com/products/extjs/ . Some of the high points that attracted me to ExtJS-4 are as follows:
- Excellent consistency in rendering graphical components cross-platform and cross-browser
- Very well structured & extensible Object/Class hierarchy familiar to OOPs developers
- Client side MVC architecture and robust data package that supports associations
- Large selection of well designed, pre-built, and configurable components/widgets
- Comprehensive documentation, training, and support (both commercial and community)
- Common code base with Sencha Touch, a framework for mobile web apps
To become familiar with ExtJS-4, I found these online resources helpful:
- Videos/Webcasts: http://vimeo.com/sencha
- Example Applications: http://www.sencha.com/products/extjs/examples/
- Online Documentation: http://docs.sencha.com/ext-js/4-0/
- User Forums: http://www.sencha.com/forum/
- Stack Overflow website: http://stackoverflow.com/questions/tagged/extjs4
Series Overview: Unfortunately, at the time of this writing, ExtJS-4 does not come with “out-of-the-box” support for document databases like CouchDB. Their data package was designed more with relational databases in mind for the back-end. So how do you get ExtJS-4 to work with CouchDB in a clean and maintainable way? That is what this tutorial is all about.
I will show how to set up an example application using a software patch created by Peter Muller to provide support for de-normalized databases in ExtJS-4 and Sencha Touch. His Forum post is here: http://www.sencha.com/forum/showthread.php?127547-Sencha-Platform-denormalized-Data-patch . Empowered with this patch, ExtJS-4 users are able to create powerful client side MVC applications that use the built in RESTful interface of CouchDB on the server side. This combination puts developers on a fast track to creating complete client/server solutions. No further customization of ExtJS is required - standard “out-of-the-box” configuration and implementation code is all that is necessary. If the patch is something that will help you with your project, please add your voice to request the patch be integrated into an official future release of the data package. The thread is being watched by Ed Spencer, Senior Software Architect at Sencha, as he is curious to see user demand. Please post your requests and comments in that thread. Peter and I are hoping the Sencha development team will eventually integrate the patch into an official release of ExtJS-4 and take over support of it.
The example app I created for this tutorial is called “mvcCouch”. It is an extension of the “MVC Application Architecture” guide example provided by Sencha in their docs here: http://docs.sencha.com/ext-js/4-0/#/guide/application_architecture . I highly recommend completing the Getting Started Guide and MVC Architecture Guide example first, before proceeding with this tutorial. The mvcCouch GUI consists of two simple grid components (master/detail) that allow the end user to interact with data and perform CRUD operations on the CouchDB server. Document data is formatted with nested JSON in this example, so the tutorial will show how to handle one-to-many associations when setting up your models, proxies, and stores in ExtJS-4. The primary store used by mvcCouch is a collection of individual documents that contain all parent and nested JSON in one body. This primary store is configured to use a REST proxy to perform CRUD with CouchDB. A secondary store uses a memory proxy to perform CRUD with just the nested JSON “records” of each document. This secondary store is programmed to sync with the primary store, thus keeping child JSON properly nested with its parent JSON for each document instance. Thanks to Peter’s patch, syncing the primary store saves all parent and child records back to the CouchDB server as one complete JSON “document”.
A hosted version of mvcCouch can be viewed here: http://shaneavery.couchone.com/mvccouch/_design/mvcCouch/index.html . Please feel free to open up a firebug or webkit console and check out what is going on when you play with mvcCouch. This hosted version of mvcCouch is built with version 4.0.0 of ExtJS and has been minified with JSBuilder, therefore source code will be hard to read. You can download non-minified source code project files, along with with dependencies, here: http://averydc.com/guest/mvcCouch.zip . I recommend you download these and get familiar with them before proceeding with this tutorial.
On With the Tutorial…
I decided to make this tutorial into a series of screen-casts to keep things interesting and more informative. Along with each video, I include a textual summary and some links to resources used in the screen-cast. Hopefully this will assist you when you need to make your own apps. My development platform is Windows 7, and my browser is Firefox 5.0 with Firebug.
Screen-cast Table of Contents:
1. Getting Started with CouchDB
2. Getting Started with ExtJS-4
3. Exploring the App - mvcCouch
4. Building the App:
4.1. Set up environment, application directories, and files – the CouchApp way
4.2. Set up Index.html, App.js, and initial Controller logic
4.3. Set up Models and Stores
4.4. Set up Views, Controller logic, and implementation code
4.5. Create the CouchDB view function
4.6. “Pushing” the App to CouchDB with CouchApp, building a minified version with Sencha SDK Tools
Comments on This Post
16 Comment(s)
@ Peter,
Since Sencha Touch uses the same data package as ExtJS-4, the tutorials should translate well for CRUD operations using CouchDB with Sencha Touch. Peter Muller has a Touch version of his patch as well. The only changes I can think of off hand would be how you implement your views using Touch specific widgets.
I am a newbie to Sencha, returning to development after 10 years, I am trying to get into Sencha Touch. I am OK with the basics eg MVC. I think that CouchDb could be a great back-end platform. Does the example translate to Sencha Touch? What do I have to look out for?
Best Regards
Peter
@Joel,
Thanks for the appreciation, I am glad I could help. Unfortunately, I will not be going to SenchaCon in Austin.
stuff!!
Thanks
Shane,
This is fantastic work and exactly what I’ve been looking for, thanks so much for the effort.
Are you going to be at SenchaCon in Austin in a few weeks? They should be bringing you there to give a presentation on this work.
@ Olivier Pons,
REF’s are used as a new way of selecting components in a controller class. Since I have more than one grid on my single page app, I need to select the grid I want to operate on before I can manipulate it in code. Therefore, I needed to define the REF (or reference) property to it first, so I can easily reference it in code. My implementation is fairly simple, but you can use REFs in much more powerful ways.
REF’s are discussed briefly in the docs here:
http://docs.sencha.com/ext-js/4-0/#/api/Ext.app.Controller
Follow the links in the examples to dig deeper. I hope you find what you’re looking for.
Hi, in your mvcCouch.controller.Users file, what does this mean:
refs:[
{
ref:‘listgrid’,
selector:‘viewport > userlist’
},
{
ref:‘detailgrid’,
selector:‘viewport > userdetail’
}
],
</pre>
@ Peter:
Nice to hear from you! Thanks for your input. I’m glad I was able to contribute something. Let me know if I can assist with any future efforts. Thanks again for providing your patch, which turns out to be the hinge this particular door swings on.
I hadn’t even though about the patch being able to support direct to DDB apps without any middleware. This is really cool stuff.
I sure hope Sencha will take this seriously and implement something similar in an upcoming release. The patch itself isn’t even that big.
@Joe:
You are correct. It is not necessary to store ID’s in the details (or “members”) array. It was just an arbitrary example to show what is possible. You can always use the array index to get at individual JSON objects in the array. That is in fact how I am pulling off the pseudo “auto-increment” of the ID field when a new “user” is added.
One possible use of an incremental field like an ID might be to allow an end user to force a particular sequence when their array objects later become listed in a CouchDB view. As you probably know, arrays can be quite arbitrary in JSON, so objects in the array may not be sequenced they way you want them to be when doing a view query.
Well done! Thanks a lot!
Is it possible not to produce (and store) the “id” property of detail’s elements for the CouchDB document? This is just a redondant data of the indexed array and could be just calculated at load and dropped at update.
Update: My online (Iriscouch hosted) version of mvcCouch is working properly with Firefox, but still not working correctly with Chrome, Safari, and IE. The JSON view response is being aggressively cached in these browsers, so when updates are made to CouchDB, these browsers still show stale JSON from their cache, instead of querying the DB.
If you are using a browser other than Firefox, you will have to manually press your browser’s “refresh” button after certain operations (GET, POST, PUT, DELETE) in order to see updated JSON from CouchDB. At least for now, anyway.
I am trying to figure out how to solve this problem. Any suggestions would be appreciated. I will report back when I have a solution to share.
Yahoo!!....Looks like you published the final two videos. I’ll get through them tonight. Then I’m going to go back and study all of the classes and options involved in the ext mvc system with your app and my app.
I built a three grid blog app a couple of weeks ago. But it didn’t have the polish that your app does. I actually split a three grid view into three controllers. Probably makes more sense to do “one-composite-widget-view to one controller” like you have done.
One thing I’m going to add is success and failure messages in the call-back of the crud functions. Failures will probably be just a messagebox. Success will be a “record saved” fading text on the grid title-bar or something similar.
Thanks again for doing this.
Wow, thanks Old Roy. It was my pleasure to do it, since it is at least “something” I am able to contribute to these projects.
Regarding Rails and CouchDB, Couch is Architect-ed such that you can replace SS JavaScript with other server-side scripting languages. There are already several libraries available that serve as a wrapper to CouchDB functionality. There are at least 3 for Ruby on Rails:
https://github.com/couchrest/couchrest_model
https://github.com/peritor/simply_stored
https://github.com/langalex/couch_potato
Hope that helps…
Shane,
Thanks very much for this series. I’ve recently come back to extjs right at the point that 4.0 was released and gravitated immediately to the mvc pattern. In the past I’ve built some complex ext widgets but always felt it was a bit messy to keep track of everything. With mvc there is finally structure.
The only problem has been finding examples. But now there is a pretty solid example - your’s - and it should be a good jumpoing off point for more and more interest in mvc in extjs.
I’m a rails person. I had to take a step back and figure out what couchdb is all about. And while I’m not about to abandon rails…..couchDB looks like a something I will be spending more time with.
Anyway - thanks…..you did what the extjs guys probably should have done. Any help you need - I’ll certainly provide if I can. I would certainly like to re-do your series for rails/extjs/mvc at some point.
NOTICE: My online (Iriscouch hosted) version of mvcCouch is currently not working properly (sometimes “updating” and “deleting” documents does not seem to work). A link to mvcCouch is here:
http://shaneavery.couchone.com/mvccouch/_design/mvcCouch/index.html
The apparent reason for the problem is my CouchDB view is being aggressively cached somewhere between IrisCouch and some of you browser clients. Therefore, the view is not refreshing in your browser, and therefore is not showing the latest changes in CouchDB to mvcCouch when a GET request is made. I am working with IrisCouch, trying to figure out what to do about it. Here is a link to my support post:
http://getsatisfaction.com/iriscouch/topics/my_views_are_not_updating
I will keep you posted, and let you know when the problem is resolved.
Thanks for your patience.





