Firebase

From G1Joshi/Agent-Skills · View interactive version

Firebase is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert Firebase & Cloud Firestore assistance covering collection/document models, real-time listeners, security rules, and offline caching. Use when building mobile/web apps with real-time sync and client auth.

How to use this skill

Skill file

Firebase (Firestore & Realtime Database)

Firebase provides two NoSQL databases:

  1. Cloud Firestore: The newer, recommended scalable database.
  2. Realtime Database: The original low-latency JSON tree sync.

When to Use

Quick Start

import { getFirestore, collection, addDoc } from "firebase/firestore";

const db = getFirestore(app);

// Add document
await addDoc(collection(db, "users"), {
  first: "Ada",
  last: "Lovelace",
  born: 1815,
});

Core Concepts

Document & Collection Hierarchy (Firestore)

Data is organized into documents containing fields, nested inside collections and subcollections:

users (Collection)
  └── user_101 (Document)
        ├── name: "Alex"
        └── orders (Subcollection)
              └── order_99 (Document)

Real-Time Snapshot Listeners

Subscribes to live document updates with zero polling boilerplate:

import { doc, onSnapshot } from "firebase/firestore";
import { db } from "./firebase-config";

const unsub = onSnapshot(doc(db, "projects", "proj_415"), (docSnap) => {
  if (docSnap.exists()) {
    console.log("Current Project Data:", docSnap.data());
  }
});

Declarative Security Rules Architecture

Enforces authorization and schema validation at the database layer:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{userId} {
      allow read, write: if request.auth != null && request.auth.uid == userId;
    }
    match /posts/{postId} {
      allow read: if resource.data.isPublished == true;
      allow create: if request.auth != null && request.resource.data.authorId == request.auth.uid;
    }
  }
}

Common Patterns

Atomic Counter Increment with Firestore Transactions

Problem: Concurrent client clicks reading and updating the same counter cause race condition data corruption.

Solution:

Use FieldValue.increment for atomic database updates:

import { doc, updateDoc, increment } from "firebase/firestore";
import { db } from "./firebaseConfig";

async function upvotePost(postId) {
  const postRef = doc(db, "posts", postId);
  await updateDoc(postRef, {
    upvotes: increment(1),
    lastActivity: new Date(),
  });
}

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

| :------------------------------------ | :------------------------------------------------------------------- | :------------------------------------------------------------------------------------------ |

| Missing or insufficient permissions | Firestore Security Rules rejecting the request. | Check request.auth in firestore.rules and verify client authentication token. |

| The query requires an index | Query filters across multiple fields or orders on a different field. | Click the generated index link in the Firebase error console to create the composite index. |

| Resource Exhausted: Quota exceeded | Daily free tier document read/write limits exceeded. | Upgrade to Blaze plan or optimize listener queries with limit() clauses. |

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.