Skip to content

ericrisco/rsc-harness / skills/firebase

firebase

Verified

Use when building on Firebase — Firestore data modeling, Security Rules, Auth and custom claims, Cloud Functions, Storage, modular Web/Admin SDK imports — including symptoms like a database open to the internet, a query rejected by rules, or a doc stuck at ~1 write/sec. NOT managed-Postgres BaaS with SQL and RLS (that is supabase).

FileKindSize
SKILL.mdskill md13.1 KB
evals/README.mdmarkdown944 B
evals/cases.yamlconfig4.2 KB
references/cloud-functions.mdmarkdown3.9 KB
references/data-modeling.mdmarkdown3.9 KB
references/security-rules.mdmarkdown4.8 KB
scripts/verify.shexecutablescript4.2 KB

references/data-modeling.md

View on GitHub
# Data modeling — denormalization, sharding, pagination, indexes

Firestore is a NoSQL document store. You pay for and wait on reads, so the unit of design is the read
path, not the normalized entity. No joins exist; you either denormalize or you fan out.

## Denormalization and fan-out

Copy the few fields a screen needs onto the document that screen reads, and update the copies on
write. Example: a feed shows `authorName` next to each post.

```ts
// Write path keeps the copy in sync (a small batch, or a Cloud Function on the user doc).
const batch = writeBatch(db);
batch.set(doc(db, "posts", postId), { authorId, authorName: user.displayName, body });
await batch.commit();
```

Fan-out write: when one event must appear in many places (a message into every member's inbox), write
N documents in a batch (max 500 ops) or trigger a Cloud Function to do the fan-out asynchronously.

## Sharded counters

A single document tolerates ~1 sustained write/sec. A "likes" counter on a viral post will exceed
that. Spread writes across N shard docs and sum on read.

```ts
const N = 10;
// increment a random shard
await updateDoc(doc(db, "posts", postId, "shards", String(Math.floor(Math.random() * N))), {
  count: increment(1),
});
// read = sum of shards
const shards = await getDocs(collection(db, "posts", postId, "shards"));
const total = shards.docs.reduce((s, d) => s + d.data().count, 0);
```

Pick N to cover peak writes/sec (each shard absorbs ~1/sec).

## Avoiding hotspots

Sequential IDs and monotonically increasing indexed fields (auto-increment, `Date.now()` written to an
indexed field at high rate) concentrate writes on one index range. Use Firestore's scattered auto-IDs
(`addDoc` / `doc(collection(db,'x'))`), and if you must index a timestamp at high write rate, add a
sharding prefix to spread the index.

## Pagination with cursors

Don't use numeric offsets (Firestore still reads the skipped docs). Use cursors.

```ts
const first = query(collection(db, "posts"), orderBy("createdAt", "desc"), limit(20));
const snap = await getDocs(first);
const last = snap.docs[snap.docs.length - 1];
const next = query(collection(db, "posts"), orderBy("createdAt", "desc"), startAfter(last), limit(20));
```

## Aggregation

For counts/sums without reading every doc, use server-side aggregation:

```ts
import { getCountFromServer } from "firebase/firestore";
const c = await getCountFromServer(query(collection(db, "posts"), where("ownerId", "==", uid)));
c.data().count; // billed as a small number of reads, not one-per-doc
```

## Collection-group queries

To query the same-named subcollection across all parents (every `messages` subcollection at once):

```ts
import { collectionGroup } from "firebase/firestore";
const recent = query(collectionGroup(db, "messages"), orderBy("createdAt", "desc"), limit(50));
```

This needs a collection-group composite index (declared in `firestore.indexes.json`) and Rules that
match the subcollection path with a wildcard.

## Composite index design

A query that filters/ranges on one field and orders by another needs a composite index. Firestore's
error in production gives you a one-click link to create it, but you should commit it to source.

```json
{
  "indexes": [
    {
      "collectionGroup": "posts",
      "queryScope": "COLLECTION",
      "fields": [
        { "fieldPath": "ownerId", "order": "ASCENDING" },
        { "fieldPath": "createdAt", "order": "DESCENDING" }
      ]
    }
  ],
  "fieldOverrides": []
}
```

Deploy with `firebase deploy --only firestore:indexes`. The emulator runs queries without these
indexes, so "works locally, fails in prod with *requires an index*" is the expected gap — keep this
file in sync and deploy it before shipping new queries.

## Query constraint caps

- `in`, `not-in`, `array-contains-any`: up to ~30 values per query.
- A single range/inequality filter applies to one field; combine with other filters via composite
  indexes. There is no `OR` across different fields without `or()` + the matching indexes.
rea-analysis-optimizationmorluto/reaOptimize REA providers and shared analysis workflows for agent context, latency, CPU, and memory while preserving correctness and Evidence. Use for analysis bottlenecks and cache or query design; CI/test-suite optimization belongs to rea-ci-tests-audit.Stars: 471kUpdated Claude CodeCodexVerifiedInstall
rea-ci-tests-auditmorluto/reaAudit REA CI and test suites for correctness, fixture drift, false passes, and measured performance; use for suite or workflow investigations and efficiency changes, rather than ordinary feature testing or a single test failure.Stars: 471kUpdated Claude CodeCodexVerifiedInstall
rea-tool-designmorluto/reaAudit, investigate, design, or change REA investigation tools, CLI/MCP contracts, provider capabilities, and Evidence semantics.Stars: 471kUpdated Claude CodeCodexVerifiedInstall
ponytail-auditDietrichGebert/ponytailQuality audit of the whole repo: bugs, security, real load, missing tests, speed, and what to delete. Most important first.Stars: 194kUpdated Claude CodeCodexVerifiedInstall