Skip to content
Merged
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
24 changes: 23 additions & 1 deletion packages/drizzle/src/find/traverseFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
type SelectType,
type Where,
} from 'payload'
import { fieldIsVirtual, fieldShouldBeLocalized } from 'payload/shared'
import { blocks, fieldIsVirtual, fieldShouldBeLocalized } from 'payload/shared'
import toSnakeCase from 'to-snake-case'

import type { BuildQueryJoinAliases, DrizzleAdapter } from '../types.js'
Expand Down Expand Up @@ -91,6 +91,7 @@ type TraverseFieldArgs = {
depth?: number
draftsEnabled?: boolean
fields: FlattenedField[]
forceWithFields?: boolean
joinQuery: JoinQuery
joins?: BuildQueryJoinAliases
locale?: string
Expand Down Expand Up @@ -119,6 +120,7 @@ export const traverseFields = ({
depth,
draftsEnabled,
fields,
forceWithFields,
joinQuery = {},
joins,
locale,
Expand Down Expand Up @@ -231,6 +233,7 @@ export const traverseFields = ({
depth,
draftsEnabled,
fields: field.flattenedFields,
forceWithFields,
joinQuery,
locale,
parentIsLocalized: parentIsLocalized || field.localized,
Expand Down Expand Up @@ -358,6 +361,7 @@ export const traverseFields = ({
depth,
draftsEnabled,
fields: block.flattenedFields,
forceWithFields: blockSelect === true,
joinQuery,
locale,
parentIsLocalized: parentIsLocalized || field.localized,
Expand Down Expand Up @@ -400,6 +404,7 @@ export const traverseFields = ({
depth,
draftsEnabled,
fields: field.flattenedFields,
forceWithFields,
joinQuery,
joins,
locale,
Expand Down Expand Up @@ -862,6 +867,23 @@ export const traverseFields = ({
}

default: {
if (forceWithFields) {
if (
(field.type === 'relationship' || field.type === 'upload') &&
(field.hasMany || Array.isArray(field.relationTo))
) {
withTabledFields.rels = true
}

if (field.type === 'number' && field.hasMany) {
withTabledFields.numbers = true
}

if (field.type === 'text' && field.hasMany) {
withTabledFields.texts = true
}
}

if (!select && !selectAllOnCurrentLevel) {
break
}
Expand Down
29 changes: 28 additions & 1 deletion test/select/getConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,34 @@ export const getConfig: () => Partial<Config> = () => ({
},
{
slug: 'rels',
fields: [],
fields: [{ type: 'text', name: 'text' }],
},
{
slug: 'relationships-blocks',
fields: [
{
type: 'blocks',
name: 'blocks',
blocks: [
{
slug: 'block',
fields: [
{
type: 'relationship',
name: 'hasMany',
relationTo: 'rels',
hasMany: true,
},
{
type: 'relationship',
name: 'hasOne',
relationTo: 'rels',
},
],
},
],
},
],
},
CustomID,
UsersCollection,
Expand Down
51 changes: 51 additions & 0 deletions test/select/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2478,6 +2478,57 @@ describe('Select', () => {
array,
})
})

it('should properly return relationships when using select on block with depth 0', async () => {
const rel_1 = await payload.create({ collection: 'rels', data: { text: 'rel-1' } })
const doc = await payload.create({
collection: 'relationships-blocks',
data: {
blocks: [
{
blockType: 'block',
hasMany: [rel_1],
hasOne: rel_1,
},
],
},
})
const result = await payload.findByID({
depth: 0,
collection: 'relationships-blocks',
id: doc.id,
select: { blocks: true },
})

expect(result.blocks[0]?.hasOne).toBe(rel_1.id)
expect(result.blocks[0]?.hasMany).toEqual([rel_1.id])
})

it('should populate relationships when using select on block', async () => {
const rel_1 = await payload.create({ collection: 'rels', data: { text: 'rel-1' } })
const doc = await payload.create({
collection: 'relationships-blocks',
data: {
blocks: [
{
blockType: 'block',
hasMany: [rel_1],
hasOne: rel_1,
},
],
},
})

const result = await payload.findByID({
depth: 1,
collection: 'relationships-blocks',
id: doc.id,
select: { blocks: true },
})

expect(result.blocks[0]?.hasOne.text).toBe('rel-1')
expect(result.blocks[0]?.hasMany[0].text).toBe('rel-1')
})
})

async function createPost() {
Expand Down
Loading
Loading