@umituz/react-native-firebase 3.0.12 → 3.0.14
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.14",
|
|
4
|
+
"description": "Firebase package for React Native mobile apps - Auth and Firestore services.",
|
|
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,261 +1,41 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Firebase Firestore Initializer
|
|
2
|
+
* Firebase Firestore Initializer for React Native
|
|
3
3
|
*
|
|
4
|
-
*
|
|
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
|
|
11
|
-
*
|
|
12
|
-
* COST SAVINGS: ~90% reduction in network reads through persistent caching
|
|
4
|
+
* Simple initialization for Firestore in React Native apps.
|
|
13
5
|
*/
|
|
14
6
|
|
|
15
|
-
import {
|
|
16
|
-
getFirestore,
|
|
17
|
-
initializeFirestore,
|
|
18
|
-
memoryLocalCache,
|
|
19
|
-
persistentLocalCache,
|
|
20
|
-
type FirestoreSettings,
|
|
21
|
-
} from 'firebase/firestore';
|
|
7
|
+
import { getFirestore, initializeFirestore } from 'firebase/firestore';
|
|
22
8
|
import type { Firestore } from 'firebase/firestore';
|
|
23
9
|
import type { FirebaseApp } from 'firebase/app';
|
|
24
10
|
|
|
25
11
|
/**
|
|
26
|
-
*
|
|
27
|
-
*/
|
|
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;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Default cache configuration
|
|
39
|
-
* Optimized for cost savings while maintaining performance
|
|
40
|
-
*/
|
|
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;
|
|
61
|
-
},
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
/**
|
|
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
|
|
91
|
-
*/
|
|
92
|
-
function createMemoryCacheConfig(config: Required<FirestoreCacheConfig>): FirestoreSettings {
|
|
93
|
-
// Memory cache - Firebase SDK manages cache size automatically
|
|
94
|
-
const cacheConfig = memoryLocalCache();
|
|
95
|
-
|
|
96
|
-
return {
|
|
97
|
-
localCache: cacheConfig,
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Initializes Firestore with optimal caching strategy based on platform
|
|
12
|
+
* Initializes Firestore for React Native
|
|
103
13
|
*
|
|
104
14
|
* @param app - Firebase app instance
|
|
105
|
-
* @param config - Cache configuration options
|
|
106
15
|
* @returns Firestore instance
|
|
107
16
|
*
|
|
108
17
|
* @example
|
|
109
18
|
* ```typescript
|
|
110
|
-
*
|
|
111
|
-
*
|
|
112
|
-
*
|
|
113
|
-
* // Custom cache size (20 MB)
|
|
114
|
-
* const db = FirebaseFirestoreInitializer.initialize(app, {
|
|
115
|
-
* cacheSizeBytes: 20 * 1024 * 1024,
|
|
116
|
-
* });
|
|
19
|
+
* import { initializeApp } from 'firebase/app';
|
|
20
|
+
* import { FirebaseFirestoreInitializer } from '@umituz/react-native-firebase/firestore';
|
|
117
21
|
*
|
|
118
|
-
*
|
|
119
|
-
* const db = FirebaseFirestoreInitializer.initialize(app
|
|
120
|
-
* forceMemoryCache: true,
|
|
121
|
-
* });
|
|
22
|
+
* const app = initializeApp(firebaseConfig);
|
|
23
|
+
* const db = FirebaseFirestoreInitializer.initialize(app);
|
|
122
24
|
* ```
|
|
123
25
|
*/
|
|
124
26
|
export class FirebaseFirestoreInitializer {
|
|
125
27
|
/**
|
|
126
|
-
* Initialize Firestore
|
|
127
|
-
*
|
|
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
|
|
28
|
+
* Initialize Firestore
|
|
132
29
|
*/
|
|
133
|
-
static initialize(
|
|
134
|
-
app: FirebaseApp,
|
|
135
|
-
config: FirestoreCacheConfig = {}
|
|
136
|
-
): Firestore {
|
|
137
|
-
const finalConfig = { ...DEFAULT_CACHE_CONFIG, ...config };
|
|
138
|
-
|
|
30
|
+
static initialize(app: FirebaseApp): Firestore {
|
|
139
31
|
try {
|
|
140
|
-
|
|
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));
|
|
32
|
+
return initializeFirestore(app, {});
|
|
167
33
|
} catch (error) {
|
|
168
34
|
// If initialization fails, get existing instance
|
|
169
|
-
// This handles cases where Firestore is already initialized
|
|
170
35
|
if (__DEV__) {
|
|
171
|
-
console.warn('[Firestore]
|
|
36
|
+
console.warn('[Firestore] Using existing instance:', error);
|
|
172
37
|
}
|
|
173
38
|
return getFirestore(app);
|
|
174
39
|
}
|
|
175
40
|
}
|
|
176
|
-
|
|
177
|
-
/**
|
|
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
|
|
210
|
-
*/
|
|
211
|
-
static async clearPersistentCache(app: FirebaseApp): Promise<void> {
|
|
212
|
-
try {
|
|
213
|
-
const db = getFirestore(app);
|
|
214
|
-
// clearPersistentCache exists in Firestore SDK but not in TypeScript definitions
|
|
215
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
216
|
-
await (db as any).clearPersistentCache();
|
|
217
|
-
if (__DEV__) {
|
|
218
|
-
console.log('[Firestore] Persistent cache cleared');
|
|
219
|
-
}
|
|
220
|
-
} catch (error) {
|
|
221
|
-
if (__DEV__) {
|
|
222
|
-
console.warn('[Firestore] Failed to clear persistent cache:', error);
|
|
223
|
-
}
|
|
224
|
-
throw error;
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
/**
|
|
230
|
-
* Cache statistics interface
|
|
231
|
-
*/
|
|
232
|
-
export interface CacheStatistics {
|
|
233
|
-
/** Platform type */
|
|
234
|
-
platform: 'web' | 'react-native' | 'node' | 'unknown';
|
|
235
|
-
/** Persistent cache available */
|
|
236
|
-
persistentCacheAvailable: boolean;
|
|
237
|
-
/** Current cache size limit */
|
|
238
|
-
cacheSizeBytes: number;
|
|
239
|
-
/** Estimated cache usage percentage */
|
|
240
|
-
estimatedCacheUsage: number;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
/**
|
|
244
|
-
* Get cache statistics for monitoring and debugging
|
|
245
|
-
*/
|
|
246
|
-
export function getCacheStatistics(): CacheStatistics {
|
|
247
|
-
const platform = Platform.isWeb()
|
|
248
|
-
? 'web'
|
|
249
|
-
: Platform.isReactNative()
|
|
250
|
-
? 'react-native'
|
|
251
|
-
: Platform.isNode()
|
|
252
|
-
? 'node'
|
|
253
|
-
: 'unknown';
|
|
254
|
-
|
|
255
|
-
return {
|
|
256
|
-
platform,
|
|
257
|
-
persistentCacheAvailable: FirebaseFirestoreInitializer.isPersistentCacheAvailable(),
|
|
258
|
-
cacheSizeBytes: FirebaseFirestoreInitializer.getEstimatedCacheSize(),
|
|
259
|
-
estimatedCacheUsage: 0, // Firestore doesn't expose actual cache size
|
|
260
|
-
};
|
|
261
41
|
}
|