Skip to content

fix(db-postgres): build near sort query properly for point fields #12240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
14 changes: 11 additions & 3 deletions packages/drizzle/src/queries/buildOrderBy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Table } from 'drizzle-orm'
import type { SQL, Table } from 'drizzle-orm'
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 { BuildQueryJoinAliases, BuildQueryResult } from './buildQuery.js'
Expand All @@ -16,6 +16,7 @@ type Args = {
joins: BuildQueryJoinAliases
locale?: string
parentIsLocalized: boolean
rawSort?: SQL
selectFields: Record<string, GenericColumn>
sort?: Sort
tableName: string
Expand All @@ -31,6 +32,7 @@ export const buildOrderBy = ({
joins,
locale,
parentIsLocalized,
rawSort,
selectFields,
sort,
tableName,
Expand Down Expand Up @@ -74,12 +76,18 @@ export const buildOrderBy = ({
value: sortProperty,
})
if (sortTable?.[sortTableColumnName]) {
let order = sortDirection === 'asc' ? asc : desc

if (rawSort) {
order = () => rawSort
}

orderBy.push({
column:
aliasTable && tableName === getNameFromDrizzleTable(sortTable)
? aliasTable[sortTableColumnName]
: sortTable[sortTableColumnName],
order: sortDirection === 'asc' ? asc : desc,
order,
})

selectFields[sortTableColumnName] = sortTable[sortTableColumnName]
Expand Down
1 change: 1 addition & 0 deletions packages/drizzle/src/queries/buildQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const buildQuery = function buildQuery({
joins,
locale,
parentIsLocalized,
rawSort: context.rawSort,
selectFields,
sort: context.sort,
tableName,
Expand Down
3 changes: 2 additions & 1 deletion packages/drizzle/src/queries/parseParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { buildAndOrConditions } from './buildAndOrConditions.js'
import { getTableColumnFromPath } from './getTableColumnFromPath.js'
import { sanitizeQueryValue } from './sanitizeQueryValue.js'

export type QueryContext = { sort: Sort }
export type QueryContext = { rawSort?: SQL; sort: Sort }

type Args = {
adapter: DrizzleAdapter
Expand Down Expand Up @@ -348,6 +348,7 @@ export function parseParams({
}
if (geoConstraints.length) {
context.sort = relationOrPath
context.rawSort = sql`${table[columnName]} <-> ST_SetSRID(ST_MakePoint(${lng}, ${lat}), 4326)`
constraints.push(and(...geoConstraints))
}
break
Expand Down
Loading