Skill v1.0.0
currentAutomated scan96/100name: firebase-architect description: "Designs Firestore models, security rules, Cloud Functions v2, and App Check. Use when building a Firebase feature, writing rules or triggers, or wiring a Firestore data layer." when_to_use: "NOT for auditing existing rules (firebase-security-auditor agent), engine choice or SQL (use database-architect), or backfills (use data-migration)." argument-hint: "[feature-or-collection]" metadata: version: "1.0.0" verified: 2026-09-23
Firebase Architect
Outcome: a Firebase feature design that is secure by default and keeps Firebase out of the domain layer — collection tree, document shapes, access-pattern → index table, security rules with emulator tests, and any server logic as Functions v2. Done when every collection the clients touch has a rule and a rules test, every query has an index, and no domain or presentation code imports a Firebase type. Path rules in rules/firebase.md (Claude Code) carry the same standards for code edits.
Pre-Processing (Auto-Context)
Context — run these read-only commands first; skip any that fail or aren't permitted (they only tailor the output):
- Firebase files:
ls firebase.json firestore.rules storage.rules firestore.indexes.json 2>/dev/null || echo "(none)" - Databases and functions config:
grep -nE '"(database|source|runtime)"' firebase.json 2>/dev/null | head -8 || echo "(no firebase.json)" - Functions runtime:
grep -nE '"(node|firebase-functions|firebase-admin)"' functions/package.json 2>/dev/null | head -4 || echo "(no functions/)"
Step 1: Classify
| Request | Output | |
|---|---|---|
| Data model | Collection tree + document shapes + access-pattern/index table + rules | |
| Security rules | firestore.rules changes + rules unit tests | |
| Server logic | Functions v2 trigger or callable + idempotency plan | |
| Client data layer | DTO + data source + repository impl (Android/iOS/web) | |
| Full feature | All of the above, scoped to the feature | |
| Review or question | Findings with severity, or an answer; no files |
Step 2: Gather Context
Confirm: feature and collections (new or existing), who reads/writes what (owner, org member, admin, public), offline needs (then pair with offline-first), scale, and whether a named database is in use.
Step 3: Data Model Rules
- Clean Architecture boundary: no
FirebaseFirestore,DocumentReference,Timestamp, orDocumentSnapshotabove the data layer. DTOs map to domain models in the repository; listeners becomeFlowviacallbackFlow(Android) orAsyncStream(iOS); writes returnResult. - IDs:
users/{uid}uses the Auth UID; entities use auto-IDs; slugs only for known-key lookups. No sequential or date-prefixed IDs on high-write collections (index hotspots) — store the time in a field. - Shape: subcollection for unbounded or separately secured children; arrays only for small bounded sets; 1 MiB doc cap; ~1 sustained write/sec per document, so shard counters.
- Timestamps:
createdAt/updatedAtset withFieldValue.serverTimestamp()(import { FieldValue } from 'firebase-admin/firestore'on the server;serverTimestamp()fromfirebase/firestoreon web). Rules enforcerequest.resource.data.updatedAt == request.time. - Denormalized copies are listed in the design with the Function that maintains each one.
- Named databases (up to 100 per project) for residency or hard tenant isolation only; each has its own rules, indexes, and
firebase.jsonentry, and triggers must passdatabase: '<id>'.
Step 4: Security Rules Patterns
Deny by default; every match is explicit. Patterns Cure uses:
rules_version = '2';service cloud.firestore {match /databases/{database}/documents {function signedIn() { return request.auth != null; }function isOwner(uid) { return signedIn() && request.auth.uid == uid; }function hasRole(role) { return signedIn() && request.auth.token.role == role; } // custom claim, set server-sidefunction onlyChanges(keys) { return request.resource.data.diff(resource.data).affectedKeys().hasOnly(keys); }match /users/{uid} {allow read: if isOwner(uid) || hasRole('admin');allow create: if isOwner(uid)&& request.resource.data.keys().hasOnly(['displayName', 'createdAt', 'updatedAt'])&& request.resource.data.createdAt == request.time;allow update: if isOwner(uid) && onlyChanges(['displayName', 'updatedAt'])&& request.resource.data.updatedAt == request.time;allow delete: if false; // deletion goes through a Function that cleans up subcollections}}}
- Privileged fields (
role,plan,credits,status) are never client-writable; they change only via Admin SDK in Functions. Use custom claims for roles, not a client-readablerolefield. listqueries must be constrained by the rule (rules are not filters): a ruleresource.data.ownerId == request.auth.uidrequires the client query to includewhere('ownerId', '==', uid).- Minimise
get()/exists()lookups in hot rules — each is a billed read and adds latency. - Every rule change ships with
@firebase/rules-unit-testingtests against the emulator (allowed and denied cases). Mirror the same discipline instorage.rules(content type + size limits per path).
Step 5: Cloud Functions v2
- Triggers from
firebase-functions/v2/firestore:onDocumentCreated,onDocumentUpdated,onDocumentDeleted,onDocumentWritten(not the v1onCreate/onWrite). HTTPS/callables fromfirebase-functions/v2/https. - Delivery is at-least-once: make handlers idempotent (use
event.idor a deterministic target ID) and guard against self-triggering loops when a trigger writes to the document it watches. - Admin SDK is modular:
initializeApp()fromfirebase-admin/app,getFirestore()fromfirebase-admin/firestore,getAuth()fromfirebase-admin/auth. - Callables that clients invoke set
enforceAppCheck: true; setregionandminInstancesexplicitly for latency-sensitive paths; secrets viadefineSecret, never plain env vars.
Step 6: App Check
Enable App Check for Firestore, Storage, Functions, and Firebase AI Logic before launch: Play Integrity (Android), App Attest with DeviceCheck fallback (iOS), reCAPTCHA Enterprise (web). Roll out in monitor mode, watch the verified-request ratio in the console, then enforce. Debug providers only in debug builds; register CI debug tokens as secrets. App Check reduces abuse; it does not replace rules.
Code/Artifact Generation
Applies only when Step 1 calls for building (not reviews or questions). Read existing firestore.rules, firestore.indexes.json, and firebase.json first and extend them; never overwrite.
Produce the files the classification needs, from this set: rules + rules tests, index entries, TypeScript document types for Functions, platform DTOs (Kotlin data class / Swift Codable) for the platforms the repo has, and v2 triggers or callables. Deliver the requested feature; don't restructure unrelated collections or rules.
Current Defaults (verified 2026-09-23)
| Component | Default | Source | |
|---|---|---|---|
| Android SDK | Firebase BoM 34.x (34.19.0 on 2026-09-09); main modules only — KTX artifacts were removed from BoM 34.0.0, and their Kotlin APIs now live in the main modules | firebase.google.com/support/release-notes/android | |
| Functions runtime | Node.js 22 ("engines": {"node": "22"}; Cloud Run functions support to 2027-10-31). Node 20's decommission date is 2026-10-30: treat it as unsupported and move any function still on it. Node 24 is supported on Cloud Run functions to 2028-10-31 but not listed by the Firebase docs (checked 2026-10-24) — confirm Firebase CLI support before use | firebase.google.com/docs/functions/manage-functions; docs.cloud.google.com/functions/docs/runtime-support | |
| Functions API | 2nd gen (firebase-functions/v2/*), TypeScript | firebase.google.com/docs/functions/firestore-events | |
| Emulators | firestore 8080, auth 9099, functions 5001, storage 9199 | — | |
| Rules tests | @firebase/rules-unit-testing + the project's test runner | — |
Related skills
firebase-security-auditoragent — audit of existing rules.database-architect— engine choice, indexes, SQL.data-migration— reshaping or backfilling existing Firestore data.offline-first— local cache, sync, conflicts.security-review— broader threat model.