Skip to content

Commit 02ff288

Browse files
r1tsuukendelljoseph
authored andcommitted
fix(db-postgres): sort by distance when the near operator is used (#12185)
Fixes #12090, in MongoDB the documents are sorted by distance automatically whenever the `near` operation is used, which we have in the docs: > When querying using the near operator, the returned documents will be sorted by nearest first. This fixes this incosistensty between Postgres and MongoDB. ⚠️ This change potentially can cause to produce different results, if you used the `near` operator without `sort: 'pointFieldName'`.
1 parent d56dbec commit 02ff288

File tree

4 files changed

+26
-14
lines changed

4 files changed

+26
-14
lines changed

packages/drizzle/src/queries/buildAndOrConditions.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import type { FlattenedField, Where } from 'payload'
33

44
import type { DrizzleAdapter, GenericColumn } from '../types.js'
55
import type { BuildQueryJoinAliases } from './buildQuery.js'
6+
import type { QueryContext } from './parseParams.js'
67

78
import { parseParams } from './parseParams.js'
89

910
export function buildAndOrConditions({
1011
adapter,
1112
aliasTable,
13+
context,
1214
fields,
1315
joins,
1416
locale,
@@ -21,6 +23,7 @@ export function buildAndOrConditions({
2123
adapter: DrizzleAdapter
2224
aliasTable?: Table
2325
collectionSlug?: string
26+
context: QueryContext
2427
fields: FlattenedField[]
2528
globalSlug?: string
2629
joins: BuildQueryJoinAliases
@@ -41,6 +44,7 @@ export function buildAndOrConditions({
4144
const result = parseParams({
4245
adapter,
4346
aliasTable,
47+
context,
4448
fields,
4549
joins,
4650
locale,

packages/drizzle/src/queries/buildQuery.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { PgTableWithColumns } from 'drizzle-orm/pg-core'
33
import type { FlattenedField, Sort, Where } from 'payload'
44

55
import type { DrizzleAdapter, GenericColumn, GenericTable } from '../types.js'
6+
import type { QueryContext } from './parseParams.js'
67

78
import { buildOrderBy } from './buildOrderBy.js'
89
import { parseParams } from './parseParams.js'
@@ -52,24 +53,14 @@ const buildQuery = function buildQuery({
5253
id: adapter.tables[tableName].id,
5354
}
5455

55-
const orderBy = buildOrderBy({
56-
adapter,
57-
aliasTable,
58-
fields,
59-
joins,
60-
locale,
61-
parentIsLocalized,
62-
selectFields,
63-
sort,
64-
tableName,
65-
})
66-
6756
let where: SQL
6857

58+
const context: QueryContext = { sort }
6959
if (incomingWhere && Object.keys(incomingWhere).length > 0) {
7060
where = parseParams({
7161
adapter,
7262
aliasTable,
63+
context,
7364
fields,
7465
joins,
7566
locale,
@@ -81,6 +72,18 @@ const buildQuery = function buildQuery({
8172
})
8273
}
8374

75+
const orderBy = buildOrderBy({
76+
adapter,
77+
aliasTable,
78+
fields,
79+
joins,
80+
locale,
81+
parentIsLocalized,
82+
selectFields,
83+
sort: context.sort,
84+
tableName,
85+
})
86+
8487
return {
8588
joins,
8689
orderBy,

packages/drizzle/src/queries/parseParams.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { SQL, Table } from 'drizzle-orm'
2-
import type { FlattenedField, Operator, Where } from 'payload'
2+
import type { FlattenedField, Operator, Sort, Where } from 'payload'
33

44
import { and, isNotNull, isNull, ne, notInArray, or, sql } from 'drizzle-orm'
55
import { PgUUID } from 'drizzle-orm/pg-core'
@@ -14,9 +14,12 @@ import { buildAndOrConditions } from './buildAndOrConditions.js'
1414
import { getTableColumnFromPath } from './getTableColumnFromPath.js'
1515
import { sanitizeQueryValue } from './sanitizeQueryValue.js'
1616

17+
export type QueryContext = { sort: Sort }
18+
1719
type Args = {
1820
adapter: DrizzleAdapter
1921
aliasTable?: Table
22+
context: QueryContext
2023
fields: FlattenedField[]
2124
joins: BuildQueryJoinAliases
2225
locale?: string
@@ -30,6 +33,7 @@ type Args = {
3033
export function parseParams({
3134
adapter,
3235
aliasTable,
36+
context,
3337
fields,
3438
joins,
3539
locale,
@@ -57,6 +61,7 @@ export function parseParams({
5761
const builtConditions = buildAndOrConditions({
5862
adapter,
5963
aliasTable,
64+
context,
6065
fields,
6166
joins,
6267
locale,
@@ -342,6 +347,7 @@ export function parseParams({
342347
)
343348
}
344349
if (geoConstraints.length) {
350+
context.sort = relationOrPath
345351
constraints.push(and(...geoConstraints))
346352
}
347353
break

test/collections-rest/int.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1218,7 +1218,6 @@ describe('collections-rest', () => {
12181218
.GET(`/${pointSlug}`, {
12191219
query: {
12201220
limit: 5,
1221-
sort: 'point',
12221221
where: {
12231222
point: {
12241223
// querying large enough range to include all docs

0 commit comments

Comments
 (0)