@umituz/react-native-firebase 1.13.57 → 1.13.59
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/README.md +277 -0
- package/package.json +1 -1
- package/scripts/README.md +513 -0
- package/src/auth/README.md +339 -0
- package/src/auth/domain/README.md +264 -0
- package/src/auth/domain/errors/README.md +291 -0
- package/src/auth/infrastructure/config/README.md +239 -0
- package/src/auth/infrastructure/services/README.md +346 -0
- package/src/auth/infrastructure/stores/README.md +407 -0
- package/src/auth/presentation/hooks/README.md +442 -0
- package/src/domain/README.md +628 -0
- package/src/firestore/README.md +566 -0
- package/src/firestore/domain/README.md +325 -0
- package/src/firestore/domain/constants/README.md +332 -0
- package/src/firestore/domain/entities/README.md +286 -0
- package/src/firestore/domain/errors/README.md +389 -0
- package/src/firestore/infrastructure/config/README.md +239 -0
- package/src/firestore/infrastructure/middleware/README.md +316 -0
- package/src/firestore/infrastructure/repositories/README.md +425 -0
- package/src/firestore/infrastructure/services/README.md +332 -0
- package/src/firestore/types/pagination/README.md +332 -0
- package/src/firestore/utils/README.md +574 -0
- package/src/firestore/utils/dateUtils/README.md +171 -0
- package/src/firestore/utils/document-mapper.helper/README.md +309 -0
- package/src/firestore/utils/pagination.helper/README.md +298 -0
- package/src/firestore/utils/path-resolver/README.md +277 -0
- package/src/firestore/utils/query-builder/README.md +291 -0
- package/src/firestore/utils/quota-error-detector/README.md +355 -0
- package/src/infrastructure/README.md +408 -0
- package/src/infrastructure/config/README.md +262 -0
- package/src/presentation/README.md +556 -0
- package/src/storage/README.md +493 -0
- package/src/storage/deleter/README.md +370 -0
- package/src/storage/types/README.md +313 -0
- package/src/storage/uploader/README.md +409 -0
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
# Query Builder
|
|
2
|
+
|
|
3
|
+
Advanced Firestore query building utility with support for filtering, sorting, pagination, and date ranges.
|
|
4
|
+
|
|
5
|
+
## Purpose
|
|
6
|
+
|
|
7
|
+
Provides a fluent interface for building complex Firestore queries while handling Firestore limitations (e.g., IN operator 10-item limit).
|
|
8
|
+
|
|
9
|
+
## For AI Agents
|
|
10
|
+
|
|
11
|
+
### Before Using Query Builder
|
|
12
|
+
|
|
13
|
+
1. **UNDERSTAND** Firestore query limitations
|
|
14
|
+
2. **USE** builder instead of raw Firestore queries
|
|
15
|
+
3. **RESPECT** compound query limitations
|
|
16
|
+
4. **HANDLE** large arrays with chunking
|
|
17
|
+
|
|
18
|
+
### Required Practices
|
|
19
|
+
|
|
20
|
+
1. **Use query builder** for complex queries
|
|
21
|
+
2. **Handle IN operator limits** (max 10 values)
|
|
22
|
+
3. **Apply modifiers in correct order** (filters → date range → sort → cursor → limit)
|
|
23
|
+
4. **Use pagination** for large datasets
|
|
24
|
+
|
|
25
|
+
### Forbidden Practices
|
|
26
|
+
|
|
27
|
+
## ❌ NEVER
|
|
28
|
+
|
|
29
|
+
- Build queries manually with Firestore SDK
|
|
30
|
+
- Use IN operator with more than 10 values without chunking
|
|
31
|
+
- Apply modifiers in wrong order
|
|
32
|
+
- Create compound queries that violate Firestore rules
|
|
33
|
+
- Ignore pagination for large result sets
|
|
34
|
+
|
|
35
|
+
## ⚠️ Avoid
|
|
36
|
+
|
|
37
|
+
- Multiple range filters on different fields
|
|
38
|
+
- Sorting + filtering on different fields without composite index
|
|
39
|
+
- Very large IN arrays (use pagination instead)
|
|
40
|
+
- Complex OR queries (performance impact)
|
|
41
|
+
|
|
42
|
+
## Usage Strategies
|
|
43
|
+
|
|
44
|
+
### For Complex Filtering
|
|
45
|
+
|
|
46
|
+
**Strategy:** Use FieldFilter array for multiple conditions.
|
|
47
|
+
|
|
48
|
+
**When to Use:**
|
|
49
|
+
- Filtering by multiple fields
|
|
50
|
+
- Combining equality and range filters
|
|
51
|
+
- Building dynamic queries based on user input
|
|
52
|
+
|
|
53
|
+
**Approach:**
|
|
54
|
+
1. Create FieldFilter objects for each condition
|
|
55
|
+
2. Add to baseFilters array
|
|
56
|
+
3. Pass to buildQuery with other options
|
|
57
|
+
|
|
58
|
+
### For Date Range Queries
|
|
59
|
+
|
|
60
|
+
**Strategy:** Use dateRange option for time-based filtering.
|
|
61
|
+
|
|
62
|
+
**When to Use:**
|
|
63
|
+
- Filtering by creation/update time
|
|
64
|
+
- Date-based analytics queries
|
|
65
|
+
- Time-series data retrieval
|
|
66
|
+
|
|
67
|
+
**Approach:**
|
|
68
|
+
1. Specify field name (e.g., 'createdAt')
|
|
69
|
+
2. Provide startDate and/or endDate as timestamps
|
|
70
|
+
3. Combine with sorting on same field
|
|
71
|
+
|
|
72
|
+
### For Pagination
|
|
73
|
+
|
|
74
|
+
**Strategy:** Use cursorValue with limit for cursor-based pagination.
|
|
75
|
+
|
|
76
|
+
**When to Use:**
|
|
77
|
+
- Infinite scroll implementations
|
|
78
|
+
- Large dataset navigation
|
|
79
|
+
- Ordered result sets
|
|
80
|
+
|
|
81
|
+
**Approach:**
|
|
82
|
+
1. Store cursor from last page
|
|
83
|
+
2. Pass as cursorValue in next request
|
|
84
|
+
3. Always use with sort and limit
|
|
85
|
+
|
|
86
|
+
### For Large IN Arrays
|
|
87
|
+
|
|
88
|
+
**Strategy:** Let builder handle chunking automatically.
|
|
89
|
+
|
|
90
|
+
**When to Use:**
|
|
91
|
+
- Filtering by list of IDs
|
|
92
|
+
- Multiple category selection
|
|
93
|
+
- Tag-based filtering
|
|
94
|
+
|
|
95
|
+
**Approach:**
|
|
96
|
+
1. Create filter with operator: 'in'
|
|
97
|
+
2. Pass array (any size supported)
|
|
98
|
+
3. Builder splits into chunks of 10 automatically
|
|
99
|
+
|
|
100
|
+
## Configuration Options
|
|
101
|
+
|
|
102
|
+
### QueryBuilderOptions
|
|
103
|
+
|
|
104
|
+
**collectionName** (required)
|
|
105
|
+
- Target collection path
|
|
106
|
+
- Type: string
|
|
107
|
+
|
|
108
|
+
**baseFilters** (optional)
|
|
109
|
+
- Array of field filters
|
|
110
|
+
- Type: FieldFilter[]
|
|
111
|
+
- Applied first in query chain
|
|
112
|
+
|
|
113
|
+
**dateRange** (optional)
|
|
114
|
+
- Date range filter
|
|
115
|
+
- Type: { field: string; startDate?: number; endDate?: number }
|
|
116
|
+
- Use Timestamps for dates
|
|
117
|
+
|
|
118
|
+
**sort** (optional)
|
|
119
|
+
- Sort configuration
|
|
120
|
+
- Type: { field: string; order?: 'asc' | 'desc' }
|
|
121
|
+
- Defaults to 'desc' if not specified
|
|
122
|
+
|
|
123
|
+
**limitValue** (optional)
|
|
124
|
+
- Maximum documents to return
|
|
125
|
+
- Type: number
|
|
126
|
+
- Use for pagination and performance
|
|
127
|
+
|
|
128
|
+
**cursorValue** (optional)
|
|
129
|
+
- Pagination cursor (timestamp)
|
|
130
|
+
- Type: number
|
|
131
|
+
- Requires sort to be set
|
|
132
|
+
|
|
133
|
+
### FieldFilter
|
|
134
|
+
|
|
135
|
+
**field** (required)
|
|
136
|
+
- Document field to filter on
|
|
137
|
+
- Type: string
|
|
138
|
+
|
|
139
|
+
**operator** (required)
|
|
140
|
+
- Comparison operator
|
|
141
|
+
- Type: WhereFilterOp (==, !=, >, >=, <, <=, in, array-contains)
|
|
142
|
+
|
|
143
|
+
**value** (required)
|
|
144
|
+
- Filter value
|
|
145
|
+
- Type: string | number | boolean | string[] | number[]
|
|
146
|
+
|
|
147
|
+
## Helper Functions
|
|
148
|
+
|
|
149
|
+
**Import from:** `src/firestore/utils/query-builder`
|
|
150
|
+
|
|
151
|
+
### `createInFilter(field, values)`
|
|
152
|
+
|
|
153
|
+
Create filter for IN operator.
|
|
154
|
+
|
|
155
|
+
**Use For:**
|
|
156
|
+
- Filtering by multiple values
|
|
157
|
+
- ID list queries
|
|
158
|
+
- Category/tag filtering
|
|
159
|
+
|
|
160
|
+
**Note:** Automatically chunks arrays larger than 10 values.
|
|
161
|
+
|
|
162
|
+
### `createEqualFilter(field, value)`
|
|
163
|
+
|
|
164
|
+
Create filter for equality comparison.
|
|
165
|
+
|
|
166
|
+
**Use For:**
|
|
167
|
+
- Single value matches
|
|
168
|
+
- ID lookups
|
|
169
|
+
- Status filtering
|
|
170
|
+
|
|
171
|
+
## Query Building Order
|
|
172
|
+
|
|
173
|
+
**REQUIRED ORDER:** Modifiers must be applied in this sequence:
|
|
174
|
+
|
|
175
|
+
1. **Collection reference** (base)
|
|
176
|
+
2. **Base filters** (where clauses)
|
|
177
|
+
3. **Date range** (>= and <=)
|
|
178
|
+
4. **Sort** (orderBy)
|
|
179
|
+
5. **Cursor** (startAfter)
|
|
180
|
+
6. **Limit** (limit)
|
|
181
|
+
|
|
182
|
+
**Why this order?**
|
|
183
|
+
- Firestore requires specific order for composite queries
|
|
184
|
+
- Filters before sorts
|
|
185
|
+
- Sorts before cursors
|
|
186
|
+
- Cursors before limits
|
|
187
|
+
|
|
188
|
+
## Common Mistakes to Avoid
|
|
189
|
+
|
|
190
|
+
1. ❌ Manual query construction
|
|
191
|
+
- ✅ Use buildQuery utility
|
|
192
|
+
|
|
193
|
+
2. ❌ IN operator with 11+ values without chunking
|
|
194
|
+
- ✅ Builder handles this automatically
|
|
195
|
+
|
|
196
|
+
3. ❌ Wrong modifier order
|
|
197
|
+
- ✅ Follow prescribed order in buildQuery
|
|
198
|
+
|
|
199
|
+
4. ❌ Pagination without sort
|
|
200
|
+
- ✅ Always include sort when using cursor
|
|
201
|
+
|
|
202
|
+
5. ❌ Multiple range filters on different fields
|
|
203
|
+
- ✅ Use single range filter or create composite index
|
|
204
|
+
|
|
205
|
+
## AI Agent Instructions
|
|
206
|
+
|
|
207
|
+
### When Adding Query Features
|
|
208
|
+
|
|
209
|
+
1. Check Firestore limitations first
|
|
210
|
+
2. Handle edge cases (null, undefined, empty arrays)
|
|
211
|
+
3. Document query capabilities
|
|
212
|
+
4. Update this README
|
|
213
|
+
|
|
214
|
+
### When Building Queries
|
|
215
|
+
|
|
216
|
+
1. Use buildQuery, not raw Firestore SDK
|
|
217
|
+
2. Handle IN operator limits (10 items max per clause)
|
|
218
|
+
3. Respect composite query requirements
|
|
219
|
+
4. Always paginate large result sets
|
|
220
|
+
5. Create composite indexes in Firebase Console when needed
|
|
221
|
+
|
|
222
|
+
### For Complex Queries
|
|
223
|
+
|
|
224
|
+
1. Break into multiple queries if needed
|
|
225
|
+
2. Consider client-side filtering for complex logic
|
|
226
|
+
3. Use denormalization for better query patterns
|
|
227
|
+
4. Document query requirements
|
|
228
|
+
|
|
229
|
+
## Code Quality Standards
|
|
230
|
+
|
|
231
|
+
### TypeScript
|
|
232
|
+
|
|
233
|
+
- All options must have explicit types
|
|
234
|
+
- Export types for consumer use
|
|
235
|
+
- Use strict type checking
|
|
236
|
+
- Document generic type parameters
|
|
237
|
+
|
|
238
|
+
### Error Handling
|
|
239
|
+
|
|
240
|
+
- Validate required options
|
|
241
|
+
- Provide clear error messages
|
|
242
|
+
- Handle Firestore limitations gracefully
|
|
243
|
+
- Log warnings for anti-patterns
|
|
244
|
+
|
|
245
|
+
## Performance Considerations
|
|
246
|
+
|
|
247
|
+
### Query Optimization
|
|
248
|
+
|
|
249
|
+
1. **Limit results** always use limitValue
|
|
250
|
+
2. **Create indexes** for composite queries
|
|
251
|
+
3. **Avoid large offsets** use cursor-based pagination
|
|
252
|
+
4. **Select specific fields** when possible (future feature)
|
|
253
|
+
|
|
254
|
+
### Quota Management
|
|
255
|
+
|
|
256
|
+
1. Each query reads all matched documents
|
|
257
|
+
2. Use limit to control reads
|
|
258
|
+
3. Date range filters reduce scanned documents
|
|
259
|
+
4. Monitor query performance in Firebase Console
|
|
260
|
+
|
|
261
|
+
## Related Documentation
|
|
262
|
+
|
|
263
|
+
- [Firestore Module README](../README.md)
|
|
264
|
+
- [Repository README](../../infrastructure/repositories/README.md)
|
|
265
|
+
- [Pagination Helper README](../pagination.helper/README.md)
|
|
266
|
+
- [Date Utilities README](../dateUtils/README.md)
|
|
267
|
+
|
|
268
|
+
## Firebase Query Limitations
|
|
269
|
+
|
|
270
|
+
### Composite Queries
|
|
271
|
+
|
|
272
|
+
- Maximum 1 range filter (<, <=, >, >=)
|
|
273
|
+
- Range filter and sort must be on same field
|
|
274
|
+
- Requires composite index for most combinations
|
|
275
|
+
|
|
276
|
+
### IN Operator
|
|
277
|
+
|
|
278
|
+
- Maximum 10 values per query
|
|
279
|
+
- Builder auto-chunks larger arrays
|
|
280
|
+
- Each chunk counts as separate query for quota
|
|
281
|
+
|
|
282
|
+
### OR Queries
|
|
283
|
+
|
|
284
|
+
- Maximum 30 disjunctions
|
|
285
|
+
- Can impact performance
|
|
286
|
+
- Use sparingly
|
|
287
|
+
|
|
288
|
+
---
|
|
289
|
+
|
|
290
|
+
**Last Updated:** 2025-01-08
|
|
291
|
+
**Maintainer:** Firestore Module Team
|
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
# Quota Error Detector
|
|
2
|
+
|
|
3
|
+
Utilities for detecting and handling Firestore quota errors and retryable errors.
|
|
4
|
+
|
|
5
|
+
## Purpose
|
|
6
|
+
|
|
7
|
+
Provides reliable detection of Firestore quota errors (resource-exhausted, quota-exceeded) and retryable transient errors (unavailable, deadline-exceeded) for proper error handling and user communication.
|
|
8
|
+
|
|
9
|
+
## For AI Agents
|
|
10
|
+
|
|
11
|
+
### Before Using Error Detection
|
|
12
|
+
|
|
13
|
+
1. **ALWAYS** wrap Firestore operations in try-catch
|
|
14
|
+
2. **CHECK** error type before handling
|
|
15
|
+
3. **PROVIDE** user-friendly messages for quota errors
|
|
16
|
+
4. **IMPLEMENT** retry logic for retryable errors
|
|
17
|
+
5. **LOG** errors appropriately
|
|
18
|
+
|
|
19
|
+
### Required Practices
|
|
20
|
+
|
|
21
|
+
1. **Use isQuotaError()** for quota detection
|
|
22
|
+
2. **Use getQuotaErrorMessage()** for user-facing messages
|
|
23
|
+
3. **Use isRetryableError()** for retry logic
|
|
24
|
+
4. **Handle quota errors gracefully** (stop operations, inform user)
|
|
25
|
+
5. **Implement exponential backoff** for retryable errors
|
|
26
|
+
|
|
27
|
+
### Forbidden Practices
|
|
28
|
+
|
|
29
|
+
## ❌ NEVER
|
|
30
|
+
|
|
31
|
+
- Ignore quota errors (continue operations)
|
|
32
|
+
- Show raw error messages to users
|
|
33
|
+
- Retry quota errors (won't succeed)
|
|
34
|
+
- Assume all errors are quota errors
|
|
35
|
+
- Log sensitive error data
|
|
36
|
+
|
|
37
|
+
## ⚠️ Avoid
|
|
38
|
+
|
|
39
|
+
- Too many retries (implement max retry limit)
|
|
40
|
+
- No delay between retries (use backoff)
|
|
41
|
+
- Confusing retryable with quota errors
|
|
42
|
+
- Not tracking quota usage
|
|
43
|
+
- Blocking UI for retryable errors
|
|
44
|
+
|
|
45
|
+
## Usage Strategies
|
|
46
|
+
|
|
47
|
+
### For Quota Error Handling
|
|
48
|
+
|
|
49
|
+
**Strategy:** Detect quota errors and stop operations immediately.
|
|
50
|
+
|
|
51
|
+
**When to Use:**
|
|
52
|
+
- After Firestore write operations
|
|
53
|
+
- After Firestore read operations (if quota-limited)
|
|
54
|
+
- In error boundaries
|
|
55
|
+
- In background sync operations
|
|
56
|
+
|
|
57
|
+
**Approach:**
|
|
58
|
+
1. Catch errors from Firestore operations
|
|
59
|
+
2. Check with isQuotaError()
|
|
60
|
+
3. Show user-friendly message
|
|
61
|
+
4. Disable operations that consume quota
|
|
62
|
+
5. Track quota exceeded state
|
|
63
|
+
|
|
64
|
+
**User Message Strategy:**
|
|
65
|
+
- Inform user clearly about quota limit
|
|
66
|
+
- Suggest upgrade plan or waiting
|
|
67
|
+
- Provide action buttons (upgrade, contact support)
|
|
68
|
+
- Don't show technical details
|
|
69
|
+
|
|
70
|
+
### For Retryable Error Handling
|
|
71
|
+
|
|
72
|
+
**Strategy:** Implement retry logic with exponential backoff.
|
|
73
|
+
|
|
74
|
+
**When to Use:**
|
|
75
|
+
- Network timeout errors
|
|
76
|
+
- Temporary service unavailability
|
|
77
|
+
- Deadline exceeded errors
|
|
78
|
+
- Automatic retry operations
|
|
79
|
+
|
|
80
|
+
**Approach:**
|
|
81
|
+
1. Check with isRetryableError()
|
|
82
|
+
2. Wait with exponential backoff (1s, 2s, 4s, 8s...)
|
|
83
|
+
3. Retry operation
|
|
84
|
+
4. Max retries: 3-5
|
|
85
|
+
5. Give up after max retries
|
|
86
|
+
|
|
87
|
+
**Exponential Backoff:**
|
|
88
|
+
- Retry 1: Wait 1 second
|
|
89
|
+
- Retry 2: Wait 2 seconds
|
|
90
|
+
- Retry 3: Wait 4 seconds
|
|
91
|
+
- Retry 4: Wait 8 seconds
|
|
92
|
+
- Max: 5 retries
|
|
93
|
+
|
|
94
|
+
### For Error Logging
|
|
95
|
+
|
|
96
|
+
**Strategy:** Log errors appropriately based on type.
|
|
97
|
+
|
|
98
|
+
**When to Use:**
|
|
99
|
+
- Monitoring error rates
|
|
100
|
+
- Debugging issues
|
|
101
|
+
- Analytics and alerting
|
|
102
|
+
|
|
103
|
+
**Approach:**
|
|
104
|
+
1. Log retryable errors as warnings
|
|
105
|
+
2. Log quota errors as errors
|
|
106
|
+
3. Include error context (operation, collection)
|
|
107
|
+
4. Don't log sensitive data (user data, tokens)
|
|
108
|
+
5. Aggregate error metrics
|
|
109
|
+
|
|
110
|
+
## Error Detection Logic
|
|
111
|
+
|
|
112
|
+
### Quota Error Detection
|
|
113
|
+
|
|
114
|
+
**isQuotaError(error)** checks for:
|
|
115
|
+
|
|
116
|
+
**Error Codes:**
|
|
117
|
+
- `resource-exhausted`
|
|
118
|
+
- `quota-exceeded`
|
|
119
|
+
- `RESOURCE_EXHAUSTED`
|
|
120
|
+
|
|
121
|
+
**Error Messages:**
|
|
122
|
+
- "quota"
|
|
123
|
+
- "exceeded"
|
|
124
|
+
- "limit"
|
|
125
|
+
- "too many requests"
|
|
126
|
+
|
|
127
|
+
**Detection Strategy:**
|
|
128
|
+
1. Check error.code for known quota codes
|
|
129
|
+
2. Check error.message for quota-related keywords
|
|
130
|
+
3. Case-insensitive message matching
|
|
131
|
+
|
|
132
|
+
### Retryable Error Detection
|
|
133
|
+
|
|
134
|
+
**isRetryableError(error)** checks for:
|
|
135
|
+
|
|
136
|
+
**Error Codes:**
|
|
137
|
+
- `unavailable` - Service temporarily unavailable
|
|
138
|
+
- `deadline-exceeded` - Request timeout
|
|
139
|
+
- `aborted` - Operation cancelled (can retry)
|
|
140
|
+
|
|
141
|
+
**Detection Strategy:**
|
|
142
|
+
1. Check error.code for retryable codes
|
|
143
|
+
2. Only code-based detection (more reliable)
|
|
144
|
+
3. Conservative (only retry if safe)
|
|
145
|
+
|
|
146
|
+
## API Reference
|
|
147
|
+
|
|
148
|
+
### `isQuotaError(error: unknown): boolean`
|
|
149
|
+
|
|
150
|
+
Check if error is a Firestore quota error.
|
|
151
|
+
|
|
152
|
+
**Parameters:**
|
|
153
|
+
- **error**: unknown - Error object from Firestore operation
|
|
154
|
+
|
|
155
|
+
**Returns:** boolean - True if quota error detected
|
|
156
|
+
|
|
157
|
+
**Use For:**
|
|
158
|
+
- Detecting quota exceeded
|
|
159
|
+
- Stopping operations
|
|
160
|
+
- Showing user messages
|
|
161
|
+
|
|
162
|
+
### `isRetryableError(error: unknown): boolean`
|
|
163
|
+
|
|
164
|
+
Check if error is retryable (transient).
|
|
165
|
+
|
|
166
|
+
**Parameters:**
|
|
167
|
+
- **error**: unknown - Error object from Firestore operation
|
|
168
|
+
|
|
169
|
+
**Returns:** boolean - True if error is retryable
|
|
170
|
+
|
|
171
|
+
**Use For:**
|
|
172
|
+
- Implementing retry logic
|
|
173
|
+
- Background sync operations
|
|
174
|
+
- Automatic recovery
|
|
175
|
+
|
|
176
|
+
### `getQuotaErrorMessage(): string`
|
|
177
|
+
|
|
178
|
+
Get user-friendly quota error message.
|
|
179
|
+
|
|
180
|
+
**Returns:** string - User-facing error message
|
|
181
|
+
|
|
182
|
+
**Use For:**
|
|
183
|
+
- Displaying to users
|
|
184
|
+
- Error notifications
|
|
185
|
+
- In-app messaging
|
|
186
|
+
|
|
187
|
+
**Message:** "Daily quota exceeded. Please try again tomorrow or upgrade your plan."
|
|
188
|
+
|
|
189
|
+
## Error Handling Patterns
|
|
190
|
+
|
|
191
|
+
### Pattern 1: Quota-Aware Operations
|
|
192
|
+
|
|
193
|
+
**Strategy:** Check for quota errors and handle gracefully
|
|
194
|
+
|
|
195
|
+
**Implementation Steps:**
|
|
196
|
+
1. Wrap Firestore operations in try-catch
|
|
197
|
+
2. Check if error is quota error with isQuotaError()
|
|
198
|
+
3. Show user-friendly message with getQuotaErrorMessage()
|
|
199
|
+
4. Disable further operations when quota exceeded
|
|
200
|
+
5. Log quota errors for monitoring
|
|
201
|
+
|
|
202
|
+
**Error Handling Flow:**
|
|
203
|
+
- Detect quota errors immediately
|
|
204
|
+
- Show clear user message
|
|
205
|
+
- Set quota exceeded flag in UI
|
|
206
|
+
- Prevent further quota-consuming operations
|
|
207
|
+
- Allow retry after quota reset
|
|
208
|
+
|
|
209
|
+
### Pattern 2: Retry with Backoff
|
|
210
|
+
|
|
211
|
+
**Strategy:** Retry only retryable errors with exponential backoff
|
|
212
|
+
|
|
213
|
+
**Implementation Steps:**
|
|
214
|
+
1. Define max retry attempts (typically 3)
|
|
215
|
+
2. Wrap operation in retry loop
|
|
216
|
+
3. Check if error is retryable with isRetryableError()
|
|
217
|
+
4. Calculate exponential delay: 2^attempt * 1000ms
|
|
218
|
+
5. Continue or throw based on retry count
|
|
219
|
+
|
|
220
|
+
**Retry Rules:**
|
|
221
|
+
- Retry retryable errors (resource exhaustion, rate limits)
|
|
222
|
+
- Never retry quota errors (will not succeed)
|
|
223
|
+
- Use exponential backoff between retries
|
|
224
|
+
- Set max retry limit to prevent infinite loops
|
|
225
|
+
- Return last error after max retries
|
|
226
|
+
|
|
227
|
+
### Pattern 3: Error Boundary
|
|
228
|
+
|
|
229
|
+
**Strategy:** Create React hook for error handling
|
|
230
|
+
|
|
231
|
+
**State Management:**
|
|
232
|
+
- Track quota exceeded status
|
|
233
|
+
- Track current error
|
|
234
|
+
- Provide execute function for operations
|
|
235
|
+
|
|
236
|
+
**Error Handling Logic:**
|
|
237
|
+
1. Check if error is quota error
|
|
238
|
+
2. Set quota exceeded flag if true
|
|
239
|
+
3. Check if error is retryable
|
|
240
|
+
4. Retry operation once if retryable
|
|
241
|
+
5. Set error state for other errors
|
|
242
|
+
|
|
243
|
+
**Hook Return Values:**
|
|
244
|
+
- execute function for operations
|
|
245
|
+
- quotaExceeded boolean flag
|
|
246
|
+
- error object with details
|
|
247
|
+
|
|
248
|
+
## Common Mistakes to Avoid
|
|
249
|
+
|
|
250
|
+
1. ❌ Retrying quota errors
|
|
251
|
+
- ✅ Quota errors will not succeed on retry
|
|
252
|
+
|
|
253
|
+
2. ❌ Showing raw error messages
|
|
254
|
+
- ✅ Use getQuotaErrorMessage() for user-friendly text
|
|
255
|
+
|
|
256
|
+
3. ❌ No retry limit for retryable errors
|
|
257
|
+
- ✅ Always implement max retry limit (3-5)
|
|
258
|
+
|
|
259
|
+
4. ❌ Ignoring error types
|
|
260
|
+
- ✅ Check error type before handling
|
|
261
|
+
|
|
262
|
+
5. ❌ Blocking UI for retryable errors
|
|
263
|
+
- ✅ Retry in background, show loading indicator
|
|
264
|
+
|
|
265
|
+
## AI Agent Instructions
|
|
266
|
+
|
|
267
|
+
### When Handling Firestore Errors
|
|
268
|
+
|
|
269
|
+
1. Always check error type before handling
|
|
270
|
+
2. Use isQuotaError() for quota detection
|
|
271
|
+
3. Use isRetryableError() for retry logic
|
|
272
|
+
4. Implement proper handling for each type
|
|
273
|
+
5. Provide user feedback for quota errors
|
|
274
|
+
|
|
275
|
+
### When Implementing Retry Logic
|
|
276
|
+
|
|
277
|
+
1. Only retry if isRetryableError() returns true
|
|
278
|
+
2. Implement exponential backoff (2^n * 1000ms)
|
|
279
|
+
3. Set max retry limit (3-5 retries)
|
|
280
|
+
4. Don't retry quota errors
|
|
281
|
+
5. Log retry attempts for monitoring
|
|
282
|
+
|
|
283
|
+
### For User Experience
|
|
284
|
+
|
|
285
|
+
1. Show clear messages for quota errors
|
|
286
|
+
2. Provide action buttons (upgrade, contact)
|
|
287
|
+
3. Use loading states for retryable errors
|
|
288
|
+
4. Don't block UI for transient errors
|
|
289
|
+
5. Track quota state globally
|
|
290
|
+
|
|
291
|
+
## Code Quality Standards
|
|
292
|
+
|
|
293
|
+
### TypeScript
|
|
294
|
+
|
|
295
|
+
- Use unknown type for error parameter
|
|
296
|
+
- Type guard functions properly
|
|
297
|
+
- Narrow error type before accessing properties
|
|
298
|
+
- Export for use across modules
|
|
299
|
+
|
|
300
|
+
### Error Handling
|
|
301
|
+
|
|
302
|
+
- Always handle quota errors explicitly
|
|
303
|
+
- Never swallow errors silently
|
|
304
|
+
- Log errors appropriately
|
|
305
|
+
- Provide context in logs
|
|
306
|
+
|
|
307
|
+
## Performance Considerations
|
|
308
|
+
|
|
309
|
+
### Retry Strategy
|
|
310
|
+
|
|
311
|
+
- Exponential backoff prevents hammering service
|
|
312
|
+
- Max retries prevent infinite loops
|
|
313
|
+
- Short delays for user-facing operations
|
|
314
|
+
- Longer delays for background operations
|
|
315
|
+
|
|
316
|
+
### Quota Monitoring
|
|
317
|
+
|
|
318
|
+
- Track quota usage proactively
|
|
319
|
+
- Show warnings before hitting limit
|
|
320
|
+
- Implement graceful degradation
|
|
321
|
+
- Cache data to reduce quota usage
|
|
322
|
+
|
|
323
|
+
## Related Documentation
|
|
324
|
+
|
|
325
|
+
- [Firestore Module README](../README.md)
|
|
326
|
+
- [Firestore Error Codes](https://firebase.google.com/docs/firestore/manage-rest/errors)
|
|
327
|
+
- [Quota Management](../../infrastructure/services/quota-tracker-service/README.md)
|
|
328
|
+
|
|
329
|
+
## Firebase Quota Best Practices
|
|
330
|
+
|
|
331
|
+
### Quota Types
|
|
332
|
+
|
|
333
|
+
**Daily Quota:**
|
|
334
|
+
- Reads: 50,000/day (free tier)
|
|
335
|
+
- Writes: 20,000/day (free tier)
|
|
336
|
+
- Deletes: 20,000/day (free tier)
|
|
337
|
+
|
|
338
|
+
**Strategies:**
|
|
339
|
+
- Monitor usage before hitting limits
|
|
340
|
+
- Implement caching to reduce reads
|
|
341
|
+
- Batch writes to reduce write count
|
|
342
|
+
- Use pagination to limit result size
|
|
343
|
+
|
|
344
|
+
### Quota Exceeded Handling
|
|
345
|
+
|
|
346
|
+
1. **Stop operations** - Don't waste quota on failed operations
|
|
347
|
+
2. **Inform user** - Clear message about what happened
|
|
348
|
+
3. **Provide options** - Upgrade plan, wait, contact support
|
|
349
|
+
4. **Track state** - Remember quota exceeded across app
|
|
350
|
+
5. **Retry later** - Allow retry when quota resets
|
|
351
|
+
|
|
352
|
+
---
|
|
353
|
+
|
|
354
|
+
**Last Updated:** 2025-01-08
|
|
355
|
+
**Maintainer:** Firestore Module Team
|