triniwiz / triniwiz/nativescript-plugins

[couchbase] Add setJSON methods and make android compile

Open
#224 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
87
Forks
57
Avg merge
1h 36m
Merged PRs (30d)
4

Description

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch @triniwiz/nativescript-couchbase@3.0.0 for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/@triniwiz/nativescript-couchbase/index.android.js b/node_modules/@triniwiz/nativescript-couchbase/index.android.js
index 23533a1..c815234 100644
--- a/node_modules/@triniwiz/nativescript-couchbase/index.android.js
+++ b/node_modules/@triniwiz/nativescript-couchbase/index.android.js
@@ -935,6 +935,10 @@ export class MutableDocument extends Document {
         this.native.setBlob(key, value.native);
         return this;
     }
+    setJSON(value) {
+        this.native.setJSON(JSON.stringify(value));
+        return this;
+    }
     toJSON() {
         const keys = this.getKeys();
         const ret = {};
diff --git a/node_modules/@triniwiz/nativescript-couchbase/index.d.ts b/node_modules/@triniwiz/nativescript-couchbase/index.d.ts
index 175ef6b..5a458dc 100644
--- a/node_modules/@triniwiz/nativescript-couchbase/index.d.ts
+++ b/node_modules/@triniwiz/nativescript-couchbase/index.d.ts
@@ -239,6 +239,8 @@ export class MutableDocument extends Omit<Document, 'toMutable'> {
 
 	setArray(key: string, value: Array): this;
 
+	setJSON(value: { [key: string]: ValueType }): this;
+
 	toJSON();
 }
 
diff --git a/node_modules/@triniwiz/nativescript-couchbase/index.ios.js b/node_modules/@triniwiz/nativescript-couchbase/index.ios.js
index fd85a0c..9aa201c 100644
--- a/node_modules/@triniwiz/nativescript-couchbase/index.ios.js
+++ b/node_modules/@triniwiz/nativescript-couchbase/index.ios.js
@@ -891,6 +891,10 @@ export class MutableDocument extends Document {
         this.native.setBlobForKey(value.native, key);
         return this;
     }
+    setJSON(value) {
+        this.native.setJSONError(JSON.stringify(value));
+        return this;
+    }
     toJSON() {
         const keys = this.getKeys();
         const ret = {};
diff --git a/node_modules/@triniwiz/nativescript-couchbase/platforms/android/java/com/github/triniwiz/couchbase/Couchbase.kt b/node_modules/@triniwiz/nativescript-couchbase/platforms/android/java/com/github/triniwiz/couchbase/Couchbase.kt
index 52287c2..23591c0 100644
--- a/node_modules/@triniwiz/nativescript-couchbase/platforms/android/java/com/github/triniwiz/couchbase/Couchbase.kt
+++ b/node_modules/@triniwiz/nativescript-couchbase/platforms/android/java/com/github/triniwiz/couchbase/Couchbase.kt
@@ -22,136 +22,6 @@ class Couchbase {
       }
     }
     
-    @JvmStatic
-    fun getDocument(database: Database, id: String?): String? {
-      return id?.let {
-        database.getDocument(it)?.let {
-          return toJSON(it)
-        } ?: run {
-          null
-        }
-      } ?: run {
-        null
-      }
-    }
-
-
-    @JvmStatic
-    fun getDocuments(database: Database, ids: kotlin.Array<String>): String {
-      val args = mutableListOf<Expression>()
-      ids.forEach {
-        args.add(Expression.string(it))
-      }
-      return queryResultsToJSON(QueryBuilder.select(SelectResult.all(), SelectResult.expression(Meta.id)).from(DataSource.database(database)).where(
-        Meta.id.`in`(*args.toTypedArray())
-      ), true)
-    }
-
-
-    @JvmStatic
-    fun queryResultsToJSON(query: Query, isAll: Boolean): String {
-      val json = JSONArray()
-      try {
-        query.execute().allResults().forEach { item ->
-          val keys = item.keys
-          val obj = JSONObject()
-          for (key in keys) {
-            val nativeItem = item.getValue(key)
-            if (isAll && nativeItem as? Dictionary != null) {
-              (nativeItem as? Dictionary)?.let {
-                for (cblKey in it.keys) {
-                  obj.put(cblKey, deserialize(nativeItem.getValue(cblKey)))
-                }
-              }
-            } else {
-              obj.put(key, deserialize(nativeItem))
-            }
-          }
-          json.put(obj)
-        }
-      } catch (e: java.lang.Exception) {
-      }
-      return json.toString()
-    }
-
-    @JvmStatic
-    fun toJSON(document: Document): String? {
-      val json = JSONObject()
-      return try {
-        json.put("id", document.id)
-        json.put("revisionID", document.revisionID)
-        for (key in document.keys) {
-          json.put(key, deserialize(document.getValue(key)))
-        }
-        json.toString()
-      } catch (e: Exception) {
-        null
-      }
-    }
-
-    @JvmStatic
-    fun fromJSON(json: String): MutableDocument? {
-      return fromJSON(json, null)
-    }
-
-    @JvmStatic
-    fun fromJSON(json: String, id: String?): MutableDocument? {
-      return try {
-        val doc = id?.let {
-          MutableDocument(id)
-        } ?: MutableDocument()
-        val jsonObject = JSONObject(json)
-        for (key in jsonObject.keys()) {
-          serialize(jsonObject[key], doc, key)
-        }
-        doc
-      } catch (e: Exception) {
-        null
-      }
-    }
-
-    @JvmStatic
-    fun updateFromJSON(database: Database, json: String, id: String): Boolean {
-      return updateFromJSON(database, json, id, ConcurrencyControl.LAST_WRITE_WINS)
-    }
-
-    @JvmStatic
-    fun updateFromJSON(database: Database, json: String, id: String, concurrencyMode: ConcurrencyControl): Boolean {
-      return database.getDocument(id)?.let {
-        return try {
-          val jsonObject = JSONObject(json)
-          val doc = it.toMutable()
-          for (key in jsonObject.keys()) {
-            serialize(jsonObject[key], doc, key)
-          }
-          database.save(doc, concurrencyMode)
-        } catch (e: java.lang.Exception) {
-          false
-        }
-      } ?: false
-    }
-
-    @JvmStatic
-    fun saveFromJSON(database: Database, json: String, id: String?): String? {
-      return saveFromJSON(database, json, id, ConcurrencyControl.LAST_WRITE_WINS)
-    }
-
-    @JvmStatic
-    fun saveFromJSON(database: Database, json: String, id: String?, concurrencyMode: ConcurrencyControl): String? {
-      return fromJSON(json, id)?.let {
-        return try {
-          val saved = database.save(it, concurrencyMode)
-          if (saved) {
-            it.id
-          } else {
-            null
-          }
-        } catch (e: CouchbaseLiteException) {
-          null
-        }
-      }
-    }
-
     @JvmStatic
     private var _dateFormat: SimpleDateFormat? = null
 
diff --git a/node_modules/@triniwiz/nativescript-couchbase/platforms/android/nativescript_couchbase.aar b/node_modules/@triniwiz/nativescript-couchbase/platforms/android/nativescript_couchbase.aar
index d03cf5b..9fe02dd 100644
Binary files a/node_modules/@triniwiz/nativescript-couchbase/platforms/android/nativescript_couchbase.aar and b/node_modules/@triniwiz/nativescript-couchbase/platforms/android/nativescript_couchbase.aar differ

This issue body was partially generated by patch-package.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the repository versions of index.android.js, index.ios.js, index.d.ts, and platforms/android/java/com/github/triniwiz/couchbase/Couchbase.kt; compare them with the supplied patch. Check the Android build path and the native nativescript_couchbase.aar artifact first. Done means the setJSON API is represented across the platform bindings and Android compiles successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, kotlin, typescript
Domain
mobile-dev
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.