feat: queriable / sortable / useAsTitle virtual fields linked with a relationship field (#11805)

This PR adds an ability to specify a virtual field in this way
```js
{
  slug: 'posts',
  fields: [
    {
      name: 'title',
      type: 'text',
      required: true,
    },
  ],
},
{
  slug: 'virtual-relations',
  fields: [
    {
      name: 'postTitle',
      type: 'text',
      virtual: 'post.title',
    },
    {
      name: 'post',
      type: 'relationship',
      relationTo: 'posts',
    },
  ],
},
```

Then, every time you query `virtual-relations`, `postTitle` will be
automatically populated (even if using `depth: 0`) on the db level. This
field also, unlike `virtual: true` is available for querying / sorting /
`useAsTitle`.

Also, the field can be deeply nested to 2 or more relationships, for
example:
```
{
  name: 'postCategoryTitle',
  type: 'text',
  virtual: 'post.category.title',
},
```

Where the current collection has `post` - a relationship to `posts`, the
collection `posts` has `category` that's a relationship to `categories`
and finally `categories` has `title`.
This commit is contained in:
Sasha
2025-04-16 22:46:18 +03:00
committed by GitHub
parent c877b1ad43
commit 1c99f46e4f
16 changed files with 568 additions and 39 deletions

View File

@@ -240,8 +240,8 @@ export default buildConfig({
// highlight-start
cors: {
origins: ['http://localhost:3000'],
headers: ['x-custom-header']
}
headers: ['x-custom-header'],
},
// highlight-end
})
```

View File

@@ -352,7 +352,7 @@ const config = buildConfig({
},
],
},
{
{
slug: 'collection2',
fields: [
{
@@ -365,7 +365,7 @@ const config = buildConfig({
blocks: ['TextBlock'],
}),
],
})
}),
},
],
},

View File

@@ -63,6 +63,7 @@ To install a Database Adapter, you can run **one** of the following commands:
```
- To install the [Postgres Adapter](../database/postgres), run:
```bash
pnpm i @payloadcms/db-postgres
```
@@ -80,7 +81,7 @@ To install a Database Adapter, you can run **one** of the following commands:
#### 2. Copy Payload files into your Next.js app folder
Payload installs directly in your Next.js `/app` folder, and you'll need to place some files into that folder for Payload to run. You can copy these files from the [Blank Template](https://github.com/payloadcms/payload/tree/main/templates/blank/src/app/(payload)) on GitHub. Once you have the required Payload files in place in your `/app` folder, you should have something like this:
Payload installs directly in your Next.js `/app` folder, and you'll need to place some files into that folder for Payload to run. You can copy these files from the [Blank Template](<https://github.com/payloadcms/payload/tree/main/templates/blank/src/app/(payload)>) on GitHub. Once you have the required Payload files in place in your `/app` folder, you should have something like this:
```plaintext
app/