feat: add defaultPopulate property to collection config (#8934)

### What?
Adds `defaultPopulate` property to collection config that allows to
specify which fields to select when the collection is populated from
another document.
```ts
import type { CollectionConfig } from 'payload'

// The TSlug generic can be passed to have type safety for `defaultPopulate`.
// If avoided, the `defaultPopulate` type resolves to `SelectType`.
export const Pages: CollectionConfig<'pages'> = {
  slug: 'pages',
  // I need only slug, NOT the WHOLE CONTENT!
  defaultPopulate: {
    slug: true,
  },
  fields: [
    {
      name: 'slug',
      type: 'text',
      required: true,
    },
  ],
}
```

### Why?
This is essential for example in case of links. You don't need the whole
document, which can contain large data but only the `slug`.

### How?
Implements `defaultPopulate` when populating relationships, including
inside of lexical / slate rich text fields.
This commit is contained in:
Sasha
2024-10-30 19:41:34 +02:00
committed by GitHub
parent d38d7b8932
commit c41ef65a2b
15 changed files with 375 additions and 5 deletions

View File

@@ -1,4 +1,4 @@
import type { Collection, Field, PayloadRequest, RichTextField } from 'payload'
import type { Collection, Field, PayloadRequest, RichTextField, SelectType } from 'payload'
import { createDataloaderCacheKey } from 'payload'
@@ -13,6 +13,7 @@ type Arguments = {
key: number | string
overrideAccess?: boolean
req: PayloadRequest
select?: SelectType
showHiddenFields: boolean
}
@@ -26,6 +27,7 @@ export const populate = async ({
key,
overrideAccess,
req,
select,
showHiddenFields,
}: {
collection: Collection
@@ -44,6 +46,7 @@ export const populate = async ({
fallbackLocale: req.locale,
locale: req.fallbackLocale,
overrideAccess: typeof overrideAccess === 'undefined' ? false : overrideAccess,
select,
showHiddenFields,
transactionID: req.transactionID,
}),

View File

@@ -48,6 +48,7 @@ export const recurseNestedFields = ({
key: i,
overrideAccess,
req,
select: collection.config.defaultPopulate,
showHiddenFields,
}),
)
@@ -69,6 +70,7 @@ export const recurseNestedFields = ({
key: i,
overrideAccess,
req,
select: collection.config.defaultPopulate,
showHiddenFields,
}),
)
@@ -94,6 +96,7 @@ export const recurseNestedFields = ({
key: 'value',
overrideAccess,
req,
select: collection.config.defaultPopulate,
showHiddenFields,
}),
)
@@ -114,6 +117,7 @@ export const recurseNestedFields = ({
key: field.name,
overrideAccess,
req,
select: collection.config.defaultPopulate,
showHiddenFields,
}),
)

View File

@@ -54,6 +54,7 @@ export const recurseRichText = ({
key: 'value',
overrideAccess,
req,
select: collection.config.defaultPopulate,
showHiddenFields,
}),
)
@@ -93,6 +94,7 @@ export const recurseRichText = ({
key: 'value',
overrideAccess,
req,
select: collection.config.defaultPopulate,
showHiddenFields,
}),
)