Skip to content

Commit

Permalink
fix: throw on missing entity during readUnique
Browse files Browse the repository at this point in the history
  • Loading branch information
etienne-bechara committed Apr 13, 2021
1 parent 82affe8 commit 8bcf10d
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions source/orm/orm.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,17 @@ export abstract class OrmService<Entity> {
}

/**
* Read a supposedly unique entity, if the constraint fails throw a conflict exception.
* Read a supposedly unique entity, throws an exception if either none or more than one.
* @param params
* @param options
*/
public async readUnique(params: OrmReadParams<Entity>, options: OrmReadOptions<Entity> = { }): Promise<Entity> {
const entities = await this.read(params, options);

if (entities.length === 0) {
throw new NotFoundException('entity with given constraints does not exist');
}

if (entities.length > 1) {
throw new ConflictException({
message: 'unique constraint references more than one entity',
Expand Down Expand Up @@ -270,10 +274,10 @@ export abstract class OrmService<Entity> {
}

/**
* Deletes a single entity by its id.
* Removes a single entity by its id.
* @param id
*/
public async deleteById(id: string): Promise<Entity> {
public async removeById(id: string): Promise<Entity> {
const entity = await this.readById(id);
return this.remove(entity);
}
Expand Down

0 comments on commit 8bcf10d

Please sign in to comment.