cbl-ionic 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (190) hide show
  1. package/CblIonic.podspec +18 -0
  2. package/LICENSE +201 -0
  3. package/README.md +19 -0
  4. package/android/build.gradle +69 -0
  5. package/android/src/main/AndroidManifest.xml +2 -0
  6. package/android/src/main/java/cbl/js/kotiln/CollectionConfigDto.kt +5 -0
  7. package/android/src/main/java/cbl/js/kotiln/CollectionDocumentResult.kt +7 -0
  8. package/android/src/main/java/cbl/js/kotiln/CollectionDto.kt +9 -0
  9. package/android/src/main/java/cbl/js/kotiln/CollectionManager.kt +232 -0
  10. package/android/src/main/java/cbl/js/kotiln/ConfigDto.kt +5 -0
  11. package/android/src/main/java/cbl/js/kotiln/DatabaseManager.kt +183 -0
  12. package/android/src/main/java/cbl/js/kotiln/DocumentDto.kt +6 -0
  13. package/android/src/main/java/cbl/js/kotiln/FileSystemHelper.kt +18 -0
  14. package/android/src/main/java/cbl/js/kotiln/LoggingManager.kt +30 -0
  15. package/android/src/main/java/cbl/js/kotiln/ReplicatorManager.kt +123 -0
  16. package/android/src/main/java/cbl/js/kotiln/ScopeDto.kt +6 -0
  17. package/android/src/main/java/com/couchbase/ionic/CblIonicPluginPlugin.kt +1832 -0
  18. package/android/src/main/java/com/couchbase/ionic/IndexDto.kt +12 -0
  19. package/android/src/main/java/com/couchbase/ionic/IndexHelper.kt +30 -0
  20. package/android/src/main/java/com/couchbase/ionic/PluginHelper.kt +283 -0
  21. package/android/src/main/java/com/couchbase/ionic/QueryHelper.kt +62 -0
  22. package/android/src/main/java/com/couchbase/ionic/ReplicatorHelper.kt +174 -0
  23. package/android/src/main/res/.gitkeep +0 -0
  24. package/dist/esm/cblite-js/cblite/core-types.d.ts +634 -0
  25. package/dist/esm/cblite-js/cblite/core-types.js +2 -0
  26. package/dist/esm/cblite-js/cblite/core-types.js.map +1 -0
  27. package/dist/esm/cblite-js/cblite/index.d.ts +2 -0
  28. package/dist/esm/cblite-js/cblite/index.js +4 -0
  29. package/dist/esm/cblite-js/cblite/index.js.map +1 -0
  30. package/dist/esm/cblite-js/cblite/src/abstract-index.d.ts +14 -0
  31. package/dist/esm/cblite-js/cblite/src/abstract-index.js +12 -0
  32. package/dist/esm/cblite-js/cblite/src/abstract-index.js.map +1 -0
  33. package/dist/esm/cblite-js/cblite/src/authenticator.d.ts +6 -0
  34. package/dist/esm/cblite-js/cblite/src/authenticator.js +3 -0
  35. package/dist/esm/cblite-js/cblite/src/authenticator.js.map +1 -0
  36. package/dist/esm/cblite-js/cblite/src/basic-authenticator.d.ts +42 -0
  37. package/dist/esm/cblite-js/cblite/src/basic-authenticator.js +59 -0
  38. package/dist/esm/cblite-js/cblite/src/basic-authenticator.js.map +1 -0
  39. package/dist/esm/cblite-js/cblite/src/blob.d.ts +58 -0
  40. package/dist/esm/cblite-js/cblite/src/blob.js +86 -0
  41. package/dist/esm/cblite-js/cblite/src/blob.js.map +1 -0
  42. package/dist/esm/cblite-js/cblite/src/collation.d.ts +20 -0
  43. package/dist/esm/cblite-js/cblite/src/collation.js +64 -0
  44. package/dist/esm/cblite-js/cblite/src/collation.js.map +1 -0
  45. package/dist/esm/cblite-js/cblite/src/collection-config.d.ts +24 -0
  46. package/dist/esm/cblite-js/cblite/src/collection-config.js +30 -0
  47. package/dist/esm/cblite-js/cblite/src/collection-config.js.map +1 -0
  48. package/dist/esm/cblite-js/cblite/src/collection.d.ts +189 -0
  49. package/dist/esm/cblite-js/cblite/src/collection.js +354 -0
  50. package/dist/esm/cblite-js/cblite/src/collection.js.map +1 -0
  51. package/dist/esm/cblite-js/cblite/src/concurrency-control.d.ts +4 -0
  52. package/dist/esm/cblite-js/cblite/src/concurrency-control.js +8 -0
  53. package/dist/esm/cblite-js/cblite/src/concurrency-control.js.map +1 -0
  54. package/dist/esm/cblite-js/cblite/src/couchbase-lite-exception.d.ts +6 -0
  55. package/dist/esm/cblite-js/cblite/src/couchbase-lite-exception.js +15 -0
  56. package/dist/esm/cblite-js/cblite/src/couchbase-lite-exception.js.map +1 -0
  57. package/dist/esm/cblite-js/cblite/src/database-configuration.d.ts +52 -0
  58. package/dist/esm/cblite-js/cblite/src/database-configuration.js +75 -0
  59. package/dist/esm/cblite-js/cblite/src/database-configuration.js.map +1 -0
  60. package/dist/esm/cblite-js/cblite/src/database-logging.d.ts +13 -0
  61. package/dist/esm/cblite-js/cblite/src/database-logging.js +13 -0
  62. package/dist/esm/cblite-js/cblite/src/database-logging.js.map +1 -0
  63. package/dist/esm/cblite-js/cblite/src/database.d.ts +254 -0
  64. package/dist/esm/cblite-js/cblite/src/database.js +514 -0
  65. package/dist/esm/cblite-js/cblite/src/database.js.map +1 -0
  66. package/dist/esm/cblite-js/cblite/src/definitions.d.ts +7 -0
  67. package/dist/esm/cblite-js/cblite/src/definitions.js +2 -0
  68. package/dist/esm/cblite-js/cblite/src/definitions.js.map +1 -0
  69. package/dist/esm/cblite-js/cblite/src/document-replication.d.ts +13 -0
  70. package/dist/esm/cblite-js/cblite/src/document-replication.js +34 -0
  71. package/dist/esm/cblite-js/cblite/src/document-replication.js.map +1 -0
  72. package/dist/esm/cblite-js/cblite/src/document.d.ts +199 -0
  73. package/dist/esm/cblite-js/cblite/src/document.js +333 -0
  74. package/dist/esm/cblite-js/cblite/src/document.js.map +1 -0
  75. package/dist/esm/cblite-js/cblite/src/endpoint.d.ts +3 -0
  76. package/dist/esm/cblite-js/cblite/src/endpoint.js +2 -0
  77. package/dist/esm/cblite-js/cblite/src/endpoint.js.map +1 -0
  78. package/dist/esm/cblite-js/cblite/src/engine-locator.d.ts +42 -0
  79. package/dist/esm/cblite-js/cblite/src/engine-locator.js +50 -0
  80. package/dist/esm/cblite-js/cblite/src/engine-locator.js.map +1 -0
  81. package/dist/esm/cblite-js/cblite/src/expression.d.ts +134 -0
  82. package/dist/esm/cblite-js/cblite/src/expression.js +374 -0
  83. package/dist/esm/cblite-js/cblite/src/expression.js.map +1 -0
  84. package/dist/esm/cblite-js/cblite/src/file-system.d.ts +7 -0
  85. package/dist/esm/cblite-js/cblite/src/file-system.js +14 -0
  86. package/dist/esm/cblite-js/cblite/src/file-system.js.map +1 -0
  87. package/dist/esm/cblite-js/cblite/src/full-text-expression.d.ts +28 -0
  88. package/dist/esm/cblite-js/cblite/src/full-text-expression.js +38 -0
  89. package/dist/esm/cblite-js/cblite/src/full-text-expression.js.map +1 -0
  90. package/dist/esm/cblite-js/cblite/src/full-text-index.d.ts +47 -0
  91. package/dist/esm/cblite-js/cblite/src/full-text-index.js +77 -0
  92. package/dist/esm/cblite-js/cblite/src/full-text-index.js.map +1 -0
  93. package/dist/esm/cblite-js/cblite/src/index-builder.d.ts +20 -0
  94. package/dist/esm/cblite-js/cblite/src/index-builder.js +25 -0
  95. package/dist/esm/cblite-js/cblite/src/index-builder.js.map +1 -0
  96. package/dist/esm/cblite-js/cblite/src/index.d.ts +42 -0
  97. package/dist/esm/cblite-js/cblite/src/index.js +43 -0
  98. package/dist/esm/cblite-js/cblite/src/index.js.map +1 -0
  99. package/dist/esm/cblite-js/cblite/src/meta-expression.d.ts +10 -0
  100. package/dist/esm/cblite-js/cblite/src/meta-expression.js +30 -0
  101. package/dist/esm/cblite-js/cblite/src/meta-expression.js.map +1 -0
  102. package/dist/esm/cblite-js/cblite/src/meta.d.ts +7 -0
  103. package/dist/esm/cblite-js/cblite/src/meta.js +8 -0
  104. package/dist/esm/cblite-js/cblite/src/meta.js.map +1 -0
  105. package/dist/esm/cblite-js/cblite/src/mutable-document.d.ts +373 -0
  106. package/dist/esm/cblite-js/cblite/src/mutable-document.js +481 -0
  107. package/dist/esm/cblite-js/cblite/src/mutable-document.js.map +1 -0
  108. package/dist/esm/cblite-js/cblite/src/ordering.d.ts +14 -0
  109. package/dist/esm/cblite-js/cblite/src/ordering.js +31 -0
  110. package/dist/esm/cblite-js/cblite/src/ordering.js.map +1 -0
  111. package/dist/esm/cblite-js/cblite/src/parameter.d.ts +18 -0
  112. package/dist/esm/cblite-js/cblite/src/parameter.js +38 -0
  113. package/dist/esm/cblite-js/cblite/src/parameter.js.map +1 -0
  114. package/dist/esm/cblite-js/cblite/src/parameters.d.ts +17 -0
  115. package/dist/esm/cblite-js/cblite/src/parameters.js +61 -0
  116. package/dist/esm/cblite-js/cblite/src/parameters.js.map +1 -0
  117. package/dist/esm/cblite-js/cblite/src/query.d.ts +83 -0
  118. package/dist/esm/cblite-js/cblite/src/query.js +146 -0
  119. package/dist/esm/cblite-js/cblite/src/query.js.map +1 -0
  120. package/dist/esm/cblite-js/cblite/src/replicated-document.d.ts +25 -0
  121. package/dist/esm/cblite-js/cblite/src/replicated-document.js +51 -0
  122. package/dist/esm/cblite-js/cblite/src/replicated-document.js.map +1 -0
  123. package/dist/esm/cblite-js/cblite/src/replicator-activity-level.d.ts +7 -0
  124. package/dist/esm/cblite-js/cblite/src/replicator-activity-level.js +14 -0
  125. package/dist/esm/cblite-js/cblite/src/replicator-activity-level.js.map +1 -0
  126. package/dist/esm/cblite-js/cblite/src/replicator-change.d.ts +14 -0
  127. package/dist/esm/cblite-js/cblite/src/replicator-change.js +14 -0
  128. package/dist/esm/cblite-js/cblite/src/replicator-change.js.map +1 -0
  129. package/dist/esm/cblite-js/cblite/src/replicator-configuration.d.ts +341 -0
  130. package/dist/esm/cblite-js/cblite/src/replicator-configuration.js +516 -0
  131. package/dist/esm/cblite-js/cblite/src/replicator-configuration.js.map +1 -0
  132. package/dist/esm/cblite-js/cblite/src/replicator-progress.d.ts +9 -0
  133. package/dist/esm/cblite-js/cblite/src/replicator-progress.js +20 -0
  134. package/dist/esm/cblite-js/cblite/src/replicator-progress.js.map +1 -0
  135. package/dist/esm/cblite-js/cblite/src/replicator-status.d.ts +13 -0
  136. package/dist/esm/cblite-js/cblite/src/replicator-status.js +29 -0
  137. package/dist/esm/cblite-js/cblite/src/replicator-status.js.map +1 -0
  138. package/dist/esm/cblite-js/cblite/src/replicator.d.ts +108 -0
  139. package/dist/esm/cblite-js/cblite/src/replicator.js +224 -0
  140. package/dist/esm/cblite-js/cblite/src/replicator.js.map +1 -0
  141. package/dist/esm/cblite-js/cblite/src/result.d.ts +4 -0
  142. package/dist/esm/cblite-js/cblite/src/result.js +2 -0
  143. package/dist/esm/cblite-js/cblite/src/result.js.map +1 -0
  144. package/dist/esm/cblite-js/cblite/src/scope.d.ts +31 -0
  145. package/dist/esm/cblite-js/cblite/src/scope.js +34 -0
  146. package/dist/esm/cblite-js/cblite/src/scope.js.map +1 -0
  147. package/dist/esm/cblite-js/cblite/src/session-authenticator.d.ts +32 -0
  148. package/dist/esm/cblite-js/cblite/src/session-authenticator.js +52 -0
  149. package/dist/esm/cblite-js/cblite/src/session-authenticator.js.map +1 -0
  150. package/dist/esm/cblite-js/cblite/src/url-endpoint.d.ts +8 -0
  151. package/dist/esm/cblite-js/cblite/src/url-endpoint.js +12 -0
  152. package/dist/esm/cblite-js/cblite/src/url-endpoint.js.map +1 -0
  153. package/dist/esm/cblite-js/cblite/src/value-index.d.ts +32 -0
  154. package/dist/esm/cblite-js/cblite/src/value-index.js +54 -0
  155. package/dist/esm/cblite-js/cblite/src/value-index.js.map +1 -0
  156. package/dist/esm/cblite-js/cblite/src/variable-expression.d.ts +7 -0
  157. package/dist/esm/cblite-js/cblite/src/variable-expression.js +15 -0
  158. package/dist/esm/cblite-js/cblite/src/variable-expression.js.map +1 -0
  159. package/dist/esm/couchbase-lite/capacitor-engine.d.ts +124 -0
  160. package/dist/esm/couchbase-lite/capacitor-engine.js +303 -0
  161. package/dist/esm/couchbase-lite/capacitor-engine.js.map +1 -0
  162. package/dist/esm/definitions.d.ts +12 -0
  163. package/dist/esm/definitions.js +4 -0
  164. package/dist/esm/definitions.js.map +1 -0
  165. package/dist/esm/index.d.ts +4 -0
  166. package/dist/esm/index.js +5 -0
  167. package/dist/esm/index.js.map +1 -0
  168. package/dist/esm/ionic-couchbase-lite.d.ts +3 -0
  169. package/dist/esm/ionic-couchbase-lite.js +4 -0
  170. package/dist/esm/ionic-couchbase-lite.js.map +1 -0
  171. package/dist/plugin.cjs.js +4400 -0
  172. package/dist/plugin.cjs.js.map +1 -0
  173. package/dist/plugin.js +4403 -0
  174. package/dist/plugin.js.map +1 -0
  175. package/ios/Plugin/CblIonicPlugin.swift +1583 -0
  176. package/ios/Plugin/CblIonicPluginPlugin.h +10 -0
  177. package/ios/Plugin/CblIonicPluginPlugin.m +86 -0
  178. package/ios/Plugin/Info.plist +24 -0
  179. package/ios/Plugin/cbl-js-swift/CollectionManager.swift +489 -0
  180. package/ios/Plugin/cbl-js-swift/DatabaseManager.swift +319 -0
  181. package/ios/Plugin/cbl-js-swift/FileSystemHelper.swift +22 -0
  182. package/ios/Plugin/cbl-js-swift/IndexHelper.swift +36 -0
  183. package/ios/Plugin/cbl-js-swift/LICENSE +201 -0
  184. package/ios/Plugin/cbl-js-swift/LoggingManager.swift +79 -0
  185. package/ios/Plugin/cbl-js-swift/MapHelper.swift +92 -0
  186. package/ios/Plugin/cbl-js-swift/QueryHelper.swift +74 -0
  187. package/ios/Plugin/cbl-js-swift/README.md +9 -0
  188. package/ios/Plugin/cbl-js-swift/ReplicatorHelper.swift +184 -0
  189. package/ios/Plugin/cbl-js-swift/ReplicatorManager.swift +157 -0
  190. package/package.json +86 -0
@@ -0,0 +1,1832 @@
1
+ @file:OptIn(DelicateCoroutinesApi::class)
2
+
3
+ package com.couchbase.ionic
4
+
5
+ import cbl.js.kotiln.CollectionManager
6
+ import cbl.js.kotiln.DatabaseManager
7
+ import cbl.js.kotiln.FileSystemHelper
8
+ import cbl.js.kotiln.LoggingManager
9
+ import cbl.js.kotiln.ReplicatorManager
10
+ import kotlinx.coroutines.Dispatchers
11
+ import kotlinx.coroutines.withContext
12
+
13
+ import com.getcapacitor.JSObject
14
+ import com.getcapacitor.Plugin
15
+ import com.getcapacitor.PluginCall
16
+ import com.getcapacitor.PluginMethod
17
+ import com.getcapacitor.annotation.CapacitorPlugin
18
+ import com.couchbase.lite.*
19
+ import com.getcapacitor.JSArray
20
+ import kotlinx.coroutines.DelicateCoroutinesApi
21
+ import kotlinx.coroutines.GlobalScope
22
+ import kotlinx.coroutines.launch
23
+ import org.json.JSONArray
24
+
25
+ import org.json.JSONException
26
+
27
+ @CapacitorPlugin(name = "CblIonicPlugin")
28
+ @Suppress("FunctionName")
29
+ class CblIonicPluginPlugin : Plugin() {
30
+
31
+ private val collectionChangeListeners: MutableMap<String, ListenerToken> = mutableMapOf()
32
+ private val collectionDocumentChangeListeners: MutableMap<String, ListenerToken> =
33
+ mutableMapOf()
34
+ private val queryChangeListeners: MutableMap<String, ListenerToken> = mutableMapOf()
35
+ private val replicationChangeListeners: MutableMap<String, ListenerToken> = mutableMapOf()
36
+
37
+ override fun load() {
38
+ CouchbaseLite.init(bridge.context, true)
39
+ }
40
+
41
+ @PluginMethod(returnType = PluginMethod.RETURN_CALLBACK)
42
+ @Throws(JSONException::class)
43
+ fun collection_AddChangeListener(call: PluginCall) {
44
+ val collectionDto = PluginHelper.getCollectionDtoFromCall(call)
45
+ if (collectionDto == null || collectionDto.isError) {
46
+ return
47
+ }
48
+ val (strToken, isTokenError) = PluginHelper.getStringFromCall(call, "changeListenerToken")
49
+ if (isTokenError || strToken.isNullOrEmpty()) {
50
+ return
51
+ }
52
+ GlobalScope.launch {
53
+ withContext(Dispatchers.IO) {
54
+ try {
55
+ val col = DatabaseManager.getCollection(
56
+ collectionDto.collectionName,
57
+ collectionDto.scopeName,
58
+ collectionDto.databaseName
59
+ )
60
+ call.setKeepAlive(true)
61
+ withContext(Dispatchers.Main) {
62
+ col?.let { collection ->
63
+ val listenerToken = collection.addChangeListener { change ->
64
+ val results = JSObject()
65
+ results.put("documentIDs", JSONArray(change.documentIDs))
66
+ call.resolve(results)
67
+ }
68
+ collectionChangeListeners.put(strToken, listenerToken)
69
+ }
70
+ }
71
+ } catch (e: Exception) {
72
+ call.reject("${e.message}")
73
+ }
74
+ }
75
+ }
76
+ }
77
+
78
+ @PluginMethod(returnType = PluginMethod.RETURN_CALLBACK)
79
+ @Throws(JSONException::class)
80
+ fun collection_AddDocumentChangeListener(call: PluginCall) {
81
+ val collectionDto = PluginHelper.getCollectionDtoFromCall(call)
82
+ if (collectionDto == null || collectionDto.isError) {
83
+ return
84
+ }
85
+ val (documentId, isDocumentIdError) = PluginHelper.getStringFromCall(call, "documentId")
86
+ val (strToken, isTokenError) = PluginHelper.getStringFromCall(call, "changeListenerToken")
87
+ if (isTokenError || strToken.isNullOrEmpty() || isDocumentIdError || documentId.isNullOrEmpty()) {
88
+ return
89
+ }
90
+ GlobalScope.launch {
91
+ withContext(Dispatchers.IO) {
92
+ try {
93
+ val col = DatabaseManager.getCollection(
94
+ collectionDto.collectionName,
95
+ collectionDto.scopeName,
96
+ collectionDto.databaseName
97
+ )
98
+ call.setKeepAlive(true)
99
+ withContext(Dispatchers.Main) {
100
+ col?.let { collection ->
101
+ val listenerToken =
102
+ collection.addDocumentChangeListener(documentId) { change ->
103
+ val results = JSObject()
104
+ results.put("documentID", change.documentID)
105
+ results.put("collectionName", change.collection.name)
106
+ results.put("scopeName", change.collection.scope.name)
107
+ results.put("databaseName", collectionDto.databaseName)
108
+ call.resolve(results)
109
+ }
110
+ collectionDocumentChangeListeners.put(strToken, listenerToken)
111
+ }
112
+ }
113
+ } catch (e: Exception) {
114
+ call.reject("${e.message}")
115
+ }
116
+ }
117
+ }
118
+ }
119
+
120
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
121
+ @Throws(JSONException::class)
122
+ fun collection_CreateCollection(call: PluginCall) {
123
+ val collectionDto = PluginHelper.getCollectionDtoFromCall(call)
124
+ if (collectionDto == null || collectionDto.isError) {
125
+ return
126
+ }
127
+ GlobalScope.launch {
128
+ withContext(Dispatchers.IO) {
129
+ try {
130
+ val collection = DatabaseManager.createCollection(
131
+ collectionDto.collectionName,
132
+ collectionDto.scopeName,
133
+ collectionDto.databaseName
134
+ )
135
+ collection?.let { col ->
136
+ val colResult = JSObject()
137
+ val scopeResult = JSObject()
138
+ colResult.put("name", col.name)
139
+ scopeResult.put("name", col.scope.name)
140
+ scopeResult.put("databaseName", collectionDto.databaseName)
141
+ colResult.put("scope", scopeResult)
142
+ return@withContext withContext(Dispatchers.Main) {
143
+ call.resolve(colResult)
144
+ }
145
+ }
146
+ return@withContext withContext(Dispatchers.Main) {
147
+ call.reject("Error: Collection not created")
148
+ }
149
+ } catch (e: Exception) {
150
+ return@withContext withContext(Dispatchers.Main) {
151
+ call.reject("${e.message}")
152
+ }
153
+ }
154
+ }
155
+ }
156
+ }
157
+
158
+ /**
159
+ * collection_CreateIndex - used to create an index in the collection
160
+ * date
161
+ *
162
+ * requires the following key/value pairs in the PluginCall:
163
+ *
164
+ * indexName - string name of the index
165
+ *
166
+ * type - string name of the type, options must be: "value" or "full-text"
167
+ *
168
+ * index - dictionary of the index items
169
+ *
170
+ * name - name of the database
171
+ *
172
+ * collectionName - name of the collection
173
+ *
174
+ * scopeName - name of the scope
175
+ *
176
+ * @param call PluginCall object from Ionic
177
+ * @return date of expiration if successful, error message if not
178
+ */
179
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
180
+ fun collection_CreateIndex(call: PluginCall) {
181
+ val collectionDto = PluginHelper.getCollectionDtoFromCall(call)
182
+ if (collectionDto == null || collectionDto.isError) {
183
+ return
184
+ }
185
+ val indexDto = PluginHelper.getIndexDtoFromCall(call)
186
+ if (indexDto == null || indexDto.isError) {
187
+ return
188
+ }
189
+ GlobalScope.launch {
190
+ withContext(Dispatchers.IO) {
191
+ try {
192
+ indexDto.indexType?.let { type ->
193
+ indexDto.indexName?.let { name ->
194
+ indexDto.indexItems?.let { items ->
195
+ var index: Index? = null
196
+ if (type == "value") {
197
+ val indexes = IndexHelper.makeValueIndexItems(items)
198
+ index = IndexBuilder.valueIndex(*indexes)
199
+ } else if (type == "full-text") {
200
+ val indexes = IndexHelper.makeFullTextIndexItems(items)
201
+ index = IndexBuilder.fullTextIndex(*indexes)
202
+ }
203
+ index?.let { idx ->
204
+ CollectionManager.createIndex(
205
+ name,
206
+ idx,
207
+ collectionDto.collectionName,
208
+ collectionDto.scopeName,
209
+ collectionDto.databaseName
210
+ )
211
+ return@withContext withContext(Dispatchers.Main) {
212
+ call.resolve()
213
+ }
214
+ }
215
+ }
216
+ }
217
+ }
218
+ return@withContext withContext(Dispatchers.Main) {
219
+ call.reject("Error: No index type, name, data, or items provided - could not create index")
220
+ }
221
+
222
+ } catch (e: Exception) {
223
+ return@withContext withContext(Dispatchers.Main) {
224
+ call.reject("${e.message}")
225
+ }
226
+ }
227
+ }
228
+ }
229
+ }
230
+
231
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
232
+ @Throws(JSONException::class)
233
+ fun collection_DeleteCollection(call: PluginCall) {
234
+ val collectionDto = PluginHelper.getCollectionDtoFromCall(call)
235
+ if (collectionDto == null || collectionDto.isError) {
236
+ return
237
+ }
238
+ GlobalScope.launch {
239
+ withContext(Dispatchers.IO) {
240
+ try {
241
+ DatabaseManager.deleteCollection(
242
+ collectionDto.collectionName,
243
+ collectionDto.scopeName,
244
+ collectionDto.databaseName
245
+ )
246
+ return@withContext withContext(Dispatchers.Main) {
247
+ call.resolve()
248
+ }
249
+ } catch (e: Exception) {
250
+ return@withContext withContext(Dispatchers.Main) {
251
+ call.reject("${e.message}")
252
+ }
253
+ }
254
+ }
255
+ }
256
+ }
257
+
258
+ /**
259
+ * collection_DeleteDocument - used to delete a document from the collection
260
+ *
261
+ * requires the following key/value pairs in the PluginCall:
262
+ *
263
+ * docId - the document id
264
+ *
265
+ * concurrencyControl - value of the concurrency control enum
266
+ *
267
+ * name - name of the database
268
+ *
269
+ * collectionName - name of the collection
270
+ *
271
+ * scopeName - name of the scope
272
+ *
273
+ * @param call PluginCall object from Ionic
274
+ * @return concurrency results if used if successful, error message if not
275
+ */
276
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
277
+ @Throws(JSONException::class)
278
+ fun collection_DeleteDocument(call: PluginCall) {
279
+ var docConcurrencyControl: ConcurrencyControl?
280
+ val collectionDto = PluginHelper.getCollectionDtoFromCall(call)
281
+ if (collectionDto == null || collectionDto.isError) {
282
+ return
283
+ }
284
+ val (documentId, isError) = PluginHelper.getStringFromCall(call, "docId")
285
+ if (isError || documentId.isNullOrEmpty()) {
286
+ return
287
+ }
288
+ GlobalScope.launch {
289
+ withContext(Dispatchers.IO) {
290
+ try {
291
+ var result: Boolean? = null
292
+ val concurrencyControlValue = call.getInt("concurrencyControl")
293
+ if (concurrencyControlValue != null) {
294
+ docConcurrencyControl =
295
+ PluginHelper.getConcurrencyControlFromInt(concurrencyControlValue)
296
+ docConcurrencyControl?.let { conControl ->
297
+ result = CollectionManager.deleteDocument(
298
+ documentId,
299
+ collectionDto.collectionName,
300
+ collectionDto.scopeName,
301
+ collectionDto.databaseName,
302
+ conControl
303
+ )
304
+
305
+ }
306
+ } else {
307
+ result = CollectionManager.deleteDocument(
308
+ documentId,
309
+ collectionDto.collectionName,
310
+ collectionDto.scopeName,
311
+ collectionDto.databaseName
312
+ )
313
+ }
314
+ return@withContext withContext(Dispatchers.Main) {
315
+ val results = JSObject()
316
+ results.put("concurrencyControlResult", result)
317
+ call.resolve(results)
318
+ }
319
+
320
+ } catch (e: Exception) {
321
+ return@withContext withContext(Dispatchers.Main) {
322
+ call.reject("${e.message}")
323
+ }
324
+ }
325
+ }
326
+ }
327
+ }
328
+
329
+ /**
330
+ * collection_DeleteIndex - used to delete an index in the collection
331
+ * date
332
+ *
333
+ * requires the following key/value pairs in the PluginCall:
334
+ *
335
+ * indexName - string name of the index
336
+ *
337
+ * name - name of the database
338
+ *
339
+ * collectionName - name of the collection
340
+ *
341
+ * scopeName - name of the scope
342
+ *
343
+ * @param call PluginCall object from Ionic
344
+ * @return date of expiration if successful, error message if not
345
+ */
346
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
347
+ @Throws(JSONException::class)
348
+ fun collection_DeleteIndex(call: PluginCall) {
349
+ val collectionDto = PluginHelper.getCollectionDtoFromCall(call)
350
+ if (collectionDto == null || collectionDto.isError) {
351
+ return
352
+ }
353
+ val (indexName, isIndexNameError) = PluginHelper.getStringFromCall(call, "indexName")
354
+ if (isIndexNameError || indexName.isNullOrEmpty()) {
355
+ return
356
+ }
357
+ GlobalScope.launch {
358
+ withContext(Dispatchers.IO) {
359
+ try {
360
+ CollectionManager.deleteIndex(
361
+ indexName,
362
+ collectionDto.collectionName,
363
+ collectionDto.scopeName,
364
+ collectionDto.databaseName
365
+ )
366
+ return@withContext withContext(Dispatchers.Main) {
367
+ call.resolve()
368
+ }
369
+ } catch (e: Exception) {
370
+ return@withContext withContext(Dispatchers.Main) {
371
+ call.reject("${e.message}")
372
+ }
373
+ }
374
+ }
375
+ }
376
+ }
377
+
378
+ /**
379
+ * collection_GetBlobContent - used to get blob content in an document
380
+ *
381
+ * requires the following key/value pairs in the PluginCall:
382
+ *
383
+ * documentId - the document id
384
+ *
385
+ * key - the key that the blob is stored under in the document
386
+ *
387
+ * name - name of the database
388
+ *
389
+ * collectionName - name of the collection
390
+ *
391
+ * scopeName - name of the scope
392
+ *
393
+ * @param call PluginCall object from Ionic
394
+ * @return nothing if successful, error message if not
395
+ */
396
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
397
+ @Throws(JSONException::class)
398
+ fun collection_GetBlobContent(call: PluginCall) {
399
+ val collectionDto = PluginHelper.getCollectionDtoFromCall(call)
400
+ if (collectionDto == null || collectionDto.isError) {
401
+ return
402
+ }
403
+ val (key, isKeyError) = PluginHelper.getStringFromCall(call, "key")
404
+ if (isKeyError || key.isNullOrEmpty()) {
405
+ return
406
+ }
407
+ val (documentId, isDocumentIdError) = PluginHelper.getStringFromCall(call, "documentId")
408
+ if (isDocumentIdError || documentId.isNullOrEmpty()) {
409
+ return
410
+ }
411
+ GlobalScope.launch {
412
+ withContext(Dispatchers.IO) {
413
+ try {
414
+ val blobData = CollectionManager.getBlobContent(
415
+ key,
416
+ documentId,
417
+ collectionDto.collectionName,
418
+ collectionDto.scopeName,
419
+ collectionDto.databaseName
420
+ )
421
+ return@withContext withContext(Dispatchers.Main) {
422
+ val results = JSObject()
423
+ if (blobData == null) {
424
+ results.put("data", emptyArray<Byte>())
425
+ } else {
426
+ results.put("data", JSONArray(blobData))
427
+ }
428
+ call.resolve(results)
429
+ }
430
+ } catch (e: Exception) {
431
+ return@withContext withContext(Dispatchers.Main) {
432
+ call.reject("${e.message}")
433
+ }
434
+ }
435
+ }
436
+ }
437
+ }
438
+
439
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
440
+ @Throws(JSONException::class)
441
+ fun collection_GetDefault(call: PluginCall) {
442
+ val (name, isNameError) = PluginHelper.getStringFromCall(call, "name")
443
+ if (isNameError) {
444
+ return
445
+ }
446
+ GlobalScope.launch {
447
+ withContext(Dispatchers.IO) {
448
+ try {
449
+ name?.let { databaseName ->
450
+ val collection = DatabaseManager.defaultCollection(databaseName)
451
+ collection?.let {
452
+ val result = JSObject()
453
+ val scopeResult = JSObject()
454
+ result.put("name", collection.name)
455
+ scopeResult.put("name", collection.scope.name)
456
+ scopeResult.put("databaseName", name)
457
+ result.put("scope", scopeResult)
458
+ return@withContext withContext(Dispatchers.Main) {
459
+ call.resolve(result)
460
+ }
461
+ }
462
+ return@withContext withContext(Dispatchers.Main) {
463
+ //should never get to this code hopefully
464
+ call.reject("Error: No default collection found")
465
+ }
466
+ }
467
+ } catch (e: Exception) {
468
+ return@withContext withContext(Dispatchers.Main) {
469
+ call.reject("${e.message}")
470
+ }
471
+ }
472
+ }
473
+ }
474
+ }
475
+
476
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
477
+ fun collection_GetCollections(call: PluginCall) {
478
+ val scopeDto = PluginHelper.getScopeDtoFromCall(call)
479
+ if (scopeDto == null || scopeDto.isError) {
480
+ return
481
+ }
482
+ GlobalScope.launch {
483
+ withContext(Dispatchers.IO) {
484
+ try {
485
+ val results = JSObject()
486
+ val collectionArray = JSArray()
487
+ val collections =
488
+ DatabaseManager.getCollections(scopeDto.scopeName, scopeDto.databaseName)
489
+ collections?.let { cols ->
490
+ for (collection in cols) {
491
+ val collectionObject = JSObject()
492
+ collectionObject.put("name", collection.name)
493
+ val scopeObject = JSObject()
494
+ scopeObject.put("name", collection.scope.name)
495
+ scopeObject.put("databaseName", scopeDto.databaseName)
496
+ collectionObject.put("scope", scopeObject)
497
+ collectionArray.put(collectionObject)
498
+ results.put("collections", collectionArray)
499
+ }
500
+ }
501
+ return@withContext withContext(Dispatchers.Main) {
502
+ results.put("collections", collectionArray)
503
+ call.resolve(results)
504
+ }
505
+ } catch (e: Exception) {
506
+ return@withContext withContext(Dispatchers.Main) {
507
+ call.reject("${e.message}")
508
+ }
509
+ }
510
+ }
511
+ }
512
+ }
513
+
514
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
515
+ fun collection_GetCollection(call: PluginCall) {
516
+ val collectionDto = PluginHelper.getCollectionDtoFromCall(call)
517
+ if (collectionDto == null || collectionDto.isError) {
518
+ return
519
+ }
520
+ GlobalScope.launch {
521
+ withContext(Dispatchers.IO) {
522
+ try {
523
+ val collection = DatabaseManager.getCollection(
524
+ collectionDto.collectionName,
525
+ collectionDto.scopeName,
526
+ collectionDto.databaseName
527
+ )
528
+ collection?.let { col ->
529
+ val colResult = JSObject()
530
+ val scopeResult = JSObject()
531
+ colResult.put("name", col.name)
532
+ scopeResult.put("name", col.scope.name)
533
+ scopeResult.put("databaseName", collectionDto.databaseName)
534
+ colResult.put("scope", scopeResult)
535
+ return@withContext withContext(Dispatchers.Main) {
536
+ call.resolve(colResult)
537
+ }
538
+ }
539
+ return@withContext withContext(Dispatchers.Main) {
540
+ call.reject("Error: couldn't resolve collection")
541
+ }
542
+ } catch (e: Exception) {
543
+ return@withContext withContext(Dispatchers.Main) {
544
+ call.reject("${e.message}")
545
+ }
546
+ }
547
+ }
548
+ }
549
+ }
550
+
551
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
552
+ @Throws(JSONException::class)
553
+ fun collection_GetCount(call: PluginCall) {
554
+ val collectionDto = PluginHelper.getCollectionDtoFromCall(call)
555
+ if (collectionDto == null || collectionDto.isError) {
556
+ return
557
+ }
558
+ GlobalScope.launch {
559
+ withContext(Dispatchers.IO) {
560
+ try {
561
+ val count = CollectionManager.documentsCount(
562
+ collectionDto.collectionName,
563
+ collectionDto.scopeName,
564
+ collectionDto.databaseName
565
+ )
566
+ val results = JSObject()
567
+ results.put("count", count)
568
+ return@withContext withContext(Dispatchers.Main) {
569
+ call.resolve(results)
570
+ }
571
+ } catch (e: Exception) {
572
+ return@withContext withContext(Dispatchers.Main) {
573
+ call.reject("${e.message}")
574
+ }
575
+ }
576
+ }
577
+ }
578
+ }
579
+
580
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
581
+ fun collection_GetDocument(call: PluginCall) {
582
+ val collectionDto = PluginHelper.getCollectionDtoFromCall(call)
583
+ if (collectionDto == null || collectionDto.isError) {
584
+ return
585
+ }
586
+ val (documentId, isError) = PluginHelper.getStringFromCall(call, "docId")
587
+ if (isError || documentId.isNullOrEmpty()) {
588
+ return
589
+ }
590
+ GlobalScope.launch {
591
+ withContext(Dispatchers.IO) {
592
+ try {
593
+ val document = CollectionManager.getDocument(
594
+ documentId,
595
+ collectionDto.collectionName,
596
+ collectionDto.scopeName,
597
+ collectionDto.databaseName
598
+ )
599
+ document?.let {
600
+ val results = PluginHelper.documentToMap(it)
601
+ return@withContext withContext(Dispatchers.Main) {
602
+ call.resolve(results)
603
+ }
604
+ }
605
+ return@withContext withContext(Dispatchers.Main) {
606
+ val results = JSObject()
607
+ call.resolve(results)
608
+ }
609
+ } catch (e: Exception) {
610
+ return@withContext withContext(Dispatchers.Main) {
611
+ call.reject("${e.message}")
612
+ }
613
+ }
614
+ }
615
+ }
616
+ }
617
+
618
+ /**
619
+ * collection_GetDocumentExpiration - used to get the document expiration
620
+ * date
621
+ *
622
+ * requires the following key/value pairs in the PluginCall:
623
+ *
624
+ * docId - the document id to set the expiration on
625
+ *
626
+ * name - name of the database
627
+ *
628
+ * collectionName - name of the collection
629
+ *
630
+ * scopeName - name of the scope
631
+ *
632
+ * @param call PluginCall object from Ionic
633
+ * @return date of expiration if successful, error message if not
634
+ */
635
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
636
+ fun collection_GetDocumentExpiration(call: PluginCall) {
637
+ val collectionDto = PluginHelper.getCollectionDtoFromCall(call)
638
+ if (collectionDto == null || collectionDto.isError) {
639
+ return
640
+ }
641
+ val (documentId, isDocumentIdError) = PluginHelper.getStringFromCall(call, "docId")
642
+ if (isDocumentIdError || documentId.isNullOrEmpty()) {
643
+ return
644
+ }
645
+ GlobalScope.launch {
646
+ withContext(Dispatchers.IO) {
647
+ try {
648
+ val expiration = CollectionManager.getDocumentExpiration(
649
+ documentId,
650
+ collectionDto.collectionName,
651
+ collectionDto.scopeName,
652
+ collectionDto.databaseName
653
+ )
654
+ return@withContext withContext(Dispatchers.Main) {
655
+ val results = JSObject()
656
+ results.put("date", expiration)
657
+ call.resolve(results)
658
+ }
659
+ } catch (e: Exception) {
660
+ return@withContext withContext(Dispatchers.Main) {
661
+ call.reject("${e.message}")
662
+ }
663
+ }
664
+ }
665
+ }
666
+ }
667
+
668
+ /**
669
+ * collection_GetIndexes - used to get an array (string) of indexes
670
+ * in the collection
671
+ *
672
+ * requires the following key/value pairs in the PluginCall:
673
+ *
674
+ * name - name of the database
675
+ *
676
+ * collectionName - name of the collection
677
+ *
678
+ * scopeName - name of the scope
679
+ *
680
+ * @param call PluginCall object from Ionic
681
+ * @return date of expiration if successful, error message if not
682
+ */
683
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
684
+ @Throws(JSONException::class)
685
+ fun collection_GetIndexes(call: PluginCall) {
686
+ val collectionDto = PluginHelper.getCollectionDtoFromCall(call)
687
+ if (collectionDto == null || collectionDto.isError) {
688
+ return
689
+ }
690
+ GlobalScope.launch {
691
+ withContext(Dispatchers.IO) {
692
+ try {
693
+ val indexes = CollectionManager.getIndexes(
694
+ collectionDto.collectionName,
695
+ collectionDto.scopeName,
696
+ collectionDto.databaseName
697
+ )
698
+ val results = JSObject()
699
+ val arrayResults = JSArray(indexes)
700
+ results.put("indexes", arrayResults)
701
+ call.resolve(results)
702
+ } catch (e: Exception) {
703
+ call.reject("${e.message}")
704
+ }
705
+ }
706
+ }
707
+ }
708
+
709
+ /**
710
+ * collection_PurgeDocument - used to purge a document from the collection
711
+ *
712
+ * requires the following key/value pairs in the PluginCall:
713
+ *
714
+ * docId - the document id
715
+ *
716
+ * name - name of the database
717
+ *
718
+ * collectionName - name of the collection
719
+ *
720
+ * scopeName - name of the scope
721
+ *
722
+ * @param call PluginCall object from Ionic
723
+ * @return nothing if successful, error message if not
724
+ */
725
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
726
+ @Throws(JSONException::class)
727
+ fun collection_PurgeDocument(call: PluginCall) {
728
+ val collectionDto = PluginHelper.getCollectionDtoFromCall(call)
729
+ if (collectionDto == null || collectionDto.isError) {
730
+ return
731
+ }
732
+ val (documentId, isError) = PluginHelper.getStringFromCall(call, "docId")
733
+ if (isError || documentId.isNullOrEmpty()) {
734
+ return
735
+ }
736
+ GlobalScope.launch {
737
+ withContext(Dispatchers.IO) {
738
+ try {
739
+ CollectionManager.purgeDocument(
740
+ documentId,
741
+ collectionDto.collectionName,
742
+ collectionDto.scopeName,
743
+ collectionDto.databaseName
744
+ )
745
+ return@withContext withContext(Dispatchers.Main) {
746
+ call.resolve()
747
+ }
748
+ } catch (e: Exception) {
749
+ return@withContext withContext(Dispatchers.Main) {
750
+ call.reject("${e.message}")
751
+ }
752
+ }
753
+ }
754
+ }
755
+ }
756
+
757
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
758
+ @Throws(JSONException::class)
759
+ fun collection_RemoveChangeListener(call: PluginCall) {
760
+ val collectionDto = PluginHelper.getCollectionDtoFromCall(call)
761
+ if (collectionDto == null || collectionDto.isError) {
762
+ return
763
+ }
764
+ val (strToken, isTokenError) = PluginHelper.getStringFromCall(call, "changeListenerToken")
765
+ if (isTokenError || strToken.isNullOrEmpty()) {
766
+ return
767
+ }
768
+ GlobalScope.launch {
769
+ withContext(Dispatchers.IO) {
770
+ try {
771
+ val token = collectionChangeListeners[strToken]
772
+ token?.remove()
773
+ collectionChangeListeners.remove(strToken)
774
+ return@withContext withContext(Dispatchers.Main) {
775
+ call.resolve()
776
+ }
777
+ } catch (e: Exception) {
778
+ call.reject("${e.message}")
779
+ }
780
+ }
781
+ }
782
+ }
783
+
784
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
785
+ @Throws(JSONException::class)
786
+ fun collection_RemoveDocumentChangeListener(call: PluginCall) {
787
+ val collectionDto = PluginHelper.getCollectionDtoFromCall(call)
788
+ if (collectionDto == null || collectionDto.isError) {
789
+ return
790
+ }
791
+ val (strToken, isTokenError) = PluginHelper.getStringFromCall(call, "changeListenerToken")
792
+ if (isTokenError || strToken.isNullOrEmpty()) {
793
+ return
794
+ }
795
+ GlobalScope.launch {
796
+ withContext(Dispatchers.IO) {
797
+ try {
798
+ val token = collectionDocumentChangeListeners[strToken]
799
+ token?.remove()
800
+ collectionDocumentChangeListeners.remove(strToken)
801
+ return@withContext withContext(Dispatchers.Main) {
802
+ call.resolve()
803
+ }
804
+ } catch (e: Exception) {
805
+ call.reject("${e.message}")
806
+ }
807
+ }
808
+ }
809
+
810
+ }
811
+
812
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
813
+ fun collection_Save(call: PluginCall) {
814
+ var docConcurrencyControl: ConcurrencyControl? = null
815
+ val collectionDto = PluginHelper.getCollectionDtoFromCall(call)
816
+ val documentDto = PluginHelper.getDocumentDtoFromCall(call)
817
+ if (collectionDto == null || collectionDto.isError || documentDto == null || documentDto.isError) {
818
+ return
819
+ }
820
+ val documentId = call.getString("id")
821
+ val docId = if (documentId.isNullOrEmpty()) {
822
+ ""
823
+ } else {
824
+ documentId
825
+ }
826
+ GlobalScope.launch {
827
+ withContext(Dispatchers.IO) {
828
+ try {
829
+ val concurrencyControlValue = call.getInt("concurrencyControl")
830
+ concurrencyControlValue?.let {
831
+ docConcurrencyControl = PluginHelper.getConcurrencyControlFromInt(it)
832
+ }
833
+ val blobs = PluginHelper.getBlobsFromString(documentDto.blobs)
834
+ val result = CollectionManager.saveDocument(
835
+ docId,
836
+ documentDto.document,
837
+ blobs,
838
+ docConcurrencyControl,
839
+ collectionDto.collectionName,
840
+ collectionDto.scopeName,
841
+ collectionDto.databaseName
842
+ )
843
+ val results = JSObject()
844
+ results.put("_id", result._id)
845
+ results.put("_revId", result._revId)
846
+ results.put("_sequence", result._sequence)
847
+ results.put("concurrencyResult", result._concurrencyControl)
848
+ return@withContext withContext(Dispatchers.Main) {
849
+ call.resolve(results)
850
+ }
851
+
852
+ } catch (e: Exception) {
853
+ return@withContext withContext(Dispatchers.Main) {
854
+ call.reject("${e.message}")
855
+ }
856
+ }
857
+ }
858
+ }
859
+ }
860
+
861
+ /**
862
+ * collection_SetDocumentExpiration - used to set the document expiration
863
+ * passed in from Ionic
864
+ *
865
+ * requires the following key/value pairs in the PluginCall:
866
+ *
867
+ * docId - the document id
868
+ *
869
+ * expiration - the expiration date to set on the document - must
870
+ * be in a string in the format `yyyy-MM-dd HH:mm:ss`
871
+ *
872
+ * name - name of the database
873
+ *
874
+ * collectionName - name of the collection
875
+ *
876
+ * scopeName - name of the scope
877
+ *
878
+ * @param call PluginCall object from Ionic
879
+ * @return nothing if successful, error message if not
880
+ */
881
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
882
+ @Throws(JSONException::class)
883
+ fun collection_SetDocumentExpiration(call: PluginCall) {
884
+ val collectionDto = PluginHelper.getCollectionDtoFromCall(call)
885
+ if (collectionDto == null || collectionDto.isError) {
886
+ return
887
+ }
888
+ val (expiration, isExpirationError) = PluginHelper.getStringFromCall(call, "expiration")
889
+ if (isExpirationError || expiration.isNullOrEmpty()) {
890
+ return
891
+ }
892
+ val (documentId, isDocumentIdError) = PluginHelper.getStringFromCall(call, "docId")
893
+ if (isDocumentIdError || documentId.isNullOrEmpty()) {
894
+ return
895
+ }
896
+ GlobalScope.launch {
897
+ withContext(Dispatchers.IO) {
898
+ try {
899
+ CollectionManager.setDocumentExpiration(
900
+ documentId,
901
+ expiration,
902
+ collectionDto.collectionName,
903
+ collectionDto.scopeName,
904
+ collectionDto.databaseName
905
+ )
906
+ return@withContext withContext(Dispatchers.Main) {
907
+ call.resolve()
908
+ }
909
+ } catch (e: Exception) {
910
+ return@withContext withContext(Dispatchers.Main) {
911
+ call.reject("${e.message}")
912
+ }
913
+ }
914
+ }
915
+ }
916
+ }
917
+
918
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
919
+ fun database_Close(call: PluginCall) {
920
+ val (name, isError) = PluginHelper.getStringFromCall(call, "name")
921
+ if (isError) {
922
+ return
923
+ }
924
+ GlobalScope.launch {
925
+ name?.let { databaseName ->
926
+ withContext(Dispatchers.IO) {
927
+ try {
928
+ DatabaseManager.closeDatabase(databaseName)
929
+ return@withContext withContext(Dispatchers.Main) {
930
+ call.resolve()
931
+ }
932
+ } catch (e: Exception) {
933
+ return@withContext withContext(Dispatchers.Main) {
934
+ call.reject("${e.message}")
935
+ }
936
+ }
937
+ }
938
+ }
939
+ }
940
+ }
941
+
942
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
943
+ fun database_ChangeEncryptionKey(call: PluginCall) {
944
+ val (name, isError) = PluginHelper.getStringFromCall(call, "name")
945
+ if (isError) {
946
+ return
947
+ }
948
+ val newKey = call.getString("newKey")
949
+ GlobalScope.launch {
950
+ withContext(Dispatchers.IO) {
951
+ name?.let { databaseName ->
952
+ try {
953
+ DatabaseManager.changeEncryptionKey(databaseName, newKey)
954
+ return@withContext withContext(Dispatchers.Main) {
955
+ call.resolve()
956
+ }
957
+ } catch (e: Exception) {
958
+ return@withContext withContext(Dispatchers.Main) {
959
+ call.reject("${e.message}")
960
+ }
961
+ }
962
+ }
963
+ }
964
+ }
965
+ }
966
+
967
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
968
+ fun database_Copy(call: PluginCall) {
969
+ val (newName, isNewNameError) = PluginHelper.getStringFromCall(call, "newName")
970
+ val (path, isPathError) = PluginHelper.getStringFromCall(call, "path")
971
+ if (isNewNameError || isPathError) {
972
+ return
973
+ }
974
+ val config = call.getObject("config")
975
+ ?: return call.reject("No database configuration provided")
976
+ GlobalScope.launch {
977
+ withContext(Dispatchers.IO) {
978
+ try {
979
+ path?.let { filePath ->
980
+ newName?.let { newDatabaseName ->
981
+ DatabaseManager.copy(filePath, newDatabaseName, config, bridge.context)
982
+ return@withContext withContext(Dispatchers.Main) {
983
+ call.resolve()
984
+ }
985
+ }
986
+ }
987
+ return@withContext withContext(Dispatchers.Main) {
988
+ call.reject("Error: no file path returned for copy")
989
+ }
990
+ } catch (e: Exception) {
991
+ return@withContext withContext(Dispatchers.Main) {
992
+ call.reject("${e.message}")
993
+ }
994
+ }
995
+ }
996
+ }
997
+ }
998
+
999
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
1000
+ fun database_Delete(call: PluginCall) {
1001
+ val (name, isError) = PluginHelper.getStringFromCall(call, "name")
1002
+ if (isError) {
1003
+ return
1004
+ }
1005
+ GlobalScope.launch {
1006
+ withContext(Dispatchers.IO) {
1007
+ name?.let { databaseName ->
1008
+ try {
1009
+ DatabaseManager.delete(databaseName)
1010
+ return@withContext withContext(Dispatchers.Main) {
1011
+ call.resolve()
1012
+ }
1013
+ } catch (e: Exception) {
1014
+ return@withContext withContext(Dispatchers.Main) {
1015
+ call.reject("${e.message}")
1016
+ }
1017
+ }
1018
+ }
1019
+ }
1020
+ }
1021
+ }
1022
+
1023
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
1024
+ @Throws(JSONException::class)
1025
+ fun database_Exists(call: PluginCall) {
1026
+ val (name, isNameError) = PluginHelper.getStringFromCall(call, "existsName")
1027
+ val (path, isPathError) = PluginHelper.getStringFromCall(call, "directory")
1028
+ if (isNameError || isPathError) {
1029
+ return
1030
+ }
1031
+ try {
1032
+ name?.let { databaseName ->
1033
+ path?.let { directory ->
1034
+ val exists = DatabaseManager.exists(databaseName, directory)
1035
+ val results = JSObject()
1036
+ results.put("exists", exists)
1037
+ call.resolve(results)
1038
+ return
1039
+ }
1040
+ }
1041
+ call.reject("Error: Unknown error checking for database existence")
1042
+ return
1043
+ } catch (e: Exception) {
1044
+ call.reject("${e.message}")
1045
+ }
1046
+ }
1047
+
1048
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
1049
+ fun database_GetPath(call: PluginCall) {
1050
+ val (name, isError) = PluginHelper.getStringFromCall(call, "name")
1051
+ if (isError) {
1052
+ return
1053
+ }
1054
+ GlobalScope.launch {
1055
+ withContext(Dispatchers.IO) {
1056
+ name?.let { databaseName ->
1057
+ try {
1058
+ val path = DatabaseManager.getPath(databaseName)
1059
+ withContext(Dispatchers.Main) {
1060
+ if (path.isEmpty()) {
1061
+ call.reject("Error: Path is empty")
1062
+ } else {
1063
+ val results = JSObject()
1064
+ results.put("path", path)
1065
+ call.resolve(results)
1066
+ }
1067
+ }
1068
+ } catch (e: Exception) {
1069
+ return@withContext withContext(Dispatchers.Main) {
1070
+ call.reject("${e.message}")
1071
+ }
1072
+ }
1073
+ }
1074
+ }
1075
+ }
1076
+ }
1077
+
1078
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
1079
+ fun database_Open(call: PluginCall) {
1080
+ val (name, isError) = PluginHelper.getStringFromCall(call, "name")
1081
+ if (isError) {
1082
+ return
1083
+ }
1084
+ val config = call.getObject("config")
1085
+ if (config == null) {
1086
+ call.reject("No database configuration provided")
1087
+ return
1088
+ }
1089
+ GlobalScope.launch {
1090
+ name?.let { databaseName ->
1091
+ withContext(Dispatchers.IO) {
1092
+ try {
1093
+ DatabaseManager.openDatabase(databaseName, config, bridge.context)
1094
+ return@withContext withContext(Dispatchers.Main) {
1095
+ call.resolve()
1096
+ }
1097
+ } catch (e: Exception) {
1098
+ return@withContext withContext(Dispatchers.Main) {
1099
+ call.reject("${e.message}")
1100
+ }
1101
+ }
1102
+ }
1103
+ }
1104
+ }
1105
+ }
1106
+
1107
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
1108
+ fun database_PerformMaintenance(call: PluginCall) {
1109
+ GlobalScope.launch {
1110
+ withContext(Dispatchers.IO) {
1111
+ val (name, isNameError) = PluginHelper.getStringFromCall(call, "name")
1112
+ val (maintenanceTypeInt, isMaintenanceTypeError) = PluginHelper.getIntFromCall(
1113
+ call,
1114
+ "maintenanceType"
1115
+ )
1116
+ if (isNameError || isMaintenanceTypeError) {
1117
+ return@withContext
1118
+ }
1119
+ try {
1120
+ name?.let { databaseName ->
1121
+ val maintenanceType = when (maintenanceTypeInt) {
1122
+ 0 -> MaintenanceType.COMPACT
1123
+ 1 -> MaintenanceType.REINDEX
1124
+ 2 -> MaintenanceType.INTEGRITY_CHECK
1125
+ 3 -> MaintenanceType.OPTIMIZE
1126
+ else -> MaintenanceType.FULL_OPTIMIZE
1127
+ }
1128
+ DatabaseManager.performMaintenance(databaseName, maintenanceType)
1129
+ return@withContext withContext(Dispatchers.Main) {
1130
+ call.resolve()
1131
+ }
1132
+ }
1133
+ return@withContext withContext(Dispatchers.Main) {
1134
+ call.reject("Error: No database name provided")
1135
+ }
1136
+
1137
+ } catch (e: Exception) {
1138
+ return@withContext withContext(Dispatchers.Main) {
1139
+ call.reject("${e.message}")
1140
+ }
1141
+ }
1142
+ }
1143
+ }
1144
+ }
1145
+
1146
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
1147
+ @Throws(JSONException::class)
1148
+ fun database_SetLogLevel(call: PluginCall) {
1149
+ GlobalScope.launch {
1150
+ withContext(Dispatchers.IO) {
1151
+ val (domain, isDomainError) = PluginHelper.getStringFromCall(call, "domain")
1152
+ val (levelInt, isLevelError) = PluginHelper.getIntFromCall(
1153
+ call,
1154
+ "logLevel"
1155
+ )
1156
+ if (isDomainError || isLevelError) {
1157
+ return@withContext
1158
+ }
1159
+ try {
1160
+ domain?.let { logDomain ->
1161
+ levelInt?.let { logLevel ->
1162
+ LoggingManager.setLogLevel(logDomain, logLevel)
1163
+ return@withContext withContext(Dispatchers.Main) {
1164
+ call.resolve()
1165
+ }
1166
+ }
1167
+ }
1168
+ return@withContext withContext(Dispatchers.Main) {
1169
+ call.reject("Error: No domain or log level provided")
1170
+ }
1171
+ } catch (e: Exception) {
1172
+ return@withContext withContext(Dispatchers.Main) {
1173
+ call.reject("${e.message}")
1174
+ }
1175
+ }
1176
+ }
1177
+ }
1178
+ }
1179
+
1180
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
1181
+ @Throws(JSONException::class)
1182
+ fun database_SetFileLoggingConfig(call: PluginCall) {
1183
+ val (databaseName, isDatabaseNameError) = PluginHelper.getStringFromCall(call, "name")
1184
+ val config = call.getObject("config")
1185
+ if (isDatabaseNameError || databaseName.isNullOrEmpty() || config == null) {
1186
+ return
1187
+ }
1188
+ GlobalScope.launch {
1189
+ withContext(Dispatchers.IO) {
1190
+ try {
1191
+ //if we don't have a valid directory, fail
1192
+ val directory = config.getString("directory")
1193
+ if (directory.isNullOrEmpty()) {
1194
+ return@withContext withContext(Dispatchers.Main) {
1195
+ call.reject("Error: No directory provided")
1196
+ }
1197
+ }
1198
+ val fileConfig = LogFileConfiguration(directory)
1199
+ val maxRotateCount = config.optInt("maxRotateCount", -1)
1200
+ if (maxRotateCount > 0) {
1201
+ fileConfig.maxRotateCount = maxRotateCount
1202
+ }
1203
+ val maxSize = config.optLong("maxSize", -1)
1204
+ if (maxSize > 0) {
1205
+ fileConfig.maxSize = maxSize
1206
+ }
1207
+ val usePlainText = config.optBoolean("usePlainText", false)
1208
+ fileConfig.setUsePlaintext(usePlainText)
1209
+ val levelValue = config.optInt("level", 0)
1210
+ val logLevel = LoggingManager.getLogLevel(levelValue)
1211
+ Database.log.file.let {
1212
+ it.config = fileConfig
1213
+ it.level = logLevel
1214
+ }
1215
+ return@withContext withContext(Dispatchers.Main) {
1216
+ call.resolve()
1217
+ }
1218
+ } catch (e: Exception) {
1219
+ call.reject("${e.message}")
1220
+ }
1221
+ }
1222
+ }
1223
+ }
1224
+
1225
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
1226
+ @Throws(JSONException::class)
1227
+ fun file_GetDefaultPath(call: PluginCall) {
1228
+ val defaultPath = FileSystemHelper.fileGetDefaultPath(bridge.context)
1229
+ val results = JSObject()
1230
+ results.put("path", defaultPath)
1231
+ call.resolve(results)
1232
+ }
1233
+
1234
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
1235
+ @Throws(JSONException::class)
1236
+ fun file_GetFileNamesInDirectory(call: PluginCall) {
1237
+ val (directoryPath, isDirectoryPathError) = PluginHelper.getStringFromCall(call, "path")
1238
+ if (isDirectoryPathError || directoryPath.isNullOrEmpty()) {
1239
+ return
1240
+ }
1241
+ GlobalScope.launch {
1242
+ withContext(Dispatchers.IO) {
1243
+ try {
1244
+ val fileNames = FileSystemHelper.listFilesAndDirectories(directoryPath)
1245
+ val jsonObject = JSObject()
1246
+ val jsonArray = JSArray()
1247
+ fileNames.forEach {
1248
+ jsonArray.put(it)
1249
+ }
1250
+ jsonObject.put("files", jsonArray)
1251
+ call.resolve(jsonObject)
1252
+ } catch (e: Exception) {
1253
+ call.reject("${e.message}")
1254
+ }
1255
+ }
1256
+ }
1257
+ }
1258
+
1259
+ @PluginMethod(returnType = PluginMethod.RETURN_CALLBACK)
1260
+ @Throws(JSONException::class)
1261
+ fun query_AddChangeListener(call: PluginCall) {
1262
+ val (databaseName, isDatabaseNameError) = PluginHelper.getStringFromCall(call, "name")
1263
+ val (token, isTokenError) = PluginHelper.getStringFromCall(call, "changeListenerToken")
1264
+ val (strQuery, isQueryError) = PluginHelper.getStringFromCall(call, "query")
1265
+ if (isDatabaseNameError || databaseName.isNullOrEmpty()
1266
+ || isQueryError || strQuery.isNullOrEmpty()
1267
+ || isTokenError || token.isNullOrEmpty()
1268
+ ) {
1269
+ return
1270
+ }
1271
+ GlobalScope.launch {
1272
+ withContext(Dispatchers.IO) {
1273
+ try {
1274
+ var queryParameters: Parameters? = null
1275
+ val parametersObj = call.getObject("parameters")
1276
+ if (parametersObj != null && parametersObj.length() > 0) {
1277
+ queryParameters = QueryHelper.getQueryParameters(parametersObj)
1278
+ }
1279
+ val db = DatabaseManager.getDatabase(databaseName)
1280
+ db?.let { database ->
1281
+
1282
+ //keep the call alive for callback
1283
+ call.setKeepAlive(true)
1284
+
1285
+ val query = database.createQuery(strQuery)
1286
+ queryParameters?.let {
1287
+ query.parameters = it
1288
+ }
1289
+ withContext(Dispatchers.Main) {
1290
+ val listener = query.addChangeListener { change ->
1291
+ change.error?.let { error ->
1292
+ call.reject("${error.message}")
1293
+ }
1294
+ change.results?.let { resultSet ->
1295
+ val results = JSObject()
1296
+ val jsonResults = resultSet.map { it.toJSON() }
1297
+ val stringResults = jsonResults.joinToString(",")
1298
+ val returnResults = "[$stringResults]"
1299
+ results.put("data", returnResults)
1300
+
1301
+ call.resolve(results)
1302
+ }
1303
+ }
1304
+ queryChangeListeners.put(token, listener)
1305
+ }
1306
+ }
1307
+ } catch (e: Exception) {
1308
+ call.reject("${e.message}")
1309
+ }
1310
+ }
1311
+ }
1312
+ }
1313
+
1314
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
1315
+ @Throws(JSONException::class)
1316
+ fun query_Execute(call: PluginCall) {
1317
+ val (databaseName, isDatabaseNameError) = PluginHelper.getStringFromCall(call, "name")
1318
+ val (query, isQueryError) = PluginHelper.getStringFromCall(call, "query")
1319
+ if (isDatabaseNameError || databaseName.isNullOrEmpty() || isQueryError || query.isNullOrEmpty()) {
1320
+ return
1321
+ }
1322
+ GlobalScope.launch {
1323
+ withContext(Dispatchers.IO) {
1324
+ try {
1325
+ var queryParameters: Parameters? = null
1326
+ val parametersObj = call.getObject("parameters")
1327
+ if (parametersObj != null && parametersObj.length() > 0) {
1328
+ queryParameters = QueryHelper.getQueryParameters(parametersObj)
1329
+ }
1330
+ val results = DatabaseManager.executeQuery(query, databaseName, queryParameters)
1331
+ return@withContext withContext(Dispatchers.Main) {
1332
+ val resultsObj = JSObject()
1333
+ resultsObj.put("data", results)
1334
+ call.resolve(resultsObj)
1335
+ }
1336
+ } catch (e: Exception) {
1337
+ call.reject("${e.message}")
1338
+ }
1339
+ }
1340
+ }
1341
+ }
1342
+
1343
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
1344
+ @Throws(JSONException::class)
1345
+ fun query_Explain(call: PluginCall) {
1346
+ val (databaseName, isDatabaseNameError) = PluginHelper.getStringFromCall(call, "name")
1347
+ val (query, isQueryError) = PluginHelper.getStringFromCall(call, "query")
1348
+ if (isDatabaseNameError || databaseName.isNullOrEmpty() || isQueryError || query.isNullOrEmpty()) {
1349
+ return
1350
+ }
1351
+ GlobalScope.launch {
1352
+ withContext(Dispatchers.IO) {
1353
+ try {
1354
+ var queryParameters: Parameters? = null
1355
+ val parametersObj = call.getObject("parameters")
1356
+ if (parametersObj != null && parametersObj.length() > 0) {
1357
+ queryParameters = QueryHelper.getQueryParameters(parametersObj)
1358
+ }
1359
+ val results = DatabaseManager.explainQuery(query, databaseName, queryParameters)
1360
+ return@withContext withContext(Dispatchers.Main) {
1361
+ val resultsObj = JSObject()
1362
+ resultsObj.put("data", results)
1363
+ call.resolve(resultsObj)
1364
+ }
1365
+ } catch (e: Exception) {
1366
+ call.reject("${e.message}")
1367
+ }
1368
+ }
1369
+ }
1370
+ }
1371
+
1372
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
1373
+ @Throws(JSONException::class)
1374
+ fun query_RemoveChangeListener(call: PluginCall) {
1375
+ val (databaseName, isDatabaseNameError) = PluginHelper.getStringFromCall(call, "name")
1376
+ val (strToken, isTokenError) = PluginHelper.getStringFromCall(call, "changeListenerToken")
1377
+ if (isDatabaseNameError || databaseName.isNullOrEmpty() ||
1378
+ isTokenError || strToken.isNullOrEmpty()
1379
+ ) {
1380
+ return
1381
+ }
1382
+ GlobalScope.launch {
1383
+ withContext(Dispatchers.IO) {
1384
+ try {
1385
+ val token = queryChangeListeners[strToken]
1386
+ token?.remove()
1387
+ queryChangeListeners.remove(strToken)
1388
+ return@withContext withContext(Dispatchers.Main) {
1389
+ call.resolve()
1390
+ }
1391
+ } catch (e: Exception) {
1392
+ call.reject("${e.message}")
1393
+ }
1394
+ }
1395
+ }
1396
+ }
1397
+
1398
+ @PluginMethod(returnType = PluginMethod.RETURN_CALLBACK)
1399
+ @Throws(JSONException::class)
1400
+ fun replicator_AddChangeListener(call: PluginCall) {
1401
+ val (replicatorId, isReplicatorIdError) = PluginHelper.getStringFromCall(
1402
+ call,
1403
+ "replicatorId"
1404
+ )
1405
+ val (token, isTokenError) = PluginHelper.getStringFromCall(call, "changeListenerToken")
1406
+ if (isReplicatorIdError ||
1407
+ replicatorId.isNullOrEmpty() ||
1408
+ isTokenError ||
1409
+ token.isNullOrEmpty()
1410
+ ) {
1411
+ return
1412
+ }
1413
+ GlobalScope.launch {
1414
+ withContext(Dispatchers.IO) {
1415
+ try {
1416
+ val replicator = ReplicatorManager.getReplicator(replicatorId)
1417
+ replicator?.let {
1418
+ call.setKeepAlive(true)
1419
+ val listenerToken = it.addChangeListener { change ->
1420
+ val result =
1421
+ ReplicatorHelper.generateReplicatorStatusJson(change.status)
1422
+ call.resolve(result)
1423
+ }
1424
+ ReplicatorManager.replicatorChangeListeners.put(token, listenerToken)
1425
+ }
1426
+
1427
+ } catch (e: Exception) {
1428
+ call.reject("${e.message}")
1429
+ }
1430
+ }
1431
+ }
1432
+ }
1433
+
1434
+ @PluginMethod(returnType = PluginMethod.RETURN_CALLBACK)
1435
+ @Throws(JSONException::class)
1436
+ fun replicator_AddDocumentChangeListener(call: PluginCall) {
1437
+ val (replicatorId, isReplicatorIdError) = PluginHelper.getStringFromCall(
1438
+ call,
1439
+ "replicatorId"
1440
+ )
1441
+ val (token, isTokenError) = PluginHelper.getStringFromCall(call, "changeListenerToken")
1442
+ if (isReplicatorIdError ||
1443
+ replicatorId.isNullOrEmpty() ||
1444
+ isTokenError ||
1445
+ token.isNullOrEmpty()
1446
+ ) {
1447
+ return
1448
+ }
1449
+ GlobalScope.launch {
1450
+ withContext(Dispatchers.IO) {
1451
+ try {
1452
+ val replicator = ReplicatorManager.getReplicator(replicatorId)
1453
+ replicator?.let {
1454
+ call.setKeepAlive(true)
1455
+ val listenerToken = it.addDocumentReplicationListener { change ->
1456
+ val result =
1457
+ ReplicatorHelper.generateReplicatorDocumentChangeJson(change)
1458
+ call.resolve(result)
1459
+ }
1460
+ ReplicatorManager.replicatorChangeListeners.put(token, listenerToken)
1461
+ }
1462
+
1463
+ } catch (e: Exception) {
1464
+ call.reject("${e.message}")
1465
+ }
1466
+ }
1467
+ }
1468
+ }
1469
+
1470
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
1471
+ @Throws(JSONException::class)
1472
+ fun replicator_Cleanup(call: PluginCall) {
1473
+ val (replicatorId, isReplicatorIdError) = PluginHelper.getStringFromCall(
1474
+ call,
1475
+ "replicatorId"
1476
+ )
1477
+ if (isReplicatorIdError || replicatorId.isNullOrEmpty()) {
1478
+ return
1479
+ }
1480
+ GlobalScope.launch {
1481
+ withContext(Dispatchers.IO) {
1482
+ try {
1483
+ ReplicatorManager.getReplicator(replicatorId)?.let { replicator ->
1484
+ replicator.stop()
1485
+ ReplicatorManager.removeReplicator(replicatorId)
1486
+ }
1487
+ call.resolve()
1488
+ } catch (e: Exception) {
1489
+ call.reject("${e.message}")
1490
+ }
1491
+ }
1492
+ }
1493
+ }
1494
+
1495
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
1496
+ @Throws(JSONException::class)
1497
+ fun replicator_Create(call: PluginCall) {
1498
+ val configObj = call.getObject("config")
1499
+ if (configObj == null || configObj.length() == 0) {
1500
+ call.reject("Error: No config provided")
1501
+ return
1502
+ }
1503
+ GlobalScope.launch {
1504
+ withContext(Dispatchers.IO) {
1505
+ try {
1506
+ val collectionConfigJson = configObj.getString("collectionConfig")
1507
+ if (collectionConfigJson.isNullOrEmpty()) {
1508
+ call.reject("Error: No collection config provided")
1509
+ return@withContext
1510
+ }
1511
+ val collectionConfig = JSONArray(collectionConfigJson)
1512
+ if (collectionConfig.length() == 0) {
1513
+ call.reject("Error: couldn't parse collection configuration")
1514
+ return@withContext
1515
+ }
1516
+ val replicatorConfig =
1517
+ ReplicatorHelper.replicatorConfigFromJson(configObj, collectionConfig)
1518
+ val replicatorId = ReplicatorManager.createReplicator(replicatorConfig)
1519
+ if (replicatorId.isEmpty()) {
1520
+ return@withContext withContext(Dispatchers.Main) {
1521
+ call.reject("Error: couldn't create replicator")
1522
+ }
1523
+ } else {
1524
+ return@withContext withContext(Dispatchers.Main) {
1525
+ val results = JSObject()
1526
+ results.put("replicatorId", replicatorId)
1527
+ call.resolve(results)
1528
+ }
1529
+ }
1530
+
1531
+ } catch (e: Exception) {
1532
+ call.reject("${e.message}")
1533
+ }
1534
+ }
1535
+ }
1536
+ }
1537
+
1538
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
1539
+ @Throws(JSONException::class)
1540
+ fun replicator_GetPendingDocumentIds(call: PluginCall) {
1541
+ val (replicatorId, isReplicatorIdError) = PluginHelper.getStringFromCall(
1542
+ call,
1543
+ "replicatorId"
1544
+ )
1545
+ if (isReplicatorIdError || replicatorId.isNullOrEmpty()) {
1546
+ return
1547
+ }
1548
+ val collectionDto = PluginHelper.getCollectionDtoFromCall(call)
1549
+ if (collectionDto == null || collectionDto.isError) {
1550
+ return
1551
+ }
1552
+ GlobalScope.launch {
1553
+ withContext(Dispatchers.IO) {
1554
+ try {
1555
+ val pendingDocIds = ReplicatorManager.pendingDocIs(
1556
+ replicatorId,
1557
+ collectionDto.collectionName,
1558
+ collectionDto.scopeName,
1559
+ collectionDto.databaseName
1560
+ )
1561
+ if (pendingDocIds.isNotEmpty()) {
1562
+ val results = JSObject()
1563
+ val array = JSArray(pendingDocIds)
1564
+ results.put("pendingDocumentIds", array)
1565
+ call.resolve(results)
1566
+ } else {
1567
+ call.resolve()
1568
+ }
1569
+ } catch (e: Exception) {
1570
+ call.reject("${e.message}")
1571
+ }
1572
+ }
1573
+ }
1574
+ }
1575
+
1576
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
1577
+ @Throws(JSONException::class)
1578
+ fun replicator_GetStatus(call: PluginCall) {
1579
+ val (replicatorId, isReplicatorIdError) = PluginHelper.getStringFromCall(
1580
+ call,
1581
+ "replicatorId"
1582
+ )
1583
+ if (isReplicatorIdError || replicatorId.isNullOrEmpty()) {
1584
+ return
1585
+ }
1586
+ GlobalScope.launch {
1587
+ withContext(Dispatchers.IO) {
1588
+ try {
1589
+ val status = ReplicatorManager.getStatus(replicatorId)
1590
+ val jsonStatus = ReplicatorHelper.generateReplicatorStatusJson(status)
1591
+ call.resolve(jsonStatus)
1592
+ } catch (e: Exception) {
1593
+ call.reject("${e.message}")
1594
+ }
1595
+ }
1596
+ }
1597
+ }
1598
+
1599
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
1600
+ @Throws(JSONException::class)
1601
+ fun replicator_IsDocumentPending(call: PluginCall) {
1602
+ val (replicatorId, isReplicatorIdError) = PluginHelper.getStringFromCall(
1603
+ call,
1604
+ "replicatorId"
1605
+ )
1606
+ if (isReplicatorIdError || replicatorId.isNullOrEmpty()) {
1607
+ return
1608
+ }
1609
+ val collectionDto = PluginHelper.getCollectionDtoFromCall(call)
1610
+ if (collectionDto == null || collectionDto.isError) {
1611
+ return
1612
+ }
1613
+ val (documentId, isDocumentIdError) = PluginHelper.getStringFromCall(call, "documentId")
1614
+ if (isDocumentIdError || documentId.isNullOrEmpty()) {
1615
+ return
1616
+ }
1617
+ GlobalScope.launch {
1618
+ withContext(Dispatchers.IO) {
1619
+ try {
1620
+ val status = ReplicatorManager.isDocumentPending(
1621
+ documentId,
1622
+ replicatorId,
1623
+ collectionDto.collectionName,
1624
+ collectionDto.scopeName,
1625
+ collectionDto.databaseName
1626
+ )
1627
+ val jsonObject = JSObject()
1628
+ jsonObject.put("isPending", status)
1629
+ call.resolve(jsonObject)
1630
+ } catch (e: Exception) {
1631
+ call.reject("${e.message}")
1632
+ }
1633
+ }
1634
+ }
1635
+ }
1636
+
1637
+ @PluginMethod(returnType = PluginMethod.RETURN_CALLBACK)
1638
+ @Throws(JSONException::class)
1639
+ fun replicator_RemoveChangeListener(call: PluginCall) {
1640
+ val (replicatorId, isReplicatorIdError) = PluginHelper.getStringFromCall(
1641
+ call,
1642
+ "replicatorId"
1643
+ )
1644
+ val (token, isTokenError) = PluginHelper.getStringFromCall(call, "changeListenerToken")
1645
+ if (isReplicatorIdError ||
1646
+ replicatorId.isNullOrEmpty() ||
1647
+ isTokenError ||
1648
+ token.isNullOrEmpty()
1649
+ ) {
1650
+ return
1651
+ }
1652
+ GlobalScope.launch {
1653
+ withContext(Dispatchers.IO) {
1654
+ try {
1655
+ val replicator = ReplicatorManager.getReplicator(replicatorId)
1656
+ replicator?.let {
1657
+ ReplicatorManager.removeChangeListener(replicatorId, token)
1658
+ }
1659
+ } catch (e: Exception) {
1660
+ call.reject("${e.message}")
1661
+ }
1662
+ }
1663
+ }
1664
+ }
1665
+
1666
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
1667
+ @Throws(JSONException::class)
1668
+ fun replicator_ResetCheckpoint(call: PluginCall) {
1669
+ val (replicatorId, isReplicatorIdError) = PluginHelper.getStringFromCall(
1670
+ call,
1671
+ "replicatorId"
1672
+ )
1673
+ if (isReplicatorIdError || replicatorId.isNullOrEmpty()) {
1674
+ return
1675
+ }
1676
+ GlobalScope.launch {
1677
+ withContext(Dispatchers.IO) {
1678
+ try {
1679
+ ReplicatorManager.resetCheckpoint(replicatorId)
1680
+ call.resolve()
1681
+ } catch (e: Exception) {
1682
+ call.reject("${e.message}")
1683
+ }
1684
+ }
1685
+ }
1686
+ }
1687
+
1688
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
1689
+ @Throws(JSONException::class)
1690
+ fun replicator_Start(call: PluginCall) {
1691
+ val (replicatorId, isReplicatorIdError) = PluginHelper.getStringFromCall(
1692
+ call,
1693
+ "replicatorId"
1694
+ )
1695
+ if (isReplicatorIdError || replicatorId.isNullOrEmpty()) {
1696
+ return
1697
+ }
1698
+ GlobalScope.launch {
1699
+ withContext(Dispatchers.IO) {
1700
+ try {
1701
+ ReplicatorManager.start(replicatorId)
1702
+ call.resolve()
1703
+ } catch (e: Exception) {
1704
+ call.reject("${e.message}")
1705
+ }
1706
+ }
1707
+ }
1708
+ }
1709
+
1710
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
1711
+ @Throws(JSONException::class)
1712
+ fun replicator_Stop(call: PluginCall) {
1713
+ val (replicatorId, isReplicatorIdError) = PluginHelper.getStringFromCall(
1714
+ call,
1715
+ "replicatorId"
1716
+ )
1717
+ if (isReplicatorIdError || replicatorId.isNullOrEmpty()) {
1718
+ return
1719
+ }
1720
+ GlobalScope.launch {
1721
+ withContext(Dispatchers.IO) {
1722
+ try {
1723
+ ReplicatorManager.stop(replicatorId)
1724
+ call.resolve()
1725
+ } catch (e: Exception) {
1726
+ call.reject("${e.message}")
1727
+ }
1728
+ }
1729
+ }
1730
+ }
1731
+
1732
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
1733
+ fun scope_GetDefault(call: PluginCall) {
1734
+ val (name, isNameError) = PluginHelper.getStringFromCall(call, "name")
1735
+ if (isNameError) {
1736
+ return
1737
+ }
1738
+ GlobalScope.launch {
1739
+ withContext(Dispatchers.IO) {
1740
+ try {
1741
+ name?.let { databaseName ->
1742
+ val scope = DatabaseManager.defaultScope(databaseName)
1743
+ scope?.let {
1744
+ val results = JSObject()
1745
+ results.put("name", scope.name)
1746
+ results.put("databaseName", name)
1747
+ return@withContext withContext(Dispatchers.Main) {
1748
+ call.resolve(results)
1749
+ }
1750
+ }
1751
+ }
1752
+ return@withContext withContext(Dispatchers.Main) {
1753
+ call.reject("Error: No database name or default scope found")
1754
+ }
1755
+ } catch (e: Exception) {
1756
+ return@withContext withContext(Dispatchers.Main) {
1757
+ call.reject("${e.message}")
1758
+ }
1759
+ }
1760
+ }
1761
+ }
1762
+ }
1763
+
1764
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
1765
+ @Throws(JSONException::class)
1766
+ fun scope_GetScope(call: PluginCall) {
1767
+ val scopeDto = PluginHelper.getScopeDtoFromCall(call)
1768
+ if (scopeDto == null || scopeDto.isError) {
1769
+ return
1770
+ }
1771
+ GlobalScope.launch {
1772
+ withContext(Dispatchers.IO) {
1773
+ try {
1774
+ val scope = DatabaseManager.getScope(scopeDto.databaseName, scopeDto.scopeName)
1775
+ scope?.let {
1776
+ val results = JSObject()
1777
+ results.put("name", scope.name)
1778
+ results.put("databaseName", scopeDto.databaseName)
1779
+ return@withContext withContext(Dispatchers.Main) {
1780
+ call.resolve(results)
1781
+ }
1782
+ }
1783
+ return@withContext withContext(Dispatchers.Main) {
1784
+ call.resolve()
1785
+ }
1786
+ } catch (e: Exception) {
1787
+ return@withContext withContext(Dispatchers.Main) {
1788
+ call.reject("${e.message}")
1789
+ }
1790
+ }
1791
+ }
1792
+ }
1793
+ }
1794
+
1795
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
1796
+ @Throws(JSONException::class)
1797
+ fun scope_GetScopes(call: PluginCall) {
1798
+ val (name, isNameError) = PluginHelper.getStringFromCall(call, "name")
1799
+ if (isNameError) {
1800
+ return
1801
+ }
1802
+ GlobalScope.launch {
1803
+ withContext(Dispatchers.IO) {
1804
+ try {
1805
+ name?.let { databaseName ->
1806
+ val scopes = DatabaseManager.scopes(databaseName)
1807
+ val results = JSObject()
1808
+ val scopeArray = JSArray()
1809
+ for (scope in scopes) {
1810
+ val scopeObject = JSObject()
1811
+ scopeObject.put("name", scope.name)
1812
+ scopeObject.put("databaseName", name)
1813
+ scopeArray.put(scopeObject)
1814
+ }
1815
+ results.put("scopes", scopeArray)
1816
+ return@withContext withContext(Dispatchers.Main) {
1817
+ call.resolve(results)
1818
+ }
1819
+ }
1820
+ return@withContext withContext(Dispatchers.Main) {
1821
+ call.reject("Error: No database name provided or scope found")
1822
+ }
1823
+ } catch (e: Exception) {
1824
+ return@withContext withContext(Dispatchers.Main) {
1825
+ call.reject("${e.message}")
1826
+ }
1827
+ }
1828
+ }
1829
+ }
1830
+ }
1831
+
1832
+ }