Files
payload/packages/db-postgres/src/createGlobal.ts
2024-04-25 10:23:03 -04:00

31 lines
856 B
TypeScript

import type { CreateGlobalArgs } from 'payload/database'
import type { PayloadRequestWithData, TypeWithID } from 'payload/types'
import toSnakeCase from 'to-snake-case'
import type { PostgresAdapter } from './types.js'
import { upsertRow } from './upsertRow/index.js'
export async function createGlobal<T extends TypeWithID>(
this: PostgresAdapter,
{ slug, data, req = {} as PayloadRequestWithData }: CreateGlobalArgs,
): Promise<T> {
const db = this.sessions[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
}