Skip to content

feat: add Firestore CategoryStore with name reservations - #33

Merged
TechnicPotato merged 2 commits into
mainfrom
firestore-category-store
Nov 22, 2026
Merged

TechnicPotato merged 2 commits into
mainfrom
firestore-category-store

Conversation

@TechnicPotato

Copy link
Copy Markdown
Owner

Summary

Adds a Firestore implementation of CategoryStore. It passes the same 18 contract tests as the in-memory store, run against the Firestore emulator.

Category names are unique per user, ignoring case (ADR 0001). Firestore has no unique constraints, so each name has a reservation document in categoryNames. Its ID is the normalised name (name:<trimmed, lower-cased name>) and it holds { categoryId }. Because "Work" and "work" map to the same document ID, only one of them can be reserved.

How each method works

Method Mechanism Why
create Batch: create the category and its reservation No reads needed. If the reservation already exists, Firestore rejects the whole batch with ALREADY_EXISTS, which becomes name-taken. This also covers two concurrent creates of the same name.
get / list Plain reads list sorts in code with localeCompare("en", { sensitivity: "base" }), because Firestore's orderBy is case-sensitive.
delete Transaction: read the category, then delete it and its reservation The reservation key comes from the stored name. The transaction stops a concurrent rename from leaving a reservation behind that no category owns.
rename Transaction: read the category and the new name's reservation, then update the name and move the reservation name-taken and not-found are returned before any write. A rename that only changes capitalisation keeps the same reservation and only updates the name.

Other details:

  • The converter validates documents with Zod when reading, and stores only name, since the ID is the document ID.
  • Invalid document IDs (for example ones containing /) are treated as not found rather than throwing.
  • Expected outcomes (not-found, name-taken) are returned as result values. Anything unexpected is rethrown.

Refactor

isValidDocumentId and isNotFoundError moved from the task store to a shared lib/firestore.ts, together with the new isAlreadyExistsError. The task store now imports them, and its behaviour is unchanged.

Testing

  • firestore-category-store.test.ts runs the shared contract suite against the emulator. It's skipped when FIRESTORE_EMULATOR_HOST isn't set, as with the task store.
  • Each test uses its own collection prefix (category-test-<uuid>-), so tests don't share data.
  • Locally: 90/90 tests pass, and typecheck, lint and format check are clean.

Not in this PR

  • Category API routes, and categoryId on tasks (later Phase 3 steps).
  • Moving a deleted category's tasks to Uncategorized. That comes with categoryId on tasks.

@TechnicPotato TechnicPotato changed the title Firestore category store feat: add Firestore CategoryStore with name reservations Nov 22, 2026
@TechnicPotato
TechnicPotato merged commit 329c088 into main Nov 22, 2026
4 checks passed
@TechnicPotato
TechnicPotato deleted the firestore-category-store branch November 22, 2026 06:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant