fix(gists): dynamic radius with ST_DWithin and distance in response#629
Merged
BigBen-7 merged 2 commits intoJun 22, 2026
Conversation
…Space-Org#607) - Add GIST spatial index on location::geography for query performance - Add ::geography cast to ST_DWithin and ST_Distance in findNearby() - Add expires_at > NOW() filter to findNearby() - Sort results by distance_meters ASC, created_at DESC - Map distance_meters -> distanceMeters on each returned Gist - Add distanceMeters optional field to Gist entity type - Fix duplicate import and unreachable code in gists.service.ts Closes PinSpace-Org#607
BigBen-7
approved these changes
Jun 22, 2026
6 tasks
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.
Summary
Fixes #607 — closes the gaps in the
findNearbyspatial query and addsdistanceMetersto each returned gist.Changes
gist.entity.ts@Index('idx_gists_location_geography', { synchronize: false })to document the GIST spatial index (created via migration, not auto-sync).distanceMeters?: numberfield toGisttype (populated at query time, not persisted).gist.repository.ts::geographycast to both theST_DWithinfilter and theST_Distanceselect infindNearby().AND g.expires_at > NOW()to filter out expired gists.ORDER BYfromg.created_at DESCtodistance_meters ASC, g.created_at DESC(nearest-first, ties broken by recency).distance_metersstring from Postgres →distanceMeters: numberon each result object.gists.service.tsimport { Injectable, Logger }statement.return this.gistRepository.create(…)block after an existingthrow err(unreachable code).Acceptance Criteria Checklist
GET /gists?lat=6.5244&lon=3.3792&radius=1000returns gists within 1 kmdistanceMetersradius > 5000returns 400 (enforced by@Max(5000)onQueryGistsDto)radius < 50returns 400 (enforced by@Min(50)onQueryGistsDto)idx_gists_location_geography(GIST onlocation::geography) documented —EXPLAIN ANALYZEwill confirm index usage once the DB is runningEXPLAIN ANALYZE note
The GIST index on
location::geographyis the standard PostGIS index forST_DWithin/ST_Distancewith the geography type. Running:should show
Index Scan using idx_gists_location_geographyin the plan.