The `localized` properly was not stripped out of referenced block fields, if any parent was localized. For normal fields, this is done in sanitizeConfig. As the same referenced block config can be used in both a localized and non-localized config, we are not able to strip it out inside sanitizeConfig by modifying the block config. Instead, this PR had to bring back tedious logic to handle it everywhere the `field.localized` property is accessed. For backwards-compatibility, we need to keep the existing sanitizeConfig logic. In 4.0, we should remove it to benefit from better test coverage of runtime field.localized handling - for now, this is done for our test suite using the `PAYLOAD_DO_NOT_SANITIZE_LOCALIZED_PROPERTY` flag.
94 lines
2.4 KiB
TypeScript
94 lines
2.4 KiB
TypeScript
import type { Table } from 'drizzle-orm'
|
|
import type { FlattenedField, Sort } from 'payload'
|
|
|
|
import { asc, desc } from 'drizzle-orm'
|
|
|
|
import type { DrizzleAdapter, GenericColumn } from '../types.js'
|
|
import type { BuildQueryJoinAliases, BuildQueryResult } from './buildQuery.js'
|
|
|
|
import { getNameFromDrizzleTable } from '../utilities/getNameFromDrizzleTable.js'
|
|
import { getTableColumnFromPath } from './getTableColumnFromPath.js'
|
|
|
|
type Args = {
|
|
adapter: DrizzleAdapter
|
|
aliasTable?: Table
|
|
fields: FlattenedField[]
|
|
joins: BuildQueryJoinAliases
|
|
locale?: string
|
|
parentIsLocalized: boolean
|
|
selectFields: Record<string, GenericColumn>
|
|
sort?: Sort
|
|
tableName: string
|
|
}
|
|
|
|
/**
|
|
* Gets the order by column and direction constructed from the sort argument adds the column to the select fields and joins if necessary
|
|
*/
|
|
export const buildOrderBy = ({
|
|
adapter,
|
|
aliasTable,
|
|
fields,
|
|
joins,
|
|
locale,
|
|
parentIsLocalized,
|
|
selectFields,
|
|
sort,
|
|
tableName,
|
|
}: Args): BuildQueryResult['orderBy'] => {
|
|
const orderBy: BuildQueryResult['orderBy'] = []
|
|
|
|
if (!sort) {
|
|
const createdAt = adapter.tables[tableName]?.createdAt
|
|
if (createdAt) {
|
|
sort = '-createdAt'
|
|
} else {
|
|
sort = '-id'
|
|
}
|
|
}
|
|
|
|
if (typeof sort === 'string') {
|
|
sort = [sort]
|
|
}
|
|
|
|
for (const sortItem of sort) {
|
|
let sortProperty: string
|
|
let sortDirection: 'asc' | 'desc'
|
|
if (sortItem[0] === '-') {
|
|
sortProperty = sortItem.substring(1)
|
|
sortDirection = 'desc'
|
|
} else {
|
|
sortProperty = sortItem
|
|
sortDirection = 'asc'
|
|
}
|
|
try {
|
|
const { columnName: sortTableColumnName, table: sortTable } = getTableColumnFromPath({
|
|
adapter,
|
|
collectionPath: sortProperty,
|
|
fields,
|
|
joins,
|
|
locale,
|
|
parentIsLocalized,
|
|
pathSegments: sortProperty.replace(/__/g, '.').split('.'),
|
|
selectFields,
|
|
tableName,
|
|
value: sortProperty,
|
|
})
|
|
if (sortTable?.[sortTableColumnName]) {
|
|
orderBy.push({
|
|
column:
|
|
aliasTable && tableName === getNameFromDrizzleTable(sortTable)
|
|
? aliasTable[sortTableColumnName]
|
|
: sortTable[sortTableColumnName],
|
|
order: sortDirection === 'asc' ? asc : desc,
|
|
})
|
|
|
|
selectFields[sortTableColumnName] = sortTable[sortTableColumnName]
|
|
}
|
|
} catch (err) {
|
|
// continue
|
|
}
|
|
}
|
|
|
|
return orderBy
|
|
}
|