Skip to content

Commit

Permalink
fix: remove collection findByID caching (#5034)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanRibbens committed Feb 16, 2024
1 parent 25cee8b commit 1ac943e
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 33 deletions.
1 change: 0 additions & 1 deletion packages/payload/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@
"jwt-decode": "3.1.2",
"md5": "2.3.0",
"method-override": "3.0.0",
"micro-memoize": "4.1.2",
"minimist": "1.2.8",
"mkdirp": "1.0.4",
"monaco-editor": "0.38.0",
Expand Down
26 changes: 1 addition & 25 deletions packages/payload/src/collections/operations/findByID.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/* eslint-disable no-underscore-dangle */
import memoize from 'micro-memoize'

import type { FindOneArgs } from '../../database/types'
import type { PayloadRequest } from '../../express/types'
import type { Collection, TypeWithID } from '../config/types'
Expand Down Expand Up @@ -32,7 +30,6 @@ async function findByID<T extends TypeWithID>(incomingArgs: Arguments): Promise<

try {
const shouldCommit = await initTransaction(args.req)
const { transactionID } = args.req

// /////////////////////////////////////
// beforeOperation - Collection
Expand Down Expand Up @@ -90,25 +87,7 @@ async function findByID<T extends TypeWithID>(incomingArgs: Arguments): Promise<

if (!findOneArgs.where.and[0].id) throw new NotFound(t)

if (!req.findByID) {
req.findByID = { [transactionID]: {} }
} else if (!req.findByID[transactionID]) {
req.findByID[transactionID] = {}
}

if (!req.findByID[transactionID][collectionConfig.slug]) {
const nonMemoizedFindByID = async (query: FindOneArgs) => req.payload.db.findOne(query)

req.findByID[transactionID][collectionConfig.slug] = memoize(nonMemoizedFindByID, {
isPromise: true,
maxSize: 100,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore This is straight from their docs, bad typings
transformKey: JSON.stringify,
})
}

let result = (await req.findByID[transactionID][collectionConfig.slug](findOneArgs)) as T
let result: T = await req.payload.db.findOne(findOneArgs)

if (!result) {
if (!disableErrors) {
Expand All @@ -118,9 +97,6 @@ async function findByID<T extends TypeWithID>(incomingArgs: Arguments): Promise<
return null
}

// Clone the result - it may have come back memoized
result = JSON.parse(JSON.stringify(result))

// /////////////////////////////////////
// Replace document with draft if available
// /////////////////////////////////////
Expand Down
7 changes: 0 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1ac943e

Please sign in to comment.