diff --git a/dev/src/payload-types.ts b/dev/src/payload-types.ts index 7b18984..aead87a 100644 --- a/dev/src/payload-types.ts +++ b/dev/src/payload-types.ts @@ -97,7 +97,6 @@ export interface Page { export interface Media { id: string; text?: string | null; - createdDuringDevelopment?: boolean | null; updatedAt: string; createdAt: string; url?: string | null; @@ -201,7 +200,6 @@ export interface PagesSelect { */ export interface MediaSelect { text?: T; - createdDuringDevelopment?: T; updatedAt?: T; createdAt?: T; url?: T; diff --git a/src/lib/api-endpoints/switch.ts b/src/lib/api-endpoints/switch.ts index f187b2e..e3f8a59 100644 --- a/src/lib/api-endpoints/switch.ts +++ b/src/lib/api-endpoints/switch.ts @@ -1,8 +1,8 @@ -import { DatabaseAdapter, Endpoint, PayloadRequest } from 'payload' +import type { DatabaseAdapter, Endpoint, PayloadRequest } from 'payload' import { getEnv, setEnv } from '../env' import { backup, restore } from '../db/mongo' import { formatFileSize } from '../utils' -import { type GetDatabaseAdapter } from '../db/getDbaFunction' +import type { GetDatabaseAdapter } from '../db/getDbaFunction' import { addDevelopmentSettingsToUploadCollection, removeDevelopmentSettingsFromUploadCollection, @@ -53,6 +53,24 @@ export const switchEndpoint = ({ getDatabaseAdapter }: SwitchEndpointArgs): Endp await req.payload.db.destroy() } + req.payload.config.collections = Object.values(req.payload.config.collections || []).map( + (collection) => { + if (newEnv === 'development') { + return addDevelopmentSettingsToUploadCollection(collection) + } else { + return removeDevelopmentSettingsFromUploadCollection(collection) + } + }, + ) + + Object.values(req.payload.collections || {}).forEach((collection) => { + if (newEnv === 'development') { + collection.config = addDevelopmentSettingsToUploadCollection(collection.config) + } else { + collection.config = removeDevelopmentSettingsFromUploadCollection(collection.config) + } + }) + const newDb = getDatabaseAdapter().init({ payload: req.payload }) req.payload.db = newDb as unknown as DatabaseAdapter req.payload.db.payload = req.payload @@ -70,14 +88,6 @@ export const switchEndpoint = ({ getDatabaseAdapter }: SwitchEndpointArgs): Endp await restore(req.payload.db.connection, backupString, req.payload.logger) } - Object.entries(req.payload.collections || {}).forEach(([slug, collection]) => { - if (newEnv === 'development') { - collection.config = addDevelopmentSettingsToUploadCollection(collection.config) - } else { - collection.config = removeDevelopmentSettingsFromUploadCollection(collection.config) - } - }) - logger.info('Switched to ' + newEnv + ' environment') const res: SwitchEndpointOutput = {