Accessing CouchBaseLite via Java Script

96 views
Skip to first unread message

gaurav...@gmail.com

unread,
May 19, 2015, 11:59:20 AM5/19/15
to mobile-c...@googlegroups.com
New to CouchBase Lite.  Have few very basic queries:

- Is there a JS API to access CouchBase Lite DB via Phonegap plugin on iOS ?   The kind of JS APIs Pouch DB exposes ?

http://developer.couchbase.com/mobile/get-started/get-started-mobile/phonegap/index.html This link seems to talk about a phonegap plugin but when I see the plugin code it does do much; how then the ToDo application uses the get and put function which get internally map to iOS native CouchBase Lite API functions?

-  What is the underlying storage mechanism ?  By looking at the API it looks like it uses CoreData internally ?

Jens Alfke

unread,
May 19, 2015, 12:06:52 PM5/19/15
to mobile-c...@googlegroups.com

On May 19, 2015, at 8:55 AM, gaurav...@gmail.com wrote:

New to CouchBase Lite.  Have few very basic queries:

- Is there a JS API to access CouchBase Lite DB via Phonegap plugin on iOS ?   The kind of JS APIs Pouch DB exposes ?

There is a REST API (compatible with CouchDB’s). JS applications generally use wrapper functions that call that API.


-  http://developer.couchbase.com/mobile/get-started/get-started-mobile/phonegap/index.html This link seems to talk about a phonegap plugin but when I see the plugin code it does do much;

The functionality is in the CouchbaseLite and CouchbaseLiteListener frameworks.

how then the ToDo application uses the get and put function which get internally map to iOS native CouchBase Lite API functions?

By calling the REST API. If you look for the definitions of the get and put JS functions in the ToDoLite source, you’ll see that they use XMLHTTPRequest.

-  What is the underlying storage mechanism ?  By looking at the API it looks like it uses CoreData internally ?

No, it uses SQLite directly for storage. In version 1.1 there is an option to use ForestDB, a new key-value store developed at Couchbase which is significantly faster.

—Jens

gaurav...@gmail.com

unread,
May 20, 2015, 11:36:14 AM5/20/15
to mobile-c...@googlegroups.com
Thanks
Can you please confirm if this is how it works:

- JS Wrappers internally call Rest APIs which internally call Cardova Plugin which uses the CouchbaseLite and CouchbaseLiteListener frameworks to update data in SQLLite storage.  Is this understanding correct ?

- So in a Cardova application for every REST API invoked an equivalent plugin code that gets called.  If that is the case how do the app differentiate when it needs to use the REST API to call the server or it needs to divert the REST API calls to the Cardova Plugin .

Jens Alfke

unread,
May 20, 2015, 11:45:23 AM5/20/15
to mobile-c...@googlegroups.com
On May 20, 2015, at 8:36 AM, gaurav...@gmail.com wrote:

- JS Wrappers internally call Rest APIs which internally call Cardova Plugin which uses the CouchbaseLite and CouchbaseLiteListener frameworks to update data in SQLLite storage.  Is this understanding correct ?

The plugin isn’t involved. The REST API goes directly to the CouchbaseLiteListener’s handlers.
(All the plugin does is initialize Couchbase Lite. That’s why there’s so little code in it.)

- So in a Cardova application for every REST API invoked an equivalent plugin code that gets called.  If that is the case how do the app differentiate when it needs to use the REST API to call the server or it needs to divert the REST API calls to the Cardova Plugin .

The app never needs to talk directly to a server. It only talks to Couchbase Lite.

—Jens

gaurav...@gmail.com

unread,
May 21, 2015, 4:56:25 AM5/21/15
to mobile-c...@googlegroups.com
Thanks again for the prompt response.

- So the CouchbaseLiteListener  internally creates a HTTP server on the device itself and all the REST API calls are submitted to this local HTTP server on the device itself.
The actual magic of inserting/updating data happens inside the CouchbaseLiteListener  . 


- Why CouchBaseLite chose this round about way of invoking the plugin ?  Wouldn't it have been easier if they would have provided a one to one mapping of the JS APIs in the pluging itself.


-  We checked the URL that gets used when the plugin is initialized .  It is something like this http://lite.couch.   How does this URL maps to local host ? 

Jens Alfke

unread,
May 21, 2015, 11:52:21 AM5/21/15
to mobile-c...@googlegroups.com
On May 21, 2015, at 1:56 AM, gaurav...@gmail.com wrote:

- So the CouchbaseLiteListener  internally creates a HTTP server on the device itself and all the REST API calls are submitted to this local HTTP server on the device itself.
The actual magic of inserting/updating data happens inside the CouchbaseLiteListener  .  

It’s not really going through an HTTP server. The listener just sets up an NSURLProtocol that instructs WebKit / CFNetwork to route all HTTP requests for the host “lite.couchbase.” to its own handlers instead of sending them over the network. This is a lot more lightweight (and secure) than going through a socket.

- Why CouchBaseLite chose this round about way of invoking the plugin ?  Wouldn't it have been easier if they would have provided a one to one mapping of the JS APIs in the pluging itself.

Couchbase Lite does not have a JavaScript API. In addition to its native API (Objective-C or Java or C#) there is a REST API, as defined by CouchDB  There are a number of JS convenience wrappers people have created around this, with different APIs, and you’re free to choose whichever one you want.

There happens to be a different CouchDB compatible database, PouchDB, that’s implemented directly in JS and has its own direct JS API. Maybe in the future we’ll implement a similar API that directly calls into Couchbase Lite, but that’s not something we’ve decided to do yet.

—Jens

gaurav...@gmail.com

unread,
May 25, 2015, 3:50:02 AM5/25/15
to mobile-c...@googlegroups.com
Thanks Jens.   It was very useful.

As we are exploring more on this framework we are coming up with new complex scenarios that we need to address.  I am putting down those and request to provide guidance on
how CouchBase Lite Mobile will handle these scenarios:

- We need to do concurrent write and read. We want to have the native objective c code do a write using the APIs provided into the CouchBase Lite Objective C APIs.  Concurrently we want to read via JS/REST APIs via Cordova Plugin.
So basically we want to do sync in native and read via JS/REST APIs from with in the WebView concurrently.  However while the native code is doing write ; we were not able to read via the REST API/JS via Cordova plugin.
  Is this type of complex scenario supported ?

-  How to do concurrent read and write with in WebView via REST APIs/JS ?  Is it supported ? So basically if the concurrent native sync and read via web view does not work than we would want to have concurrent read and write using JS only ? 


- What all needs to be done if we would want to have Cordova plugin use CoreData in place of SQLLite ?


- Can we use pure JQuery Ajax calls to perform all REST APIs operations via Cordova Plugin ?

Jens Alfke

unread,
May 25, 2015, 1:46:39 PM5/25/15
to mobile-c...@googlegroups.com
On May 25, 2015, at 12:50 AM, gaurav...@gmail.com wrote:

So basically we want to do sync in native and read via JS/REST APIs from with in the WebView concurrently.  However while the native code is doing write ; we were not able to read via the REST API/JS via Cordova plugin.

SQLite (at least in the way we’re using it) doesn’t support concurrent writes and reads: an active write transaction blocks reads. But writes and reads can interleave. You may be having problems because you’re creating one very long transaction and doing all the work in it. If you need to support reads at the same time, try using shorter transactions.

(The iOS 1.1 release, coming in a few days, supports a new storage engine called ForestDB that doesn’t have this limitation. It allows concurrent reads and writes. Details are here.)

 -  How to do concurrent read and write with in WebView via REST APIs/JS ?  Is it supported ? So basically if the concurrent native sync and read via web view does not work than we would want to have concurrent read and write using JS only ?  

Since XHRs are async, you can start as many as you want. The underlying database has the same concurrency limitations as I described above, but the REST API doesn’t allow you to create arbitrarily long transactions so that should be less of a problem.

—Jens

gaurav...@gmail.com

unread,
May 28, 2015, 11:58:17 AM5/28/15
to mobile-c...@googlegroups.com
Thanks again for all the prompt responses.

One security risk that we are anticipating is that the underlying SQLLite file will get backed up automatically on iCloud.

Is there any way in the REST API to configure for it to be not backed up on iCloud or we need to do it via the native code as given in here

https://github.com/couchbase/couchbase-lite-ios/issues/357 by using the CBLManager.excludedFromBackup

Jens Alfke

unread,
May 28, 2015, 12:05:07 PM5/28/15
to mobile-c...@googlegroups.com

On May 28, 2015, at 8:58 AM, gaurav...@gmail.com wrote:

Is there any way in the REST API to configure for it to be not backed up on iCloud or we need to do it via the native code as given in here
https://github.com/couchbase/couchbase-lite-ios/issues/357 by using the CBLManager.excludedFromBackup 

The status is as reported in the issue. There’s a native API now, and requests were filed to add a JS wrapper for PhoneGap apps, but those haven’t been implemented yet. If you want to ‘upvote’ them, add comments.

—Jens

gaurav...@gmail.com

unread,
Jun 3, 2015, 8:05:00 AM6/3/15
to mobile-c...@googlegroups.com
Many thanks Jens.

I didn't find any REST API documentation for querying a Design Doc.   How do we query using a Design doc using REST API after we have created the Design Doc

Jens Alfke

unread,
Jun 3, 2015, 11:57:46 AM6/3/15
to mobile-c...@googlegroups.com

On Jun 3, 2015, at 5:05 AM, gaurav...@gmail.com wrote:

I didn't find any REST API documentation for querying a Design Doc.   How do we query using a Design doc using REST API after we have created the Design Doc 

Well, it would be here, except it looks like no one filled it in. Sigh! So you can read the equivalent documentation for CouchDB.

—Jens
Reply all
Reply to author
Forward
0 new messages