Skip to content

perf: remove duplicative deep loops during field sanitization #12402

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
151 changes: 0 additions & 151 deletions packages/payload/src/collections/config/reservedFieldNames.ts

This file was deleted.

16 changes: 9 additions & 7 deletions packages/payload/src/collections/config/sanitize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
addDefaultsToCollectionConfig,
addDefaultsToLoginWithUsernameConfig,
} from './defaults.js'
import { sanitizeAuthFields, sanitizeUploadFields } from './reservedFieldNames.js'
import { sanitizeCompoundIndexes } from './sanitizeCompoundIndexes.js'
import { validateUseAsTitle } from './useAsTitle.js'

Expand All @@ -43,7 +42,9 @@ export const sanitizeCollection = async (
if (collection._sanitized) {
return collection as SanitizedCollectionConfig
}

collection._sanitized = true

// /////////////////////////////////
// Make copy of collection config
// /////////////////////////////////
Expand All @@ -57,7 +58,9 @@ export const sanitizeCollection = async (
const validRelationships = _validRelationships ?? config.collections.map((c) => c.slug) ?? []

const joins: SanitizedJoins = {}

const polymorphicJoins: SanitizedJoin[] = []

sanitized.fields = await sanitizeFields({
collectionConfig: sanitized,
config,
Expand Down Expand Up @@ -96,17 +99,21 @@ export const sanitizeCollection = async (
// add default timestamps fields only as needed
let hasUpdatedAt: boolean | null = null
let hasCreatedAt: boolean | null = null

sanitized.fields.some((field) => {
if (fieldAffectsData(field)) {
if (field.name === 'updatedAt') {
hasUpdatedAt = true
}

if (field.name === 'createdAt') {
hasCreatedAt = true
}
}

return hasCreatedAt && hasUpdatedAt
})

if (!hasUpdatedAt) {
sanitized.fields.push({
name: 'updatedAt',
Expand All @@ -119,6 +126,7 @@ export const sanitizeCollection = async (
label: ({ t }) => t('general:updatedAt'),
})
}

if (!hasCreatedAt) {
sanitized.fields.push({
name: 'createdAt',
Expand Down Expand Up @@ -175,9 +183,6 @@ export const sanitizeCollection = async (
sanitized.upload = {}
}

// sanitize fields for reserved names
sanitizeUploadFields(sanitized.fields, sanitized)

sanitized.upload.cacheTags = sanitized.upload?.cacheTags ?? true
sanitized.upload.bulkUpload = sanitized.upload?.bulkUpload ?? true
sanitized.upload.staticDir = sanitized.upload.staticDir || sanitized.slug
Expand All @@ -195,9 +200,6 @@ export const sanitizeCollection = async (
}

if (sanitized.auth) {
// sanitize fields for reserved names
sanitizeAuthFields(sanitized.fields, sanitized)

sanitized.auth = addDefaultsToAuthConfig(
typeof sanitized.auth === 'boolean' ? {} : sanitized.auth,
)
Expand Down
2 changes: 1 addition & 1 deletion packages/payload/src/collections/config/useAsTitle.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { CollectionConfig } from '../../index.js'

import { InvalidConfiguration } from '../../errors/InvalidConfiguration.js'
import { fieldAffectsData, fieldIsVirtual } from '../../fields/config/types.js'
import { fieldAffectsData } from '../../fields/config/types.js'
import flattenFields from '../../utilities/flattenTopLevelFields.js'

/**
Expand Down
2 changes: 2 additions & 0 deletions packages/payload/src/config/sanitize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const sanitizeAdminConfig = (configToSanitize: Config): Partial<SanitizedConfig>
// add default user collection if none provided
if (!sanitizedConfig?.admin?.user) {
const firstCollectionWithAuth = sanitizedConfig.collections.find(({ auth }) => Boolean(auth))

if (firstCollectionWithAuth) {
sanitizedConfig.admin.user = firstCollectionWithAuth.slug
} else {
Expand All @@ -69,6 +70,7 @@ const sanitizeAdminConfig = (configToSanitize: Config): Partial<SanitizedConfig>
const userCollection = sanitizedConfig.collections.find(
({ slug }) => slug === sanitizedConfig.admin.user,
)

if (!userCollection || !userCollection.auth) {
throw new InvalidConfiguration(
`${sanitizedConfig.admin.user} is not a valid admin user collection`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Config } from '../../config/types.js'
import type { CollectionConfig, Field } from '../../index.js'

import { ReservedFieldName } from '../../errors/index.js'
import { sanitizeCollection } from './sanitize.js'
import { sanitizeCollection } from '../../collections/config/sanitize.js'

describe('reservedFieldNames - collections -', () => {
const config = {
Expand All @@ -25,6 +25,7 @@ describe('reservedFieldNames - collections -', () => {
label: 'some-collection',
},
]

await expect(async () => {
await sanitizeCollection(
// @ts-expect-error
Expand Down Expand Up @@ -53,6 +54,7 @@ describe('reservedFieldNames - collections -', () => {
label: 'some-collection',
},
]

await expect(async () => {
await sanitizeCollection(
// @ts-expect-error
Expand Down Expand Up @@ -93,6 +95,7 @@ describe('reservedFieldNames - collections -', () => {
label: 'some-collection',
},
]

await expect(async () => {
await sanitizeCollection(
// @ts-expect-error
Expand Down Expand Up @@ -121,6 +124,7 @@ describe('reservedFieldNames - collections -', () => {
label: 'some-collection',
},
]

await expect(async () => {
await sanitizeCollection(
// @ts-expect-error
Expand Down Expand Up @@ -149,6 +153,7 @@ describe('reservedFieldNames - collections -', () => {
label: 'some-collection',
},
]

await expect(async () => {
await sanitizeCollection(
// @ts-expect-error
Expand Down
48 changes: 48 additions & 0 deletions packages/payload/src/fields/config/reservedFieldNames.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Reserved field names for collections with auth config enabled
*/
export const reservedBaseAuthFieldNames = [
/* 'email',
'resetPasswordToken',
'resetPasswordExpiration', */
'salt',
'hash',
]

/**
* Reserved field names for auth collections with verify: true
*/
export const reservedVerifyFieldNames = [
/* '_verified', '_verificationToken' */
]

/**
* Reserved field names for auth collections with useApiKey: true
*/
export const reservedAPIKeyFieldNames = [
/* 'enableAPIKey', 'apiKeyIndex', 'apiKey' */
]

/**
* Reserved field names for collections with upload config enabled
*/
export const reservedBaseUploadFieldNames = [
'file',
/* 'mimeType',
'thumbnailURL',
'width',
'height',
'filesize',
'filename',
'url',
'focalX',
'focalY',
'sizes', */
]

/**
* Reserved field names for collections with versions enabled
*/
export const reservedVersionsFieldNames = [
/* '__v', '_status' */
]
Loading
Loading