fix(drizzle): use alias for localized field sorting (#8396)
Fixes https://github.com/payloadcms/payload/issues/7015
This commit is contained in:
@@ -55,6 +55,7 @@ export const buildOrderBy = ({
|
|||||||
pathSegments: sortPath.replace(/__/g, '.').split('.'),
|
pathSegments: sortPath.replace(/__/g, '.').split('.'),
|
||||||
selectFields,
|
selectFields,
|
||||||
tableName,
|
tableName,
|
||||||
|
useAlias: true,
|
||||||
value: sortPath,
|
value: sortPath,
|
||||||
})
|
})
|
||||||
orderBy.column = sortTable?.[sortTableColumnName]
|
orderBy.column = sortTable?.[sortTableColumnName]
|
||||||
|
|||||||
@@ -53,6 +53,7 @@ type Args = {
|
|||||||
* If creating a new table name for arrays and blocks, this suffix should be appended to the table name
|
* If creating a new table name for arrays and blocks, this suffix should be appended to the table name
|
||||||
*/
|
*/
|
||||||
tableNameSuffix?: string
|
tableNameSuffix?: string
|
||||||
|
useAlias?: boolean
|
||||||
/**
|
/**
|
||||||
* The raw value of the query before sanitization
|
* The raw value of the query before sanitization
|
||||||
*/
|
*/
|
||||||
@@ -78,6 +79,7 @@ export const getTableColumnFromPath = ({
|
|||||||
selectFields,
|
selectFields,
|
||||||
tableName,
|
tableName,
|
||||||
tableNameSuffix = '',
|
tableNameSuffix = '',
|
||||||
|
useAlias,
|
||||||
value,
|
value,
|
||||||
}: Args): TableColumn => {
|
}: Args): TableColumn => {
|
||||||
const fieldPath = incomingSegments[0]
|
const fieldPath = incomingSegments[0]
|
||||||
@@ -139,6 +141,7 @@ export const getTableColumnFromPath = ({
|
|||||||
selectFields,
|
selectFields,
|
||||||
tableName: newTableName,
|
tableName: newTableName,
|
||||||
tableNameSuffix,
|
tableNameSuffix,
|
||||||
|
useAlias,
|
||||||
value,
|
value,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -159,6 +162,7 @@ export const getTableColumnFromPath = ({
|
|||||||
selectFields,
|
selectFields,
|
||||||
tableName: newTableName,
|
tableName: newTableName,
|
||||||
tableNameSuffix: `${tableNameSuffix}${toSnakeCase(field.name)}_`,
|
tableNameSuffix: `${tableNameSuffix}${toSnakeCase(field.name)}_`,
|
||||||
|
useAlias,
|
||||||
value,
|
value,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -177,6 +181,7 @@ export const getTableColumnFromPath = ({
|
|||||||
selectFields,
|
selectFields,
|
||||||
tableName: newTableName,
|
tableName: newTableName,
|
||||||
tableNameSuffix,
|
tableNameSuffix,
|
||||||
|
useAlias,
|
||||||
value,
|
value,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -212,6 +217,7 @@ export const getTableColumnFromPath = ({
|
|||||||
selectFields,
|
selectFields,
|
||||||
tableName: newTableName,
|
tableName: newTableName,
|
||||||
tableNameSuffix: `${tableNameSuffix}${toSnakeCase(field.name)}_`,
|
tableNameSuffix: `${tableNameSuffix}${toSnakeCase(field.name)}_`,
|
||||||
|
useAlias,
|
||||||
value,
|
value,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -339,6 +345,7 @@ export const getTableColumnFromPath = ({
|
|||||||
rootTableName,
|
rootTableName,
|
||||||
selectFields,
|
selectFields,
|
||||||
tableName: newTableName,
|
tableName: newTableName,
|
||||||
|
useAlias,
|
||||||
value,
|
value,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -397,6 +404,7 @@ export const getTableColumnFromPath = ({
|
|||||||
rootTableName,
|
rootTableName,
|
||||||
selectFields: blockSelectFields,
|
selectFields: blockSelectFields,
|
||||||
tableName: newTableName,
|
tableName: newTableName,
|
||||||
|
useAlias,
|
||||||
value,
|
value,
|
||||||
})
|
})
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -633,6 +641,7 @@ export const getTableColumnFromPath = ({
|
|||||||
rootTableName: newTableName,
|
rootTableName: newTableName,
|
||||||
selectFields,
|
selectFields,
|
||||||
tableName: newTableName,
|
tableName: newTableName,
|
||||||
|
useAlias,
|
||||||
value,
|
value,
|
||||||
})
|
})
|
||||||
} else if (
|
} else if (
|
||||||
@@ -685,6 +694,7 @@ export const getTableColumnFromPath = ({
|
|||||||
pathSegments: pathSegments.slice(1),
|
pathSegments: pathSegments.slice(1),
|
||||||
selectFields,
|
selectFields,
|
||||||
tableName: newTableName,
|
tableName: newTableName,
|
||||||
|
useAlias,
|
||||||
value,
|
value,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -698,15 +708,21 @@ export const getTableColumnFromPath = ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (fieldAffectsData(field)) {
|
if (fieldAffectsData(field)) {
|
||||||
|
let newTable = adapter.tables[newTableName]
|
||||||
|
|
||||||
if (field.localized && adapter.payload.config.localization) {
|
if (field.localized && adapter.payload.config.localization) {
|
||||||
// If localized, we go to localized table and set aliasTable to undefined
|
// If localized, we go to localized table and set aliasTable to undefined
|
||||||
// so it is not picked up below to be used as targetTable
|
// so it is not picked up below to be used as targetTable
|
||||||
const parentTable = aliasTable || adapter.tables[tableName]
|
const parentTable = aliasTable || adapter.tables[tableName]
|
||||||
newTableName = `${tableName}${adapter.localesSuffix}`
|
newTableName = `${tableName}${adapter.localesSuffix}`
|
||||||
|
|
||||||
|
newTable = useAlias
|
||||||
|
? getTableAlias({ adapter, tableName: newTableName }).newAliasTable
|
||||||
|
: adapter.tables[newTableName]
|
||||||
|
|
||||||
joins.push({
|
joins.push({
|
||||||
condition: eq(parentTable.id, adapter.tables[newTableName]._parentID),
|
condition: eq(parentTable.id, newTable._parentID),
|
||||||
table: adapter.tables[newTableName],
|
table: newTable,
|
||||||
})
|
})
|
||||||
|
|
||||||
aliasTable = undefined
|
aliasTable = undefined
|
||||||
@@ -714,13 +730,13 @@ export const getTableColumnFromPath = ({
|
|||||||
if (locale !== 'all') {
|
if (locale !== 'all') {
|
||||||
constraints.push({
|
constraints.push({
|
||||||
columnName: '_locale',
|
columnName: '_locale',
|
||||||
table: adapter.tables[newTableName],
|
table: newTable,
|
||||||
value: locale,
|
value: locale,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const targetTable = aliasTable || adapter.tables[newTableName]
|
const targetTable = aliasTable || newTable
|
||||||
|
|
||||||
selectFields[`${newTableName}.${columnPrefix}${field.name}`] =
|
selectFields[`${newTableName}.${columnPrefix}${field.name}`] =
|
||||||
targetTable[`${columnPrefix}${field.name}`]
|
targetTable[`${columnPrefix}${field.name}`]
|
||||||
|
|||||||
@@ -281,6 +281,29 @@ describe('Localization', () => {
|
|||||||
expect(result.docs.map(({ id }) => id)).toContain(localizedPost.id)
|
expect(result.docs.map(({ id }) => id)).toContain(localizedPost.id)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('by localized field value with sorting', async () => {
|
||||||
|
const doc_1 = await payload.create({ collection, data: { title: 'word_b' } })
|
||||||
|
const doc_2 = await payload.create({ collection, data: { title: 'word_a' } })
|
||||||
|
const doc_3 = await payload.create({ collection, data: { title: 'word_c' } })
|
||||||
|
|
||||||
|
await payload.create({ collection, data: { title: 'others_c' } })
|
||||||
|
|
||||||
|
const { docs } = await payload.find({
|
||||||
|
collection,
|
||||||
|
sort: 'title',
|
||||||
|
where: {
|
||||||
|
title: {
|
||||||
|
like: 'word',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(docs).toHaveLength(3)
|
||||||
|
expect(docs[0].id).toBe(doc_2.id)
|
||||||
|
expect(docs[1].id).toBe(doc_1.id)
|
||||||
|
expect(docs[2].id).toBe(doc_3.id)
|
||||||
|
})
|
||||||
|
|
||||||
if (['mongodb'].includes(process.env.PAYLOAD_DATABASE)) {
|
if (['mongodb'].includes(process.env.PAYLOAD_DATABASE)) {
|
||||||
describe('Localized sorting', () => {
|
describe('Localized sorting', () => {
|
||||||
let localizedAccentPostOne: LocalizedPost
|
let localizedAccentPostOne: LocalizedPost
|
||||||
|
|||||||
Reference in New Issue
Block a user