Skip to content

Latest commit

 

History

History
354 lines (266 loc) · 8.58 KB

File metadata and controls

354 lines (266 loc) · 8.58 KB

Firestore Index Deployment Guide

🎯 Overview

This guide explains how to deploy Firestore indexes for the notifications feature. The app will work immediately with the simplified query, but deploying indexes will improve performance.


🚀 Quick Start (No Index Required)

Good News! The app is already configured to work without waiting for index deployment.

Current Implementation

The NotificationsScreen.tsx uses:

  • Simple query: orderBy('createdAt', 'desc') only
  • Client-side filtering: Filters active/expired notifications in the app
  • Works immediately: No Firebase configuration needed

Testing Now

  1. Open the app
  2. Navigate to Notifications tab
  3. Notifications should load successfully
  4. No index errors!

⚡ Performance Optimization (Optional)

For better performance with large datasets, deploy the Firestore indexes.

Method 1: Automatic via Firebase CLI (Recommended)

Prerequisites

# Install Firebase CLI if not already installed
npm install -g firebase-tools

# Login to Firebase
firebase login

# Initialize Firebase (if not done)
firebase init firestore

Deploy Indexes

# From project root directory
cd /data/data/com.termux/files/home/Reshme_Info

# Deploy indexes
firebase deploy --only firestore:indexes

Expected Output:

✔ Deploy complete!

Indexes deployed:
- notifications (isActive, createdAt)
- cocoonPrices (multiple indexes)
- dailySummaries (multiple indexes)

Index build status: https://console.firebase.google.com/firestore/indexes

Build Time: 2-10 minutes depending on existing data


Method 2: Manual via Firebase Console

Step 1: Access Firebase Console

  1. Go to Firebase Console
  2. Select your project: reshmeinfo
  3. Navigate to Firestore Database
  4. Click Indexes tab

Step 2: Create Composite Index

Click Create Index and enter:

Collection: notifications
Fields to index:
  1. isActive    →  Ascending
  2. createdAt   →  Descending
Query scope: Collection

Step 3: Click "Create Index"

  • Status will show "Building..."
  • Wait 2-10 minutes for completion
  • Refresh page to see "Enabled" status

Method 3: Use Auto-Generated Link

Firebase provided this direct link in your error:

https://console.firebase.google.com/v1/r/project/reshmeinfo/firestore/indexes?create_composite=ClBwcm9qZWN0cy9yZXNobWVpbmZvL2RhdGFiYXNlcy8oZGVmYXVsdCkvY29sbGVjdGlvbkdyb3Vwcy9ub3RpZmljYXRpb25zL2luZGV4ZXMvXxABGgwKCGlzQWN0aXZlEAEaDQoJY3JlYXRlZEF0EAIaDAoIX19uYW1lX18QAg

Steps:

  1. Click the link above (or from your error message)
  2. Firebase Console opens with pre-filled index configuration
  3. Click "Create Index"
  4. Wait for build completion

🔄 Switching to Optimized Query

After Index Deployment

Once the index is built (status shows "Enabled"), enable the optimized query:

Edit: screens/NotificationsScreen.tsx (lines 42-53)

Comment out the simple query:

// OPTION 1: Simple query (works without index - use this for immediate testing)
// const q = query(
//   collection(db, COLLECTIONS.NOTIFICATIONS),
//   orderBy('createdAt', 'desc')
// );

Uncomment the optimized query:

// OPTION 2: After deploying indexes, uncomment this for better performance
const q = query(
  collection(db, COLLECTIONS.NOTIFICATIONS),
  where('isActive', '==', true),
  orderBy('createdAt', 'desc')
);

Remove client-side filtering (line 63):

// Before (with client-side filter)
if (data.isActive && (!expiresAt || expiresAt > new Date())) {

// After (index handles isActive)
if (!expiresAt || expiresAt > new Date()) {

📊 Index Details

Notifications Index

{
  "collectionGroup": "notifications",
  "queryScope": "COLLECTION",
  "fields": [
    {
      "fieldPath": "isActive",
      "order": "ASCENDING"
    },
    {
      "fieldPath": "createdAt",
      "order": "DESCENDING"
    }
  ]
}

Why This Index?

  • isActive: Filters only active notifications
  • createdAt: Sorts by newest first
  • Combined: Efficient query for notification feed

🧪 Testing Index Deployment

Verify Index Status

Firebase Console

  1. Go to Firestore → Indexes
  2. Look for "notifications" collection
  3. Status should show "Enabled" (green)

Test Query

// This query should work without errors
const q = query(
  collection(db, COLLECTIONS.NOTIFICATIONS),
  where('isActive', '==', true),
  orderBy('createdAt', 'desc')
);

📈 Performance Comparison

Without Index (Current)

Query: orderBy('createdAt', 'desc')
Filter: Client-side (JavaScript)
Documents read: All notifications
Performance: Good for <100 notifications

With Index (After Deployment)

Query: where('isActive', true) + orderBy('createdAt', 'desc')
Filter: Server-side (Firestore)
Documents read: Only active notifications
Performance: Excellent for any size

Benefits

  • ✅ Faster query execution
  • ✅ Reduced bandwidth (fewer documents)
  • ✅ Lower Firestore read costs
  • ✅ Better scalability

🔧 Troubleshooting

Index Build Stuck

Problem: Index shows "Building..." for >15 minutes

Solutions:

  1. Refresh the Firebase Console page
  2. Check for existing data in notifications collection
  3. Delete index and recreate
  4. Contact Firebase support if persistent

Index Error After Deployment

Problem: Still getting index error after deployment

Solutions:

  1. Verify index status is "Enabled" (not "Building")
  2. Check index fields match exactly:
    • isActive (Ascending)
    • createdAt (Descending)
  3. Restart app / clear cache
  4. Check Firestore rules allow reads

Wrong Index Created

Problem: Created index but still getting errors

Solution: Delete incorrect index and recreate:

  1. Firestore Console → Indexes
  2. Find notifications index
  3. Click delete (⋮ menu)
  4. Create correct index using this guide

💰 Cost Implications

Firestore Pricing

  • Index storage: Minimal cost (few KB per index)
  • Read operations: Optimized query reduces reads
  • Build time: One-time operation (free)

Cost Savings with Index

With 1000 notifications, 100 active:

Without index:

  • Reads all 1000 documents
  • Client filters to 100
  • Cost: 1000 reads

With index:

  • Reads only 100 active documents
  • Server filters efficiently
  • Cost: 100 reads
  • Savings: 90%

🎯 Best Practices

Index Management

  • ✅ Deploy indexes before launching features
  • ✅ Monitor index build completion
  • ✅ Keep firestore.indexes.json in version control
  • ✅ Document custom indexes in code comments
  • ✅ Test queries in development first

Query Optimization

  • ✅ Use indexes for frequently-run queries
  • ✅ Combine filters efficiently (indexed first)
  • ✅ Avoid querying all documents when possible
  • ✅ Use client-side filtering only as fallback

📋 Deployment Checklist

Pre-Deployment

  • Updated firestore.indexes.json
  • Tested simplified query (works now)
  • Documented index requirements

Deployment

  • Choose deployment method (CLI/Console/Link)
  • Create notifications index
  • Wait for "Enabled" status (2-10 min)
  • Verify index in Firebase Console

Post-Deployment

  • Switch to optimized query in code
  • Test notifications load correctly
  • Monitor performance improvements
  • Remove client-side filtering (optional)

🔗 Useful Links


📞 Support

Getting Help


📝 Summary

Current State ✅

  • App works immediately with simplified query
  • No index required for testing
  • Client-side filtering handles active/expired logic

Optional Optimization ⚡

  • Deploy index for better performance
  • Reduces Firestore reads by 90%
  • Scales better with more notifications

Action Required

None for testing! Deploy index later for production optimization.


Last Updated: October 30, 2025 Index Status: Not deployed (app works without it) Next Step: Test notifications, deploy index before production