CouchDB as Cordova / Phonegap Database

goal

I want to create a cross-platform mobile application with cordova / phonegap , which requires a database (only on the client side). My target platform is mainly Android and iOS . I chose couchbase-lite as my repository.

Problem

But I could not find good documentation for couchbase-lite in phonegap . Only I found the rest api and the todo-lite app on github and the Play Store .

I could not understand if I do not have a server-side implementation, how can I get a url by which I can send a POST / GET / PUT / DELETE request.

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

Why is Couchbase-lite (not important for everyone)

For those who will suggest me to choose another db, I just share my findings ...
From cordova 5.0 documentation, there are several options

  • LocalSorage: The 5 MB limit is not enough for the application.
  • WebSQL: I'm scared of his future since w3c has stopped planning.
  • IndexDB: Currently underdeveloped and unavailable for Android and iOS.
  • Parameters based on plugins:. Motivating the NoSQL score. I tried couchbase-lite . Which supports the plugin for both the Android platform and iOS.
+6
source share
6 answers

I was looking for this too, and I think I finally found something. Turns out you need to use a coaxial library to communicate with Couchbase Lite db. The Couchbase Lite plugin provides only one getURL method, which returns the LOCAL internal URL for Couchbase Lite. Then you should use coaxial code to create the db object and run queries against it.

Basically, it seems that Couchbaselite is accessed through REST requests using an internal URL. But it hurts, so you need to use a coaxial cable. With a coaxial interface, it seems that REST operations are becoming available as functions on objects..put, del, etc. All API links are listed here:

Here is the complete specification of REST operations for Couchbase Lite on mobile devices - your CRUD operations

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

I found a very nice article about this here - it's about tuning and all CRUD operations.

Couchase Lite in Cordoba via Coax

Hope this helps you ...

+5
source

Alternative suggestions could be PouchDB and CouchDB.

You can synchronize your CouchDB directly with PouchDB, and it is smart enough to use any storage on the device, i.e. Localstorage, IDB, WebSQL.

If you don't need full replication, you can create some middleware to control what is replicated to PouchDB from CouchDB (you can specify which documents from the database will be replicated)

Api is pretty simple and the documentation is on the website.

http://pouchdb.com/

+3
source

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

No syntax differences in coding, only difference

  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 unlimited in new versions of Android / iOS devices

+2
source

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

 //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 ) } ) } ) } 
+2
source

I understand that this is a bit late, but you can find a good video on the Couchbase website (you will have to submit your email, etc., but it is pretty painless). @LorinBeer goes through the local data store in the phonegap application in about 15 minutes (starts almost exactly at 15:00).

Using PhoneGap and Couchbase Lite to Build High-Intensity Applications

There's a demo repo, ANOTER

+1
source

Following the link is the todo-lite application in your question, it shows you how to use the couch-DB database.
They provided a link to the index.js file, which contains implementation details (practical) information about couch-db lite.
I think you might have missed the link. You can view the index.js file for implementation details.

0
source

Source: https://habr.com/ru/post/972648/


All Articles