Skip to content

Commit 84f5370

Browse files
Anthony Comtoisrewiko
authored andcommitted
fix(crud-cache): Do not use cache for update and deletion of ressources
1 parent fbbfcbf commit 84f5370

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

packages/crud-typeorm/src/typeorm-crud.service.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ export class TypeOrmCrudService<T> extends CrudService<T> {
177177
public async updateOne(req: CrudRequest, dto: DeepPartial<T>): Promise<T> {
178178
const { allowParamsOverride, returnShallow } = req.options.routes.updateOneBase;
179179
const paramsFilters = this.getParamFilters(req.parsed);
180+
// disable cache while updating
181+
req.options.query.cache = false;
180182
const found = await this.getOneOrFail(req, returnShallow);
181183
const toSave = !allowParamsOverride
182184
? { ...found, ...dto, ...paramsFilters, ...req.parsed.authPersist }
@@ -200,6 +202,8 @@ export class TypeOrmCrudService<T> extends CrudService<T> {
200202
* @param dto
201203
*/
202204
public async recoverOne(req: CrudRequest): Promise<T> {
205+
// disable cache while recovering
206+
req.options.query.cache = false;
203207
const found = await this.getOneOrFail(req, false, true);
204208
return this.repo.recover(found);
205209
}
@@ -212,6 +216,8 @@ export class TypeOrmCrudService<T> extends CrudService<T> {
212216
public async replaceOne(req: CrudRequest, dto: DeepPartial<T>): Promise<T> {
213217
const { allowParamsOverride, returnShallow } = req.options.routes.replaceOneBase;
214218
const paramsFilters = this.getParamFilters(req.parsed);
219+
// disable cache while replacing
220+
req.options.query.cache = false;
215221
const [_, found] = await oO(this.getOneOrFail(req, returnShallow));
216222
const toSave = !allowParamsOverride
217223
? { ...(found || {}), ...dto, ...paramsFilters, ...req.parsed.authPersist }
@@ -247,6 +253,8 @@ export class TypeOrmCrudService<T> extends CrudService<T> {
247253
*/
248254
public async deleteOne(req: CrudRequest): Promise<void | T> {
249255
const { returnDeleted } = req.options.routes.deleteOneBase;
256+
// disable cache while deleting
257+
req.options.query.cache = false;
250258
const found = await this.getOneOrFail(req, returnDeleted);
251259
const toReturn = returnDeleted
252260
? plainToClass(this.entityType, { ...found })

packages/crud-typeorm/test/c.basic-crud.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ describe('#crud-typeorm', () => {
198198
},
199199
query: {
200200
persist: ['isActive'],
201-
cache: 0,
201+
cache: 10000,
202202
},
203203
validation: {
204204
transform: true,

0 commit comments

Comments
 (0)