@umituz/react-native-firebase 3.0.11 → 3.0.13
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.
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/react-native-firebase",
|
|
3
|
-
"version": "3.0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "3.0.13",
|
|
4
|
+
"description": "Firebase package for React Native mobile apps - Auth and Firestore services using Firebase JS SDK. No web or Node.js support. Memory-only caching (no IndexedDB).",
|
|
5
5
|
"main": "./src/index.ts",
|
|
6
6
|
"types": "./src/index.ts",
|
|
7
7
|
"scripts": {
|
package/src/domains/firestore/infrastructure/config/initializers/FirebaseFirestoreInitializer.ts
CHANGED
|
@@ -1,259 +1,141 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Firebase Firestore Initializer
|
|
2
|
+
* Firebase Firestore Initializer for React Native
|
|
3
3
|
*
|
|
4
|
-
* Single Responsibility: Initialize Firestore instance with
|
|
4
|
+
* Single Responsibility: Initialize Firestore instance with memory caching
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
* -
|
|
8
|
-
* -
|
|
9
|
-
* -
|
|
10
|
-
* -
|
|
6
|
+
* IMPORTANT: This package is designed for React Native mobile applications ONLY.
|
|
7
|
+
* - No web support
|
|
8
|
+
* - No Node.js support
|
|
9
|
+
* - No IndexedDB (idb package) support
|
|
10
|
+
* - Memory-only caching for optimal performance
|
|
11
11
|
*
|
|
12
|
-
*
|
|
12
|
+
* React Native does not support IndexedDB, so we use Firebase's memory cache.
|
|
13
13
|
*/
|
|
14
14
|
|
|
15
15
|
import {
|
|
16
16
|
getFirestore,
|
|
17
17
|
initializeFirestore,
|
|
18
18
|
memoryLocalCache,
|
|
19
|
-
persistentLocalCache,
|
|
20
|
-
type FirestoreSettings,
|
|
21
19
|
} from 'firebase/firestore';
|
|
22
20
|
import type { Firestore } from 'firebase/firestore';
|
|
23
21
|
import type { FirebaseApp } from 'firebase/app';
|
|
24
22
|
|
|
25
23
|
/**
|
|
26
|
-
*
|
|
24
|
+
* Firestore configuration options for React Native
|
|
27
25
|
*/
|
|
28
|
-
export interface
|
|
29
|
-
/**
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
forceMemoryCache?: boolean;
|
|
26
|
+
export interface FirestoreConfig {
|
|
27
|
+
/** Custom settings to pass to Firestore */
|
|
28
|
+
settings?: {
|
|
29
|
+
cacheSizeBytes?: number;
|
|
30
|
+
ignoreUndefinedProperties?: boolean;
|
|
31
|
+
};
|
|
35
32
|
}
|
|
36
33
|
|
|
37
34
|
/**
|
|
38
|
-
* Default
|
|
39
|
-
* Optimized for cost savings while maintaining performance
|
|
35
|
+
* Default configuration for React Native
|
|
40
36
|
*/
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Platform detection utilities
|
|
49
|
-
*/
|
|
50
|
-
const Platform = {
|
|
51
|
-
isWeb(): boolean {
|
|
52
|
-
return typeof window !== 'undefined' && typeof window.indexedDB !== 'undefined';
|
|
53
|
-
},
|
|
54
|
-
|
|
55
|
-
isReactNative(): boolean {
|
|
56
|
-
return typeof navigator !== 'undefined' && navigator.product === 'ReactNative';
|
|
57
|
-
},
|
|
58
|
-
|
|
59
|
-
isNode(): boolean {
|
|
60
|
-
return typeof process !== 'undefined' && process.versions?.node !== undefined;
|
|
37
|
+
const DEFAULT_CONFIG: Required<FirestoreConfig> = {
|
|
38
|
+
settings: {
|
|
39
|
+
cacheSizeBytes: 10 * 1024 * 1024, // 10 MB
|
|
40
|
+
ignoreUndefinedProperties: true,
|
|
61
41
|
},
|
|
62
42
|
};
|
|
63
43
|
|
|
64
44
|
/**
|
|
65
|
-
* Creates
|
|
66
|
-
*
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
// Note: cacheSizeBytes is deprecated, must be specified in cache object
|
|
72
|
-
const cacheConfig = persistentLocalCache({
|
|
73
|
-
cacheSizeBytes: config.cacheSizeBytes,
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
return {
|
|
77
|
-
localCache: cacheConfig,
|
|
78
|
-
};
|
|
79
|
-
} catch (error) {
|
|
80
|
-
// If persistent cache fails, fall back to memory cache
|
|
81
|
-
if (__DEV__) {
|
|
82
|
-
console.warn('[Firestore] Persistent cache failed, using memory cache:', error);
|
|
83
|
-
}
|
|
84
|
-
return createMemoryCacheConfig(config);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Creates optimized memory cache configuration for React Native
|
|
90
|
-
* Uses memory cache for platforms without IndexedDB support
|
|
45
|
+
* Creates memory cache configuration for React Native
|
|
46
|
+
*
|
|
47
|
+
* React Native uses memory cache because:
|
|
48
|
+
* 1. IndexedDB is not supported in React Native
|
|
49
|
+
* 2. The idb package causes import errors in React Native
|
|
50
|
+
* 3. Memory cache is sufficient for mobile app use cases
|
|
91
51
|
*/
|
|
92
|
-
function createMemoryCacheConfig(
|
|
93
|
-
// Memory cache - Firebase SDK manages cache size automatically
|
|
94
|
-
const cacheConfig = memoryLocalCache();
|
|
95
|
-
|
|
52
|
+
function createMemoryCacheConfig() {
|
|
96
53
|
return {
|
|
97
|
-
localCache:
|
|
54
|
+
localCache: memoryLocalCache(),
|
|
98
55
|
};
|
|
99
56
|
}
|
|
100
57
|
|
|
101
58
|
/**
|
|
102
|
-
* Initializes Firestore
|
|
59
|
+
* Initializes Firestore for React Native with memory caching
|
|
103
60
|
*
|
|
104
61
|
* @param app - Firebase app instance
|
|
105
|
-
* @param config -
|
|
62
|
+
* @param config - Optional configuration
|
|
106
63
|
* @returns Firestore instance
|
|
107
64
|
*
|
|
108
65
|
* @example
|
|
109
66
|
* ```typescript
|
|
110
|
-
*
|
|
111
|
-
*
|
|
112
|
-
*
|
|
113
|
-
* // Custom cache size (20 MB)
|
|
114
|
-
* const db = FirebaseFirestoreInitializer.initialize(app, {
|
|
115
|
-
* cacheSizeBytes: 20 * 1024 * 1024,
|
|
116
|
-
* });
|
|
67
|
+
* import { initializeApp } from 'firebase/app';
|
|
68
|
+
* import { FirebaseFirestoreInitializer } from '@umituz/react-native-firebase/firestore';
|
|
117
69
|
*
|
|
118
|
-
*
|
|
119
|
-
* const db = FirebaseFirestoreInitializer.initialize(app
|
|
120
|
-
* forceMemoryCache: true,
|
|
121
|
-
* });
|
|
70
|
+
* const app = initializeApp(firebaseConfig);
|
|
71
|
+
* const db = FirebaseFirestoreInitializer.initialize(app);
|
|
122
72
|
* ```
|
|
123
73
|
*/
|
|
124
74
|
export class FirebaseFirestoreInitializer {
|
|
125
75
|
/**
|
|
126
|
-
* Initialize Firestore with
|
|
76
|
+
* Initialize Firestore with memory cache for React Native
|
|
127
77
|
*
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
* - React Native: Memory cache
|
|
131
|
-
* - Node.js: Memory cache for server-side rendering
|
|
78
|
+
* This is the recommended way to initialize Firestore in React Native apps.
|
|
79
|
+
* Uses memory cache to avoid idb package import errors.
|
|
132
80
|
*/
|
|
133
81
|
static initialize(
|
|
134
82
|
app: FirebaseApp,
|
|
135
|
-
config:
|
|
83
|
+
config: FirestoreConfig = {}
|
|
136
84
|
): Firestore {
|
|
137
|
-
const finalConfig = { ...
|
|
85
|
+
const finalConfig = { ...DEFAULT_CONFIG, ...config };
|
|
138
86
|
|
|
139
87
|
try {
|
|
140
|
-
//
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
return initializeFirestore(app, createPersistentCacheConfig(finalConfig));
|
|
144
|
-
} catch (error) {
|
|
145
|
-
// IndexedDB may be disabled in private browsing mode
|
|
146
|
-
// Fall back to memory cache
|
|
147
|
-
if (__DEV__) {
|
|
148
|
-
console.warn('[Firestore] Persistent cache failed, using memory cache:', error);
|
|
149
|
-
}
|
|
150
|
-
return initializeFirestore(app, createMemoryCacheConfig(finalConfig));
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
// React Native with memory cache
|
|
155
|
-
// Note: React Native doesn't support IndexedDB, use memory cache
|
|
156
|
-
if (Platform.isReactNative()) {
|
|
157
|
-
return initializeFirestore(app, createMemoryCacheConfig(finalConfig));
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
// Node.js / Server-side with memory cache
|
|
161
|
-
if (Platform.isNode()) {
|
|
162
|
-
return initializeFirestore(app, createMemoryCacheConfig(finalConfig));
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// Fallback: Try persistent cache, fall back to memory
|
|
166
|
-
return initializeFirestore(app, createPersistentCacheConfig(finalConfig));
|
|
88
|
+
// Initialize Firestore with memory cache
|
|
89
|
+
const settings = createMemoryCacheConfig();
|
|
90
|
+
return initializeFirestore(app, settings);
|
|
167
91
|
} catch (error) {
|
|
168
92
|
// If initialization fails, get existing instance
|
|
169
93
|
// This handles cases where Firestore is already initialized
|
|
170
94
|
if (__DEV__) {
|
|
171
|
-
console.warn('[Firestore] Initialization
|
|
95
|
+
console.warn('[Firestore] Initialization using existing instance:', error);
|
|
172
96
|
}
|
|
173
97
|
return getFirestore(app);
|
|
174
98
|
}
|
|
175
99
|
}
|
|
176
100
|
|
|
177
101
|
/**
|
|
178
|
-
*
|
|
179
|
-
* Useful for
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
app: FirebaseApp,
|
|
183
|
-
config: Omit<FirestoreCacheConfig, 'enablePersistentCache' | 'forceMemoryCache'> = {}
|
|
184
|
-
): Firestore {
|
|
185
|
-
return this.initialize(app, {
|
|
186
|
-
...config,
|
|
187
|
-
forceMemoryCache: true,
|
|
188
|
-
enablePersistentCache: false,
|
|
189
|
-
});
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
/**
|
|
193
|
-
* Check if persistent cache is available on current platform
|
|
194
|
-
*/
|
|
195
|
-
static isPersistentCacheAvailable(): boolean {
|
|
196
|
-
return Platform.isWeb() && typeof window.indexedDB !== 'undefined';
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
* Get current cache size in bytes
|
|
201
|
-
* Note: This is an estimate, actual size may vary
|
|
202
|
-
*/
|
|
203
|
-
static getEstimatedCacheSize(config: FirestoreCacheConfig = {}): number {
|
|
204
|
-
return config.cacheSizeBytes ?? DEFAULT_CACHE_CONFIG.cacheSizeBytes;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* Clear all Firestore caches (useful for logout or data reset)
|
|
209
|
-
* WARNING: This will clear all cached data and force re-fetch
|
|
102
|
+
* Clear Firestore cache
|
|
103
|
+
* Useful for logout or data reset scenarios
|
|
104
|
+
*
|
|
105
|
+
* NOTE: With memory cache, this clears in-memory cached data
|
|
210
106
|
*/
|
|
211
|
-
static async
|
|
107
|
+
static async clearCache(app: FirebaseApp): Promise<void> {
|
|
212
108
|
try {
|
|
213
109
|
const db = getFirestore(app);
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
110
|
+
// clearPersistentCache may exist in some Firebase SDK versions
|
|
111
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
112
|
+
if (typeof (db as any).clearPersistentCache === 'function') {
|
|
113
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
114
|
+
await (db as any).clearPersistentCache();
|
|
115
|
+
if (__DEV__) {
|
|
116
|
+
console.log('[Firestore] Cache cleared successfully');
|
|
117
|
+
}
|
|
118
|
+
} else {
|
|
119
|
+
if (__DEV__) {
|
|
120
|
+
console.log('[Firestore] Memory cache does not support explicit clearing');
|
|
121
|
+
}
|
|
217
122
|
}
|
|
218
123
|
} catch (error) {
|
|
219
124
|
if (__DEV__) {
|
|
220
|
-
console.warn('[Firestore]
|
|
125
|
+
console.warn('[Firestore] Cache clearing failed:', error);
|
|
221
126
|
}
|
|
222
|
-
throw
|
|
127
|
+
// Don't throw - cache clearing is not critical
|
|
223
128
|
}
|
|
224
129
|
}
|
|
225
130
|
}
|
|
226
131
|
|
|
227
132
|
/**
|
|
228
|
-
*
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
/** Persistent cache available */
|
|
234
|
-
persistentCacheAvailable: boolean;
|
|
235
|
-
/** Current cache size limit */
|
|
236
|
-
cacheSizeBytes: number;
|
|
237
|
-
/** Estimated cache usage percentage */
|
|
238
|
-
estimatedCacheUsage: number;
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
/**
|
|
242
|
-
* Get cache statistics for monitoring and debugging
|
|
133
|
+
* Get current Firestore instance
|
|
134
|
+
* Shortcut for getFirestore from Firebase SDK
|
|
135
|
+
*
|
|
136
|
+
* @param app - Firebase app instance
|
|
137
|
+
* @returns Firestore instance
|
|
243
138
|
*/
|
|
244
|
-
export function
|
|
245
|
-
|
|
246
|
-
? 'web'
|
|
247
|
-
: Platform.isReactNative()
|
|
248
|
-
? 'react-native'
|
|
249
|
-
: Platform.isNode()
|
|
250
|
-
? 'node'
|
|
251
|
-
: 'unknown';
|
|
252
|
-
|
|
253
|
-
return {
|
|
254
|
-
platform,
|
|
255
|
-
persistentCacheAvailable: FirebaseFirestoreInitializer.isPersistentCacheAvailable(),
|
|
256
|
-
cacheSizeBytes: FirebaseFirestoreInitializer.getEstimatedCacheSize(),
|
|
257
|
-
estimatedCacheUsage: 0, // Firestore doesn't expose actual cache size
|
|
258
|
-
};
|
|
139
|
+
export function getFirestoreInstance(app: FirebaseApp): Firestore {
|
|
140
|
+
return getFirestore(app);
|
|
259
141
|
}
|