chore: add postgres updateVersion
This commit is contained in:
@@ -9,7 +9,6 @@ import { create } from './create'
|
||||
import { createGlobal } from './createGlobal'
|
||||
import { createMigration } from './createMigration'
|
||||
import { createVersion } from './createVersion'
|
||||
// import { updateVersion } from './updateVersion';
|
||||
import { deleteMany } from './deleteMany'
|
||||
import { deleteOne } from './deleteOne'
|
||||
import { deleteVersions } from './deleteVersions'
|
||||
@@ -25,6 +24,7 @@ import { commitTransaction } from './transactions/commitTransaction'
|
||||
import { rollbackTransaction } from './transactions/rollbackTransaction'
|
||||
import { updateOne } from './update'
|
||||
import { updateGlobal } from './updateGlobal'
|
||||
import { updateVersion } from './updateVersion'
|
||||
import { webpack } from './webpack'
|
||||
|
||||
// import { destroy } from './destroy';
|
||||
@@ -50,6 +50,7 @@ export function postgresAdapter(args: Args): PostgresAdapterResult {
|
||||
name: 'postgres',
|
||||
deleteMany,
|
||||
deleteOne,
|
||||
deleteVersions,
|
||||
enums: {},
|
||||
find,
|
||||
findGlobal,
|
||||
@@ -64,9 +65,8 @@ export function postgresAdapter(args: Args): PostgresAdapterResult {
|
||||
tables: {},
|
||||
updateGlobal,
|
||||
updateOne,
|
||||
updateVersion,
|
||||
webpack,
|
||||
// updateVersion,
|
||||
deleteVersions,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -4,16 +4,7 @@ import type { IndexBuilder, PgColumnBuilder, UniqueConstraintBuilder } from 'dri
|
||||
import type { Field } from 'payload/types'
|
||||
|
||||
import { relations } from 'drizzle-orm'
|
||||
import {
|
||||
index,
|
||||
integer,
|
||||
numeric,
|
||||
pgTable,
|
||||
serial,
|
||||
timestamp,
|
||||
unique,
|
||||
varchar,
|
||||
} from 'drizzle-orm/pg-core'
|
||||
import { index, integer, numeric, pgTable, serial, timestamp, unique, varchar, } from 'drizzle-orm/pg-core'
|
||||
import { fieldAffectsData } from 'payload/types'
|
||||
import toSnakeCase from 'to-snake-case'
|
||||
|
||||
|
||||
@@ -380,27 +380,10 @@ export const traverseFields = ({
|
||||
|
||||
valuesToTransform.forEach(({ localeKey, ref, value }) => {
|
||||
if (typeof value !== 'undefined') {
|
||||
const formattedValue = value
|
||||
|
||||
// switch (field.type) {
|
||||
// // case 'date': {
|
||||
// // if (typeof fieldData === 'string') {
|
||||
// // const parsedDate = new Date(fieldData)
|
||||
// // formattedValue = parsedDate
|
||||
// // }
|
||||
|
||||
// // break
|
||||
// // }
|
||||
|
||||
// default: {
|
||||
// break
|
||||
// }
|
||||
// }
|
||||
|
||||
if (localeKey) {
|
||||
ref[localeKey][columnName] = formattedValue
|
||||
ref[localeKey][columnName] = value
|
||||
} else {
|
||||
ref[columnName] = formattedValue
|
||||
ref[columnName] = value
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
42
packages/db-postgres/src/updateVersion.ts
Normal file
42
packages/db-postgres/src/updateVersion.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import type { UpdateVersion } from 'payload/database'
|
||||
import type { PayloadRequest, SanitizedCollectionConfig } from 'payload/types'
|
||||
|
||||
import { buildVersionCollectionFields } from 'payload/versions'
|
||||
import toSnakeCase from 'to-snake-case'
|
||||
|
||||
import type { PostgresAdapter } from './types'
|
||||
|
||||
import buildQuery from './queries/buildQuery'
|
||||
import { upsertRow } from './upsertRow'
|
||||
|
||||
export const updateVersion: UpdateVersion = async function updateVersion(
|
||||
this: PostgresAdapter,
|
||||
{ id, collectionSlug, locale, req = {} as PayloadRequest, versionData, where: whereArg },
|
||||
) {
|
||||
const db = this.sessions?.[req.transactionID] || this.db
|
||||
const collectionConfig: SanitizedCollectionConfig = this.payload.collections[collectionSlug].config
|
||||
const whereToUse = whereArg || { id: { equals: id } }
|
||||
const tableName = `_${toSnakeCase(collectionSlug)}_versions`
|
||||
const fields = buildVersionCollectionFields(collectionConfig)
|
||||
|
||||
const { where } = await buildQuery({
|
||||
adapter: this,
|
||||
fields,
|
||||
locale,
|
||||
tableName,
|
||||
where: whereToUse,
|
||||
})
|
||||
|
||||
const result = await upsertRow({
|
||||
id,
|
||||
adapter: this,
|
||||
data: versionData,
|
||||
db,
|
||||
fields,
|
||||
operation: 'update',
|
||||
tableName,
|
||||
where,
|
||||
})
|
||||
|
||||
return result
|
||||
}
|
||||
@@ -138,6 +138,7 @@ export const upsertRow = async ({
|
||||
if (operation === 'update') {
|
||||
await deleteExistingRowsByPath({
|
||||
adapter,
|
||||
db,
|
||||
localeColumnName: 'locale',
|
||||
newRows: relationsToInsert,
|
||||
parentColumnName: 'parent',
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import type { SQL } from 'drizzle-orm'
|
||||
import type { Field } from 'payload/types'
|
||||
|
||||
import type { GenericColumn, PostgresAdapter } from '../types'
|
||||
import type { DrizzleDB } from '../types'
|
||||
import type { DrizzleDB, GenericColumn, PostgresAdapter } from '../types'
|
||||
|
||||
type BaseArgs = {
|
||||
adapter: PostgresAdapter
|
||||
|
||||
Reference in New Issue
Block a user