Skip to content

Commit

Permalink
fix(crud-cache): Do not use cache for update and deletion of ressources
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony Comtois committed Apr 3, 2021
1 parent 6113c67 commit bc304e1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
8 changes: 8 additions & 0 deletions packages/crud-typeorm/src/typeorm-crud.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ export class TypeOrmCrudService<T> extends CrudService<T> {
public async updateOne(req: CrudRequest, dto: DeepPartial<T>): Promise<T> {
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 }
Expand All @@ -200,6 +202,8 @@ export class TypeOrmCrudService<T> extends CrudService<T> {
* @param dto
*/
public async recoverOne(req: CrudRequest): Promise<T> {
// disable cache while recovering
req.options.query.cache = false;
const found = await this.getOneOrFail(req, false, true);
return this.repo.recover(found);
}
Expand All @@ -212,6 +216,8 @@ export class TypeOrmCrudService<T> extends CrudService<T> {
public async replaceOne(req: CrudRequest, dto: DeepPartial<T>): Promise<T> {
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 }
Expand Down Expand Up @@ -247,6 +253,8 @@ export class TypeOrmCrudService<T> extends CrudService<T> {
*/
public async deleteOne(req: CrudRequest): Promise<void | T> {
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 })
Expand Down
2 changes: 1 addition & 1 deletion packages/crud-typeorm/test/c.basic-crud.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ describe('#crud-typeorm', () => {
},
query: {
persist: ['isActive'],
cache: 0,
cache: 10000,
},
validation: {
transform: true,
Expand Down

0 comments on commit bc304e1

Please sign in to comment.