feat: add Prisma read‑replica service and inject into AnalyticsServic…#387
Open
d3v-active wants to merge 2 commits into
Open
feat: add Prisma read‑replica service and inject into AnalyticsServic…#387d3v-active wants to merge 2 commits into
d3v-active wants to merge 2 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #246
Summary
This PR introduces a dedicated read‑replica layer for Prisma operations. A new service (
PrismaReadReplicaService) is responsible for handling all read‑only queries (find*,count*,aggregate*) against a secondary database connection (DATABASE_URL_READ). The primary Prisma client (PrismaService) continues to handle write operations. The change is applied to the analytics module and is verified with a new end‑to‑end test.Why this change is needed
Key Changes
src/prisma/prisma-read-replica.service.ts(new)PrismaClientinstance usingDATABASE_URL_READ.DATABASE_URLif the replica URL is not defined.onModuleInit,onModuleDestroy) to manage the replica client’s connection pool.invokemethod that dynamically forwards any Prisma call.find,count, oraggregateto the replica client; all other calls are delegated to the primaryPrismaService.src/prisma/prisma.module.tsPrismaReadReplicaServicealongside the existingPrismaServiceso it can be injected wherever needed.src/analytics/analytics.service.tsPrismaService(for writes) andPrismaReadReplicaService(for reads).claim.findManyandcampaign.count) to usethis.prismaRead.invoke(...).$transactionused for updates) continue to use the primaryPrismaService.E2E Test –
analytics.e2e-spec.ts(new artifact)PrismaServiceandPrismaReadReplicaServicewith jest mocks.AnalyticsService.PrismaReadReplicaService.invokeis called the expected number of times (four calls total, two per request).Git Commit
feat: add Prisma read‑replica service and inject into AnalyticsService for read‑heavy queries.Implementation Details
invokemethod inspects the method name using a regular expression (/^(find|count|aggregate)/). If it matches, the call is executed on the replica client; otherwise, it falls back to the primary service. This approach avoids having to manually replace every Prisma call throughout the codebase.DATABASE_URL_READis not set, the replica service logs a warning and reverts to the primary URL, ensuring the application remains functional in environments where a replica is not provisioned.PrismaModule, other modules can request it via NestJS’s DI container. The explicit injection strategy (instead of a global wrapper) respects the user’s request to limit changes to only the services we modify.Testing & Verification
npm test) – all existing tests pass.npm run test:e2e) – the test succeeds, confirming that reads are routed through the replica while a write is simulated.DATABASE_URLandDATABASE_URL_READset shows the replica client connecting successfully (log “Read‑replica Prisma client connected”).Impact