Overview
Reseñas Gastronómicas uses Firebase Cloud Firestore as its backend database, providing:- Real-time synchronization across all connected clients
- Offline support with automatic sync when reconnected
- Scalable NoSQL database with flexible data modeling
- Server-side timestamps for consistent ordering
- Built-in security with Firebase Security Rules
Architecture
Firebase Initialization
Application Entry Point
Firebase is initialized at application startup:src/js/app.js:2-35
FirebaseService Initialization
The service layer initializes its Firestore reference:src/js/components/firebase.js:2-7
This is called during DataStore initialization:
src/js/modules/datastore.js:8-12
Data Model
Review Document Structure
Collection Organization
- Collection name:
reviews - Document IDs: Auto-generated by Firestore
- Ordering: By
timestampfield (descending)
CRUD Operations
Create (Add Review)
Service Layer Implementation:src/js/components/firebase.js:24-38
Key Features:
- Uses server timestamp for consistent ordering across timezones
- Adds client-side ISO timestamp for compatibility
- Returns document ID with the review data
- Throws errors for proper error handling upstream
src/js/modules/datastore.js:39-48
Read (Get Reviews)
Initial Load:src/js/components/firebase.js:10-21
Features:
- Ordered by timestamp (newest first)
- Maps document ID into the data object
- Returns empty array on error (graceful degradation)
Update (Modify Review)
Service Layer:src/js/components/firebase.js:41-54
Features:
- Adds updatedAt timestamp to track modifications
- Uses document ID to target specific review
- Preserves original createdAt and timestamp
src/js/modules/datastore.js:50-58
Delete (Remove Review)
Service Layer:src/js/components/firebase.js:57-65
DataStore Layer:
src/js/modules/datastore.js:60-68
Real-time Synchronization
Snapshot Listener
The application uses Firestore’s snapshot listener for real-time updates:src/js/components/firebase.js:68-78
How It Works:
- Establishes WebSocket connection to Firestore
- Receives initial snapshot with all documents
- Triggers callback whenever data changes (add/update/delete)
- Returns unsubscribe function for cleanup
Setting Up Real-time Sync
DataStore Implementation:src/js/modules/datastore.js:26-37
Cleanup on Destroy:
src/js/modules/datastore.js:109-113
Real-time Flow Diagram
UI Integration
Automatic UI Updates
When data changes, the UI automatically re-renders:src/js/modules/ui.js:9-22
No Manual Refresh Required
Because of the real-time listener:- Additions appear immediately in all clients
- Updates reflect instantly across devices
- Deletions remove items from all views
- No polling or manual refresh needed
Error Handling & Fallbacks
Graceful Degradation
If Firebase fails to load, the app falls back to sample data:src/js/modules/datastore.js:14-24
Sample Data
src/js/modules/datastore.js:93-107
User Feedback During Operations
Loading states are shown during async operations:src/js/modules/form.js:12-68
Performance Optimizations
1. Server-Side Ordering
Firestore handles sorting, reducing client-side processing:2. Efficient Real-time Updates
Snapshot listeners only send changes, not the entire dataset:3. Single Listener Pattern
Only one active listener prevents duplicate subscriptions:4. Optimistic UI Updates
The real-time listener provides instant feedback:- User adds review → immediately appears in their UI
- Firestore confirms → updates all other connected clients
- No manual refresh or reload needed
Security Considerations
Firebase Security Rules (Recommended)
Although not in the source code, you should configure Firestore Security Rules:Input Sanitization
All user input is escaped before rendering:Server Timestamps
Using server timestamps prevents client-side manipulation:Offline Support
Firestore SDK provides automatic offline support:How It Works
- Local Cache: Firestore caches data locally in browser storage
- Offline Reads: App can read cached data when offline
- Offline Writes: Write operations are queued
- Automatic Sync: When reconnected, queued operations execute
No Code Required
Offline support is enabled by default in Firestore Web SDK:User Experience
- Read operations work offline using cached data
- Write operations queue and sync when online
- Snapshot listeners continue to work with cached data
- Automatic retry of failed operations when reconnected
Testing Firebase Integration
Local Development
-
Firebase Emulator Suite (recommended):
-
Update connection in development:
Sample Data for Testing
TheloadSampleData() method provides fallback data for testing without Firebase.
Best Practices
- Always use server timestamps for ordering and consistency
- Implement error handling at every Firebase call
- Cleanup listeners when components unmount
- Use transactions for atomic multi-document updates
- Implement Security Rules to protect your data
- Monitor usage in Firebase Console to avoid quota limits
- Cache aggressively for offline support
- Validate data both client-side and server-side (Security Rules)
Common Patterns
Pattern 1: Async/Await with Try/Catch
Pattern 2: Real-time Listener with Cleanup
Pattern 3: Optimistic UI with Event Broadcasting
Troubleshooting
Problem: Real-time updates not working
Solution: Check that listener is properly set up:Problem: Timestamp ordering issues
Solution: Ensure server timestamp is used:Problem: Data not persisting
Solution: Check Firestore Security Rules allow writes:Next Steps
- Review Application Architecture for overall design
- Explore Module Documentation for component details
- Check DataStore API Reference for data access methods
- Read Firebase Documentation for advanced features