couchbaselabs / Couchbase-Lite-PhoneGap-Plugin

Plugin to install Couchbase Lite in your PhoneGap app on iOS or Android
183 stars 67 forks source link

Configuration option for using ForestDB #79

Open lasselaakkonen opened 8 years ago

lasselaakkonen commented 8 years ago

It would be good, if this plugin included a configuration option for using ForestDB.

keithdmoore commented 8 years ago

+1

swaheed2 commented 8 years ago

+1

HanzhiDou commented 8 years ago

Is there any hard way to use ForestDB at the moment?

I tried to build couchbase-lite-android with hard coded storage type as ForestDB in Manager.java (getDefaultOptions and in openDatabase), and replaced aar and jars in this plugin's lib directory, and also add forestdb aar to plugin.xml, after reinstalled the plugin from local, seems ForestDB is not used, i can not observe any changes.

swaheed2 commented 8 years ago

@HanzhiDou I was able to use ForestDB using these steps:

1) Update the method do_PUT_Database in src/main/java/com/couchbase/lite/router/Router.java and generate the jar file by running ./gradlew build. The jar file will appear at couchbase-lite-java-core/build/libs

Updated do_PUT_Database

if (db.exists()) {
    return new Status(Status.DUPLICATE);
}
try {
    // note: synchronized 
    Map<String, Object> body = getBodyAsDictionary();
    if (body == null) { 
        Log.i(TAG, "No body for do_PUT_Database");
        db.open();
    } 
    else{ 
        DatabaseOptions options = new DatabaseOptions();
        // ForestDB || SQLite
        if(body.containsKey("storageType")){ 
            String storageType = (String) body.get("storageType");
            if(storageType.equals(Manager.SQLITE_STORAGE) || storageType.equals(Manager.FORESTDB_STORAGE)){
                Log.d(TAG, "Storage type: " + storageType);
                options.setStorageType(storageType);
            }
        }
        db.open(options);
    }
} catch (CouchbaseLiteException e) {
    return e.getCBLStatus();
}

setResponseLocation(connection.getURL());
return new Status(Status.CREATED);

2) add the forestdb dependency to plugins\com.couchbase.lite.phonegap\src\android\build.gradle

dependencies {
   compile(name:'couchbase-lite-android-1.3.0', ext:'aar')
   compile(name:'couchbase-lite-android-sqlite-custom-1.3.0', ext:'aar') 
   compile(name:'couchbase-lite-android-forestdb-1.3.0', ext:'aar')
}

3) add the couchbase-lite-android-forestdb-1.3.0.aar to\ plugins\com.couchbase.lite.phonegap\lib\android\lib**

(downloaded from http://files.couchbase.com/maven2/com/couchbase/lite/couchbase-lite-android-forestdb/1.3.0/)

4) Update to plugins\com.couchbase.lite.phonegap\plugins.xml

5) When you are creating the database on the client side, pass the configuration as the request body:

 var data = {
    storageType : "ForestDB"
    //ForestDB
    //SQLite
};

6) Finally, a very important step is to recreate the project in cordova so the plugin gets updated

     cordova platform rm android
     cordova platform add android
HanzhiDou commented 8 years ago

@swaheed2 thanks a lot for the detail instruction! I will try it.