Update overview.mdx

This commit is contained in:
James Mikrut
2024-06-24 14:09:23 -04:00
committed by GitHub
parent 776e3f7069
commit 2396a70e45

View File

@@ -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<InitOptions>
seed?: boolean
}
export const getPayloadClient = async ({ initOptions, seed }: Args = {}): Promise<Payload> => {
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<void> => {
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).