Skip to content

Commit 775e6e4

Browse files
authored
fix(drizzle): use alias for localized field sorting (#8396)
Fixes #7015
1 parent 57f93c9 commit 775e6e4

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

packages/drizzle/src/queries/buildOrderBy.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export const buildOrderBy = ({
5555
pathSegments: sortPath.replace(/__/g, '.').split('.'),
5656
selectFields,
5757
tableName,
58+
useAlias: true,
5859
value: sortPath,
5960
})
6061
orderBy.column = sortTable?.[sortTableColumnName]

packages/drizzle/src/queries/getTableColumnFromPath.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ type Args = {
5353
* If creating a new table name for arrays and blocks, this suffix should be appended to the table name
5454
*/
5555
tableNameSuffix?: string
56+
useAlias?: boolean
5657
/**
5758
* The raw value of the query before sanitization
5859
*/
@@ -78,6 +79,7 @@ export const getTableColumnFromPath = ({
7879
selectFields,
7980
tableName,
8081
tableNameSuffix = '',
82+
useAlias,
8183
value,
8284
}: Args): TableColumn => {
8385
const fieldPath = incomingSegments[0]
@@ -139,6 +141,7 @@ export const getTableColumnFromPath = ({
139141
selectFields,
140142
tableName: newTableName,
141143
tableNameSuffix,
144+
useAlias,
142145
value,
143146
})
144147
}
@@ -159,6 +162,7 @@ export const getTableColumnFromPath = ({
159162
selectFields,
160163
tableName: newTableName,
161164
tableNameSuffix: `${tableNameSuffix}${toSnakeCase(field.name)}_`,
165+
useAlias,
162166
value,
163167
})
164168
}
@@ -177,6 +181,7 @@ export const getTableColumnFromPath = ({
177181
selectFields,
178182
tableName: newTableName,
179183
tableNameSuffix,
184+
useAlias,
180185
value,
181186
})
182187
}
@@ -212,6 +217,7 @@ export const getTableColumnFromPath = ({
212217
selectFields,
213218
tableName: newTableName,
214219
tableNameSuffix: `${tableNameSuffix}${toSnakeCase(field.name)}_`,
220+
useAlias,
215221
value,
216222
})
217223
}
@@ -339,6 +345,7 @@ export const getTableColumnFromPath = ({
339345
rootTableName,
340346
selectFields,
341347
tableName: newTableName,
348+
useAlias,
342349
value,
343350
})
344351
}
@@ -397,6 +404,7 @@ export const getTableColumnFromPath = ({
397404
rootTableName,
398405
selectFields: blockSelectFields,
399406
tableName: newTableName,
407+
useAlias,
400408
value,
401409
})
402410
} catch (error) {
@@ -633,6 +641,7 @@ export const getTableColumnFromPath = ({
633641
rootTableName: newTableName,
634642
selectFields,
635643
tableName: newTableName,
644+
useAlias,
636645
value,
637646
})
638647
} else if (
@@ -685,6 +694,7 @@ export const getTableColumnFromPath = ({
685694
pathSegments: pathSegments.slice(1),
686695
selectFields,
687696
tableName: newTableName,
697+
useAlias,
688698
value,
689699
})
690700
}
@@ -698,29 +708,35 @@ export const getTableColumnFromPath = ({
698708
}
699709

700710
if (fieldAffectsData(field)) {
711+
let newTable = adapter.tables[newTableName]
712+
701713
if (field.localized && adapter.payload.config.localization) {
702714
// If localized, we go to localized table and set aliasTable to undefined
703715
// so it is not picked up below to be used as targetTable
704716
const parentTable = aliasTable || adapter.tables[tableName]
705717
newTableName = `${tableName}${adapter.localesSuffix}`
706718

719+
newTable = useAlias
720+
? getTableAlias({ adapter, tableName: newTableName }).newAliasTable
721+
: adapter.tables[newTableName]
722+
707723
joins.push({
708-
condition: eq(parentTable.id, adapter.tables[newTableName]._parentID),
709-
table: adapter.tables[newTableName],
724+
condition: eq(parentTable.id, newTable._parentID),
725+
table: newTable,
710726
})
711727

712728
aliasTable = undefined
713729

714730
if (locale !== 'all') {
715731
constraints.push({
716732
columnName: '_locale',
717-
table: adapter.tables[newTableName],
733+
table: newTable,
718734
value: locale,
719735
})
720736
}
721737
}
722738

723-
const targetTable = aliasTable || adapter.tables[newTableName]
739+
const targetTable = aliasTable || newTable
724740

725741
selectFields[`${newTableName}.${columnPrefix}${field.name}`] =
726742
targetTable[`${columnPrefix}${field.name}`]

test/localization/int.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,29 @@ describe('Localization', () => {
281281
expect(result.docs.map(({ id }) => id)).toContain(localizedPost.id)
282282
})
283283

284+
it('by localized field value with sorting', async () => {
285+
const doc_1 = await payload.create({ collection, data: { title: 'word_b' } })
286+
const doc_2 = await payload.create({ collection, data: { title: 'word_a' } })
287+
const doc_3 = await payload.create({ collection, data: { title: 'word_c' } })
288+
289+
await payload.create({ collection, data: { title: 'others_c' } })
290+
291+
const { docs } = await payload.find({
292+
collection,
293+
sort: 'title',
294+
where: {
295+
title: {
296+
like: 'word',
297+
},
298+
},
299+
})
300+
301+
expect(docs).toHaveLength(3)
302+
expect(docs[0].id).toBe(doc_2.id)
303+
expect(docs[1].id).toBe(doc_1.id)
304+
expect(docs[2].id).toBe(doc_3.id)
305+
})
306+
284307
if (['mongodb'].includes(process.env.PAYLOAD_DATABASE)) {
285308
describe('Localized sorting', () => {
286309
let localizedAccentPostOne: LocalizedPost

0 commit comments

Comments
 (0)