Telerik blogs

By: Stephen Forte and Mehfuz Hossain

Modern applications demand more out of their backend databases. For example, today a small application can easily scale to a million users and require a flexible data model. NoSQL has been the solution developers have turned to when they need to address these Big Data requirements.

Couchbase Lite is a NoSQL self-contained embedded JSON document database that lets you build fast and responsive apps even when network is unavailable.  By using Couchbase Lite developers can quickly add offline support in their apps for greater user engagement and productivity.

Telerik has recently partnered with Couchbase on a .NET LINQ Provider and now also is proud to announce the Couchbase Lite verified plugin in our Cordova marketplace.

An easy way to get started with this plugin is by clicking the “Try Plugin in AppBuilder”  that creates a TodoLite demo app in your AppBuilder account.

Couchbase Lite is based on the same semantics of the Couchbase server and based on the RESTful API that let apps talk with device storage in a wide variety of ways.


The plugin is exposed via window.cbLite and in the deviceready event  you need to do the following to get the server initialized and capture the endpoint which is used for performing various CRUD operations later:


function onDeviceReady() {
cblite.getURL(function(err, url) {
    if (err){
      alert("err "+JSON.stringify(err))
    // Couchbase Lite initialized.
    // TODO: You app specific logic.

As I have mentioned already, it is possible to create, retrieve Couchbase documents via REST calls. However, there is a project in github called coax that can abstract the manual REST with simple operations. In the above example as I get the server url, I can wrap it with coax in the following way:

var db = coax([url, "todos"]);
db.get(function(err, res, body){
db.put(function(err, res, body){

This wraps a database object based on the server url which I have used to create the todos database once. Internally, it builds the request url for given parameters to process the REST call and provides you with simple async operations (like shown above) to perform.

In a similar way , I can create the views:

var design = "_design/todo-view"
db.put(design, {
    views : {
        lists : {
            map : function(doc) {
                if (doc.type == "list" && 
                      doc.created_at && doc.title) {
                    emit(doc.created_at, doc.title)
        tasks : {
            map : function(doc) {
                if (doc.type == "task" && 
                    doc.created_at && doc.title && doc.list_id) {
                    emit([doc.list_id, doc.created_at],
                            checked : doc.checked ? "checked" : "",
                            title : doc.title,
                            image : 
                (doc._attachments && doc._attachments["image.jpg"])
        profiles : {
            map : function(doc){
                if (doc.type == "profile" && doc.user_id && {

Here, we have three views for TodoLite app which are tasks, lists and profiles with custom mapreduce functions. Coax will help process the HTTP body from JSON parameter with Couchbase specific encodings and make a PUT request for the todo-view.

Views are similar to schema for documents you create; which sets Couchbase apart from others. Based on the views created, I can perform various operations like marking a task completed in the following way:

db.get(id, function(err, doc){
    doc.checked = !doc.checked
    doc.updated_at = new Date()
    // update
    db.put(id, doc, function(){})

So far I can create a database, views and do CRUD operations on documents. But Couchbase Lite is also mind blowing when you bring in the sync gateway into the equation. When configured for the same endpoint and account, data will sync between devices almost instantly.  The demo app already connects to a dedicated demo cloud server (see index.js of the Couchbase Sync Gateway demo app), but for other purposes you can run your own sync gateway in the cloud or host your own service. To do the latter, download the sync gateway , bring up the command prompt and run the 'sync_gateway' executable in the 'bin' folder. The executable requires a few configuration parameters. To quickly get started you can use the configuration file in demo repository like this:

./sync_gateway sync-gateway-config.json

You would also need to update the REMOTE_SYNC_URL variable in index.js of the demo app to point your server or hosting service where sync gateway is running to see this awesome feature in action.

In this post, I have outlined the easiest way to include Couchbase Lite support in your hybrid app. You have seen how to use coax to make requests to Couchbase Lite server and configure Sync Gateway to sync data between devices.

Download the Couchbase Lite Cordova Plugin today.

PS, it is recommended  that you check the REST API documentation to get an overview of various operations that Couchbase Lite supports:

In addition, check out coax from github which is an awesome utility to complement Couchbase Lite:

About the Author

Steve Forte

 sits on the board of several start-ups including Triton Works. Stephen is also the Microsoft Regional Director for the NY Metro region and speaks regularly at industry conferences around the world. He has written several books on application and database development including Programming SQL Server 2008 (MS Press).


Comments are disabled in preview mode.