From bc304e100838442aa2ef22cca08a2b4307432e0b Mon Sep 17 00:00:00 2001 From: Anthony Comtois Date: Sat, 3 Apr 2021 11:04:24 +0000 Subject: [PATCH] fix(crud-cache): Do not use cache for update and deletion of ressources --- packages/crud-typeorm/src/typeorm-crud.service.ts | 8 ++++++++ packages/crud-typeorm/test/c.basic-crud.spec.ts | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/crud-typeorm/src/typeorm-crud.service.ts b/packages/crud-typeorm/src/typeorm-crud.service.ts index 69a4d83f..8c6c8063 100644 --- a/packages/crud-typeorm/src/typeorm-crud.service.ts +++ b/packages/crud-typeorm/src/typeorm-crud.service.ts @@ -177,6 +177,8 @@ export class TypeOrmCrudService extends CrudService { public async updateOne(req: CrudRequest, dto: DeepPartial): Promise { const { allowParamsOverride, returnShallow } = req.options.routes.updateOneBase; const paramsFilters = this.getParamFilters(req.parsed); + // disable cache while updating + req.options.query.cache = false; const found = await this.getOneOrFail(req, returnShallow); const toSave = !allowParamsOverride ? { ...found, ...dto, ...paramsFilters, ...req.parsed.authPersist } @@ -200,6 +202,8 @@ export class TypeOrmCrudService extends CrudService { * @param dto */ public async recoverOne(req: CrudRequest): Promise { + // disable cache while recovering + req.options.query.cache = false; const found = await this.getOneOrFail(req, false, true); return this.repo.recover(found); } @@ -212,6 +216,8 @@ export class TypeOrmCrudService extends CrudService { public async replaceOne(req: CrudRequest, dto: DeepPartial): Promise { const { allowParamsOverride, returnShallow } = req.options.routes.replaceOneBase; const paramsFilters = this.getParamFilters(req.parsed); + // disable cache while replacing + req.options.query.cache = false; const [_, found] = await oO(this.getOneOrFail(req, returnShallow)); const toSave = !allowParamsOverride ? { ...(found || {}), ...dto, ...paramsFilters, ...req.parsed.authPersist } @@ -247,6 +253,8 @@ export class TypeOrmCrudService extends CrudService { */ public async deleteOne(req: CrudRequest): Promise { const { returnDeleted } = req.options.routes.deleteOneBase; + // disable cache while deleting + req.options.query.cache = false; const found = await this.getOneOrFail(req, returnDeleted); const toReturn = returnDeleted ? plainToClass(this.entityType, { ...found }) diff --git a/packages/crud-typeorm/test/c.basic-crud.spec.ts b/packages/crud-typeorm/test/c.basic-crud.spec.ts index c6d9fb7b..e812d67a 100644 --- a/packages/crud-typeorm/test/c.basic-crud.spec.ts +++ b/packages/crud-typeorm/test/c.basic-crud.spec.ts @@ -198,7 +198,7 @@ describe('#crud-typeorm', () => { }, query: { persist: ['isActive'], - cache: 0, + cache: 10000, }, validation: { transform: true,