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.
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
.
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
couchbase-lite
. Which has plugin support for both Android and iOS platform.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...
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/
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
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 )
} )
} )
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With