Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions packages/payload/src/config/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ import { type ClientGlobalConfig, createClientGlobalConfigs } from '../globals/c
export type ServerOnlyRootProperties = keyof Pick<
SanitizedConfig,
| 'bin'
| 'collectionsBySlug'
| 'cors'
| 'csrf'
| 'custom'
| 'db'
| 'editor'
| 'email'
| 'endpoints'
| 'globalsBySlug'
| 'graphQL'
| 'hooks'
| 'i18n'
Expand Down Expand Up @@ -99,6 +101,8 @@ export const serverOnlyConfigProperties: readonly Partial<ServerOnlyRootProperti
'logger',
'kv',
'queryPresets',
'collectionsBySlug',
'globalsBySlug',
// `admin`, `onInit`, `localization`, `collections`, and `globals` are all handled separately
]

Expand Down
37 changes: 37 additions & 0 deletions packages/payload/src/config/sanitize.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { describe, expect, it } from 'vitest'

import { sanitizeConfig } from './sanitize.js'

describe('sanitizeConfig', () => {
it('should populate collectionsBySlug map from collections array', async () => {
const config = await sanitizeConfig({
collections: [
{ slug: 'posts', fields: [] },
{ slug: 'users', auth: true, fields: [] },
],
globals: [{ slug: 'settings', fields: [] }],
} as any)

expect(config.collectionsBySlug).toBeInstanceOf(Map)
expect(config.collectionsBySlug.size).toBe(config.collections.length)
expect(config.collectionsBySlug.get('posts')).toBe(
config.collections.find((c) => c.slug === 'posts'),
)
})

it('should populate globalsBySlug map from globals array', async () => {
const config = await sanitizeConfig({
collections: [],
globals: [
{ slug: 'settings', fields: [] },
{ slug: 'nav', fields: [] },
],
} as any)

expect(config.globalsBySlug).toBeInstanceOf(Map)
expect(config.globalsBySlug.size).toBe(config.globals.length)
expect(config.globalsBySlug.get('settings')).toBe(
config.globals.find((g) => g.slug === 'settings'),
)
})
})
19 changes: 17 additions & 2 deletions packages/payload/src/config/sanitize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import { en } from '@payloadcms/translations/languages/en'
import { deepMergeSimple } from '@payloadcms/translations/utilities'

import type { OrderableJoinInfo } from '../fields/config/sanitizeJoinField.js'
import type { CollectionSlug, GlobalSlug, SanitizedCollectionConfig } from '../index.js'
import type {
CollectionSlug,
GlobalSlug,
SanitizedCollectionConfig,
SanitizedGlobalConfig,
} from '../index.js'
import type { SanitizedJobsConfig } from '../queues/config/types/index.js'
import type {
Config,
Expand Down Expand Up @@ -504,5 +509,15 @@ export const sanitizeConfig = async (incomingConfig: Config): Promise<SanitizedC

await Promise.all(promises)

return config as SanitizedConfig
const collectionsBySlug = new Map<string, SanitizedCollectionConfig>()
for (const collection of config.collections as SanitizedCollectionConfig[]) {
collectionsBySlug.set(collection.slug, collection)
}

const globalsBySlug = new Map<string, SanitizedGlobalConfig>()
for (const global of config.globals ?? []) {
globalsBySlug.set(global.slug, global)
}

return { ...config, collectionsBySlug, globalsBySlug } as SanitizedConfig
}
2 changes: 2 additions & 0 deletions packages/payload/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1586,10 +1586,12 @@ export type SanitizedConfig = {
} & DeepRequired<Config['admin']>
blocks?: FlattenedBlock[]
collections: SanitizedCollectionConfig[]
collectionsBySlug: Map<string, SanitizedCollectionConfig>
/** Default richtext editor to use for richText fields */
editor?: RichTextAdapter<any, any, any>
endpoints: Endpoint[]
globals: SanitizedGlobalConfig[]
globalsBySlug: Map<string, SanitizedGlobalConfig>
i18n: Required<I18nOptions>
jobs: SanitizedJobsConfig
localization: false | SanitizedLocalizationConfig
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-import-export/src/export/createExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export const createExport = async (args: CreateExportArgs) => {
}

const locale = localeFromInput ?? localeFromReq
const collectionConfig = payload.config.collections.find(({ slug }) => slug === collectionSlug)
const collectionConfig = payload.config.collectionsBySlug.get(collectionSlug)

if (!collectionConfig) {
throw new APIError(`Collection with slug ${collectionSlug} not found.`)
Expand Down
4 changes: 1 addition & 3 deletions packages/plugin-import-export/src/import/createImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,7 @@ export const createImport = async ({
})
}

const collectionConfig = req.payload.config.collections.find(
({ slug }) => slug === collectionSlug,
)
const collectionConfig = req.payload.config.collectionsBySlug.get(collectionSlug)

if (!collectionConfig) {
if (!collectionSlug) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ export const getCreateCollectionImportTask = (
}

// Get the collection config for the imports collection
const collectionConfig = req.payload.config.collections.find(
(c) => c.slug === importCollection,
)
const collectionConfig = req.payload.config.collectionsBySlug.get(importCollection)

if (!collectionConfig) {
throw new Error(`Collection config not found for: ${importCollection}`)
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-mcp/src/utils/getVirtualFieldNames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { fieldIsVirtual } from 'payload/shared'
* Returns the names of all top-level virtual fields for a given collection slug.
*/
export function getCollectionVirtualFieldNames(config: SanitizedConfig, slug: string): string[] {
const collection = config.collections.find((c) => c.slug === slug)
const collection = config.collectionsBySlug.get(slug)

if (!collection) {
return []
Expand All @@ -21,7 +21,7 @@ export function getCollectionVirtualFieldNames(config: SanitizedConfig, slug: st
* Returns the names of all top-level virtual fields for a given global slug.
*/
export function getGlobalVirtualFieldNames(config: SanitizedConfig, slug: string): string[] {
const global = config.globals.find((g) => g.slug === slug)
const global = config.globalsBySlug.get(slug)

if (!global) {
return []
Expand Down
24 changes: 12 additions & 12 deletions packages/plugin-seo/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ export const seoPlugin =
const result = pluginConfig.generateTitle
? await pluginConfig.generateTitle({
...data,
collectionConfig: config.collections?.find(
(c) => c.slug === reqData.collectionSlug,
collectionConfig: req.payload.config.collectionsBySlug.get(
reqData.collectionSlug ?? '',
),
globalConfig: config.globals?.find((g) => g.slug === reqData.globalSlug),
globalConfig: req.payload.config.globalsBySlug.get(reqData.globalSlug ?? ''),
req,
} satisfies Parameters<GenerateTitle>[0])
: ''
Expand All @@ -168,10 +168,10 @@ export const seoPlugin =
const result = pluginConfig.generateDescription
? await pluginConfig.generateDescription({
...data,
collectionConfig: config.collections?.find(
(c) => c.slug === reqData.collectionSlug,
collectionConfig: req.payload.config.collectionsBySlug.get(
reqData.collectionSlug ?? '',
),
globalConfig: config.globals?.find((g) => g.slug === reqData.globalSlug),
globalConfig: req.payload.config.globalsBySlug.get(reqData.globalSlug ?? ''),
req,
} satisfies Parameters<GenerateDescription>[0])
: ''
Expand All @@ -192,10 +192,10 @@ export const seoPlugin =
const result = pluginConfig.generateURL
? await pluginConfig.generateURL({
...data,
collectionConfig: config.collections?.find(
(c) => c.slug === reqData.collectionSlug,
collectionConfig: req.payload.config.collectionsBySlug.get(
reqData.collectionSlug ?? '',
),
globalConfig: config.globals?.find((g) => g.slug === reqData.globalSlug),
globalConfig: req.payload.config.globalsBySlug.get(reqData.globalSlug ?? ''),
req,
} satisfies Parameters<GenerateURL>[0])
: ''
Expand All @@ -216,10 +216,10 @@ export const seoPlugin =
const result = pluginConfig.generateImage
? await pluginConfig.generateImage({
...data,
collectionConfig: config.collections?.find(
(c) => c.slug === reqData.collectionSlug,
collectionConfig: req.payload.config.collectionsBySlug.get(
reqData.collectionSlug ?? '',
),
globalConfig: config.globals?.find((g) => g.slug === reqData.globalSlug),
globalConfig: req.payload.config.globalsBySlug.get(reqData.globalSlug ?? ''),
req,
} satisfies Parameters<GenerateImage>[0])
: ''
Expand Down
Loading