perf: optimize virtual fields that reference ID (#12159)

This PR optimizes the new virtual fields with relationships feature
https://github.com/payloadcms/payload/pull/11805 when the path
references the ID field, for example:
```
{
  name: 'postCategoryID',
  type: 'number',
  virtual: 'post.category.id',
},
```

Previously, we did additional population of `category`, which is
unnecessary as we can always grab the ID from the `category` value
itself. One less querying step.
This commit is contained in:
Sasha
2025-04-18 21:39:55 +03:00
committed by GitHub
parent df7a3692f7
commit fdff5871f6
4 changed files with 113 additions and 12 deletions

View File

@@ -1993,11 +1993,63 @@ describe('database', () => {
collection: 'virtual-relations',
depth: 0,
where: { id: { equals: id } },
draft: true,
})
expect(draft.docs[0]?.postTitle).toBe('my-title')
})
it('should allow virtual field as reference to ID', async () => {
const post = await payload.create({ collection: 'posts', data: { title: 'my-title' } })
const { id } = await payload.create({
collection: 'virtual-relations',
depth: 0,
data: { post: post.id },
})
const docDepth2 = await payload.findByID({ collection: 'virtual-relations', id })
expect(docDepth2.postID).toBe(post.id)
const docDepth0 = await payload.findByID({ collection: 'virtual-relations', id, depth: 0 })
expect(docDepth0.postID).toBe(post.id)
})
it('should allow virtual field as reference to custom ID', async () => {
const customID = await payload.create({ collection: 'custom-ids', data: {} })
const { id } = await payload.create({
collection: 'virtual-relations',
depth: 0,
data: { customID: customID.id },
})
const docDepth2 = await payload.findByID({ collection: 'virtual-relations', id })
expect(docDepth2.customIDValue).toBe(customID.id)
const docDepth0 = await payload.findByID({
collection: 'virtual-relations',
id,
depth: 0,
})
expect(docDepth0.customIDValue).toBe(customID.id)
})
it('should allow deep virtual field as reference to ID', async () => {
const category = await payload.create({
collection: 'categories',
data: { title: 'category-3' },
})
const post = await payload.create({
collection: 'posts',
data: { category: category.id, title: 'my-title-3' },
})
const { id } = await payload.create({
collection: 'virtual-relations',
depth: 0,
data: { post: post.id },
})
const docDepth2 = await payload.findByID({ collection: 'virtual-relations', id })
expect(docDepth2.postCategoryID).toBe(category.id)
const docDepth0 = await payload.findByID({ collection: 'virtual-relations', id, depth: 0 })
expect(docDepth0.postCategoryID).toBe(category.id)
})
it('should allow virtual field with reference localized', async () => {
const post = await payload.create({
collection: 'posts',