Skip to content

Commit 0f79eed

Browse files
authored
fix: where querying by a join field with relationship nested to an array (#16101)
Fixes #16095 --- - To see the specific tasks where the Asana app for GitHub is being used, see below: - https://app.asana.com/0/0/1213862751667806
1 parent 3c40241 commit 0f79eed

File tree

3 files changed

+74
-9
lines changed

3 files changed

+74
-9
lines changed

packages/db-mongodb/src/queries/buildSearchParams.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,16 @@ export async function buildSearchParam({
193193
let ref = doc
194194

195195
for (const segment of joinPath.split('.')) {
196-
if (typeof ref === 'object' && ref) {
196+
if (Array.isArray(ref)) {
197+
ref = ref
198+
.map((item) => (typeof item === 'object' && item ? item[segment] : undefined))
199+
.flat()
200+
.filter((item) => item != null)
201+
} else if (typeof ref === 'object' && ref) {
197202
ref = ref[segment]
203+
} else {
204+
ref = undefined
205+
break
198206
}
199207
}
200208

packages/drizzle/src/queries/getTableColumnFromPath.ts

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -476,14 +476,53 @@ export const getTableColumnFromPath = ({
476476
existingTable || getTableAlias({ adapter, tableName: newTableName }).newAliasTable
477477

478478
if (!existingTable) {
479-
joins.push({
480-
condition: eq(
481-
newAliasTable[field.on.replaceAll('.', '_')],
482-
aliasTable ? aliasTable.id : adapter.tables[tableName].id,
483-
),
484-
queryPath: `${constraintPath}${field.name}`,
485-
table: newAliasTable,
486-
})
479+
const onSegments = field.on.split('.')
480+
const collectionFlattenedFields =
481+
adapter.payload.collections[field.collection].config.flattenedFields
482+
const firstSegField =
483+
onSegments.length > 1
484+
? collectionFlattenedFields.find((f) => f.name === onSegments[0])
485+
: null
486+
487+
const arrayTableName =
488+
firstSegField?.type === 'array'
489+
? adapter.tableNameMap.get(`${newTableName}_${toSnakeCase(onSegments[0])}`)
490+
: undefined
491+
492+
if (arrayTableName) {
493+
// join from main table to array table
494+
const { newAliasTable: arrayAliasTable } = getTableAlias({
495+
adapter,
496+
tableName: arrayTableName,
497+
})
498+
499+
joins.push({
500+
condition: eq(
501+
arrayAliasTable[onSegments.slice(1).join('_')],
502+
aliasTable ? aliasTable.id : adapter.tables[tableName].id,
503+
),
504+
queryPath: `${constraintPath}${field.name}._array`,
505+
table: arrayAliasTable,
506+
})
507+
508+
joins.push({
509+
condition: eq(
510+
(newAliasTable as PgTableWithColumns<any>).id,
511+
arrayAliasTable._parentID,
512+
),
513+
queryPath: `${constraintPath}${field.name}`,
514+
table: newAliasTable,
515+
})
516+
} else {
517+
joins.push({
518+
condition: eq(
519+
newAliasTable[field.on.replaceAll('.', '_')],
520+
aliasTable ? aliasTable.id : adapter.tables[tableName].id,
521+
),
522+
queryPath: `${constraintPath}${field.name}`,
523+
table: newAliasTable,
524+
})
525+
}
487526
}
488527

489528
if (newCollectionPath === 'id') {

test/joins/int.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,6 +1865,24 @@ describe('Joins Field', () => {
18651865
expect(found.docs[0].id).toBe(category.id)
18661866
})
18671867

1868+
it('should support where querying by a join field with relationship nested to an array', async () => {
1869+
const category = await payload.create({ collection: 'categories', data: {} })
1870+
const post = await payload.create({
1871+
collection: 'posts',
1872+
data: { array: [{ category: category.id }], title: 'array-join-where-test' },
1873+
})
1874+
const found = await payload.find({
1875+
collection: 'categories',
1876+
where: { 'arrayPosts.title': { equals: 'array-join-where-test' } },
1877+
})
1878+
1879+
expect(found.docs).toHaveLength(1)
1880+
expect(found.docs[0].id).toBe(category.id)
1881+
1882+
await payload.delete({ collection: 'posts', id: post.id })
1883+
await payload.delete({ collection: 'categories', id: category.id })
1884+
})
1885+
18681886
it('should support where querying by a join field multiple times', async () => {
18691887
const category = await payload.create({ collection: 'categories', data: {} })
18701888
await payload.create({

0 commit comments

Comments
 (0)