rsnext/examples/cms-payload/payload/payloadClient.ts
Steven 4466ba436b
chore(examples): use default prettier for examples/templates (#60530)
## Description
This PR ensures that the default prettier config is used for examples
and templates.

This config is compatible with `prettier@3` as well (upgrading prettier
is bigger change that can be a future PR).

## Changes
- Updated `.prettierrc.json` in root with `"trailingComma": "es5"` (will
be needed upgrading to prettier@3)
- Added `examples/.prettierrc.json` with default config (this will
change every example)
- Added `packages/create-next-app/templates/.prettierrc.json` with
default config (this will change every template)

## Related

- Fixes #54402
- Closes #54409
2024-01-11 16:01:44 -07:00

49 lines
1.2 KiB
TypeScript

import { getPayload } from "payload/dist/payload";
import config from "./payload.config";
if (!process.env.MONGODB_URI) {
throw new Error("MONGODB_URI environment variable is missing");
}
if (!process.env.PAYLOAD_SECRET) {
throw new Error("PAYLOAD_SECRET environment variable is missing");
}
/**
* Global is used here to maintain a cached connection across hot reloads
* in development. This prevents connections growing exponentially
* during API Route usage.
*
* Source: https://github.com/vercel/next.js/blob/canary/examples/with-mongodb-mongoose/lib/dbConnect.js
*/
let cached = (global as any).payload;
if (!cached) {
cached = (global as any).payload = { client: null, promise: null };
}
export const getPayloadClient = async () => {
if (cached.client) {
return cached.client;
}
if (!cached.promise) {
cached.promise = await getPayload({
// Make sure that your environment variables are filled out accordingly
mongoURL: process.env.MONGODB_URI as string,
secret: process.env.PAYLOAD_SECRET as string,
config: config,
});
}
try {
cached.client = await cached.promise;
} catch (e) {
cached.promise = null;
throw e;
}
return cached.client;
};
export default getPayloadClient;