Skip to content

Commit f0fdc40

Browse files
author
A-J Roos
committed
add-test/don't invalidate the cache if the cache method was excluded
1 parent 3c97212 commit f0fdc40

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ export const createPrismaRedisCache = ({
132132
// Query the database for any Prisma method (mutation method) or Prisma model we excluded from the cache
133133
result = await fetchFromPrisma(params);
134134

135+
// If we successfully executed the Mutation we clear and invalidate the cache for the Prisma model
135136
if (defaultMutationMethods.includes(params.action as PrismaMutationAction)) {
136-
// If we successfully executed the Mutation we clear and invalidate the cache for the Prisma model
137137
await cache.invalidateAll(`${params.model}~*`);
138138
}
139139
}

test/index.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,40 @@ test("should invalidate a single Prisma model cache after data mutation", async
273273
// Test that we keep other cached queries that shouldn't be cleared
274274
expect(JSON.parse((await redis.get(cacheKey2)) as string)).toMatchObject(dbValue);
275275
});
276+
277+
test("should not invalidate a Prisma model if cache method is excluded", async () => {
278+
const middleware = createPrismaRedisCache({
279+
storage: { type: "redis", options: { client: redis, invalidation: true } },
280+
cacheTime,
281+
excludeMethods: ["findFirst"],
282+
});
283+
284+
// Run a "fake" User Prisma query
285+
await middleware(
286+
{
287+
args,
288+
action: action1,
289+
model: model1,
290+
dataPath: [],
291+
runInTransaction: false,
292+
},
293+
next,
294+
);
295+
296+
// Run a "fake" Post Prisma query
297+
await middleware(
298+
{
299+
args,
300+
action: action2,
301+
model: model1,
302+
dataPath: [],
303+
runInTransaction: false,
304+
},
305+
next,
306+
);
307+
308+
// Test if the cached query was fetched from the cache
309+
expect(JSON.parse((await redis.get(cacheKey1)) as string)).toMatchObject(dbValue);
310+
// Test that the excluded cache method was not cached
311+
assert.equal(JSON.parse((await redis.get(cacheKey2)) as string), null);
312+
});

0 commit comments

Comments
 (0)