CouchDB as Cordova/Phonegap database
Asked Answered
S

6

6

Target

I want to build a cross-platform mobile app with cordova/phonegap which require a database (client-side only). My target platform is mainly Android and iOS. I have chosen couchbase-lite as my storage.

Problem

But I could not find any good documentation for couchbase-lite in phonegap. Only I found some rest api and an application todo-lite in github and Play Store.

I could not understand if I don't have any server side implementation how could I get a url at which I can sent POST/GET/PUT/DELETE request.

Can anyone suggest me a way by which I can install, connect and run CRUD operation in couchbase-lite database locally in Android and iOS using cordova/phonegap.

Why Couchbase-lite (Not Important for all)

For those who will suggest me to choose another db, I am just sharing my findings...
From cordova 5.0 storage documentation there are several choices

  • LocalSorage: 5 MB limit is not sufficient for the application.
  • WebSQL: I'm afraid about the future of it since w3c dropped its planning.
  • IndexDB: Currently underdevelopment and not available for Android and iOS.
  • Plugin-Based Options: By motivating the evaluation of NoSQL. I have tried couchbase-lite. Which has plugin support for both Android and iOS platform.
Selves answered 22/7, 2014 at 12:7 Comment(1)
The Couchbase Lite documentation is very hard to find, and the documentation is not good. Furthermore, its been very hard to get answers on the forums or on IRC. So far it has been kinda frustrating to work with - Im guessing most of their resources are for paying customers only unfortunately.Albur
D
5

I have been looking for this as well and I think I finally found something. It turns out you need to use coax library to communicate with Couchbase Lite db. The Couchbase Lite plugin exposes only one methode getURL which returns a LOCAL internal URL for Couchbase Lite. Then you have to use coax to create a db object and run queries against it.

Basically it looks like access to Couchbaselite is all via REST queries using internally formed URL. But it is a pain to do therefore you need to use coax. With coax it seems the REST ops get exposed as functions on the objects..put, del, etc. the full reference to the API is listed here is good detail:

Here is the full spec of the REST operations for Couchbase Lite on mobile - your CRUD ops

Here is the Coax with simple instructions on how the rest queries work in the context of CouchDB

I found a very nice article on this here - it talks about the set up and all CRUD ops.

Couchase Lite in Cordova via Coax

Hope this help you...

Detail answered 12/12, 2014 at 12:16 Comment(0)
F
4

An alternate suggestion could be PouchDB and CouchDB.

You could sync your CouchDB straight to PouchDB and it is clever enough to use whatever storage is available on the device ie Localstorage, IDB, WebSQL.

If you don't want full replication then you could build some middleware to control what gets replicated to PouchDB from CouchDB (you can specify which documents from which database get replicated)

The api is pretty straightforward and the documentation is all on the website.

http://pouchdb.com/

Fossiliferous answered 27/9, 2014 at 16:56 Comment(0)
E
2

You can use Phonegap Cordova SQLite Plugin support iOS as well as Android

No syntactic differnces in coding only difference is

    db = window.openDatabase("DBNAME", "1.0", "Description", 200000); // WebSQL

    db = window.sqlitePlugin.openDatabase("DBNAME", "1.0", "Description", 200000); // SQLite Plugin
    db.transaction(function(tx){
    tx.executeSql("CREATE TABLE demo(id INTEGER,name TEXT)");
    });

Storage is unlimited in new version Android/iOS devices

Excitability answered 22/7, 2014 at 12:53 Comment(1)
I already explored this. It actually satisfy my/question's need except that currently the plugin is not stable see readme. That's why I didn't refer it in my findings.Selves
W
2

The code that your looking for is in the todo-lite phonegap app in the function setupConfig. you will need the modules.js, zepto.min.js and zepto.touch.js files from the todolite-phonegap app.

//check if couchbase lite plugin is installed
if (!window.cblite) { return alert( 'Couchbase Lite not installed' ) }

//get your local url from the plugin
cblite.getURL( function(err, url) {
    console.log( "getURL: " + JSON.stringify( [ err, url ] ) )
    if (err) { return alert( JSON.stringfiy( err ) ) }

    var xmlHttp = new XMLHttpRequest()
    xmlHttp.open( 'GET', url, false )
    xmlHttp.send( null )

    window.server = coax( url );

    var db = coax( [ url, appDbName ] );

    setupDb( db, function(err, info) {
        if (err) { return alert( JSON.stringify( err ) ) } 

        // now your db connection is setup you do CRUD operations by

        //GET
        db.get( "myDocumentID", function (error, doc) { 
            if( error ) {
                if( error.status == 404 ) {
                    //INSERT
                    var myDocument = { "key" : "value" };
                    db.put( "myDocumentID", myDocument, function( error, ok ) {
                        if (error) { return alert( JSON.stringify( error ) }
                        //success
                    } );
                } else { return alert(JSON.stringify( error) ) }
            } else {
                //UPDATE
                doc.my_key = "value";
                //DELETE
                doc._deleted = true;
                db.put("myDocumentID", doc, function(error, ok) {
                     if (error) { return alert( JSON.stringify( error ) }
                     //success
                } );
            }
        } );
    } );
} );

function setupDb(db, cb) {
    db.get( function(err, res, body) {
        db.put( function(err, res, body) {
            db.get( cb )
        } )
    } )
}
Wilfordwilfred answered 23/10, 2014 at 22:13 Comment(0)
G
1

I realize this is a bit late but you can find a good video on the Couchbase site (you'll have to hand over your email etc but it's fairly painless). @LorinBeer goes through setting up a local data store in a phonegap app in about 15 minutes (starts at almost exactly 15:00 in).

Using PhoneGap and Couchbase Lite to Create Data-Intensive Applications

There's a demo repo, ANOTER, too

Grettagreuze answered 27/9, 2014 at 4:43 Comment(0)
U
0

At the link mentioned application todo-lite in your question, it shows you how to use the couch-DB database.
They have provided a link of the index.js file, which contains the the implementation (how-to) details of couch-db lite.
I think you may have missed the link. You can have a look at the index.js file for the implementation details.

Ung answered 22/7, 2014 at 14:2 Comment(1)
I have explored the project little-bit, but I'm looking/asking for some clear example of install, db connect and CRUD operation locally in Android and iOS using cordova/phonegap. Like this install, connect and CRUD Operation.Selves

© 2022 - 2024 — McMap. All rights reserved.