## 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
46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
import type { CreateGlobalVersionArgs, PayloadRequest, TypeWithID, TypeWithVersion } from 'payload'
|
|
|
|
import { sql } from 'drizzle-orm'
|
|
import { buildVersionGlobalFields } from 'payload'
|
|
import toSnakeCase from 'to-snake-case'
|
|
|
|
import type { PostgresAdapter } from './types.js'
|
|
|
|
import { upsertRow } from './upsertRow/index.js'
|
|
|
|
export async function createGlobalVersion<T extends TypeWithID>(
|
|
this: PostgresAdapter,
|
|
{ autosave, globalSlug, req = {} as PayloadRequest, versionData }: CreateGlobalVersionArgs,
|
|
) {
|
|
const db = this.sessions[await req.transactionID]?.db || this.drizzle
|
|
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,
|
|
latest: true,
|
|
version: versionData,
|
|
},
|
|
db,
|
|
fields: buildVersionGlobalFields(global),
|
|
operation: 'create',
|
|
req,
|
|
tableName,
|
|
})
|
|
|
|
const table = this.tables[tableName]
|
|
|
|
if (global.versions.drafts) {
|
|
await db.execute(sql`
|
|
UPDATE ${table}
|
|
SET latest = false
|
|
WHERE ${table.id} != ${result.id};
|
|
`)
|
|
}
|
|
|
|
return result
|
|
}
|