Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

Commit

Permalink
fix bug reported in #165
Browse files Browse the repository at this point in the history
  • Loading branch information
Asjas committed Jul 19, 2022
1 parent 76e4f2f commit 6be270c
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ export const createPrismaRedisCache = ({
ttl: cacheTime,
};

// Do not cache any Prisma method specified in the defaultExcludeCacheMethods option
const excludedCacheMethods: PrismaAction[] = defaultCacheMethods.filter((cacheMethod) => {
return !excludeMethods.includes(cacheMethod);
});

const cache: any = createCache(cacheOptions);

const middleware: Middleware = async (params, next) => {
Expand All @@ -51,13 +46,19 @@ export const createPrismaRedisCache = ({
return next(params);
};

// Do not cache any Prisma method specified in the defaultExcludeCacheMethods option
const excludedCacheMethods: PrismaAction[] = defaultCacheMethods.filter((cacheMethod) => {
return excludeMethods.includes(cacheMethod);
});

// Do not cache any Prisma method that has been excluded
if (excludedCacheMethods?.includes(params.action)) {
if (!excludedCacheMethods?.includes(params.action)) {
// Add a cache function for each model specified in the models option
models?.forEach(({ model, cacheTime, cacheKey, excludeMethods }) => {
// Only define the cache function for a model if it doesn't exist yet and hasn't been excluded
if (
!cache[model] &&
model === params.model &&
!excludeModels?.includes(params.model) &&
!excludeMethods?.includes(params.action as PrismaQueryAction)
) {
Expand Down Expand Up @@ -117,7 +118,8 @@ export const createPrismaRedisCache = ({
// Only cache the data if the Prisma model hasn't been excluded and if the Prisma method wasn't excluded either
if (
!excludeModels?.includes(params.model) &&
excludedCacheMethods?.includes(params.action) &&
!excludedCacheMethods?.includes(params.action) &&
!defaultMutationMethods?.includes(params.action as PrismaMutationAction) &&
typeof cacheFunction === "function"
) {
try {
Expand Down

0 comments on commit 6be270c

Please sign in to comment.