Skip to content

Commit

Permalink
fix: add populate support for delete
Browse files Browse the repository at this point in the history
  • Loading branch information
etienne-bechara committed Aug 8, 2021
1 parent 8a4e846 commit ece90a1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
5 changes: 4 additions & 1 deletion source/orm/orm.interface/orm.delete.options.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export interface OrmDeleteOptions {
import { Populate } from '@mikro-orm/core';

export interface OrmDeleteOptions<Entity> {
disableFlush?: boolean;
populate?: Populate<Entity>;
}
12 changes: 8 additions & 4 deletions source/orm/orm.repository/orm.repository.delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ export abstract class OrmDeleteRepository<Entity> extends OrmUpdateRepository<En
* @param entities
* @param options
*/
public async delete(entities: Entity | Entity[], options: OrmDeleteOptions = { }): Promise<Entity[]> {
const { disableFlush } = options;
public async delete(entities: Entity | Entity[], options: OrmDeleteOptions<Entity> = { }): Promise<Entity[]> {
const { disableFlush, populate } = options;
const entityArray = Array.isArray(entities) ? entities : [ entities ];
if (!entities || entityArray.length === 0) return [ ];

if (populate) {
await this.populate(entityArray, populate);
}

try {
disableFlush
? this.remove(entityArray)
Expand All @@ -40,7 +44,7 @@ export abstract class OrmDeleteRepository<Entity> extends OrmUpdateRepository<En
* @param id
* @param options
*/
public async deleteById(id: string | number, options: OrmDeleteOptions = { }): Promise<Entity> {
public async deleteById(id: string | number, options: OrmDeleteOptions<Entity> = { }): Promise<Entity> {
const entity = await this.readByIdOrFail(id);
await this.delete(entity, options);
return entity;
Expand All @@ -51,7 +55,7 @@ export abstract class OrmDeleteRepository<Entity> extends OrmUpdateRepository<En
* @param entity
* @param options
*/
public async deleteOne(entity: Entity, options: OrmDeleteOptions = { }): Promise<Entity> {
public async deleteOne(entity: Entity, options: OrmDeleteOptions<Entity> = { }): Promise<Entity> {
const [ deletedEntity ] = await this.delete(entity, options);
return deletedEntity;
}
Expand Down

0 comments on commit ece90a1

Please sign in to comment.