Skip to content

Commit bbc9803

Browse files
authored
feat(repository): remove multiple db calls for find by id (#88)
#87
1 parent c17cfcc commit bbc9803

File tree

1 file changed

+6
-15
lines changed

1 file changed

+6
-15
lines changed

src/repositories/soft-crud.repository.base.ts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,9 @@ export abstract class SoftCrudRepository<
175175
[pk]: id,
176176
} as Condition<T>;
177177
}
178-
179-
//As parent method findById have filter: FilterExcludingWhere<T>
180-
//so we need add check here.
181-
const entityToRemove = await super.findOne(filter, options);
182-
183-
if (entityToRemove) {
184-
// Now call super
185-
return super.findById(id, filter, options);
178+
const entity = await super.findById(id, filter, options);
179+
if (entity && !entity.deleted) {
180+
return entity;
186181
} else {
187182
throw new HttpErrors.NotFound(ErrorKeys.EntityNotFound);
188183
}
@@ -194,13 +189,9 @@ export abstract class SoftCrudRepository<
194189
filter?: Filter<T>,
195190
options?: Options,
196191
): Promise<T & Relations> {
197-
//As parent method findById have filter: FilterExcludingWhere<T>
198-
//so we need add check here.
199-
const entityToRemove = await super.findOne(filter, options);
200-
201-
if (entityToRemove) {
202-
// Now call super
203-
return super.findById(id, filter, options);
192+
const entity = await super.findById(id, filter, options);
193+
if (entity) {
194+
return entity;
204195
} else {
205196
throw new HttpErrors.NotFound(ErrorKeys.EntityNotFound);
206197
}

0 commit comments

Comments
 (0)