From 2396a70e451ed2c35bb220f50a1c91769a2ce10e Mon Sep 17 00:00:00 2001 From: James Mikrut Date: Mon, 24 Jun 2024 14:09:23 -0400 Subject: [PATCH] Update overview.mdx --- docs/local-api/overview.mdx | 110 ------------------------------------ 1 file changed, 110 deletions(-) diff --git a/docs/local-api/overview.mdx b/docs/local-api/overview.mdx index e2d7770be..b33ee497e 100644 --- a/docs/local-api/overview.mdx +++ b/docs/local-api/overview.mdx @@ -18,13 +18,9 @@ Here are some common examples of how you can use the Local API: - Fetching Payload data within React Server Components - Seeding data via Node seed scripts that you write and maintain -<<<<<<< HEAD - Opening custom Next.js route handlers which feature additional functionality but still rely on Payload - Within Payload access control and hook functions -======= -- Opening custom routes which feature additional functionality but still rely on Payload - Within access control and hook functions ->>>>>>> e782d9942928c97c3ff567fc311c7d889f8b06d3 ## Accessing Payload @@ -464,112 +460,6 @@ const result = await payload.updateGlobal({ }) ``` -<<<<<<< HEAD -======= -## Next.js Conflict with Local API - -There is a known issue when using the Local API with Next.js version `13.4.13` and higher. Next.js executes within a -separate child process, and Payload has not been initialized yet in these instances. That means that unless you -explicitly initialize Payload within your operation, it will not be running and return no data / an empty object. - -As a workaround, we recommend leveraging the following pattern to determine and ensure Payload is initialized: - -``` -import dotenv from 'dotenv' -import path from 'path' -import type { Payload } from 'payload' -import payload from 'payload' -import type { InitOptions } from 'payload/config' -import { seed as seedData } from './seed' - -dotenv.config({ - path: path.resolve(__dirname, '../.env'), -}) - -let cached = (global as any).payload - -if (!cached) { - cached = (global as any).payload = { client: null, promise: null } -} - -interface Args { - initOptions?: Partial - seed?: boolean -} - -export const getPayloadClient = async ({ initOptions, seed }: Args = {}): Promise => { - if (!process.env.DATABASE_URI) { - throw new Error('DATABASE_URI environment variable is missing') - } - if (!process.env.PAYLOAD_SECRET) { - throw new Error('PAYLOAD_SECRET environment variable is missing') - } - if (cached.client) { - return cached.client - } - if (!cached.promise) { - cached.promise = payload.init({ - mongoURL: process.env.DATABASE_URI, - secret: process.env.PAYLOAD_SECRET, - local: initOptions?.express ? false : true, - ...(initOptions || {}), - }) - } - try { - process.env.PAYLOAD_DROP_DATABASE = seed ? 'true' : 'false' - cached.client = await cached.promise - if (seed) { - payload.logger.info('---- SEEDING DATABASE ----') - await seedData(payload) - } - } catch (e: unknown) { - cached.promise = null - throw e - } - return cached.client -} -``` - -To checkout how this works in a project, take a look at -our [custom server example](https://github.com/payloadcms/payload/blob/master/examples/custom-server/src/getPayload.ts). - -## Example Script using Local API - -The Local API is especially useful for running scripts - -```ts -import payload from 'payload' -import path from 'path' -import dotenv from 'dotenv' - -dotenv.config({ - path: path.resolve(__dirname, '../.env'), -}) - -const { PAYLOAD_SECRET } = process.env - -const doAction = async (): Promise => { - await payload.init({ - secret: PAYLOAD_SECRET, - local: true, // Enables local mode, doesn't spin up a server or frontend - }) - - // Perform any Local API operations here - await payload.find({ - collection: 'posts', - // where: {} // optional - }) - - await payload.create({ - collection: 'posts', - data: {}, - }) -} - -doAction() -``` - ->>>>>>> e782d9942928c97c3ff567fc311c7d889f8b06d3 ## TypeScript Local API calls will automatically infer your [generated types](/docs/typescript/generating-types).