A House Divided A House DividedDocumentation
Changelog
Engineering/Conventions

MongoDB access guidelines (A House Divided)

Last updated 2026-08-21
Source files

This document complements AGENTS.md (in the AHDGame app repo) database conventions. It separates safe code-level practices from database administration decisions that need human review in production.

Connection and client#

Collection access: two supported patterns#

  1. Typed collection helpers in src/lib/db/collections/, preferred when a helper already exists or you are touching a hot path that should stay consistent (e.g. getUsersCollection, getCharactersCollection, getGameStateCollection, getPartyBudgetCollection).
  2. Direct access, db.collection<DocumentType>("collectionName") is acceptable and common; keep the generic correct and the name exactly as in existing code (camelCase collection names).

Do not introduce a repository framework or generic ORM layer unless there is a strong, explicit need.

Passing Db through call chains#

When code already holds const db = await getDb() (turn processing, admin batch routes, migrations), pass db into collection helpers that accept an optional Db:

const users = await getUsersCollection(db);
const characters = await getCharactersCollection(db);

That avoids redundant getDb() awaits and keeps a single logical scope for one request or one turn.

Not every helper accepts a Db. For example, getPartyBudgetCollection() currently opens through getDb() and takes no argument. Check the helper signature before passing a connection.

Typing and shapes#

Queries: hygiene and performance#

Transactions and consistency#

When adding cross-collection updates, consider: (1) idempotency where possible, (2) admin heal routes for known failure modes, (3) explicit documentation in the relevant ahd-docs design page.

Indexing and schema changes (human review)#

These are not substitutes for code review of query patterns:

Track those in release notes, run during maintenance windows when appropriate, and validate on staging with representative data.

Index concerns implied by common patterns (for DB owners)#

The following are observations for index planning; verify with explain and production metrics:

Testing#

Connected pages