Removes PayloadRequestWithData in favour of just PayloadRequest with
optional types for `data` and `locale`
`addDataAndFileToRequest` and `addLocalesToRequestFromData` now takes in
a single argument instead of an object
```ts
// before
await addDataAndFileToRequest({ request: req })
addLocalesToRequestFromData({ request: req })
// current
await addDataAndFileToRequest(req)
addLocalesToRequestFromData(req)
```
---------
Co-authored-by: Paul Popus <paul@nouance.io>
31 lines
936 B
TypeScript
31 lines
936 B
TypeScript
import type { PayloadRequest, UpdateGlobalArgs } from 'payload'
|
|
|
|
import toSnakeCase from 'to-snake-case'
|
|
|
|
import type { PostgresAdapter } from './types.js'
|
|
|
|
import { upsertRow } from './upsertRow/index.js'
|
|
|
|
export async function updateGlobal<T extends Record<string, unknown>>(
|
|
this: PostgresAdapter,
|
|
{ slug, data, req = {} as PayloadRequest }: UpdateGlobalArgs,
|
|
): 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 existingGlobal = await db.query[tableName].findFirst({})
|
|
|
|
const result = await upsertRow<T>({
|
|
...(existingGlobal ? { id: existingGlobal.id, operation: 'update' } : { operation: 'create' }),
|
|
adapter: this,
|
|
data,
|
|
db,
|
|
fields: globalConfig.fields,
|
|
req,
|
|
tableName,
|
|
})
|
|
|
|
return result
|
|
}
|