chore: revisions to baseIDField

This commit is contained in:
James
2024-04-02 17:04:27 -04:00
parent f6d2dd520c
commit 25d475e165
3 changed files with 25 additions and 14 deletions

View File

@@ -66,9 +66,9 @@
"test:e2e": "cross-env NODE_OPTIONS=--no-deprecation NODE_NO_WARNINGS=1 tsx ./test/runE2E.ts",
"test:e2e:debug": "cross-env NODE_OPTIONS=--no-deprecation NODE_NO_WARNINGS=1 PWDEBUG=1 DISABLE_LOGGING=true playwright test",
"test:e2e:headed": "cross-env NODE_OPTIONS=--no-deprecation NODE_NO_WARNINGS=1 DISABLE_LOGGING=true playwright test --headed",
"test:int:postgres": "cross-env NODE_OPTIONS=--no-deprecation NODE_NO_WARNINGS=1 PAYLOAD_DATABASE=postgres DISABLE_LOGGING=true jest --forceExit --detectOpenHandles --config=jest.config.js",
"test:int": "cross-env NODE_OPTIONS=--no-deprecation NODE_NO_WARNINGS=1 DISABLE_LOGGING=true jest --forceExit --detectOpenHandles --config=test/jest.config.js",
"test:unit": "cross-env NODE_OPTIONS=--no-deprecation NODE_NO_WARNINGS=1 DISABLE_LOGGING=true jest --forceExit --detectOpenHandles --config=jest.config.js",
"test:int:postgres": "cross-env NODE_OPTIONS=--no-deprecation NODE_NO_WARNINGS=1 PAYLOAD_DATABASE=postgres DISABLE_LOGGING=true jest --forceExit --detectOpenHandles --config=jest.config.js --runInBand",
"test:int": "cross-env NODE_OPTIONS=--no-deprecation NODE_NO_WARNINGS=1 DISABLE_LOGGING=true jest --forceExit --detectOpenHandles --config=test/jest.config.js --runInBand",
"test:unit": "cross-env NODE_OPTIONS=--no-deprecation NODE_NO_WARNINGS=1 DISABLE_LOGGING=true jest --forceExit --detectOpenHandles --config=jest.config.js --runInBand",
"translateNewKeys": "pnpm --filter payload run translateNewKeys"
},
"devDependencies": {

View File

@@ -1,19 +1,28 @@
import ObjectIdImport from 'bson-objectid'
import type { Field, FieldHook } from '../config/types.js'
import type { Field } from '../config/types.js'
const ObjectId = (ObjectIdImport.default ||
ObjectIdImport) as unknown as typeof ObjectIdImport.default
const generateID: FieldHook = ({ operation, value }) =>
(operation !== 'create' ? value : false) || new ObjectId().toHexString()
export const baseIDField: Field = {
name: 'id',
type: 'text',
admin: {
hidden: true,
},
defaultValue: generateID,
defaultValue: () => new ObjectId().toHexString(),
hooks: {
beforeChange: [
({ operation, value }) => {
// If creating new doc, need to disregard any
// ids that have been passed in because they will cause
// primary key unique conflicts in relational DBs
if (value && operation === 'create') {
return new ObjectId().toHexString()
}
},
],
},
label: 'ID',
}

View File

@@ -1,5 +1,7 @@
import type { User } from '../auth/index.js'
import { deepCopyObject } from '../utilities/deepCopyObject.js'
type Args = {
defaultValue: unknown
locale: string | undefined
@@ -7,12 +9,7 @@ type Args = {
value?: unknown
}
const getValueWithDefault = async ({
defaultValue,
locale,
user,
value,
}: Args): Promise<unknown> => {
const getValueWithDefault = ({ defaultValue, locale, user, value }: Args): unknown => {
if (typeof value !== 'undefined') {
return value
}
@@ -21,7 +18,12 @@ const getValueWithDefault = async ({
return defaultValue({ locale, user })
}
if (typeof defaultValue === 'object') {
return deepCopyObject(defaultValue)
}
return defaultValue
}
// eslint-disable-next-line no-restricted-exports
export default getValueWithDefault