Skip to content

Comments

refactor: use $facet to merge services count query into single pipeline#249

Merged
james-cross merged 1 commit intostagingfrom
refactor/services-api-facet-optimisation
Feb 18, 2026
Merged

refactor: use $facet to merge services count query into single pipeline#249
james-cross merged 1 commit intostagingfrom
refactor/services-api-facet-optimisation

Conversation

@james-cross
Copy link
Collaborator

Summary

The services API was running two separate MongoDB aggregate calls per request: one for results and one for count. This merges them into a single round trip using $facet, and removes dead code that was adding pagination stages only to immediately strip them off.

Changes

  • Replace two aggregate() calls with a single $facet pipeline that returns both results and count
  • Remove $skip/$limit stages that were added to the pipeline then spliced off before execution
  • Remove unused $skip and $limit from the AggregationStage type
  • Add comments explaining why pagination happens in JS (services and accommodation must be interleaved by distance from separate collections)
  • Document the MAX_SERVICES_FETCH_LIMIT constant with its rationale

Testing

  • All 466 unit tests pass
  • TypeScript type-check passes with no errors
  • ESLint passes with no warnings or errors
  • No behavioural changes to the API response format or pagination logic

- Replace two separate aggregate calls (results + count) with a single $facet pipeline
- Remove dead $skip/$limit stages that were added then immediately stripped
- Add comments explaining why pagination happens in JS (distance interleaving)
- Document MAX_SERVICES_FETCH_LIMIT constant rationale
@james-cross james-cross merged commit 3953e7d into staging Feb 18, 2026
6 checks passed
@james-cross james-cross deleted the refactor/services-api-facet-optimisation branch February 18, 2026 21:15
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