@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
@@ -17,6 +17,7 @@
17
17
 
18
18
  #import "RNFBFirestoreModule.h"
19
19
  #import <React/RCTUtils.h>
20
+ #import "FirebaseFirestoreInternal/FIRPersistentCacheIndexManager.h"
20
21
  #import "RNFBFirestoreCommon.h"
21
22
  #import "RNFBPreferences.h"
22
23
 
@@ -45,9 +46,10 @@ RCT_EXPORT_METHOD(setLogLevel : (FIRLoggerLevel)loggerLevel) {
45
46
 
46
47
  RCT_EXPORT_METHOD(disableNetwork
47
48
  : (FIRApp *)firebaseApp
49
+ : (NSString *)databaseId
48
50
  : (RCTPromiseResolveBlock)resolve
49
51
  : (RCTPromiseRejectBlock)reject) {
50
- [[RNFBFirestoreCommon getFirestoreForApp:firebaseApp]
52
+ [[RNFBFirestoreCommon getFirestoreForApp:firebaseApp databaseId:databaseId]
51
53
  disableNetworkWithCompletion:^(NSError *error) {
52
54
  if (error) {
53
55
  [RNFBFirestoreCommon promiseRejectFirestoreException:reject error:error];
@@ -59,9 +61,10 @@ RCT_EXPORT_METHOD(disableNetwork
59
61
 
60
62
  RCT_EXPORT_METHOD(enableNetwork
61
63
  : (FIRApp *)firebaseApp
64
+ : (NSString *)databaseId
62
65
  : (RCTPromiseResolveBlock)resolve
63
66
  : (RCTPromiseRejectBlock)reject) {
64
- [[RNFBFirestoreCommon getFirestoreForApp:firebaseApp]
67
+ [[RNFBFirestoreCommon getFirestoreForApp:firebaseApp databaseId:databaseId]
65
68
  enableNetworkWithCompletion:^(NSError *error) {
66
69
  if (error) {
67
70
  [RNFBFirestoreCommon promiseRejectFirestoreException:reject error:error];
@@ -73,36 +76,40 @@ RCT_EXPORT_METHOD(enableNetwork
73
76
 
74
77
  RCT_EXPORT_METHOD(settings
75
78
  : (FIRApp *)firebaseApp
79
+ : (NSString *)databaseId
76
80
  : (NSDictionary *)settings
77
81
  : (RCTPromiseResolveBlock)resolve
78
82
  : (RCTPromiseRejectBlock)reject) {
79
83
  NSString *appName = [RNFBSharedUtils getAppJavaScriptName:firebaseApp.name];
84
+ NSString *firestoreKey = [RNFBFirestoreCommon createFirestoreKeyWithAppName:appName
85
+ databaseId:databaseId];
80
86
 
81
87
  if (settings[@"cacheSizeBytes"]) {
82
- NSString *cacheKey = [NSString stringWithFormat:@"%@_%@", FIRESTORE_CACHE_SIZE, appName];
88
+ NSString *cacheKey = [NSString stringWithFormat:@"%@_%@", FIRESTORE_CACHE_SIZE, firestoreKey];
83
89
  [[RNFBPreferences shared] setIntegerValue:cacheKey
84
90
  integerValue:[settings[@"cacheSizeBytes"] integerValue]];
85
91
  }
86
92
 
87
93
  if (settings[@"host"]) {
88
- NSString *hostKey = [NSString stringWithFormat:@"%@_%@", FIRESTORE_HOST, appName];
94
+ NSString *hostKey = [NSString stringWithFormat:@"%@_%@", FIRESTORE_HOST, firestoreKey];
89
95
  [[RNFBPreferences shared] setStringValue:hostKey stringValue:settings[@"host"]];
90
96
  }
91
97
 
92
98
  if (settings[@"persistence"]) {
93
- NSString *persistenceKey = [NSString stringWithFormat:@"%@_%@", FIRESTORE_PERSISTENCE, appName];
99
+ NSString *persistenceKey =
100
+ [NSString stringWithFormat:@"%@_%@", FIRESTORE_PERSISTENCE, firestoreKey];
94
101
  [[RNFBPreferences shared] setBooleanValue:persistenceKey
95
102
  boolValue:[settings[@"persistence"] boolValue]];
96
103
  }
97
104
 
98
105
  if (settings[@"ssl"]) {
99
- NSString *sslKey = [NSString stringWithFormat:@"%@_%@", FIRESTORE_SSL, appName];
106
+ NSString *sslKey = [NSString stringWithFormat:@"%@_%@", FIRESTORE_SSL, firestoreKey];
100
107
  [[RNFBPreferences shared] setBooleanValue:sslKey boolValue:[settings[@"ssl"] boolValue]];
101
108
  }
102
109
 
103
110
  if (settings[@"serverTimestampBehavior"]) {
104
111
  NSString *key =
105
- [NSString stringWithFormat:@"%@_%@", FIRESTORE_SERVER_TIMESTAMP_BEHAVIOR, appName];
112
+ [NSString stringWithFormat:@"%@_%@", FIRESTORE_SERVER_TIMESTAMP_BEHAVIOR, firestoreKey];
106
113
  [[RNFBPreferences shared] setStringValue:key stringValue:settings[@"serverTimestampBehavior"]];
107
114
  }
108
115
 
@@ -111,11 +118,12 @@ RCT_EXPORT_METHOD(settings
111
118
 
112
119
  RCT_EXPORT_METHOD(loadBundle
113
120
  : (FIRApp *)firebaseApp
121
+ : (NSString *)databaseId
114
122
  : (nonnull NSString *)bundle
115
123
  : (RCTPromiseResolveBlock)resolve
116
124
  : (RCTPromiseRejectBlock)reject) {
117
125
  NSData *bundleData = [bundle dataUsingEncoding:NSUTF8StringEncoding];
118
- [[RNFBFirestoreCommon getFirestoreForApp:firebaseApp]
126
+ [[RNFBFirestoreCommon getFirestoreForApp:firebaseApp databaseId:databaseId]
119
127
  loadBundle:bundleData
120
128
  completion:^(FIRLoadBundleTaskProgress *progress, NSError *error) {
121
129
  if (error) {
@@ -128,9 +136,10 @@ RCT_EXPORT_METHOD(loadBundle
128
136
 
129
137
  RCT_EXPORT_METHOD(clearPersistence
130
138
  : (FIRApp *)firebaseApp
139
+ : (NSString *)databaseId
131
140
  : (RCTPromiseResolveBlock)resolve
132
141
  : (RCTPromiseRejectBlock)reject) {
133
- [[RNFBFirestoreCommon getFirestoreForApp:firebaseApp]
142
+ [[RNFBFirestoreCommon getFirestoreForApp:firebaseApp databaseId:databaseId]
134
143
  clearPersistenceWithCompletion:^(NSError *error) {
135
144
  if (error) {
136
145
  [RNFBFirestoreCommon promiseRejectFirestoreException:reject error:error];
@@ -142,15 +151,20 @@ RCT_EXPORT_METHOD(clearPersistence
142
151
 
143
152
  RCT_EXPORT_METHOD(useEmulator
144
153
  : (FIRApp *)firebaseApp
154
+ : (NSString *)databaseId
145
155
  : (nonnull NSString *)host
146
156
  : (NSInteger)port) {
147
157
  if (emulatorConfigs == nil) {
148
158
  emulatorConfigs = [[NSMutableDictionary alloc] init];
149
159
  }
150
- if (!emulatorConfigs[firebaseApp.name]) {
151
- FIRFirestore *firestore = [RNFBFirestoreCommon getFirestoreForApp:firebaseApp];
160
+
161
+ NSString *firestoreKey = [RNFBFirestoreCommon createFirestoreKeyWithAppName:firebaseApp.name
162
+ databaseId:databaseId];
163
+ if (!emulatorConfigs[firestoreKey]) {
164
+ FIRFirestore *firestore = [RNFBFirestoreCommon getFirestoreForApp:firebaseApp
165
+ databaseId:databaseId];
152
166
  [firestore useEmulatorWithHost:host port:port];
153
- emulatorConfigs[firebaseApp.name] = @YES;
167
+ emulatorConfigs[firestoreKey] = @YES;
154
168
 
155
169
  // It is not sufficient to just use emulator. You have toggle SSL off too.
156
170
  FIRFirestoreSettings *settings = firestore.settings;
@@ -161,9 +175,10 @@ RCT_EXPORT_METHOD(useEmulator
161
175
 
162
176
  RCT_EXPORT_METHOD(waitForPendingWrites
163
177
  : (FIRApp *)firebaseApp
178
+ : (NSString *)databaseId
164
179
  : (RCTPromiseResolveBlock)resolve
165
180
  : (RCTPromiseRejectBlock)reject) {
166
- [[RNFBFirestoreCommon getFirestoreForApp:firebaseApp]
181
+ [[RNFBFirestoreCommon getFirestoreForApp:firebaseApp databaseId:databaseId]
167
182
  waitForPendingWritesWithCompletion:^(NSError *error) {
168
183
  if (error) {
169
184
  [RNFBFirestoreCommon promiseRejectFirestoreException:reject error:error];
@@ -175,20 +190,56 @@ RCT_EXPORT_METHOD(waitForPendingWrites
175
190
 
176
191
  RCT_EXPORT_METHOD(terminate
177
192
  : (FIRApp *)firebaseApp
193
+ : (NSString *)databaseId
178
194
  : (RCTPromiseResolveBlock)resolve
179
195
  : (RCTPromiseRejectBlock)reject) {
180
- FIRFirestore *instance = [RNFBFirestoreCommon getFirestoreForApp:firebaseApp];
196
+ FIRFirestore *instance = [RNFBFirestoreCommon getFirestoreForApp:firebaseApp
197
+ databaseId:databaseId];
181
198
 
182
199
  [instance terminateWithCompletion:^(NSError *error) {
183
200
  if (error) {
184
201
  [RNFBFirestoreCommon promiseRejectFirestoreException:reject error:error];
185
202
  } else {
186
- [instanceCache removeObjectForKey:[firebaseApp name]];
203
+ NSString *firestoreKey = [RNFBFirestoreCommon createFirestoreKeyWithAppName:firebaseApp.name
204
+ databaseId:databaseId];
205
+ [instanceCache removeObjectForKey:firestoreKey];
187
206
  resolve(nil);
188
207
  }
189
208
  }];
190
209
  }
191
210
 
211
+ RCT_EXPORT_METHOD(persistenceCacheIndexManager
212
+ : (FIRApp *)firebaseApp
213
+ : (NSString *)databaseId
214
+ : (NSInteger)requestType
215
+ : (RCTPromiseResolveBlock)resolve
216
+ : (RCTPromiseRejectBlock)reject) {
217
+ FIRPersistentCacheIndexManager *persistentCacheIndexManager =
218
+ [RNFBFirestoreCommon getFirestoreForApp:firebaseApp databaseId:databaseId]
219
+ .persistentCacheIndexManager;
220
+
221
+ if (persistentCacheIndexManager) {
222
+ switch (requestType) {
223
+ case 0:
224
+ [persistentCacheIndexManager enableIndexAutoCreation];
225
+ break;
226
+ case 1:
227
+ [persistentCacheIndexManager disableIndexAutoCreation];
228
+ break;
229
+ case 2:
230
+ [persistentCacheIndexManager deleteAllIndexes];
231
+ break;
232
+ }
233
+ } else {
234
+ reject(@"firestore/index-manager-null",
235
+ @"`PersistentCacheIndexManager` is not available, persistence has not been enabled for "
236
+ @"Firestore",
237
+ nil);
238
+ return;
239
+ }
240
+ resolve(nil);
241
+ }
242
+
192
243
  - (NSMutableDictionary *)taskProgressToDictionary:(FIRLoadBundleTaskProgress *)progress {
193
244
  NSMutableDictionary *progressMap = [[NSMutableDictionary alloc] init];
194
245
  progressMap[@"bytesLoaded"] = @(progress.bytesLoaded);
@@ -24,14 +24,16 @@
24
24
  + (NSDictionary *)querySnapshotToDictionary:(NSString *)source
25
25
  snapshot:(FIRQuerySnapshot *)snapshot
26
26
  includeMetadataChanges:(BOOL)includeMetadataChanges
27
- appName:(NSString *)appName;
27
+ appName:(NSString *)appName
28
+ databaseId:(NSString *)databaseId;
28
29
 
29
30
  + (NSDictionary *)documentChangeToDictionary:(FIRDocumentChange *)documentChange
30
31
  isMetadataChange:(BOOL)isMetadataChange
31
- appName:(NSString *)appName;
32
+ appName:(NSString *)appName
33
+ databaseId:(NSString *)databaseId;
32
34
 
33
35
  + (NSDictionary *)documentSnapshotToDictionary:(FIRDocumentSnapshot *)snapshot
34
- appName:(NSString *)appName;
36
+ firestoreKey:(NSString *)firestoreKey;
35
37
 
36
38
  + (NSDictionary *)serializeDictionary:(NSDictionary *)dictionary;
37
39
 
@@ -65,7 +65,8 @@ enum TYPE_MAP {
65
65
  + (NSDictionary *)querySnapshotToDictionary:(NSString *)source
66
66
  snapshot:(FIRQuerySnapshot *)snapshot
67
67
  includeMetadataChanges:(BOOL)includeMetadataChanges
68
- appName:(NSString *)appName {
68
+ appName:(NSString *)appName
69
+ databaseId:(NSString *)databaseId {
69
70
  NSMutableArray *metadata = [[NSMutableArray alloc] init];
70
71
  NSMutableDictionary *snapshotMap = [[NSMutableDictionary alloc] init];
71
72
 
@@ -84,7 +85,8 @@ enum TYPE_MAP {
84
85
  for (FIRDocumentChange *documentChange in documentChangesList) {
85
86
  [changes addObject:[self documentChangeToDictionary:documentChange
86
87
  isMetadataChange:false
87
- appName:appName]];
88
+ appName:appName
89
+ databaseId:databaseId]];
88
90
  }
89
91
  } else {
90
92
  // If listening to metadata changes, get the changes list with document changes array.
@@ -119,16 +121,19 @@ enum TYPE_MAP {
119
121
 
120
122
  [changes addObject:[self documentChangeToDictionary:documentMetadataChange
121
123
  isMetadataChange:isMetadataChange
122
- appName:appName]];
124
+ appName:appName
125
+ databaseId:databaseId]];
123
126
  }
124
127
  }
125
128
 
126
129
  snapshotMap[KEY_CHANGES] = changes;
127
-
130
+ NSString *firestoreKey = [RNFBFirestoreCommon createFirestoreKeyWithAppName:appName
131
+ databaseId:databaseId];
128
132
  // set documents
129
133
  NSMutableArray *documents = [[NSMutableArray alloc] init];
130
134
  for (FIRDocumentSnapshot *documentSnapshot in documentSnapshots) {
131
- [documents addObject:[self documentSnapshotToDictionary:documentSnapshot appName:appName]];
135
+ [documents addObject:[self documentSnapshotToDictionary:documentSnapshot
136
+ firestoreKey:firestoreKey]];
132
137
  }
133
138
  snapshotMap[KEY_DOCUMENTS] = documents;
134
139
 
@@ -143,7 +148,8 @@ enum TYPE_MAP {
143
148
 
144
149
  + (NSDictionary *)documentChangeToDictionary:(FIRDocumentChange *)documentChange
145
150
  isMetadataChange:(BOOL)isMetadataChange
146
- appName:(NSString *)appName {
151
+ appName:(NSString *)appName
152
+ databaseId:(NSString *)databaseId {
147
153
  NSMutableDictionary *changeMap = [[NSMutableDictionary alloc] init];
148
154
  changeMap[@"isMetadataChange"] = @(isMetadataChange);
149
155
 
@@ -154,9 +160,10 @@ enum TYPE_MAP {
154
160
  } else {
155
161
  changeMap[KEY_DOC_CHANGE_TYPE] = CHANGE_REMOVED;
156
162
  }
157
-
163
+ NSString *firestoreKey = [RNFBFirestoreCommon createFirestoreKeyWithAppName:appName
164
+ databaseId:databaseId];
158
165
  changeMap[KEY_DOC_CHANGE_DOCUMENT] = [self documentSnapshotToDictionary:documentChange.document
159
- appName:appName];
166
+ firestoreKey:firestoreKey];
160
167
 
161
168
  // Note the Firestore C++ SDK here returns a maxed UInt that is != NSUIntegerMax, so we make one
162
169
  // ourselves so we can convert to -1 for JS land
@@ -180,7 +187,7 @@ enum TYPE_MAP {
180
187
 
181
188
  // Native DocumentSnapshot -> NSDictionary (for JS)
182
189
  + (NSDictionary *)documentSnapshotToDictionary:(FIRDocumentSnapshot *)snapshot
183
- appName:(NSString *)appName {
190
+ firestoreKey:(NSString *)firestoreKey {
184
191
  NSMutableArray *metadata = [[NSMutableArray alloc] init];
185
192
  NSMutableDictionary *documentMap = [[NSMutableDictionary alloc] init];
186
193
 
@@ -194,7 +201,7 @@ enum TYPE_MAP {
194
201
 
195
202
  if (snapshot.exists) {
196
203
  NSString *key =
197
- [NSString stringWithFormat:@"%@_%@", FIRESTORE_SERVER_TIMESTAMP_BEHAVIOR, appName];
204
+ [NSString stringWithFormat:@"%@_%@", FIRESTORE_SERVER_TIMESTAMP_BEHAVIOR, firestoreKey];
198
205
  NSString *behavior = [[RNFBPreferences shared] getStringValue:key defaultValue:@"none"];
199
206
 
200
207
  FIRServerTimestampBehavior serverTimestampBehavior;
@@ -61,6 +61,7 @@ RCT_EXPORT_MODULE();
61
61
 
62
62
  RCT_EXPORT_METHOD(transactionGetDocument
63
63
  : (FIRApp *)firebaseApp
64
+ : (NSString *)databaseId
64
65
  : (nonnull NSNumber *)transactionId
65
66
  : (NSString *)path
66
67
  : (RCTPromiseResolveBlock)resolve
@@ -75,7 +76,8 @@ RCT_EXPORT_METHOD(transactionGetDocument
75
76
 
76
77
  NSError *error = nil;
77
78
  FIRTransaction *transaction = [transactionState valueForKey:@"transaction"];
78
- FIRFirestore *firestore = [RNFBFirestoreCommon getFirestoreForApp:firebaseApp];
79
+ FIRFirestore *firestore = [RNFBFirestoreCommon getFirestoreForApp:firebaseApp
80
+ databaseId:databaseId];
79
81
  FIRDocumentReference *ref = [RNFBFirestoreCommon getDocumentForFirestore:firestore path:path];
80
82
  FIRDocumentSnapshot *snapshot = [transaction getDocument:ref error:&error];
81
83
 
@@ -83,8 +85,10 @@ RCT_EXPORT_METHOD(transactionGetDocument
83
85
  [RNFBFirestoreCommon promiseRejectFirestoreException:reject error:error];
84
86
  } else {
85
87
  NSString *appName = [RNFBSharedUtils getAppJavaScriptName:firebaseApp.name];
86
- NSDictionary *snapshotDict = [RNFBFirestoreSerialize documentSnapshotToDictionary:snapshot
87
- appName:appName];
88
+ NSString *firestoreKey = [RNFBFirestoreCommon createFirestoreKeyWithAppName:appName
89
+ databaseId:databaseId];
90
+ NSDictionary *snapshotDict =
91
+ [RNFBFirestoreSerialize documentSnapshotToDictionary:snapshot firestoreKey:firestoreKey];
88
92
  NSString *snapshotPath = snapshotDict[@"path"];
89
93
 
90
94
  if (snapshotPath == nil) {
@@ -96,7 +100,10 @@ RCT_EXPORT_METHOD(transactionGetDocument
96
100
  }
97
101
  }
98
102
 
99
- RCT_EXPORT_METHOD(transactionDispose : (FIRApp *)firebaseApp : (nonnull NSNumber *)transactionId) {
103
+ RCT_EXPORT_METHOD(transactionDispose
104
+ : (FIRApp *)firebaseApp
105
+ : (NSString *)databaseId
106
+ : (nonnull NSNumber *)transactionId) {
100
107
  @synchronized(transactions[[transactionId stringValue]]) {
101
108
  NSMutableDictionary *transactionState = transactions[[transactionId stringValue]];
102
109
 
@@ -112,6 +119,7 @@ RCT_EXPORT_METHOD(transactionDispose : (FIRApp *)firebaseApp : (nonnull NSNumber
112
119
 
113
120
  RCT_EXPORT_METHOD(transactionApplyBuffer
114
121
  : (FIRApp *)firebaseApp
122
+ : (NSString *)databaseId
115
123
  : (nonnull NSNumber *)transactionId
116
124
  : (NSArray *)commandBuffer) {
117
125
  @synchronized(transactions[[transactionId stringValue]]) {
@@ -128,8 +136,12 @@ RCT_EXPORT_METHOD(transactionApplyBuffer
128
136
  }
129
137
  }
130
138
 
131
- RCT_EXPORT_METHOD(transactionBegin : (FIRApp *)firebaseApp : (nonnull NSNumber *)transactionId) {
132
- FIRFirestore *firestore = [RNFBFirestoreCommon getFirestoreForApp:firebaseApp];
139
+ RCT_EXPORT_METHOD(transactionBegin
140
+ : (FIRApp *)firebaseApp
141
+ : (NSString *)databaseId
142
+ : (nonnull NSNumber *)transactionId) {
143
+ FIRFirestore *firestore = [RNFBFirestoreCommon getFirestoreForApp:firebaseApp
144
+ databaseId:databaseId];
133
145
  __block BOOL aborted = false;
134
146
  __block NSMutableDictionary *transactionState = [NSMutableDictionary new];
135
147
 
@@ -153,6 +165,7 @@ RCT_EXPORT_METHOD(transactionBegin : (FIRApp *)firebaseApp : (nonnull NSNumber *
153
165
  body:@{
154
166
  @"listenerId" : transactionId,
155
167
  @"appName" : [RNFBSharedUtils getAppJavaScriptName:firebaseApp.name],
168
+ @"databaseId" : databaseId,
156
169
  @"body" : eventMap,
157
170
  }];
158
171
  });
@@ -241,6 +254,7 @@ RCT_EXPORT_METHOD(transactionBegin : (FIRApp *)firebaseApp : (nonnull NSNumber *
241
254
  body:@{
242
255
  @"listenerId" : transactionId,
243
256
  @"appName" : [RNFBSharedUtils getAppJavaScriptName:firebaseApp.name],
257
+ @"databaseId" : databaseId,
244
258
  @"body" : eventMap,
245
259
  }];
246
260
  }
@@ -252,4 +266,4 @@ RCT_EXPORT_METHOD(transactionBegin : (FIRApp *)firebaseApp : (nonnull NSNumber *
252
266
  [firestore runTransactionWithBlock:transactionBlock completion:completionBlock];
253
267
  }
254
268
 
255
- @end
269
+ @end
@@ -0,0 +1,34 @@
1
+ /*
2
+ * Copyright (c) 2016-present Invertase Limited & Contributors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this library except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ *
16
+ */
17
+
18
+ export default class FirestorePersistentCacheIndexManager {
19
+ constructor(firestore) {
20
+ this._firestore = firestore;
21
+ }
22
+
23
+ async enableIndexAutoCreation() {
24
+ await this._firestore.native.persistenceCacheIndexManager(0);
25
+ }
26
+
27
+ async disableIndexAutoCreation() {
28
+ await this._firestore.native.persistenceCacheIndexManager(1);
29
+ }
30
+
31
+ async deleteAllIndexes() {
32
+ await this._firestore.native.persistenceCacheIndexManager(2);
33
+ }
34
+ }
package/lib/index.d.ts CHANGED
@@ -2005,6 +2005,29 @@ export namespace FirebaseFirestoreTypes {
2005
2005
  ): WriteBatch;
2006
2006
  }
2007
2007
 
2008
+ /**
2009
+ * Returns the PersistentCache Index Manager used by the given Firestore object.
2010
+ * The PersistentCacheIndexManager instance, or null if local persistent storage is not in use.
2011
+ */
2012
+ export interface PersistentCacheIndexManager {
2013
+ /**
2014
+ * Enables the SDK to create persistent cache indexes automatically for local query
2015
+ * execution when the SDK believes cache indexes can help improves performance.
2016
+ * This feature is disabled by default.
2017
+ */
2018
+ enableIndexAutoCreation(): Promise<void>;
2019
+ /**
2020
+ * Stops creating persistent cache indexes automatically for local query execution.
2021
+ * The indexes which have been created by calling `enableIndexAutoCreation()` still take effect.
2022
+ */
2023
+ disableIndexAutoCreation(): Promise<void>;
2024
+ /**
2025
+ * Removes all persistent cache indexes. Note this function also deletes indexes
2026
+ * generated by `setIndexConfiguration()`, which is deprecated.
2027
+ */
2028
+ deleteAllIndexes(): Promise<void>;
2029
+ }
2030
+
2008
2031
  /**
2009
2032
  * Represents the state of bundle loading tasks.
2010
2033
  *
@@ -2321,6 +2344,14 @@ export namespace FirebaseFirestoreTypes {
2321
2344
  * @param port: emulator port (eg, 8080)
2322
2345
  */
2323
2346
  useEmulator(host: string, port: number): void;
2347
+
2348
+ /**
2349
+ * Gets the `PersistentCacheIndexManager` instance used by this Cloud Firestore instance.
2350
+ * This is not the same as Cloud Firestore Indexes.
2351
+ * Persistent cache indexes are optional indexes that only exist within the SDK to assist in local query execution.
2352
+ * Returns `null` if local persistent storage is not in use.
2353
+ */
2354
+ persistentCacheIndexManager(): PersistentCacheIndexManager | null;
2324
2355
  }
2325
2356
 
2326
2357
  /**
@@ -2367,7 +2398,7 @@ declare module '@react-native-firebase/app' {
2367
2398
  >;
2368
2399
  }
2369
2400
  interface FirebaseApp {
2370
- firestore(): FirebaseFirestoreTypes.Module;
2401
+ firestore(databaseId?: string): FirebaseFirestoreTypes.Module;
2371
2402
  }
2372
2403
  }
2373
2404
  }
package/lib/index.js CHANGED
@@ -40,6 +40,7 @@ import FirestoreTransactionHandler from './FirestoreTransactionHandler';
40
40
  import FirestoreWriteBatch from './FirestoreWriteBatch';
41
41
  import version from './version';
42
42
  import fallBackModule from './web/RNFBFirestoreModule';
43
+ import FirestorePersistentCacheIndexManager from './FirestorePersistentCacheIndexManager';
43
44
 
44
45
  const namespace = 'firestore';
45
46
 
@@ -57,8 +58,13 @@ const nativeEvents = [
57
58
  ];
58
59
 
59
60
  class FirebaseFirestoreModule extends FirebaseModule {
60
- constructor(app, config) {
61
+ constructor(app, config, databaseId) {
61
62
  super(app, config);
63
+ if (isString(databaseId) || databaseId === undefined) {
64
+ this._customUrlOrRegion = databaseId || '(default)';
65
+ } else if (!isString(databaseId)) {
66
+ throw new Error('firebase.app().firestore(*) database ID must be a string');
67
+ }
62
68
  this._referencePath = new FirestorePath();
63
69
  this._transactionHandler = new FirestoreTransactionHandler(this);
64
70
 
@@ -79,8 +85,13 @@ class FirebaseFirestoreModule extends FirebaseModule {
79
85
 
80
86
  this._settings = {
81
87
  ignoreUndefinedProperties: false,
88
+ persistence: true,
82
89
  };
83
90
  }
91
+ // We override the FirebaseModule's `eventNameForApp()` method to include the customUrlOrRegion
92
+ eventNameForApp(...args) {
93
+ return `${this.app.name}-${this._customUrlOrRegion}-${args.join('-')}`;
94
+ }
84
95
 
85
96
  batch() {
86
97
  return new FirestoreWriteBatch(this);
@@ -354,8 +365,20 @@ class FirebaseFirestoreModule extends FirebaseModule {
354
365
  delete settings.ignoreUndefinedProperties;
355
366
  }
356
367
 
368
+ if (settings.persistence === false) {
369
+ // Required for persistentCacheIndexManager(), if this setting is `false`, it returns `null`
370
+ this._settings.persistence = false;
371
+ }
372
+
357
373
  return this.native.settings(settings);
358
374
  }
375
+
376
+ persistentCacheIndexManager() {
377
+ if (this._settings.persistence === false) {
378
+ return null;
379
+ }
380
+ return new FirestorePersistentCacheIndexManager(this);
381
+ }
359
382
  }
360
383
 
361
384
  // import { SDK_VERSION } from '@react-native-firebase/firestore';
@@ -372,7 +395,7 @@ export default createModuleNamespace({
372
395
  nativeModuleName,
373
396
  nativeEvents,
374
397
  hasMultiAppSupport: true,
375
- hasCustomUrlOrRegionSupport: false,
398
+ hasCustomUrlOrRegionSupport: true,
376
399
  ModuleClass: FirebaseFirestoreModule,
377
400
  });
378
401
 
@@ -9,6 +9,7 @@ import DocumentData = FirebaseFirestoreTypes.DocumentData;
9
9
  import Query = FirebaseFirestoreTypes.Query;
10
10
  import FieldValue = FirebaseFirestoreTypes.FieldValue;
11
11
  import FieldPath = FirebaseFirestoreTypes.FieldPath;
12
+ import PersistentCacheIndexManager = FirebaseFirestoreTypes.PersistentCacheIndexManager;
12
13
 
13
14
  /** Primitive types. */
14
15
  export type Primitive = string | number | boolean | undefined | null;
@@ -117,6 +118,18 @@ export declare function getFirestore(app: FirebaseApp): Firestore;
117
118
 
118
119
  export function getFirestore(app?: FirebaseApp): Firestore;
119
120
 
121
+ /**
122
+ * Returns the existing default {@link Firestore} instance that is associated with the
123
+ * provided {@link @firebase/app#FirebaseApp} and database ID. If no instance exists, initializes a new
124
+ * instance with default settings.
125
+ *
126
+ * @param app - The {@link @firebase/app#FirebaseApp} instance that the returned {@link Firestore}
127
+ * instance is associated with.
128
+ * @param databaseId - The ID of the Firestore database to use. If not provided, the default database is used.
129
+ * @returns The {@link Firestore}
130
+ */
131
+ export declare function getFirestore(app?: FirebaseApp, databaseId?: string): Firestore;
132
+
120
133
  /**
121
134
  * Gets a `DocumentReference` instance that refers to the document at the
122
135
  * specified absolute path.
@@ -533,6 +546,45 @@ export function namedQuery(firestore: Firestore, name: string): Query<DocumentDa
533
546
  */
534
547
  export function writeBatch(firestore: Firestore): FirebaseFirestoreTypes.WriteBatch;
535
548
 
549
+ /**
550
+ * Gets the `PersistentCacheIndexManager` instance used by this Cloud Firestore instance.
551
+ * This is not the same as Cloud Firestore Indexes.
552
+ * Persistent cache indexes are optional indexes that only exist within the SDK to assist in local query execution.
553
+ * @param {Firestore} - The Firestore instance.
554
+ * @return {PersistentCacheIndexManager | null} - The `PersistentCacheIndexManager` instance or `null` if local persistent storage is not in use.
555
+ */
556
+ export function getPersistentCacheIndexManager(
557
+ firestore: Firestore,
558
+ ): PersistentCacheIndexManager | null;
559
+ /**
560
+ * Enables the SDK to create persistent cache indexes automatically for local query
561
+ * execution when the SDK believes cache indexes can help improves performance.
562
+ * This feature is disabled by default.
563
+ * @param {PersistentCacheIndexManager} - The `PersistentCacheIndexManager` instance.
564
+ * @return {Promise<void>} - A promise that resolves when the operation is complete.
565
+ */
566
+ export function enablePersistentCacheIndexAutoCreation(
567
+ indexManager: PersistentCacheIndexManager,
568
+ ): Promise<void>;
569
+ /**
570
+ * Stops creating persistent cache indexes automatically for local query execution.
571
+ * The indexes which have been created by calling `enableIndexAutoCreation()` still take effect.
572
+ * @param {PersistentCacheIndexManager} - The `PersistentCacheIndexManager` instance.
573
+ * @return {Promise<void>} - A promise that resolves when the operation is complete.
574
+ */
575
+ export function disablePersistentCacheIndexAutoCreation(
576
+ indexManager: PersistentCacheIndexManager,
577
+ ): Promise<void>;
578
+ /**
579
+ * Removes all persistent cache indexes. Note this function also deletes indexes
580
+ * generated by `setIndexConfiguration()`, which is deprecated.
581
+ * @param {PersistentCacheIndexManager} - The `PersistentCacheIndexManager` instance.
582
+ * @return {Promise<void>} - A promise that resolves when the operation is complete.
583
+ */
584
+ export function deleteAllPersistentCacheIndexes(
585
+ indexManager: PersistentCacheIndexManager,
586
+ ): Promise<void>;
587
+
536
588
  export * from './query';
537
589
  export * from './snapshot';
538
590
  export * from './Bytes';