Skip to content

fix: deleteFromCacheByFields to mirror logic from findByFields #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 98 additions & 35 deletions package-lock.json
20 changes: 17 additions & 3 deletions src/cache.js
Original file line number Diff line number Diff line change
@@ -213,10 +213,24 @@ export const createCachingMethods = ({ collection, model, cache }) => {
await cache.delete(cacheKey)
},
deleteFromCacheByFields: async fields => {
const { loaderKey } = prepFields(fields)
const cacheKey = cachePrefix + loaderKey
const { cleanedFields, loaderKey } = prepFields(fields)

const fieldNames = Object.keys(cleanedFields)

loader.clear(loaderKey)
// match logic from findByFields
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you put duplicated logic in a helper function, for better maintainability?

if (fieldNames.length === 1) {
const field = cleanedFields[fieldNames[0]]
const fieldArray = Array.isArray(field) ? field : [field]
fieldArray.forEach(value => {
const filter = {}
filter[fieldNames[0]] = value
loader.clear(EJSON.stringify(filter))
})
} else {
loader.clear(loaderKey)
}

const cacheKey = cachePrefix + loaderKey
await cache.delete(cacheKey)
}
}