Skip to content

refactor(db): introduce DatabaseContext pattern for injectable db in tests#1013

Merged
aaight merged 1 commit intodevfrom
feature/database-context-pattern
Mar 23, 2026
Merged

refactor(db): introduce DatabaseContext pattern for injectable db in tests#1013
aaight merged 1 commit intodevfrom
feature/database-context-pattern

Conversation

@aaight
Copy link
Copy Markdown
Collaborator

@aaight aaight commented Mar 23, 2026

Summary

  • Introduces a DatabaseContext class with a createDatabaseContext() factory that encapsulates the Drizzle db and pg pool, replacing the module-level let db / let pool singletons in src/db/client.ts
  • Adds setDefaultDatabaseContext() as the proper DI mechanism for tests, replacing the _setTestDb() hack (kept as a deprecated backward-compat wrapper)
  • Preserves all existing public APIs: getDb(), closeDb(), _setTestDb() — zero consumer changes needed
  • Exports DrizzleDb type for use in typed DI scenarios
  • Updates tests/helpers/sharedMocks.ts to expose setDefaultDatabaseContext and _setTestDb in mockDbClientModule
  • Updates tests/unit/db/client.test.ts with full coverage of the new DatabaseContext, createDatabaseContext(), and setDefaultDatabaseContext() APIs

Card: https://trello.com/c/VAKnTLgw/531-as-a-developer-i-want-db-clientts-refactored-into-a-databasecontext-pattern-so-that-repositories-are-unit-testable-without-a-rea

Test plan

  • npm test passes — 6596 tests across 344 test files, all green
  • npm run typecheck passes — zero TypeScript errors
  • biome check passes — zero lint errors
  • tests/unit/db/client.test.ts — 21 tests including new coverage for setDefaultDatabaseContext, createDatabaseContext, and DatabaseContext class
  • All existing repository tests continue to pass without modification (the getDb() mock in vi.mock('../../src/db/client.js') still works)

🤖 Generated with Claude Code

🕵️ claude-code · claude-sonnet-4-6 · run details

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 23, 2026

Codecov Report

❌ Patch coverage is 96.15385% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/db/client.ts 96.15% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

Copy link
Copy Markdown
Collaborator

@nhopeatall nhopeatall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

LGTM — Clean refactoring that introduces a DatabaseContext class wrapping the Drizzle db and pg pool, replacing module-level singletons with an injectable pattern. Backward compatibility is fully preserved: getDb(), closeDb(), and _setTestDb() all continue to work without consumer changes. CI is green across all 7 checks.

Notes

Minor comment inaccuracy (not blocking): The comment in closeDb() at src/db/client.ts:106-107 says "Only close if it's a real DatabaseContext / Skip if it was set via _setTestDb" — but the code doesn't skip anything. It unconditionally calls _defaultContext.close() and relies on the try/catch to handle mock contexts (whose close() is a no-op). The comment implies conditional behavior that doesn't exist. Consider simplifying to:

// close() is safe for both real pools and mock contexts (which have a no-op close)

Subtle behavior change (verified safe): The old closeDb() only reset pool/db but did NOT clear _testDbOverride. Now closeDb() nulls _defaultContext, which also clears any test override. This is safe because: (1) integration tests call _setTestDb(null) before closeDb(), and (2) unit tests call resetDbState() which does both. All 6596 tests pass, confirming no breakage.

🕵️ claude-code · claude-opus-4-6 · run details

@aaight aaight merged commit c864083 into dev Mar 23, 2026
9 checks passed
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.

2 participants