Skip to content

fix(gists): dynamic radius with ST_DWithin and distance in response#629

Merged
BigBen-7 merged 2 commits into
PinSpace-Org:mainfrom
mogbonjubolaolasunkanmi-art:fix/607-dynamic-radius-st-dwithin
Jun 22, 2026
Merged

fix(gists): dynamic radius with ST_DWithin and distance in response#629
BigBen-7 merged 2 commits into
PinSpace-Org:mainfrom
mogbonjubolaolasunkanmi-art:fix/607-dynamic-radius-st-dwithin

Conversation

@mogbonjubolaolasunkanmi-art

Copy link
Copy Markdown
Contributor

Summary

Fixes #607 — closes the gaps in the findNearby spatial query and adds distanceMeters to each returned gist.

Changes

gist.entity.ts

  • Added @Index('idx_gists_location_geography', { synchronize: false }) to document the GIST spatial index (created via migration, not auto-sync).
  • Added distanceMeters?: number field to Gist type (populated at query time, not persisted).

gist.repository.ts

  • Added ::geography cast to both the ST_DWithin filter and the ST_Distance select in findNearby().
  • Added AND g.expires_at > NOW() to filter out expired gists.
  • Changed ORDER BY from g.created_at DESC to distance_meters ASC, g.created_at DESC (nearest-first, ties broken by recency).
  • Maps raw distance_meters string from Postgres → distanceMeters: number on each result object.

gists.service.ts

  • Removed duplicate import { Injectable, Logger } statement.
  • Removed dead return this.gistRepository.create(…) block after an existing throw err (unreachable code).

Acceptance Criteria Checklist

  • GET /gists?lat=6.5244&lon=3.3792&radius=1000 returns gists within 1 km
  • Each gist in the response includes distanceMeters
  • Results are sorted nearest-first
  • radius > 5000 returns 400 (enforced by @Max(5000) on QueryGistsDto)
  • radius < 50 returns 400 (enforced by @Min(50) on QueryGistsDto)
  • Spatial index idx_gists_location_geography (GIST on location::geography) documented — EXPLAIN ANALYZE will confirm index usage once the DB is running

EXPLAIN ANALYZE note

The GIST index on location::geography is the standard PostGIS index for ST_DWithin / ST_Distance with the geography type. Running:

EXPLAIN ANALYZE
SELECT * FROM gists
WHERE ST_DWithin(location::geography,
  ST_SetSRID(ST_MakePoint(3.3792, 6.5244), 4326)::geography, 1000)
AND expires_at > NOW();

should show Index Scan using idx_gists_location_geography in the plan.

…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 BigBen-7 merged commit 2f0824e into PinSpace-Org:main Jun 22, 2026
1 check failed
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 dynamic radius filtering with ST_DWithin and distance in response

2 participants