Skip to content

Commit

Permalink
fix: allow id to be numeric
Browse files Browse the repository at this point in the history
  • Loading branch information
etienne-bechara committed May 22, 2021
1 parent 1ccbaac commit 438616c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion source/orm/orm.interface/orm.read.params.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { EntityData, FilterQuery } from '@mikro-orm/core';

export type OrmReadParams<T> = string | FilterQuery<T> | EntityData<T>;
export type OrmReadParams<T> = string | number | FilterQuery<T> | EntityData<T>;
12 changes: 6 additions & 6 deletions source/orm/orm.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export abstract class OrmService<Entity> {
options.orderBy = { [options.sort]: options.order };
}

const findParams = typeof params === 'string'
const findParams = typeof params === 'string' || typeof params === 'number'
? { id: params }
: await this.beforeRead(params);

Expand All @@ -68,7 +68,7 @@ export abstract class OrmService<Entity> {
}

if (!readEntities[0] && options.findOrFail) {
const entityError = typeof params === 'string' ? 'id' : 'params';
const entityError = typeof params === 'string' || typeof params === 'number' ? 'id' : 'params';
throw new NotFoundException(`entity with given ${entityError} does not exist`);
}

Expand Down Expand Up @@ -101,7 +101,7 @@ export abstract class OrmService<Entity> {
* @param id
* @param options
*/
public async readById(id: string, options: OrmReadOptions<Entity> = { }): Promise<Entity> {
public async readById(id: string | number, options: OrmReadOptions<Entity> = { }): Promise<Entity> {
const entities = await this.read(id, options);
return entities[0];
}
Expand All @@ -111,7 +111,7 @@ export abstract class OrmService<Entity> {
* @param id
* @param options
*/
public async readByIdOrFail(id: string, options: OrmReadOptions<Entity> = { }): Promise<Entity> {
public async readByIdOrFail(id: string | number, options: OrmReadOptions<Entity> = { }): Promise<Entity> {
options.findOrFail = true;
return this.readById(id, options);
}
Expand Down Expand Up @@ -287,7 +287,7 @@ export abstract class OrmService<Entity> {
* @param data
* @param options
*/
public async updateById(id: string, data: EntityData<Entity>, options: OrmUpdateOptions<Entity> = { }): Promise<Entity> {
public async updateById(id: string | number, data: EntityData<Entity>, options: OrmUpdateOptions<Entity> = { }): Promise<Entity> {
const entity = await this.readByIdOrFail(id);
const updatedEntity = await this.update({ entity, data }, options);
return updatedEntity[0];
Expand Down Expand Up @@ -461,7 +461,7 @@ export abstract class OrmService<Entity> {
* Remove a single entity by its ID.
* @param id
*/
public async removeById(id: string): Promise<Entity> {
public async removeById(id: string | number): Promise<Entity> {
const entity = await this.readByIdOrFail(id);
await this.remove(entity);
return entity;
Expand Down

0 comments on commit 438616c

Please sign in to comment.