-
-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathpayload.config.ts
More file actions
70 lines (63 loc) · 2.42 KB
/
payload.config.ts
File metadata and controls
70 lines (63 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import type { CloudflareContext } from '@opennextjs/cloudflare'
import type { R2StorageOptions } from '@payloadcms/storage-r2'
import type { GetPlatformProxyOptions } from 'wrangler'
import path from 'node:path'
import process from 'node:process'
import { fileURLToPath } from 'node:url'
import { getCloudflareContext } from '@opennextjs/cloudflare'
import { sqliteD1Adapter } from '@payloadcms/db-d1-sqlite'
import { lexicalEditor } from '@payloadcms/richtext-lexical'
import { r2Storage } from '@payloadcms/storage-r2'
import { zh } from '@payloadcms/translations/languages/zh'
import { buildConfig } from 'payload'
import { Media } from './collections/Media'
import { Users } from './collections/Users'
import { Weekly } from './collections/Weekly'
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
const isCLI = process.argv.some(value => value.match(/^(generate|migrate):?/))
const isProduction = process.env.NODE_ENV === 'production'
type PayloadR2Bucket = R2StorageOptions['bucket']
const cloudflare = isCLI || !isProduction
? await getCloudflareContextFromWrangler()
: await getCloudflareContext({ async: true })
export default buildConfig({
admin: {
user: Users.slug,
importMap: {
baseDir: path.resolve(dirname),
},
},
collections: [Users, Weekly, Media],
editor: lexicalEditor(),
i18n: {
supportedLanguages: { zh },
fallbackLanguage: 'zh',
},
secret: process.env.PAYLOAD_SECRET || '',
typescript: {
outputFile: path.resolve(dirname, 'payload-types.ts'),
},
db: sqliteD1Adapter({
binding: cloudflare.env.D1,
readReplicas: 'first-primary',
}),
plugins: [
r2Storage({
bucket: cloudflare.env.R2 as unknown as PayloadR2Bucket,
collections: { media: true },
}),
],
})
// Adapted from https://github.com/opennextjs/opennextjs-cloudflare/blob/d00b3a13e42e65aad76fba41774815726422cc39/packages/cloudflare/src/api/cloudflare-context.ts#L328C36-L328C46
async function getCloudflareContextFromWrangler(): Promise<CloudflareContext> {
const { getPlatformProxy } = await import(/* webpackIgnore: true */ `${'__wrangler'.replaceAll('_', '')}`)
console.info('Getting Cloudflare context from Wrangler bindings', {
environment: process.env.CLOUDFLARE_ENV,
remoteBindings: isProduction,
})
return getPlatformProxy({
environment: process.env.CLOUDFLARE_ENV,
remoteBindings: isProduction,
} satisfies GetPlatformProxyOptions)
}