## Description This is the beta (v3) PR for the v2 PR [here](https://github.com/payloadcms/payload/pull/6857) Addresses #6800, #5108 - [x] I have read and understand the [CONTRIBUTING.md](https://github.com/payloadcms/payload/blob/main/CONTRIBUTING.md) document in this repository. ## Type of change - [x] Bug fix (non-breaking change which fixes an issue) ## Checklist: - [x] I have added tests that prove my fix is effective or that my feature works - [x] Existing test suite passes locally with my changes
30 lines
802 B
TypeScript
30 lines
802 B
TypeScript
import type { CreateGlobalArgs, PayloadRequest } from 'payload'
|
|
|
|
import toSnakeCase from 'to-snake-case'
|
|
|
|
import type { PostgresAdapter } from './types.js'
|
|
|
|
import { upsertRow } from './upsertRow/index.js'
|
|
|
|
export async function createGlobal<T extends Record<string, unknown>>(
|
|
this: PostgresAdapter,
|
|
{ slug, data, req = {} as PayloadRequest }: CreateGlobalArgs,
|
|
): Promise<T> {
|
|
const db = this.sessions[await req.transactionID]?.db || this.drizzle
|
|
const globalConfig = this.payload.globals.config.find((config) => config.slug === slug)
|
|
|
|
const tableName = this.tableNameMap.get(toSnakeCase(globalConfig.slug))
|
|
|
|
const result = await upsertRow<T>({
|
|
adapter: this,
|
|
data,
|
|
db,
|
|
fields: globalConfig.fields,
|
|
operation: 'create',
|
|
req,
|
|
tableName,
|
|
})
|
|
|
|
return result
|
|
}
|