@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.11",
4
- "description": "Unified Firebase package for React Native apps - Auth and Firestore services using Firebase JS SDK (no native modules).",
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": {
@@ -1,259 +1,141 @@
1
1
  /**
2
- * Firebase Firestore Initializer (Enhanced)
2
+ * Firebase Firestore Initializer for React Native
3
3
  *
4
- * Single Responsibility: Initialize Firestore instance with optimal caching
4
+ * Single Responsibility: Initialize Firestore instance with memory caching
5
5
  *
6
- * OPTIMIZATIONS:
7
- * - Web: Persistent IndexedDB cache (survives restarts)
8
- * - React Native: Optimized memory cache
9
- * - Configurable cache size limits (10 MB default)
10
- * - Platform-aware cache strategy
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
- * COST SAVINGS: ~90% reduction in network reads through persistent caching
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
- * Cache configuration options
24
+ * Firestore configuration options for React Native
27
25
  */
28
- export interface FirestoreCacheConfig {
29
- /** Cache size in bytes (default: 10 MB) */
30
- cacheSizeBytes?: number;
31
- /** Enable persistent cache for web (default: true) */
32
- enablePersistentCache?: boolean;
33
- /** Force memory-only cache (useful for testing) */
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 cache configuration
39
- * Optimized for cost savings while maintaining performance
35
+ * Default configuration for React Native
40
36
  */
41
- const DEFAULT_CACHE_CONFIG: Required<FirestoreCacheConfig> = {
42
- cacheSizeBytes: 10 * 1024 * 1024, // 10 MB
43
- enablePersistentCache: true,
44
- forceMemoryCache: false,
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 persistent cache configuration for web platforms
66
- * Uses IndexedDB to cache data across browser sessions
67
- */
68
- function createPersistentCacheConfig(config: Required<FirestoreCacheConfig>): FirestoreSettings {
69
- try {
70
- // Create persistent cache with IndexedDB
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(config: Required<FirestoreCacheConfig>): FirestoreSettings {
93
- // Memory cache - Firebase SDK manages cache size automatically
94
- const cacheConfig = memoryLocalCache();
95
-
52
+ function createMemoryCacheConfig() {
96
53
  return {
97
- localCache: cacheConfig,
54
+ localCache: memoryLocalCache(),
98
55
  };
99
56
  }
100
57
 
101
58
  /**
102
- * Initializes Firestore with optimal caching strategy based on platform
59
+ * Initializes Firestore for React Native with memory caching
103
60
  *
104
61
  * @param app - Firebase app instance
105
- * @param config - Cache configuration options
62
+ * @param config - Optional configuration
106
63
  * @returns Firestore instance
107
64
  *
108
65
  * @example
109
66
  * ```typescript
110
- * // Default configuration (recommended)
111
- * const db = FirebaseFirestoreInitializer.initialize(app);
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
- * // Force memory cache (testing)
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 platform-optimized caching
76
+ * Initialize Firestore with memory cache for React Native
127
77
  *
128
- * Platform Strategy:
129
- * - Web: Persistent IndexedDB cache (survives restarts, 90% cost savings)
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: FirestoreCacheConfig = {}
83
+ config: FirestoreConfig = {}
136
84
  ): Firestore {
137
- const finalConfig = { ...DEFAULT_CACHE_CONFIG, ...config };
85
+ const finalConfig = { ...DEFAULT_CONFIG, ...config };
138
86
 
139
87
  try {
140
- // Web platform with persistent cache (COST OPTIMIZED)
141
- if (!finalConfig.forceMemoryCache && Platform.isWeb()) {
142
- try {
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 failed, getting existing instance:', error);
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
- * Initialize Firestore with memory-only cache
179
- * Useful for testing or sensitive data that shouldn't be persisted
180
- */
181
- static initializeWithMemoryCache(
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 clearPersistentCache(app: FirebaseApp): Promise<void> {
107
+ static async clearCache(app: FirebaseApp): Promise<void> {
212
108
  try {
213
109
  const db = getFirestore(app);
214
- await (db as any).clearPersistentCache();
215
- if (__DEV__) {
216
- console.log('[Firestore] Persistent cache cleared');
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] Failed to clear persistent cache:', error);
125
+ console.warn('[Firestore] Cache clearing failed:', error);
221
126
  }
222
- throw error;
127
+ // Don't throw - cache clearing is not critical
223
128
  }
224
129
  }
225
130
  }
226
131
 
227
132
  /**
228
- * Cache statistics interface
229
- */
230
- export interface CacheStatistics {
231
- /** Platform type */
232
- platform: 'web' | 'react-native' | 'node' | 'unknown';
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 getCacheStatistics(): CacheStatistics {
245
- const platform = Platform.isWeb()
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
  }