Files
payloadcms/packages/db-postgres/src/createVersion.ts
2024-03-06 14:19:13 -05:00

59 lines
1.6 KiB
TypeScript

import type { CreateVersionArgs, TypeWithVersion } from 'payload/database'
import type { PayloadRequest, TypeWithID } from 'payload/types'
import { sql } from 'drizzle-orm'
import { buildVersionCollectionFields } from 'payload/versions'
import toSnakeCase from 'to-snake-case'
import type { PostgresAdapter } from './types.d.ts'
import { upsertRow } from './upsertRow/index.js'
export async function createVersion<T extends TypeWithID>(
this: PostgresAdapter,
{
autosave,
collectionSlug,
parent,
req = {} as PayloadRequest,
versionData,
}: CreateVersionArgs<T>,
) {
const db = this.sessions[req.transactionID]?.db || this.drizzle
const collection = this.payload.collections[collectionSlug].config
const collectionTableName = toSnakeCase(collectionSlug)
const tableName = `_${collectionTableName}_v`
const result = await upsertRow<TypeWithVersion<T>>({
adapter: this,
data: {
autosave,
latest: true,
parent,
version: versionData,
},
db,
fields: buildVersionCollectionFields(collection),
operation: 'create',
tableName,
req,
})
const table = this.tables[tableName]
const relationshipsTable = this.tables[`${tableName}_rels`]
if (collection.versions.drafts) {
await db.execute(sql`
UPDATE ${table}
SET latest = false
FROM ${relationshipsTable}
WHERE ${table.id} = ${relationshipsTable.parent}
AND ${relationshipsTable.path} = ${'parent'}
AND ${relationshipsTable[`${collectionSlug}ID`]} = ${parent}
AND ${table.id} != ${result.id};
`)
}
return result
}