Making sure delete is present

This commit is contained in:
kukulaka
2020-08-31 17:40:16 +01:00
parent 9d6f80149c
commit 8a21d6fd01
8 changed files with 78 additions and 17 deletions

View File

@@ -5,6 +5,7 @@ import sanitizeQuery from '../middleware/sanitize-query';
import useCollection from '../middleware/use-collection';
import checkCacheMiddleware from '../middleware/check-cache';
import setCacheMiddleware from '../middleware/set-cache';
import delCacheMiddleware from '../middleware/delete-cache';
import ActivityService from '../services/activity';
import MetaService from '../services/meta';
import { Action } from '../types';
@@ -51,6 +52,7 @@ router.post(
'/comment',
useCollection('directus_activity'),
sanitizeQuery,
delCacheMiddleware,
asyncHandler(async (req, res) => {
const service = new ActivityService({ accountability: req.accountability });
@@ -74,6 +76,7 @@ router.patch(
'/comment/:pk',
useCollection('directus_activity'),
sanitizeQuery,
delCacheMiddleware,
asyncHandler(async (req, res) => {
const service = new ActivityService({ accountability: req.accountability });
const primaryKey = await service.update(req.body, req.params.pk);
@@ -88,6 +91,7 @@ router.patch(
router.delete(
'/comment/:pk',
useCollection('directus_activity'),
delCacheMiddleware,
asyncHandler(async (req, res) => {
const service = new ActivityService({ accountability: req.accountability });
await service.delete(req.params.pk);

View File

@@ -4,6 +4,7 @@ import asyncHandler from 'express-async-handler';
import sanitizeQuery from '../middleware/sanitize-query';
import checkCacheMiddleware from '../middleware/check-cache';
import setCacheMiddleware from '../middleware/set-cache';
import delCacheMiddleware from '../middleware/delete-cache';
import CollectionsService from '../services/collections';
import useCollection from '../middleware/use-collection';
import MetaService from '../services/meta';
@@ -13,6 +14,7 @@ const router = Router();
router.post(
'/',
useCollection('directus_collections'),
delCacheMiddleware,
asyncHandler(async (req, res) => {
const collectionsService = new CollectionsService({ accountability: req.accountability });
@@ -58,6 +60,7 @@ router.get(
router.patch(
'/:collection',
useCollection('directus_collections'),
delCacheMiddleware,
asyncHandler(async (req, res) => {
const collectionsService = new CollectionsService({ accountability: req.accountability });
const collectionKey = req.params.collection.includes(',')
@@ -72,6 +75,7 @@ router.patch(
router.delete(
'/:collection',
useCollection('directus_collections'),
delCacheMiddleware,
asyncHandler(async (req, res) => {
const collectionsService = new CollectionsService({ accountability: req.accountability });
const collectionKey = req.params.collection.includes(',')

View File

@@ -5,6 +5,7 @@ import FieldsService from '../services/fields';
import validateCollection from '../middleware/collection-exists';
import checkCacheMiddleware from '../middleware/check-cache';
import setCacheMiddleware from '../middleware/set-cache';
import delCacheMiddleware from '../middleware/delete-cache';
import { schemaInspector } from '../database';
import { FieldNotFoundException, InvalidPayloadException } from '../exceptions';
import Joi from 'joi';
@@ -85,6 +86,7 @@ router.post(
'/:collection',
validateCollection,
useCollection('directus_fields'),
delCacheMiddleware,
asyncHandler(async (req, res) => {
const service = new FieldsService({ accountability: req.accountability });
@@ -108,6 +110,7 @@ router.patch(
'/:collection',
validateCollection,
useCollection('directus_fields'),
delCacheMiddleware,
asyncHandler(async (req, res) => {
const service = new FieldsService({ accountability: req.accountability });
@@ -132,6 +135,7 @@ router.patch(
'/:collection/:field',
validateCollection,
useCollection('directus_fields'),
delCacheMiddleware,
// @todo: validate field
asyncHandler(async (req, res) => {
const service = new FieldsService({ accountability: req.accountability });
@@ -152,6 +156,7 @@ router.delete(
'/:collection/:field',
validateCollection,
useCollection('directus_fields'),
delCacheMiddleware,
asyncHandler(async (req, res) => {
const service = new FieldsService({ accountability: req.accountability });

View File

@@ -4,6 +4,7 @@ import asyncHandler from 'express-async-handler';
import ItemsService from '../services/items';
import checkCacheMiddleware from '../middleware/check-cache';
import setCacheMiddleware from '../middleware/set-cache';
import delCacheMiddleware from '../middleware/delete-cache';
import sanitizeQuery from '../middleware/sanitize-query';
import collectionExists from '../middleware/collection-exists';
import MetaService from '../services/meta';
@@ -15,6 +16,7 @@ router.post(
'/:collection',
collectionExists,
sanitizeQuery,
delCacheMiddleware,
asyncHandler(async (req, res) => {
if (req.singleton) {
throw new RouteNotFoundException(req.path);
@@ -76,6 +78,7 @@ router.patch(
'/:collection',
collectionExists,
sanitizeQuery,
delCacheMiddleware,
asyncHandler(async (req, res) => {
const service = new ItemsService(req.collection, { accountability: req.accountability });
@@ -96,6 +99,7 @@ router.patch(
'/:collection/:pk',
collectionExists,
sanitizeQuery,
delCacheMiddleware,
asyncHandler(async (req, res) => {
if (req.singleton) {
throw new RouteNotFoundException(req.path);
@@ -114,6 +118,7 @@ router.patch(
router.delete(
'/:collection/:pk',
collectionExists,
delCacheMiddleware,
asyncHandler(async (req, res) => {
const service = new ItemsService(req.collection, { accountability: req.accountability });
const pk = req.params.pk.includes(',') ? req.params.pk.split(',') : req.params.pk;

View File

@@ -5,6 +5,7 @@ import sanitizeQuery from '../middleware/sanitize-query';
import PermissionsService from '../services/permissions';
import useCollection from '../middleware/use-collection';
import checkCacheMiddleware from '../middleware/check-cache';
import delCacheMiddleware from '../middleware/delete-cache';
import setCacheMiddleware from '../middleware/set-cache';
import MetaService from '../services/meta';
import { InvalidCredentialsException } from '../exceptions';
@@ -16,6 +17,7 @@ router.use(useCollection('directus_permissions'));
router.post(
'/',
delCacheMiddleware,
asyncHandler(async (req, res) => {
const service = new PermissionsService({ accountability: req.accountability });
const primaryKey = await service.create(req.body);
@@ -82,6 +84,7 @@ router.get(
router.patch(
'/:pk',
delCacheMiddleware,
asyncHandler(async (req, res) => {
const service = new PermissionsService({ accountability: req.accountability });
const primaryKey = await service.update(req.body, Number(req.params.pk));
@@ -94,6 +97,7 @@ router.patch(
router.delete(
'/:pk',
delCacheMiddleware,
asyncHandler(async (req, res) => {
const service = new PermissionsService({ accountability: req.accountability });
await service.delete(Number(req.params.pk));

View File

@@ -3,19 +3,11 @@
* and node caching
*/
import { RequestHandler } from 'express';
import redis from 'redis';
import asyncHandler from 'express-async-handler';
import CacheService from '../services/cache';
import { RedisNotFoundException } from '../exceptions';
import env from '../env';
const redisClient = redis.createClient({
enable_offline_queue: false,
host: env.CACHE_HOST,
port: env.CACHE_PORT,
password: env.CACHE_REDIS_PASSWORD,
});
const checkCacheMiddleware: RequestHandler = asyncHandler(async (req, res, next) => {
// make the key of the cache the URL
// need to check that this will work for all endpoints
@@ -29,6 +21,13 @@ const checkCacheMiddleware: RequestHandler = asyncHandler(async (req, res, next)
// we have two options here. Redis or node cache
if (env.CACHE_DRIVER === 'redis') {
const redis = require('redis');
const redisClient = redis.createClient({
enable_offline_queue: false,
host: env.CACHE_HOST,
port: env.CACHE_PORT,
password: env.CACHE_REDIS_PASSWORD,
});
if (!redisClient) {
throw new RedisNotFoundException('Redis client does not exist');
}

View File

@@ -0,0 +1,42 @@
/**
* Caching using redis
* and node caching
*/
import { RequestHandler } from 'express';
import asyncHandler from 'express-async-handler';
import CacheService from '../services/cache';
import { RedisNotFoundException } from '../exceptions';
import env from '../env';
const delCacheMiddleware: RequestHandler = asyncHandler(async (req, res, next) => {
// setting the cache
if (env.CACHE_ENABLED !== 'true') return next();
//key needs to have url, query and permissions
const key = `${req.url}${req.query}${req.permissions}`;
// we have two options here. Redis or node cache
if (env.CACHE_DRIVER === 'redis') {
const redis = require('redis');
const redisClient = redis.createClient({
enable_offline_queue: false,
host: env.CACHE_HOST,
port: env.CACHE_PORT,
password: env.CACHE_REDIS_PASSWORD,
});
if (!redisClient) {
throw new RedisNotFoundException('Redis client does not exist');
}
redisClient.del(key);
} else {
const cacheService = new CacheService();
cacheService.delCache(key);
}
return next();
});
export default delCacheMiddleware;

View File

@@ -3,20 +3,11 @@
* and node caching
*/
import { RequestHandler } from 'express';
import redis from 'redis';
import NodeCache from 'node-cache';
import asyncHandler from 'express-async-handler';
import CacheService from '../services/cache';
import { RedisNotFoundException } from '../exceptions';
import env from '../env';
const redisClient = redis.createClient({
enable_offline_queue: false,
host: env.CACHE_HOST,
port: env.CACHE_PORT,
password: env.CACHE_REDIS_PASSWORD,
});
const setCacheMiddleware: RequestHandler = asyncHandler(async (req, res, next) => {
// setting the cache
@@ -28,6 +19,13 @@ const setCacheMiddleware: RequestHandler = asyncHandler(async (req, res, next) =
// we have two options here. Redis or node cache
if (env.CACHE_DRIVER === 'redis') {
const redis = require('redis');
const redisClient = redis.createClient({
enable_offline_queue: false,
host: env.CACHE_HOST,
port: env.CACHE_PORT,
password: env.CACHE_REDIS_PASSWORD,
});
if (!redisClient) {
throw new RedisNotFoundException('Redis client does not exist');
}