Skip to content

feat(testing): add N+1 query detection helper and fix list endpoints (#1243) - #1423

Open
xtep103 wants to merge 1 commit into
Akanimoh12:test-implement-dripsfrom
xtep103:feat/n-plus-one-query-detection-1243
Open

feat(testing): add N+1 query detection helper and fix list endpoints (#1243)#1423
xtep103 wants to merge 1 commit into
Akanimoh12:test-implement-dripsfrom
xtep103:feat/n-plus-one-query-detection-1243

Conversation

@xtep103

@xtep103 xtep103 commented Aug 26, 2026

Copy link
Copy Markdown

Overview

Implements automated N+1 query detection and regression testing helpers for Prisma, and resolves existing N+1 query patterns across profiles and analytics services.

Related Issue

Closes #1243

Changes

Testing Framework & Database Observability

  • [ADD] backend/src/common/testing/queryCounter.ts
    • Added queryCounterMiddleware using Node's AsyncLocalStorage to track Prisma queries per async context.
    • Added countQueries(action) helper to inspect exact queries and execution count.
    • Added assertConstantQueryCount(runner, sizes) helper to verify that query count remains constant regardless of result-set / page size.
  • [ADD] backend/src/common/testing/queryCounter.test.ts
    • 10 unit tests verifying middleware passthrough, context isolation across concurrent calls, duration tracking, and failure diagnostics.
  • [ADD] backend/tests/helpers/queryCounter.ts
    • Re-exported testing helpers for integration tests.
  • [ADD] backend/tests/nPlusOne.test.ts
    • 17 tests verifying N+1 prevention across profiles, analytics, leaderboard, tips, notifications, goals, webhooks, subscriptions, and discovery services.
  • [MODIFY] backend/src/db/prisma.ts
    • Registered queryCounterMiddleware on the singleton Prisma client (no-op in production, active only within test counting contexts).

Service Optimization & N+1 Fixes

  • [MODIFY] backend/src/modules/profiles/profiles.service.ts
    • Replaced per-profile getTipStats() calls in listProfiles with a single batched prisma.tip.groupBy({ where: { toAddress: { in: addresses } } }) query, reducing queries from O(2N + 1) to O(3) constant.
  • [MODIFY] backend/src/modules/analytics/analytics.service.ts
    • Replaced per-row prisma.user.findUnique lookups in getTopTippers with a single batched prisma.user.findMany({ where: { stellarAddress: { in: addresses } } }).
    • Replaced per-row prisma.user.findUnique lookups for top tippers in getCreatorAnalytics with a single batched prisma.user.findMany.

Documentation

  • [ADD] backend/docs/N_PLUS_ONE_DETECTION.md
    • Complete documentation on N+1 query detection, usage examples, context architecture, and guidelines for testing new list endpoints.
  • [MODIFY] backend/docs/BACKEND_CONTRIBUTING.md
    • Added N+1 query guard guidelines and test requirements for all list/pagination endpoints.

Verification Results

✓ src/common/testing/queryCounter.test.ts (10 tests)
✓ tests/nPlusOne.test.ts (17 tests)

Test Files  2 passed (2)
     Tests  27 passed (27)
Acceptance Criteria Status
Core N+1 detection helper with AsyncLocalStorage & Prisma middleware ✅ Completed (countQueries, assertConstantQueryCount)
Fix existing N+1 patterns in profiles and analytics ✅ Fixed with batched groupBy and findMany in queries
Automated tests verifying constant query counts ✅ 27 tests passing
Developer documentation in docs/ N_PLUS_ONE_DETECTION.md added and contributing guide updated

- Introduce AsyncLocalStorage query counter and Prisma middleware (countQueries, assertConstantQueryCount)
- Fix N+1 queries in profiles.service.ts (batch tip stats aggregation in listProfiles)
- Fix N+1 queries in analytics.service.ts (batch user lookups in getTopTippers & getCreatorAnalytics)
- Add comprehensive unit and endpoint N+1 test suites
- Add N+1 testing documentation and update contributing guide

Closes Akanimoh12#1243
@drips-wave

drips-wave Bot commented Aug 26, 2026

Copy link
Copy Markdown

@xtep103 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

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.

Add N+1 query detection in tests

1 participant