From ac880831bb32cd7c55ba647e8bb032bbe1b243d3 Mon Sep 17 00:00:00 2001 From: Sarkis Date: Fri, 19 Apr 2019 16:55:54 -0700 Subject: [PATCH 1/4] add support for pagination hook fix for #74 --- src/hooks/helpers/path.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/hooks/helpers/path.js b/src/hooks/helpers/path.js index 38b4318..207e604 100644 --- a/src/hooks/helpers/path.js +++ b/src/hooks/helpers/path.js @@ -15,9 +15,10 @@ function parseNestedPath(path, params) { function parsePath(hook, config = {removePathFromCacheKey: false, parseNestedRoutes: false}) { const q = hook.params.query || {}; + const paginate = hook.params.paginate ? 'on' : 'off'; const remove = config.removePathFromCacheKey; const parseNestedRoutes = config.parseNestedRoutes; - let path = remove && hook.id ? '' : `${hook.path}`; + let path = remove && hook.id ? `paginate:${paginate}:` : `paginate:${paginate}:${hook.path}`; if (!remove && parseNestedRoutes) { path = parseNestedPath(path, hook.params); From 1474b2e7edc3247594bdddc4592ebfeb2207cbc0 Mon Sep 17 00:00:00 2001 From: Sarkis Date: Fri, 19 Apr 2019 17:05:27 -0700 Subject: [PATCH 2/4] Update path.js --- src/hooks/helpers/path.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hooks/helpers/path.js b/src/hooks/helpers/path.js index 207e604..506e354 100644 --- a/src/hooks/helpers/path.js +++ b/src/hooks/helpers/path.js @@ -15,7 +15,7 @@ function parseNestedPath(path, params) { function parsePath(hook, config = {removePathFromCacheKey: false, parseNestedRoutes: false}) { const q = hook.params.query || {}; - const paginate = hook.params.paginate ? 'on' : 'off'; + const paginate = hook.params.paginate === false ? 'off' : 'on'; const remove = config.removePathFromCacheKey; const parseNestedRoutes = config.parseNestedRoutes; let path = remove && hook.id ? `paginate:${paginate}:` : `paginate:${paginate}:${hook.path}`; From 3cf9c4cfd711939346656c5f8f2387d0aade44e1 Mon Sep 17 00:00:00 2001 From: Sarkis Date: Sat, 20 Apr 2019 15:49:27 -0700 Subject: [PATCH 3/4] fix for nested path --- src/hooks/helpers/path.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hooks/helpers/path.js b/src/hooks/helpers/path.js index 506e354..7a3ee74 100644 --- a/src/hooks/helpers/path.js +++ b/src/hooks/helpers/path.js @@ -15,13 +15,13 @@ function parseNestedPath(path, params) { function parsePath(hook, config = {removePathFromCacheKey: false, parseNestedRoutes: false}) { const q = hook.params.query || {}; - const paginate = hook.params.paginate === false ? 'off' : 'on'; + const paginate = hook.params.paginate === false ? 'off' : 'on'; // if `.paginate` is underfined it means pagination hook is enabled, that's why we are using strict check with false const remove = config.removePathFromCacheKey; const parseNestedRoutes = config.parseNestedRoutes; let path = remove && hook.id ? `paginate:${paginate}:` : `paginate:${paginate}:${hook.path}`; if (!remove && parseNestedRoutes) { - path = parseNestedPath(path, hook.params); + path = `paginate:${paginate}:${parseNestedPath(path, hook.params)}`; } if (hook.id) { From 5b260ab88e86257232b34ce0114dd18b13135f47 Mon Sep 17 00:00:00 2001 From: Sarkis Date: Sat, 20 Apr 2019 16:27:54 -0700 Subject: [PATCH 4/4] add support of pagination hook for clearSingle we don't need this adjustment for clearGroup --- src/routes/cache.js | 70 ++++++++++++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 27 deletions(-) diff --git a/src/routes/cache.js b/src/routes/cache.js index 264ed31..792f84a 100644 --- a/src/routes/cache.js +++ b/src/routes/cache.js @@ -37,37 +37,53 @@ function routes(app) { } // Gets the value of a key in the redis client - client.get(`${target}`, (err, reply) => { - if (err) { - res.status(HTTP_SERVER_ERROR).json({ - message: 'something went wrong' + err.message - }); - } else { - // If the key existed - if (reply) { - // Clear existing cached key - h.clearSingle(target).then(r => { - res.status(HTTP_OK).json({ - message: `cache cleared for key (${hasQueryString ? - 'with' : 'without'} params): ${target}`, - status: HTTP_OK - }); + const clearSingleKey = (key) => new Promise((resolve) => { + client.get(key, (err, reply) => { + if (err) { + res.status(HTTP_SERVER_ERROR).json({ + message: 'something went wrong' + err.message }); } else { - /** - * Empty reply means the key does not exist. - * Must use HTTP_OK with express as HTTP's RFC stats 204 should not - * provide a body, message would then be lost. - */ - res.status(HTTP_OK).json({ - message: `cache already cleared for key (${hasQueryString ? - 'with' : 'without'} params): ${target}`, - status: HTTP_NO_CONTENT - }); + // If the key existed + if (reply) { + // Clear existing cached key + h.clearSingle(key) + .then(r => { + resolve({ + message: `cache cleared for key (${hasQueryString ? 'with' : 'without'} params): ${key}`, + status: HTTP_OK + }); + }); + } else { + /** + * Empty reply means the key does not exist. + * Must use HTTP_OK with express as HTTP's RFC stats 204 should not + * provide a body, message would then be lost. + */ + resolve({ + message: `cache already cleared for key (${hasQueryString ? 'with' : 'without'} params): ${key}`, + status: HTTP_NO_CONTENT + }); + } } - - } + }); }); + + Promise.all([ + clearSingleKey(`paginate:on:${target}`), + clearSingleKey(`paginate:off:${target}`), + ]) + .then(([withPagResult, witoutPagResult]) => { + if (withPagResult.status === HTTP_OK) { + res.status(HTTP_OK).json(withPagResult); + } else if (witoutPagResult.status === HTTP_OK) { + res.status(HTTP_OK).json(witoutPagResult); + } else if (withPagResult.status !== HTTP_OK) { + res.status(HTTP_OK).json(withPagResult); + } else { + res.status(HTTP_OK).json(witoutPagResult); + } + }); } else { res.status(HTTP_NOT_FOUND).end(); }