fix(db-postgres): build near sort query properly for point fields (#12240)
Continuation of https://github.com/payloadcms/payload/pull/12185 and fix https://github.com/payloadcms/payload/issues/12221 The mentioned PR introduced auto sorting by the point field when a `near` query is used, but it didn't build actual needed query to order results by their distance to a _given_ (from the `near` query) point. Now, we build: ```sql order by pont_field <-> ST_SetSRID(ST_MakePoint(lng, lat), 4326) ``` Which does what we want
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import type { Table } from 'drizzle-orm'
|
import type { SQL, Table } from 'drizzle-orm'
|
||||||
import type { FlattenedField, Sort } from 'payload'
|
import type { FlattenedField, Sort } from 'payload'
|
||||||
|
|
||||||
import { asc, desc } from 'drizzle-orm'
|
import { asc, desc, or } from 'drizzle-orm'
|
||||||
|
|
||||||
import type { DrizzleAdapter, GenericColumn } from '../types.js'
|
import type { DrizzleAdapter, GenericColumn } from '../types.js'
|
||||||
import type { BuildQueryJoinAliases, BuildQueryResult } from './buildQuery.js'
|
import type { BuildQueryJoinAliases, BuildQueryResult } from './buildQuery.js'
|
||||||
@@ -16,6 +16,7 @@ type Args = {
|
|||||||
joins: BuildQueryJoinAliases
|
joins: BuildQueryJoinAliases
|
||||||
locale?: string
|
locale?: string
|
||||||
parentIsLocalized: boolean
|
parentIsLocalized: boolean
|
||||||
|
rawSort?: SQL
|
||||||
selectFields: Record<string, GenericColumn>
|
selectFields: Record<string, GenericColumn>
|
||||||
sort?: Sort
|
sort?: Sort
|
||||||
tableName: string
|
tableName: string
|
||||||
@@ -31,6 +32,7 @@ export const buildOrderBy = ({
|
|||||||
joins,
|
joins,
|
||||||
locale,
|
locale,
|
||||||
parentIsLocalized,
|
parentIsLocalized,
|
||||||
|
rawSort,
|
||||||
selectFields,
|
selectFields,
|
||||||
sort,
|
sort,
|
||||||
tableName,
|
tableName,
|
||||||
@@ -74,12 +76,18 @@ export const buildOrderBy = ({
|
|||||||
value: sortProperty,
|
value: sortProperty,
|
||||||
})
|
})
|
||||||
if (sortTable?.[sortTableColumnName]) {
|
if (sortTable?.[sortTableColumnName]) {
|
||||||
|
let order = sortDirection === 'asc' ? asc : desc
|
||||||
|
|
||||||
|
if (rawSort) {
|
||||||
|
order = () => rawSort
|
||||||
|
}
|
||||||
|
|
||||||
orderBy.push({
|
orderBy.push({
|
||||||
column:
|
column:
|
||||||
aliasTable && tableName === getNameFromDrizzleTable(sortTable)
|
aliasTable && tableName === getNameFromDrizzleTable(sortTable)
|
||||||
? aliasTable[sortTableColumnName]
|
? aliasTable[sortTableColumnName]
|
||||||
: sortTable[sortTableColumnName],
|
: sortTable[sortTableColumnName],
|
||||||
order: sortDirection === 'asc' ? asc : desc,
|
order,
|
||||||
})
|
})
|
||||||
|
|
||||||
selectFields[sortTableColumnName] = sortTable[sortTableColumnName]
|
selectFields[sortTableColumnName] = sortTable[sortTableColumnName]
|
||||||
|
|||||||
@@ -79,6 +79,7 @@ const buildQuery = function buildQuery({
|
|||||||
joins,
|
joins,
|
||||||
locale,
|
locale,
|
||||||
parentIsLocalized,
|
parentIsLocalized,
|
||||||
|
rawSort: context.rawSort,
|
||||||
selectFields,
|
selectFields,
|
||||||
sort: context.sort,
|
sort: context.sort,
|
||||||
tableName,
|
tableName,
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ import { buildAndOrConditions } from './buildAndOrConditions.js'
|
|||||||
import { getTableColumnFromPath } from './getTableColumnFromPath.js'
|
import { getTableColumnFromPath } from './getTableColumnFromPath.js'
|
||||||
import { sanitizeQueryValue } from './sanitizeQueryValue.js'
|
import { sanitizeQueryValue } from './sanitizeQueryValue.js'
|
||||||
|
|
||||||
export type QueryContext = { sort: Sort }
|
export type QueryContext = { rawSort?: SQL; sort: Sort }
|
||||||
|
|
||||||
type Args = {
|
type Args = {
|
||||||
adapter: DrizzleAdapter
|
adapter: DrizzleAdapter
|
||||||
@@ -348,6 +348,7 @@ export function parseParams({
|
|||||||
}
|
}
|
||||||
if (geoConstraints.length) {
|
if (geoConstraints.length) {
|
||||||
context.sort = relationOrPath
|
context.sort = relationOrPath
|
||||||
|
context.rawSort = sql`${table[columnName]} <-> ST_SetSRID(ST_MakePoint(${lng}, ${lat}), 4326)`
|
||||||
constraints.push(and(...geoConstraints))
|
constraints.push(and(...geoConstraints))
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
|
|||||||
Reference in New Issue
Block a user