diff --git a/Backend/src/gists/entities/gist.entity.ts b/Backend/src/gists/entities/gist.entity.ts index 393f0732..5977c685 100644 --- a/Backend/src/gists/entities/gist.entity.ts +++ b/Backend/src/gists/entities/gist.entity.ts @@ -3,6 +3,7 @@ import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, Index } from @Entity('gists') @Index('idx_gists_location_cell') @Index('idx_gists_author_address') +@Index('idx_gists_location_geography', { synchronize: false }) export class Gist { @PrimaryGeneratedColumn('uuid') id: string; diff --git a/Backend/src/gists/gist.repository.ts b/Backend/src/gists/gist.repository.ts index 299d2d9f..b7bfb585 100644 --- a/Backend/src/gists/gist.repository.ts +++ b/Backend/src/gists/gist.repository.ts @@ -92,7 +92,7 @@ export class GistRepository { const extraWhere = `AND ${clauses.join(' AND ')}`; - const items = await this.dataSource.query( + const rows = await this.dataSource.query>( ` SELECT g.id, @@ -107,15 +107,16 @@ export class GistRepository { ST_X(g.location::geometry) AS lon, ST_Y(g.location::geometry) AS lat, ST_Distance( - g.location, + g.location::geography, ST_SetSRID(ST_MakePoint($1, $2), 4326)::geography ) AS distance_meters FROM gists g WHERE ST_DWithin( - g.location, + g.location::geography, ST_SetSRID(ST_MakePoint($1, $2), 4326)::geography, $3 ) + AND g.expires_at > NOW() ${extraWhere} ORDER BY distance_meters ASC, g.created_at DESC LIMIT $4 @@ -123,6 +124,11 @@ export class GistRepository { params, ); + const items: Gist[] = rows.map((r) => ({ + ...r, + distanceMeters: parseFloat(r.distance_meters), + })); + return PaginationHelper.buildResponse(items, limit); }