Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. Download free →
CLSkills
Cloud (AWS/GCP/Azure)intermediate

Firestore CRUD

Share

Implement Firestore CRUD operations

Works with OpenClaude

You are a Google Cloud Firebase developer. The user wants to implement complete Create, Read, Update, and Delete (CRUD) operations for Firestore documents.

What to check first

  • Verify Firebase project is initialized: check for google-services.json (Android) or GoogleService-Info.plist (iOS), or confirm firebase init ran for web
  • Run npm list firebase to confirm Firebase SDK is installed (v9.0.0+ for modular SDK recommended)
  • Verify Firestore security rules allow read/write for your authentication context

Steps

  1. Initialize Firebase app using the modular SDK and get a Firestore reference with initializeApp() and getFirestore()
  2. Create documents using addDoc() (auto-generated ID) or setDoc() (custom ID) with the collection reference
  3. Read single documents with getDoc() passing a document reference, or fetch collections with getDocs() and query constraints
  4. Update existing documents using updateDoc() to modify specific fields, or setDoc() with merge: true to avoid overwriting
  5. Delete documents with deleteDoc() passing a document reference
  6. Handle Firestore timestamps using serverTimestamp() for server-side consistency
  7. Implement error handling with try-catch blocks for network and permission failures
  8. Use onSnapshot() for real-time listeners instead of one-time reads when appropriate

Code

import { initializeApp } from 'firebase/app';
import {
  getFirestore,
  collection,
  addDoc,
  setDoc,
  getDoc,
  getDocs,
  updateDoc,
  deleteDoc,
  doc,
  serverTimestamp,
  query,
  where,
  orderBy,
  limit,
  onSnapshot
} from 'firebase/firestore';

const firebaseConfig = {
  apiKey: 'YOUR_API_KEY',
  projectId: 'YOUR_PROJECT_ID',
  databaseURL: 'YOUR_DB_URL'
};

const app = initializeApp(firebaseConfig);
const db = getFirestore(app);

// CREATE: Add new document with auto-generated ID
async function createUser(userData) {
  try {
    const docRef = await addDoc(collection(db, 'users'), {
      ...userData,
      createdAt: serverTimestamp()
    });
    console.log('Document created with ID:', docRef.id);
    return docRef.id;
  } catch (error) {
    console.error('Error creating document:', error);
  }
}

// CREATE: Set document with custom ID
async function createUserWithId(userId, userData) {
  try {
    await setDoc(doc(db, 'users', userId), {
      ...userData,
      createdAt: serverTimestamp()
    });
    console.log('Document set with ID:', userId);
  } catch (error) {
    console

Note: this example was truncated in the source. See the GitHub repo for the latest full version.

Common Pitfalls

  • Treating this skill as a one-shot solution — most workflows need iteration and verification
  • Skipping the verification steps — you don't know it worked until you measure
  • Applying this skill without understanding the underlying problem — read the related docs first

When NOT to Use This Skill

  • When a simpler manual approach would take less than 10 minutes
  • On critical production systems without testing in staging first
  • When you don't have permission or authorization to make these changes

How to Verify It Worked

  • Run the verification steps documented above
  • Compare the output against your expected baseline
  • Check logs for any warnings or errors — silent failures are the worst kind

Production Considerations

  • Test in staging before deploying to production
  • Have a rollback plan — every change should be reversible
  • Monitor the affected systems for at least 24 hours after the change

Quick Info

Difficultyintermediate
Version1.0.0
AuthorClaude Skills Hub
cloudgcpfirestore

Install command:

curl -o ~/.claude/skills/firestore-crud.md https://claude-skills-hub.vercel.app/skills/cloud/firestore-crud.md

Related Cloud (AWS/GCP/Azure) Skills

Other Claude Code skills in the same category — free to download.

Want a Cloud (AWS/GCP/Azure) skill personalized to YOUR project?

This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.