Skip to content
Merged

fix #65

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/common/geo/make-radius-condition-query.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { makeSelectDistanceQuery } from "./make-select-distance-query";

export function makeRadiusConditionQuery(
tableAlias: string,
prop: string,
): string {
const distance = makeSelectDistanceQuery(tableAlias, prop);
return `${distance} <= :radius`;
return `ST_DWithin(
${tableAlias}.${prop},
ST_SetSRID(ST_MakePoint(:lng, :lat), 4326),
:radius * 1000
)`;
Comment on lines +6 to +10

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This is a great change to use spatial indexes! To improve readability and maintainability, I have a couple of suggestions:

  1. Magic Numbers: The values 4326 (SRID for WGS84) and 1000 (km to m conversion factor) are used directly. It's a good practice to extract these into named constants at the module level, or at least add comments to clarify their purpose.
  2. Formatting: The indentation within the template literal is inconsistent, which affects readability.

Here's a suggestion that improves formatting and adds SQL comments to clarify the magic numbers.

Suggested change
return `ST_DWithin(
${tableAlias}.${prop},
ST_SetSRID(ST_MakePoint(:lng, :lat), 4326),
:radius * 1000
)`;
return `ST_DWithin(
${tableAlias}.${prop},
ST_SetSRID(ST_MakePoint(:lng, :lat), 4326), -- WGS 84 SRID
:radius * 1000 -- Convert radius from km to meters
)`;

}
5 changes: 4 additions & 1 deletion src/common/geo/make-select-distance-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ export function makeSelectDistanceQuery(
tableAlias: string,
prop: string
): string {
return `ST_DistanceSphere(${tableAlias}.${prop}, ST_MakePoint(:lng, :lat)) / 1000`;
return `ST_DistanceSphere(
${tableAlias}.${prop},
ST_MakePoint(:lng, :lat)
) / 1000`;
Comment on lines +5 to +8

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The formatting of this multi-line template string has inconsistent indentation. Also, the division by the magic number 1000 to convert meters to kilometers could be made clearer. Using a SQL comment can improve readability.

Suggested change
return `ST_DistanceSphere(
${tableAlias}.${prop},
ST_MakePoint(:lng, :lat)
) / 1000`;
return `ST_DistanceSphere(
${tableAlias}.${prop},
ST_MakePoint(:lng, :lat)
) / 1000 -- Convert meters to kilometers`;

}