Skip to content

Commit

Permalink
feat(repository): remove multiple db calls for find by id
Browse files Browse the repository at this point in the history
  • Loading branch information
yeshamavani committed Sep 5, 2022
1 parent c17cfcc commit a1e5a3f
Showing 1 changed file with 6 additions and 15 deletions.
21 changes: 6 additions & 15 deletions src/repositories/soft-crud.repository.base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,9 @@ export abstract class SoftCrudRepository<
[pk]: id,
} as Condition<T>;
}

//As parent method findById have filter: FilterExcludingWhere<T>
//so we need add check here.
const entityToRemove = await super.findOne(filter, options);

if (entityToRemove) {
// Now call super
return super.findById(id, filter, options);
const entity = await super.findById(id, filter, options);
if (entity && !entity.deleted) {
return entity;
} else {
throw new HttpErrors.NotFound(ErrorKeys.EntityNotFound);
}
Expand All @@ -194,13 +189,9 @@ export abstract class SoftCrudRepository<
filter?: Filter<T>,
options?: Options,
): Promise<T & Relations> {
//As parent method findById have filter: FilterExcludingWhere<T>
//so we need add check here.
const entityToRemove = await super.findOne(filter, options);

if (entityToRemove) {
// Now call super
return super.findById(id, filter, options);
const entity = await super.findById(id, filter, options);
if (entity) {
return entity;
} else {
throw new HttpErrors.NotFound(ErrorKeys.EntityNotFound);
}
Expand Down

0 comments on commit a1e5a3f

Please sign in to comment.