Files
payload/packages/db-postgres/src/updateGlobalVersion.ts
James Mikrut bb911cc7ec fix(payload): #6800, #5108 - graphql query concurrency issues (#6857)
Fixes #6800 and #5108 by improving the `isolateObjectProperty` utility
function and flattening `req.transactionID` and
`req.transactionIDPromise` to a single `req.transactionID` property.
2024-06-25 14:57:50 -04:00

57 lines
1.4 KiB
TypeScript

import type { TypeWithVersion, UpdateGlobalVersionArgs } from 'payload/database'
import type { PayloadRequest, SanitizedGlobalConfig, TypeWithID } from 'payload/types'
import { buildVersionGlobalFields } from 'payload/versions'
import toSnakeCase from 'to-snake-case'
import type { PostgresAdapter } from './types'
import buildQuery from './queries/buildQuery'
import { upsertRow } from './upsertRow'
export async function updateGlobalVersion<T extends TypeWithID>(
this: PostgresAdapter,
{
id,
global,
locale,
req = {} as PayloadRequest,
versionData,
where: whereArg,
}: UpdateGlobalVersionArgs<T>,
) {
const db = this.sessions[await req.transactionID]?.db || this.drizzle
const globalConfig: SanitizedGlobalConfig = this.payload.globals.config.find(
({ slug }) => slug === global,
)
const whereToUse = whereArg || { id: { equals: id } }
const tableName = this.tableNameMap.get(
`_${toSnakeCase(globalConfig.slug)}${this.versionsSuffix}`,
)
const fields = buildVersionGlobalFields(globalConfig)
const { where } = await buildQuery({
adapter: this,
fields,
locale,
tableName,
where: whereToUse,
})
const result = await upsertRow<TypeWithVersion<T>>({
id,
adapter: this,
data: versionData,
db,
fields,
operation: 'update',
req,
tableName,
where,
})
return result
}