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,5 +1,5 @@
import type { GraphQLInputObjectType, GraphQLNonNull, GraphQLObjectType } from 'graphql'
import type { DeepRequired, MarkOptional } from 'ts-essentials'
import type { DeepRequired, IsAny, MarkOptional } from 'ts-essentials'
import type {
CustomPreviewButton,
@@ -382,6 +382,9 @@ export type CollectionConfig<TSlug extends CollectionSlug = any> = {
* @WARNING: If you change this property with existing data, you will need to handle the renaming of the table in your database or by using migrations
*/
dbName?: DBIdentifierName
defaultPopulate?: IsAny<SelectFromCollectionSlug<TSlug>> extends true
? SelectType
: SelectFromCollectionSlug<TSlug>
/**
* Default field to sort by in collection list view
*/

View File

@@ -2,7 +2,7 @@ import type { BatchLoadFn } from 'dataloader'
import DataLoader from 'dataloader'
import type { PayloadRequest } from '../types/index.js'
import type { PayloadRequest, SelectType } from '../types/index.js'
import type { TypeWithID } from './config/types.js'
import { isValidID } from '../utilities/isValidID.js'
@@ -55,6 +55,7 @@ const batchAndLoadDocs =
overrideAccess,
showHiddenFields,
draft,
select,
] = JSON.parse(key)
const batchKeyArray = [
@@ -67,6 +68,7 @@ const batchAndLoadDocs =
overrideAccess,
showHiddenFields,
draft,
select,
]
const batchKey = JSON.stringify(batchKeyArray)
@@ -103,6 +105,7 @@ const batchAndLoadDocs =
overrideAccess,
showHiddenFields,
draft,
select,
] = JSON.parse(batchKey)
req.transactionID = transactionID
@@ -118,6 +121,7 @@ const batchAndLoadDocs =
overrideAccess: Boolean(overrideAccess),
pagination: false,
req,
select,
showHiddenFields: Boolean(showHiddenFields),
where: {
id: {
@@ -139,6 +143,7 @@ const batchAndLoadDocs =
fallbackLocale,
locale,
overrideAccess,
select,
showHiddenFields,
transactionID: req.transactionID,
})
@@ -167,6 +172,7 @@ type CreateCacheKeyArgs = {
fallbackLocale: string
locale: string
overrideAccess: boolean
select?: SelectType
showHiddenFields: boolean
transactionID: number | Promise<number | string> | string
}
@@ -179,6 +185,7 @@ export const createDataloaderCacheKey = ({
fallbackLocale,
locale,
overrideAccess,
select,
showHiddenFields,
transactionID,
}: CreateCacheKeyArgs): string =>
@@ -193,4 +200,5 @@ export const createDataloaderCacheKey = ({
overrideAccess,
showHiddenFields,
draft,
select,
])

View File

@@ -69,6 +69,7 @@ const populate = async ({
fallbackLocale,
locale,
overrideAccess,
select: relatedCollection.config.defaultPopulate,
showHiddenFields,
transactionID: req.transactionID,
}),

View File

@@ -90,6 +90,7 @@ export const RelationshipFeature = createServerFeature<
key: 'value',
overrideAccess,
req,
select: collection.config.defaultPopulate,
showHiddenFields,
}),
)

View File

@@ -261,6 +261,7 @@ export const UploadFeature = createServerFeature<
key: 'value',
overrideAccess,
req,
select: collection.config.defaultPopulate,
showHiddenFields,
}),
)

View File

@@ -1,4 +1,4 @@
import type { PayloadRequest } from 'payload'
import type { PayloadRequest, SelectType } from 'payload'
import { createDataloaderCacheKey } from 'payload'
@@ -10,6 +10,7 @@ type Arguments = {
key: number | string
overrideAccess: boolean
req: PayloadRequest
select?: SelectType
showHiddenFields: boolean
}
@@ -23,6 +24,7 @@ export const populate = async ({
key,
overrideAccess,
req,
select,
showHiddenFields,
}: {
collectionSlug: string
@@ -46,6 +48,7 @@ export const populate = async ({
fallbackLocale: req.fallbackLocale!,
locale: req.locale!,
overrideAccess,
select,
showHiddenFields,
transactionID: req.transactionID!,
}),

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,
}),
)