Files
payload/packages/drizzle/src/createGlobalVersion.ts
Alessio Gravili c21dac1b53 perf(db-*): add option to disable returning modified documents in db methods (#11393)
This PR adds a new `returning` option to various db adapter methods. Setting it to `false` where the return value is not used will lead to performance gains, as we don't have to do additional db calls to fetch the updated document and then sanitize it.
2025-02-27 17:40:22 -05:00

70 lines
1.6 KiB
TypeScript

import type { CreateGlobalVersionArgs, TypeWithID, TypeWithVersion } from 'payload'
import { sql } from 'drizzle-orm'
import { buildVersionGlobalFields } from 'payload'
import toSnakeCase from 'to-snake-case'
import type { DrizzleAdapter } from './types.js'
import { upsertRow } from './upsertRow/index.js'
import { getTransaction } from './utilities/getTransaction.js'
export async function createGlobalVersion<T extends TypeWithID>(
this: DrizzleAdapter,
{
autosave,
createdAt,
globalSlug,
publishedLocale,
req,
select,
snapshot,
updatedAt,
versionData,
returning,
}: CreateGlobalVersionArgs,
) {
const db = await getTransaction(this, req)
const global = this.payload.globals.config.find(({ slug }) => slug === globalSlug)
const tableName = this.tableNameMap.get(`_${toSnakeCase(global.slug)}${this.versionsSuffix}`)
const result = await upsertRow<TypeWithVersion<T>>({
adapter: this,
data: {
autosave,
createdAt,
latest: true,
publishedLocale,
snapshot,
updatedAt,
version: versionData,
},
db,
fields: buildVersionGlobalFields(this.payload.config, global, true),
operation: 'create',
req,
select,
tableName,
ignoreResult: returning === false ? 'idOnly' : false,
})
const table = this.tables[tableName]
if (global.versions.drafts) {
await this.execute({
db,
sql: sql`
UPDATE ${table}
SET latest = false
WHERE ${table.id} != ${result.id};
`,
})
}
if (returning === false) {
return null
}
return result
}