@react-native-firebase/firestore 20.3.0 → 20.5.0

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 (28) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/__tests__/firestore.test.ts +292 -1
  3. package/android/src/main/java/io/invertase/firebase/firestore/UniversalFirebaseFirestoreCommon.java +17 -9
  4. package/android/src/main/java/io/invertase/firebase/firestore/UniversalFirebaseFirestoreModule.java +28 -26
  5. package/android/src/reactnative/java/io/invertase/firebase/firestore/ReactNativeFirebaseFirestoreCollectionModule.java +53 -22
  6. package/android/src/reactnative/java/io/invertase/firebase/firestore/ReactNativeFirebaseFirestoreCommon.java +5 -2
  7. package/android/src/reactnative/java/io/invertase/firebase/firestore/ReactNativeFirebaseFirestoreDocumentModule.java +35 -21
  8. package/android/src/reactnative/java/io/invertase/firebase/firestore/ReactNativeFirebaseFirestoreEvent.java +5 -1
  9. package/android/src/reactnative/java/io/invertase/firebase/firestore/ReactNativeFirebaseFirestoreModule.java +48 -16
  10. package/android/src/reactnative/java/io/invertase/firebase/firestore/ReactNativeFirebaseFirestoreQuery.java +3 -1
  11. package/android/src/reactnative/java/io/invertase/firebase/firestore/ReactNativeFirebaseFirestoreSerialize.java +13 -8
  12. package/android/src/reactnative/java/io/invertase/firebase/firestore/ReactNativeFirebaseFirestoreTransactionModule.java +12 -7
  13. package/ios/RNFBFirestore/RNFBFirestoreCollectionModule.m +78 -49
  14. package/ios/RNFBFirestore/RNFBFirestoreCommon.h +6 -2
  15. package/ios/RNFBFirestore/RNFBFirestoreCommon.m +23 -10
  16. package/ios/RNFBFirestore/RNFBFirestoreDocumentModule.m +41 -11
  17. package/ios/RNFBFirestore/RNFBFirestoreModule.m +66 -15
  18. package/ios/RNFBFirestore/RNFBFirestoreSerialize.h +5 -3
  19. package/ios/RNFBFirestore/RNFBFirestoreSerialize.m +17 -10
  20. package/ios/RNFBFirestore/RNFBFirestoreTransactionModule.m +21 -7
  21. package/lib/FirestorePersistentCacheIndexManager.js +34 -0
  22. package/lib/index.d.ts +32 -1
  23. package/lib/index.js +25 -2
  24. package/lib/modular/index.d.ts +52 -0
  25. package/lib/modular/index.js +55 -3
  26. package/lib/version.js +1 -1
  27. package/lib/web/RNFBFirestoreModule.js +61 -34
  28. package/package.json +3 -3
@@ -53,6 +53,7 @@ public class ReactNativeFirebaseFirestoreCollectionModule extends ReactNativeFir
53
53
  @ReactMethod
54
54
  public void namedQueryOnSnapshot(
55
55
  String appName,
56
+ String databaseId,
56
57
  String queryName,
57
58
  String type,
58
59
  ReadableArray filters,
@@ -64,7 +65,7 @@ public class ReactNativeFirebaseFirestoreCollectionModule extends ReactNativeFir
64
65
  return;
65
66
  }
66
67
 
67
- FirebaseFirestore firebaseFirestore = getFirestoreForApp(appName);
68
+ FirebaseFirestore firebaseFirestore = getFirestoreForApp(appName, databaseId);
68
69
  firebaseFirestore
69
70
  .getNamedQuery(queryName)
70
71
  .addOnCompleteListener(
@@ -72,15 +73,16 @@ public class ReactNativeFirebaseFirestoreCollectionModule extends ReactNativeFir
72
73
  if (task.isSuccessful()) {
73
74
  Query query = task.getResult();
74
75
  if (query == null) {
75
- sendOnSnapshotError(appName, listenerId, new NullPointerException());
76
+ sendOnSnapshotError(appName, databaseId, listenerId, new NullPointerException());
76
77
  } else {
77
78
  ReactNativeFirebaseFirestoreQuery firestoreQuery =
78
79
  new ReactNativeFirebaseFirestoreQuery(
79
- appName, query, filters, orders, options);
80
- handleQueryOnSnapshot(firestoreQuery, appName, listenerId, listenerOptions);
80
+ appName, databaseId, query, filters, orders, options);
81
+ handleQueryOnSnapshot(
82
+ firestoreQuery, appName, databaseId, listenerId, listenerOptions);
81
83
  }
82
84
  } else {
83
- sendOnSnapshotError(appName, listenerId, task.getException());
85
+ sendOnSnapshotError(appName, databaseId, listenerId, task.getException());
84
86
  }
85
87
  });
86
88
  }
@@ -88,6 +90,7 @@ public class ReactNativeFirebaseFirestoreCollectionModule extends ReactNativeFir
88
90
  @ReactMethod
89
91
  public void collectionOnSnapshot(
90
92
  String appName,
93
+ String databaseId,
91
94
  String path,
92
95
  String type,
93
96
  ReadableArray filters,
@@ -99,16 +102,21 @@ public class ReactNativeFirebaseFirestoreCollectionModule extends ReactNativeFir
99
102
  return;
100
103
  }
101
104
 
102
- FirebaseFirestore firebaseFirestore = getFirestoreForApp(appName);
105
+ FirebaseFirestore firebaseFirestore = getFirestoreForApp(appName, databaseId);
103
106
  ReactNativeFirebaseFirestoreQuery firestoreQuery =
104
107
  new ReactNativeFirebaseFirestoreQuery(
105
- appName, getQueryForFirestore(firebaseFirestore, path, type), filters, orders, options);
106
-
107
- handleQueryOnSnapshot(firestoreQuery, appName, listenerId, listenerOptions);
108
+ appName,
109
+ databaseId,
110
+ getQueryForFirestore(firebaseFirestore, path, type),
111
+ filters,
112
+ orders,
113
+ options);
114
+
115
+ handleQueryOnSnapshot(firestoreQuery, appName, databaseId, listenerId, listenerOptions);
108
116
  }
109
117
 
110
118
  @ReactMethod
111
- public void collectionOffSnapshot(String appName, int listenerId) {
119
+ public void collectionOffSnapshot(String appName, String databaseId, int listenerId) {
112
120
  ListenerRegistration listenerRegistration = collectionSnapshotListeners.get(listenerId);
113
121
  if (listenerRegistration != null) {
114
122
  listenerRegistration.remove();
@@ -120,6 +128,7 @@ public class ReactNativeFirebaseFirestoreCollectionModule extends ReactNativeFir
120
128
  @ReactMethod
121
129
  public void namedQueryGet(
122
130
  String appName,
131
+ String databaseId,
123
132
  String queryName,
124
133
  String type,
125
134
  ReadableArray filters,
@@ -127,7 +136,7 @@ public class ReactNativeFirebaseFirestoreCollectionModule extends ReactNativeFir
127
136
  ReadableMap options,
128
137
  ReadableMap getOptions,
129
138
  Promise promise) {
130
- FirebaseFirestore firebaseFirestore = getFirestoreForApp(appName);
139
+ FirebaseFirestore firebaseFirestore = getFirestoreForApp(appName, databaseId);
131
140
  firebaseFirestore
132
141
  .getNamedQuery(queryName)
133
142
  .addOnCompleteListener(
@@ -139,7 +148,7 @@ public class ReactNativeFirebaseFirestoreCollectionModule extends ReactNativeFir
139
148
  } else {
140
149
  ReactNativeFirebaseFirestoreQuery firestoreQuery =
141
150
  new ReactNativeFirebaseFirestoreQuery(
142
- appName, query, filters, orders, options);
151
+ appName, databaseId, query, filters, orders, options);
143
152
  handleQueryGet(firestoreQuery, getSource(getOptions), promise);
144
153
  }
145
154
  } else {
@@ -151,16 +160,22 @@ public class ReactNativeFirebaseFirestoreCollectionModule extends ReactNativeFir
151
160
  @ReactMethod
152
161
  public void collectionCount(
153
162
  String appName,
163
+ String databaseId,
154
164
  String path,
155
165
  String type,
156
166
  ReadableArray filters,
157
167
  ReadableArray orders,
158
168
  ReadableMap options,
159
169
  Promise promise) {
160
- FirebaseFirestore firebaseFirestore = getFirestoreForApp(appName);
170
+ FirebaseFirestore firebaseFirestore = getFirestoreForApp(appName, databaseId);
161
171
  ReactNativeFirebaseFirestoreQuery firestoreQuery =
162
172
  new ReactNativeFirebaseFirestoreQuery(
163
- appName, getQueryForFirestore(firebaseFirestore, path, type), filters, orders, options);
173
+ appName,
174
+ databaseId,
175
+ getQueryForFirestore(firebaseFirestore, path, type),
176
+ filters,
177
+ orders,
178
+ options);
164
179
 
165
180
  AggregateQuery aggregateQuery = firestoreQuery.query.count();
166
181
 
@@ -181,6 +196,7 @@ public class ReactNativeFirebaseFirestoreCollectionModule extends ReactNativeFir
181
196
  @ReactMethod
182
197
  public void collectionGet(
183
198
  String appName,
199
+ String databaseId,
184
200
  String path,
185
201
  String type,
186
202
  ReadableArray filters,
@@ -188,16 +204,22 @@ public class ReactNativeFirebaseFirestoreCollectionModule extends ReactNativeFir
188
204
  ReadableMap options,
189
205
  ReadableMap getOptions,
190
206
  Promise promise) {
191
- FirebaseFirestore firebaseFirestore = getFirestoreForApp(appName);
207
+ FirebaseFirestore firebaseFirestore = getFirestoreForApp(appName, databaseId);
192
208
  ReactNativeFirebaseFirestoreQuery firestoreQuery =
193
209
  new ReactNativeFirebaseFirestoreQuery(
194
- appName, getQueryForFirestore(firebaseFirestore, path, type), filters, orders, options);
210
+ appName,
211
+ databaseId,
212
+ getQueryForFirestore(firebaseFirestore, path, type),
213
+ filters,
214
+ orders,
215
+ options);
195
216
  handleQueryGet(firestoreQuery, getSource(getOptions), promise);
196
217
  }
197
218
 
198
219
  private void handleQueryOnSnapshot(
199
220
  ReactNativeFirebaseFirestoreQuery firestoreQuery,
200
221
  String appName,
222
+ String databaseId,
201
223
  int listenerId,
202
224
  ReadableMap listenerOptions) {
203
225
  MetadataChanges metadataChanges;
@@ -218,9 +240,9 @@ public class ReactNativeFirebaseFirestoreCollectionModule extends ReactNativeFir
218
240
  listenerRegistration.remove();
219
241
  collectionSnapshotListeners.remove(listenerId);
220
242
  }
221
- sendOnSnapshotError(appName, listenerId, exception);
243
+ sendOnSnapshotError(appName, databaseId, listenerId, exception);
222
244
  } else {
223
- sendOnSnapshotEvent(appName, listenerId, querySnapshot, metadataChanges);
245
+ sendOnSnapshotEvent(appName, databaseId, listenerId, querySnapshot, metadataChanges);
224
246
  }
225
247
  };
226
248
 
@@ -246,12 +268,15 @@ public class ReactNativeFirebaseFirestoreCollectionModule extends ReactNativeFir
246
268
 
247
269
  private void sendOnSnapshotEvent(
248
270
  String appName,
271
+ String databaseId,
249
272
  int listenerId,
250
273
  QuerySnapshot querySnapshot,
251
274
  MetadataChanges metadataChanges) {
252
275
  Tasks.call(
253
276
  getTransactionalExecutor(Integer.toString(listenerId)),
254
- () -> snapshotToWritableMap(appName, "onSnapshot", querySnapshot, metadataChanges))
277
+ () ->
278
+ snapshotToWritableMap(
279
+ appName, databaseId, "onSnapshot", querySnapshot, metadataChanges))
255
280
  .addOnCompleteListener(
256
281
  task -> {
257
282
  if (task.isSuccessful()) {
@@ -266,14 +291,16 @@ public class ReactNativeFirebaseFirestoreCollectionModule extends ReactNativeFir
266
291
  ReactNativeFirebaseFirestoreEvent.COLLECTION_EVENT_SYNC,
267
292
  body,
268
293
  appName,
294
+ databaseId,
269
295
  listenerId));
270
296
  } else {
271
- sendOnSnapshotError(appName, listenerId, task.getException());
297
+ sendOnSnapshotError(appName, databaseId, listenerId, task.getException());
272
298
  }
273
299
  });
274
300
  }
275
301
 
276
- private void sendOnSnapshotError(String appName, int listenerId, Exception exception) {
302
+ private void sendOnSnapshotError(
303
+ String appName, String databaseId, int listenerId, Exception exception) {
277
304
  WritableMap body = Arguments.createMap();
278
305
  WritableMap error = Arguments.createMap();
279
306
 
@@ -293,7 +320,11 @@ public class ReactNativeFirebaseFirestoreCollectionModule extends ReactNativeFir
293
320
 
294
321
  emitter.sendEvent(
295
322
  new ReactNativeFirebaseFirestoreEvent(
296
- ReactNativeFirebaseFirestoreEvent.COLLECTION_EVENT_SYNC, body, appName, listenerId));
323
+ ReactNativeFirebaseFirestoreEvent.COLLECTION_EVENT_SYNC,
324
+ body,
325
+ appName,
326
+ databaseId,
327
+ listenerId));
297
328
  }
298
329
 
299
330
  private Source getSource(ReadableMap getOptions) {
@@ -19,6 +19,7 @@ package io.invertase.firebase.firestore;
19
19
 
20
20
  import static io.invertase.firebase.common.ReactNativeFirebaseModule.rejectPromiseWithCodeAndMessage;
21
21
  import static io.invertase.firebase.common.ReactNativeFirebaseModule.rejectPromiseWithExceptionMap;
22
+ import static io.invertase.firebase.firestore.UniversalFirebaseFirestoreCommon.createFirestoreKey;
22
23
 
23
24
  import com.facebook.react.bridge.Promise;
24
25
  import com.google.firebase.firestore.DocumentSnapshot;
@@ -48,10 +49,12 @@ class ReactNativeFirebaseFirestoreCommon {
48
49
  }
49
50
  }
50
51
 
51
- static DocumentSnapshot.ServerTimestampBehavior getServerTimestampBehavior(String appName) {
52
+ static DocumentSnapshot.ServerTimestampBehavior getServerTimestampBehavior(
53
+ String appName, String databaseId) {
54
+ String firestoreKey = createFirestoreKey(appName, databaseId);
52
55
  UniversalFirebasePreferences preferences = UniversalFirebasePreferences.getSharedInstance();
53
56
  String key =
54
- UniversalFirebaseFirestoreStatics.FIRESTORE_SERVER_TIMESTAMP_BEHAVIOR + "_" + appName;
57
+ UniversalFirebaseFirestoreStatics.FIRESTORE_SERVER_TIMESTAMP_BEHAVIOR + "_" + firestoreKey;
55
58
  String behavior = preferences.getStringValue(key, "none");
56
59
 
57
60
  if ("estimate".equals(behavior)) {
@@ -56,12 +56,12 @@ public class ReactNativeFirebaseFirestoreDocumentModule extends ReactNativeFireb
56
56
 
57
57
  @ReactMethod
58
58
  public void documentOnSnapshot(
59
- String appName, String path, int listenerId, ReadableMap listenerOptions) {
59
+ String appName, String databaseId, String path, int listenerId, ReadableMap listenerOptions) {
60
60
  if (documentSnapshotListeners.get(listenerId) != null) {
61
61
  return;
62
62
  }
63
63
 
64
- FirebaseFirestore firebaseFirestore = getFirestoreForApp(appName);
64
+ FirebaseFirestore firebaseFirestore = getFirestoreForApp(appName, databaseId);
65
65
  DocumentReference documentReference = getDocumentForFirestore(firebaseFirestore, path);
66
66
 
67
67
  final EventListener<DocumentSnapshot> listener =
@@ -72,9 +72,9 @@ public class ReactNativeFirebaseFirestoreDocumentModule extends ReactNativeFireb
72
72
  listenerRegistration.remove();
73
73
  documentSnapshotListeners.remove(listenerId);
74
74
  }
75
- sendOnSnapshotError(appName, listenerId, exception);
75
+ sendOnSnapshotError(appName, databaseId, listenerId, exception);
76
76
  } else {
77
- sendOnSnapshotEvent(appName, listenerId, documentSnapshot);
77
+ sendOnSnapshotEvent(appName, databaseId, listenerId, documentSnapshot);
78
78
  }
79
79
  };
80
80
 
@@ -95,7 +95,7 @@ public class ReactNativeFirebaseFirestoreDocumentModule extends ReactNativeFireb
95
95
  }
96
96
 
97
97
  @ReactMethod
98
- public void documentOffSnapshot(String appName, int listenerId) {
98
+ public void documentOffSnapshot(String appName, String databaseId, int listenerId) {
99
99
  ListenerRegistration listenerRegistration = documentSnapshotListeners.get(listenerId);
100
100
  if (listenerRegistration != null) {
101
101
  listenerRegistration.remove();
@@ -104,8 +104,9 @@ public class ReactNativeFirebaseFirestoreDocumentModule extends ReactNativeFireb
104
104
  }
105
105
 
106
106
  @ReactMethod
107
- public void documentGet(String appName, String path, ReadableMap getOptions, Promise promise) {
108
- FirebaseFirestore firebaseFirestore = getFirestoreForApp(appName);
107
+ public void documentGet(
108
+ String appName, String databaseId, String path, ReadableMap getOptions, Promise promise) {
109
+ FirebaseFirestore firebaseFirestore = getFirestoreForApp(appName, databaseId);
109
110
  DocumentReference documentReference = getDocumentForFirestore(firebaseFirestore, path);
110
111
 
111
112
  Source source;
@@ -127,7 +128,7 @@ public class ReactNativeFirebaseFirestoreDocumentModule extends ReactNativeFireb
127
128
  getExecutor(),
128
129
  () -> {
129
130
  DocumentSnapshot documentSnapshot = Tasks.await(documentReference.get(source));
130
- return snapshotToWritableMap(appName, documentSnapshot);
131
+ return snapshotToWritableMap(appName, databaseId, documentSnapshot);
131
132
  })
132
133
  .addOnCompleteListener(
133
134
  task -> {
@@ -140,8 +141,8 @@ public class ReactNativeFirebaseFirestoreDocumentModule extends ReactNativeFireb
140
141
  }
141
142
 
142
143
  @ReactMethod
143
- public void documentDelete(String appName, String path, Promise promise) {
144
- FirebaseFirestore firebaseFirestore = getFirestoreForApp(appName);
144
+ public void documentDelete(String appName, String databaseId, String path, Promise promise) {
145
+ FirebaseFirestore firebaseFirestore = getFirestoreForApp(appName, databaseId);
145
146
  DocumentReference documentReference = getDocumentForFirestore(firebaseFirestore, path);
146
147
  Tasks.call(getTransactionalExecutor(), documentReference::delete)
147
148
  .addOnCompleteListener(
@@ -156,8 +157,13 @@ public class ReactNativeFirebaseFirestoreDocumentModule extends ReactNativeFireb
156
157
 
157
158
  @ReactMethod
158
159
  public void documentSet(
159
- String appName, String path, ReadableMap data, ReadableMap options, Promise promise) {
160
- FirebaseFirestore firebaseFirestore = getFirestoreForApp(appName);
160
+ String appName,
161
+ String databaseId,
162
+ String path,
163
+ ReadableMap data,
164
+ ReadableMap options,
165
+ Promise promise) {
166
+ FirebaseFirestore firebaseFirestore = getFirestoreForApp(appName, databaseId);
161
167
  DocumentReference documentReference = getDocumentForFirestore(firebaseFirestore, path);
162
168
 
163
169
  Tasks.call(getTransactionalExecutor(), () -> parseReadableMap(firebaseFirestore, data))
@@ -195,8 +201,9 @@ public class ReactNativeFirebaseFirestoreDocumentModule extends ReactNativeFireb
195
201
  }
196
202
 
197
203
  @ReactMethod
198
- public void documentUpdate(String appName, String path, ReadableMap data, Promise promise) {
199
- FirebaseFirestore firebaseFirestore = getFirestoreForApp(appName);
204
+ public void documentUpdate(
205
+ String appName, String databaseId, String path, ReadableMap data, Promise promise) {
206
+ FirebaseFirestore firebaseFirestore = getFirestoreForApp(appName, databaseId);
200
207
  DocumentReference documentReference = getDocumentForFirestore(firebaseFirestore, path);
201
208
 
202
209
  Tasks.call(getTransactionalExecutor(), () -> parseReadableMap(firebaseFirestore, data))
@@ -214,8 +221,9 @@ public class ReactNativeFirebaseFirestoreDocumentModule extends ReactNativeFireb
214
221
  }
215
222
 
216
223
  @ReactMethod
217
- public void documentBatch(String appName, ReadableArray writes, Promise promise) {
218
- FirebaseFirestore firebaseFirestore = getFirestoreForApp(appName);
224
+ public void documentBatch(
225
+ String appName, String databaseId, ReadableArray writes, Promise promise) {
226
+ FirebaseFirestore firebaseFirestore = getFirestoreForApp(appName, databaseId);
219
227
 
220
228
  Tasks.call(getTransactionalExecutor(), () -> parseDocumentBatches(firebaseFirestore, writes))
221
229
  .continueWithTask(
@@ -282,8 +290,8 @@ public class ReactNativeFirebaseFirestoreDocumentModule extends ReactNativeFireb
282
290
  }
283
291
 
284
292
  private void sendOnSnapshotEvent(
285
- String appName, int listenerId, DocumentSnapshot documentSnapshot) {
286
- Tasks.call(getExecutor(), () -> snapshotToWritableMap(appName, documentSnapshot))
293
+ String appName, String databaseId, int listenerId, DocumentSnapshot documentSnapshot) {
294
+ Tasks.call(getExecutor(), () -> snapshotToWritableMap(appName, databaseId, documentSnapshot))
287
295
  .addOnCompleteListener(
288
296
  task -> {
289
297
  if (task.isSuccessful()) {
@@ -298,14 +306,16 @@ public class ReactNativeFirebaseFirestoreDocumentModule extends ReactNativeFireb
298
306
  ReactNativeFirebaseFirestoreEvent.DOCUMENT_EVENT_SYNC,
299
307
  body,
300
308
  appName,
309
+ databaseId,
301
310
  listenerId));
302
311
  } else {
303
- sendOnSnapshotError(appName, listenerId, task.getException());
312
+ sendOnSnapshotError(appName, databaseId, listenerId, task.getException());
304
313
  }
305
314
  });
306
315
  }
307
316
 
308
- private void sendOnSnapshotError(String appName, int listenerId, Exception exception) {
317
+ private void sendOnSnapshotError(
318
+ String appName, String databaseId, int listenerId, Exception exception) {
309
319
  WritableMap body = Arguments.createMap();
310
320
  WritableMap error = Arguments.createMap();
311
321
 
@@ -325,6 +335,10 @@ public class ReactNativeFirebaseFirestoreDocumentModule extends ReactNativeFireb
325
335
 
326
336
  emitter.sendEvent(
327
337
  new ReactNativeFirebaseFirestoreEvent(
328
- ReactNativeFirebaseFirestoreEvent.DOCUMENT_EVENT_SYNC, body, appName, listenerId));
338
+ ReactNativeFirebaseFirestoreEvent.DOCUMENT_EVENT_SYNC,
339
+ body,
340
+ appName,
341
+ databaseId,
342
+ listenerId));
329
343
  }
330
344
  }
@@ -30,16 +30,19 @@ public class ReactNativeFirebaseFirestoreEvent implements NativeEvent {
30
30
  private static final String KEY_BODY = "body";
31
31
  private static final String KEY_APP_NAME = "appName";
32
32
  private static final String KEY_EVENT_NAME = "eventName";
33
+ private static final String DATABASE_ID = "databaseId";
33
34
  private String eventName;
34
35
  private WritableMap eventBody;
35
36
  private String appName;
37
+ private String databaseId;
36
38
  private int listenerId;
37
39
 
38
40
  ReactNativeFirebaseFirestoreEvent(
39
- String eventName, WritableMap eventBody, String appName, int listenerId) {
41
+ String eventName, WritableMap eventBody, String appName, String databaseId, int listenerId) {
40
42
  this.eventName = eventName;
41
43
  this.eventBody = eventBody;
42
44
  this.appName = appName;
45
+ this.databaseId = databaseId;
43
46
  this.listenerId = listenerId;
44
47
  }
45
48
 
@@ -54,6 +57,7 @@ public class ReactNativeFirebaseFirestoreEvent implements NativeEvent {
54
57
  event.putInt(KEY_ID, listenerId);
55
58
  event.putMap(KEY_BODY, eventBody);
56
59
  event.putString(KEY_APP_NAME, appName);
60
+ event.putString(DATABASE_ID, databaseId);
57
61
  event.putString(KEY_EVENT_NAME, eventName);
58
62
  return event;
59
63
  }
@@ -19,6 +19,8 @@ package io.invertase.firebase.firestore;
19
19
 
20
20
  import static io.invertase.firebase.common.RCTConvertFirebase.toHashMap;
21
21
  import static io.invertase.firebase.firestore.ReactNativeFirebaseFirestoreCommon.rejectPromiseFirestoreException;
22
+ import static io.invertase.firebase.firestore.UniversalFirebaseFirestoreCommon.createFirestoreKey;
23
+ import static io.invertase.firebase.firestore.UniversalFirebaseFirestoreCommon.getFirestoreForApp;
22
24
 
23
25
  import com.facebook.react.bridge.Arguments;
24
26
  import com.facebook.react.bridge.Promise;
@@ -28,6 +30,7 @@ import com.facebook.react.bridge.ReadableMap;
28
30
  import com.facebook.react.bridge.WritableMap;
29
31
  import com.google.firebase.firestore.FirebaseFirestore;
30
32
  import com.google.firebase.firestore.LoadBundleTaskProgress;
33
+ import com.google.firebase.firestore.PersistentCacheIndexManager;
31
34
  import io.invertase.firebase.common.ReactNativeFirebaseModule;
32
35
 
33
36
  public class ReactNativeFirebaseFirestoreModule extends ReactNativeFirebaseModule {
@@ -49,9 +52,9 @@ public class ReactNativeFirebaseFirestoreModule extends ReactNativeFirebaseModul
49
52
  }
50
53
 
51
54
  @ReactMethod
52
- public void loadBundle(String appName, String bundle, Promise promise) {
55
+ public void loadBundle(String appName, String databaseId, String bundle, Promise promise) {
53
56
  module
54
- .loadBundle(appName, bundle)
57
+ .loadBundle(appName, databaseId, bundle)
55
58
  .addOnCompleteListener(
56
59
  task -> {
57
60
  if (task.isSuccessful()) {
@@ -64,9 +67,9 @@ public class ReactNativeFirebaseFirestoreModule extends ReactNativeFirebaseModul
64
67
  }
65
68
 
66
69
  @ReactMethod
67
- public void clearPersistence(String appName, Promise promise) {
70
+ public void clearPersistence(String appName, String databaseId, Promise promise) {
68
71
  module
69
- .clearPersistence(appName)
72
+ .clearPersistence(appName, databaseId)
70
73
  .addOnCompleteListener(
71
74
  task -> {
72
75
  if (task.isSuccessful()) {
@@ -78,9 +81,9 @@ public class ReactNativeFirebaseFirestoreModule extends ReactNativeFirebaseModul
78
81
  }
79
82
 
80
83
  @ReactMethod
81
- public void waitForPendingWrites(String appName, Promise promise) {
84
+ public void waitForPendingWrites(String appName, String databaseId, Promise promise) {
82
85
  module
83
- .waitForPendingWrites(appName)
86
+ .waitForPendingWrites(appName, databaseId)
84
87
  .addOnCompleteListener(
85
88
  task -> {
86
89
  if (task.isSuccessful()) {
@@ -92,9 +95,9 @@ public class ReactNativeFirebaseFirestoreModule extends ReactNativeFirebaseModul
92
95
  }
93
96
 
94
97
  @ReactMethod
95
- public void disableNetwork(String appName, Promise promise) {
98
+ public void disableNetwork(String appName, String databaseId, Promise promise) {
96
99
  module
97
- .disableNetwork(appName)
100
+ .disableNetwork(appName, databaseId)
98
101
  .addOnCompleteListener(
99
102
  task -> {
100
103
  if (task.isSuccessful()) {
@@ -106,9 +109,9 @@ public class ReactNativeFirebaseFirestoreModule extends ReactNativeFirebaseModul
106
109
  }
107
110
 
108
111
  @ReactMethod
109
- public void enableNetwork(String appName, Promise promise) {
112
+ public void enableNetwork(String appName, String databaseId, Promise promise) {
110
113
  module
111
- .enableNetwork(appName)
114
+ .enableNetwork(appName, databaseId)
112
115
  .addOnCompleteListener(
113
116
  task -> {
114
117
  if (task.isSuccessful()) {
@@ -120,9 +123,10 @@ public class ReactNativeFirebaseFirestoreModule extends ReactNativeFirebaseModul
120
123
  }
121
124
 
122
125
  @ReactMethod
123
- public void useEmulator(String appName, String host, int port, Promise promise) {
126
+ public void useEmulator(
127
+ String appName, String databaseId, String host, int port, Promise promise) {
124
128
  module
125
- .useEmulator(appName, host, port)
129
+ .useEmulator(appName, databaseId, host, port)
126
130
  .addOnCompleteListener(
127
131
  task -> {
128
132
  if (task.isSuccessful()) {
@@ -134,9 +138,10 @@ public class ReactNativeFirebaseFirestoreModule extends ReactNativeFirebaseModul
134
138
  }
135
139
 
136
140
  @ReactMethod
137
- public void settings(String appName, ReadableMap settings, Promise promise) {
141
+ public void settings(String appName, String databaseId, ReadableMap settings, Promise promise) {
142
+ String firestoreKey = createFirestoreKey(appName, databaseId);
138
143
  module
139
- .settings(appName, toHashMap(settings))
144
+ .settings(firestoreKey, toHashMap(settings))
140
145
  .addOnCompleteListener(
141
146
  task -> {
142
147
  if (task.isSuccessful()) {
@@ -148,9 +153,9 @@ public class ReactNativeFirebaseFirestoreModule extends ReactNativeFirebaseModul
148
153
  }
149
154
 
150
155
  @ReactMethod
151
- public void terminate(String appName, Promise promise) {
156
+ public void terminate(String appName, String databaseId, Promise promise) {
152
157
  module
153
- .terminate(appName)
158
+ .terminate(appName, databaseId)
154
159
  .addOnCompleteListener(
155
160
  task -> {
156
161
  if (task.isSuccessful()) {
@@ -161,6 +166,33 @@ public class ReactNativeFirebaseFirestoreModule extends ReactNativeFirebaseModul
161
166
  });
162
167
  }
163
168
 
169
+ @ReactMethod
170
+ public void persistenceCacheIndexManager(
171
+ String appName, String databaseId, int requestType, Promise promise) {
172
+ PersistentCacheIndexManager indexManager =
173
+ getFirestoreForApp(appName, databaseId).getPersistentCacheIndexManager();
174
+ if (indexManager != null) {
175
+ switch (requestType) {
176
+ case 0:
177
+ indexManager.enableIndexAutoCreation();
178
+ break;
179
+ case 1:
180
+ indexManager.disableIndexAutoCreation();
181
+ break;
182
+ case 2:
183
+ indexManager.deleteAllIndexes();
184
+ break;
185
+ }
186
+ } else {
187
+ promise.reject(
188
+ "firestore/index-manager-null",
189
+ "`PersistentCacheIndexManager` is not available, persistence has not been enabled for"
190
+ + " Firestore");
191
+ return;
192
+ }
193
+ promise.resolve(null);
194
+ }
195
+
164
196
  private WritableMap taskProgressToWritableMap(LoadBundleTaskProgress progress) {
165
197
  WritableMap writableMap = Arguments.createMap();
166
198
  writableMap.putDouble("bytesLoaded", progress.getBytesLoaded());
@@ -38,10 +38,12 @@ import java.util.concurrent.Executor;
38
38
 
39
39
  public class ReactNativeFirebaseFirestoreQuery {
40
40
  String appName;
41
+ String databaseId;
41
42
  Query query;
42
43
 
43
44
  ReactNativeFirebaseFirestoreQuery(
44
45
  String appName,
46
+ String databaseId,
45
47
  Query query,
46
48
  ReadableArray filters,
47
49
  ReadableArray orders,
@@ -58,7 +60,7 @@ public class ReactNativeFirebaseFirestoreQuery {
58
60
  executor,
59
61
  () -> {
60
62
  QuerySnapshot querySnapshot = Tasks.await(query.get(source));
61
- return snapshotToWritableMap(this.appName, "get", querySnapshot, null);
63
+ return snapshotToWritableMap(this.appName, this.databaseId, "get", querySnapshot, null);
62
64
  });
63
65
  }
64
66
 
@@ -98,7 +98,8 @@ public class ReactNativeFirebaseFirestoreSerialize {
98
98
  * @param documentSnapshot DocumentSnapshot
99
99
  * @return WritableMap
100
100
  */
101
- static WritableMap snapshotToWritableMap(String appName, DocumentSnapshot documentSnapshot) {
101
+ static WritableMap snapshotToWritableMap(
102
+ String appName, String databaseId, DocumentSnapshot documentSnapshot) {
102
103
  WritableArray metadata = Arguments.createArray();
103
104
  WritableMap documentMap = Arguments.createMap();
104
105
  SnapshotMetadata snapshotMetadata = documentSnapshot.getMetadata();
@@ -112,7 +113,7 @@ public class ReactNativeFirebaseFirestoreSerialize {
112
113
  documentMap.putBoolean(KEY_EXISTS, documentSnapshot.exists());
113
114
 
114
115
  DocumentSnapshot.ServerTimestampBehavior timestampBehavior =
115
- getServerTimestampBehavior(appName);
116
+ getServerTimestampBehavior(appName, databaseId);
116
117
 
117
118
  if (documentSnapshot.exists()) {
118
119
  if (documentSnapshot.getData(timestampBehavior) != null) {
@@ -132,6 +133,7 @@ public class ReactNativeFirebaseFirestoreSerialize {
132
133
  */
133
134
  static WritableMap snapshotToWritableMap(
134
135
  String appName,
136
+ String databaseId,
135
137
  String source,
136
138
  QuerySnapshot querySnapshot,
137
139
  @Nullable MetadataChanges metadataChanges) {
@@ -148,7 +150,8 @@ public class ReactNativeFirebaseFirestoreSerialize {
148
150
  // indicating the data does not include these changes
149
151
  writableMap.putBoolean("excludesMetadataChanges", true);
150
152
  writableMap.putArray(
151
- KEY_CHANGES, documentChangesToWritableArray(appName, documentChangesList, null));
153
+ KEY_CHANGES,
154
+ documentChangesToWritableArray(appName, databaseId, documentChangesList, null));
152
155
  } else {
153
156
  // If listening to metadata changes, get the changes list with document changes array.
154
157
  // To indicate whether a document change was because of metadata change, we check whether
@@ -159,7 +162,7 @@ public class ReactNativeFirebaseFirestoreSerialize {
159
162
  writableMap.putArray(
160
163
  KEY_CHANGES,
161
164
  documentChangesToWritableArray(
162
- appName, documentMetadataChangesList, documentChangesList));
165
+ appName, databaseId, documentMetadataChangesList, documentChangesList));
163
166
  }
164
167
 
165
168
  SnapshotMetadata snapshotMetadata = querySnapshot.getMetadata();
@@ -167,7 +170,7 @@ public class ReactNativeFirebaseFirestoreSerialize {
167
170
 
168
171
  // set documents
169
172
  for (DocumentSnapshot documentSnapshot : documentSnapshots) {
170
- documents.pushMap(snapshotToWritableMap(appName, documentSnapshot));
173
+ documents.pushMap(snapshotToWritableMap(appName, databaseId, documentSnapshot));
171
174
  }
172
175
  writableMap.putArray(KEY_DOCUMENTS, documents);
173
176
 
@@ -188,6 +191,7 @@ public class ReactNativeFirebaseFirestoreSerialize {
188
191
  */
189
192
  private static WritableArray documentChangesToWritableArray(
190
193
  String appName,
194
+ String databaseId,
191
195
  List<DocumentChange> documentChanges,
192
196
  @Nullable List<DocumentChange> comparableDocumentChanges) {
193
197
  WritableArray documentChangesWritable = Arguments.createArray();
@@ -212,7 +216,7 @@ public class ReactNativeFirebaseFirestoreSerialize {
212
216
  }
213
217
 
214
218
  documentChangesWritable.pushMap(
215
- documentChangeToWritableMap(appName, documentChange, isMetadataChange));
219
+ documentChangeToWritableMap(appName, databaseId, documentChange, isMetadataChange));
216
220
  }
217
221
 
218
222
  return documentChangesWritable;
@@ -225,7 +229,7 @@ public class ReactNativeFirebaseFirestoreSerialize {
225
229
  * @return WritableMap
226
230
  */
227
231
  private static WritableMap documentChangeToWritableMap(
228
- String appName, DocumentChange documentChange, boolean isMetadataChange) {
232
+ String appName, String databaseId, DocumentChange documentChange, boolean isMetadataChange) {
229
233
  WritableMap documentChangeMap = Arguments.createMap();
230
234
  documentChangeMap.putBoolean("isMetadataChange", isMetadataChange);
231
235
 
@@ -242,7 +246,8 @@ public class ReactNativeFirebaseFirestoreSerialize {
242
246
  }
243
247
 
244
248
  documentChangeMap.putMap(
245
- KEY_DOC_CHANGE_DOCUMENT, snapshotToWritableMap(appName, documentChange.getDocument()));
249
+ KEY_DOC_CHANGE_DOCUMENT,
250
+ snapshotToWritableMap(appName, databaseId, documentChange.getDocument()));
246
251
 
247
252
  documentChangeMap.putInt(KEY_DOC_CHANGE_NEW_INDEX, documentChange.getNewIndex());
248
253
  documentChangeMap.putInt(KEY_DOC_CHANGE_OLD_INDEX, documentChange.getOldIndex());