From b350458053980041178bc04b24cb491f7ce13928 Mon Sep 17 00:00:00 2001 From: Fredrik Johansen Date: Fri, 10 Nov 2023 13:24:37 +0100 Subject: [PATCH 1/3] feat(plugin): adds option to disable cache on plugin register --- src/index.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index e3b834b..2ec2529 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,6 +9,7 @@ export interface PluginOptions { dynamoDbAddress?: string; tableName: string; defaultTTLSeconds: number; + disableCache?: boolean; } export const dynamodbCache: FastifyPluginAsync = async ( @@ -22,9 +23,10 @@ export const dynamodbCache: FastifyPluginAsync = async ( fastify.addHook("onRoute", (routeOptions) => { if ( - routeOptions.config && - routeOptions.config.cache && - routeOptions.config.cache.cacheEnabled === true + !opts.disableCache && // Only add cache to route if plugin setting "disableCache" is false or undefined + routeOptions.config && // Check if route config object is set + routeOptions.config.cache && // Check if route config cache object is set + routeOptions.config.cache.cacheEnabled === true // Check if cache object contains "cacheEnabled" property and that is true ) { const onRequestHook = createOnRequestHook({ dynamoClient, From e3aad18e4c33d8759eee35f4f5350fca6c6af77c Mon Sep 17 00:00:00 2001 From: Fredrik Johansen Date: Fri, 10 Nov 2023 13:25:03 +0100 Subject: [PATCH 2/3] test(plugin): updates test server with new disableCache option --- test/src/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/test/src/index.ts b/test/src/index.ts index 8a211e5..d796b22 100644 --- a/test/src/index.ts +++ b/test/src/index.ts @@ -15,6 +15,7 @@ fastify dynamoDbAddress: "http://localhost:8000", tableName: "fastify-dynamodb-cache", defaultTTLSeconds: 30, + disableCache: false, // Cache is enabled }) .register(fastifySwagger, { openapi: { From db4eb5d3971e9686b0b0375251b85f0a3e013dec Mon Sep 17 00:00:00 2001 From: Fredrik Johansen Date: Fri, 10 Nov 2023 13:25:26 +0100 Subject: [PATCH 3/3] docs(readme): updates readme with new disableCache option --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index ff2d632..a531b28 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,7 @@ const fastify = Fastify().register(dynamodbCache, { dynamoDbAddress: "http://localhost:8000", // Optional! If you are hosting your own instance of Dynamo (locally or cloud), then specify the ip address of the database here. tableName: "fastify-dynamodb-cache", // DynamoDB table name defaultTTLSeconds: 30, // Default TTL (seconds), which would be used if no TTL is specified on the endpoint. + disableCache: true, // Optional! If you want to disable caching from being set on endpoints, you can set this to true. Set it to false or leave it empty to enable cache. }); fastify.get(