Home › Firebase › Firebase (modular SDK) rules

Firebase (modular SDK) rules

Cursor rules for Firebase web apps with the modular v11+ SDK, Firestore security rules, and emulator tests.

CursorFirebase

How to use

Save this as .cursor/rules/firebase.mdc in your repository root, commit it, and your agent will pick it up automatically. Edit the commands to match your project.

Note: Review and test before use. Provided as is, without warranty. See Terms.

.cursor/rules/firebase.mdcDownload  
---
description: Firebase (modular SDK) conventions
globs: ["src/**/*.{ts,tsx}","firestore.rules","functions/**/*.ts"]
alwaysApply: false
---

# Firebase

- Use the modular SDK only: `import { getFirestore, doc, getDoc } from 'firebase/firestore'`. No `firebase/compat` imports.
- Initialize once in `src/lib/firebase.ts` (`initializeApp` guarded by `getApps().length`), export `auth`, `db`, `storage`.
- Firestore data model: design around queries. Keep documents small, use subcollections for unbounded lists, and denormalize what a screen reads together.
- Use typed converters (`withConverter`) for each collection.
- Writes that must be consistent use `runTransaction` or `writeBatch`. Timestamps with `serverTimestamp()`.
- Every query must be allowed by `firestore.rules`. Update the rules in the same change as the feature; rules check `request.auth.uid` and validate field types and sizes.
- Composite indexes go in `firestore.indexes.json`, not just the console.
- Listeners (`onSnapshot`) return an unsubscribe; call it on unmount.
- Privileged logic (payments, roles, counters) runs in Cloud Functions (v2 API, `firebase-functions/v2`) with the Admin SDK, never on the client.
- The web config is public; real secrets go in Functions secrets (`defineSecret`).

## Don't
- Don't ship `allow read, write: if true` rules, even temporarily.

## Done when
Rules tests pass against the emulator: `firebase emulators:exec --only firestore "npm test"`.

More: all Firebase rules · all Cursor rules

Related rules

Convex backend rules

Cursor rules for Convex backends: schema, validated queries/mutations/actions, indexes, and auth checks.

CursorConvex

Drizzle ORM rules

Cursor rules for Drizzle ORM with Postgres or SQLite: schema files, drizzle-kit migrations, and type-safe queries.

CursorDrizzle ORM

Mongoose (MongoDB ODM) rules

Cursor rules for Mongoose 8 with TypeScript: typed schemas, indexes, lean queries, validation, and transactions.

CursorMongoDB

Prisma ORM rules

Cursor rules for Prisma ORM 6 with PostgreSQL: schema changes, migrations, typed queries, and transactions.

CursorPrisma

Supabase backend rules

Cursor rules for apps on Supabase: Row Level Security first, migrations, typed clients and Edge Functions.

CursorSupabase

MongoDB Windsurf rules

Windsurf rules for MongoDB 8 apps with the Node.js driver: schema design, indexes, aggregation, and transactions.

WindsurfMongoDB

Get new rules weekly

One email a week: fresh rule files, agent tips, and what's changing in Cursor, Claude Code and Copilot. Free, unsubscribe anytime.

Opens the Rulestack Newsletter on beehiiv in a new tab. Or subscribe directly on beehiiv.