Skip to content

Commit

Permalink
fix(mongodb): Delete unsupported MongoDB soft deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
marian2js authored and doug-martin committed Oct 16, 2020
1 parent b99a35d commit 22decdf
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 60 deletions.
2 changes: 1 addition & 1 deletion packages/query-typeorm-mongo/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { TypeOrmMongoQueryService, TypeOrmMongoQueryServiceOpts } from './services';
export { TypeOrmMongoQueryService } from './services';
export { NestjsQueryTypeOrmMongoModule } from './module';
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@ import {
QueryService,
UpdateManyResponse,
} from '@nestjs-query/core';
import { Logger, MethodNotAllowedException, NotFoundException } from '@nestjs/common';
import { Logger, NotFoundException } from '@nestjs/common';
import { FindConditions, MongoRepository } from 'typeorm';

export interface TypeOrmMongoQueryServiceOpts<Entity> {
useSoftDelete?: boolean;
}

const mongoOperatorMapper: { [k: string]: string } = {
eq: '$eq',
neq: '$ne',
Expand Down Expand Up @@ -46,11 +42,7 @@ const mongoOperatorMapper: { [k: string]: string } = {
export class TypeOrmMongoQueryService<Entity> implements QueryService<Entity> {
protected readonly logger = new Logger(TypeOrmMongoQueryService.name);

readonly useSoftDelete: boolean;

constructor(readonly repo: MongoRepository<Entity>, opts?: TypeOrmMongoQueryServiceOpts<Entity>) {
this.useSoftDelete = opts?.useSoftDelete ?? false;
}
constructor(readonly repo: MongoRepository<Entity>) {}

// eslint-disable-next-line @typescript-eslint/naming-convention
get EntityClass(): Class<Entity> {
Expand Down Expand Up @@ -230,9 +222,6 @@ export class TypeOrmMongoQueryService<Entity> implements QueryService<Entity> {
*/
async deleteOne(id: string | number): Promise<Entity> {
const entity = await this.repo.findOneOrFail(id);
if (this.useSoftDelete) {
return this.repo.softRemove(entity);
}
return this.repo.remove(entity);
}

Expand All @@ -250,50 +239,10 @@ export class TypeOrmMongoQueryService<Entity> implements QueryService<Entity> {
* @param filter - A `Filter` to find records to delete.
*/
async deleteMany(filter: Filter<Entity>): Promise<DeleteManyResponse> {
if (this.useSoftDelete) {
const res = await this.repo.softDelete(this.buildExpression(filter));
return { deletedCount: res.affected || 0 };
}
const res = await this.repo.deleteMany(this.buildExpression(filter));
return { deletedCount: res.deletedCount || 0 };
}

/**
* Restore an entity by `id`.
*
* @example
*
* ```ts
* const restoredTodo = await this.service.restoreOne(1);
* ```
*
* @param id - The `id` of the entity to restore.
*/
async restoreOne(id: string): Promise<Entity> {
this.ensureSoftDeleteEnabled();
await this.repo.restore(id);
return this.getById(id);
}

/**
* Restores multiple records with a `@nestjs-query/core` `Filter`.
*
* @example
*
* ```ts
* const { updatedCount } = this.service.restoreMany({
* created: { lte: new Date('2020-1-1') }
* });
* ```
*
* @param filter - A `Filter` to find records to delete.
*/
async restoreMany(filter: Filter<Entity>): Promise<UpdateManyResponse> {
this.ensureSoftDeleteEnabled();
const res = await this.repo.restore(this.buildExpression(filter));
return { updatedCount: res.affected || 0 };
}

private async ensureIsEntityAndDoesNotExist(e: DeepPartial<Entity>): Promise<Entity> {
if (!(e instanceof this.EntityClass)) {
return this.ensureEntityDoesNotExist(this.repo.create(e));
Expand All @@ -317,12 +266,6 @@ export class TypeOrmMongoQueryService<Entity> implements QueryService<Entity> {
}
}

private ensureSoftDeleteEnabled(): void {
if (!this.useSoftDelete) {
throw new MethodNotAllowedException(`Restore not allowed for non soft deleted entity ${this.EntityClass.name}.`);
}
}

/**
* Adds multiple relations.
* @param relationName - The name of the relation to query for.
Expand Down

0 comments on commit 22decdf

Please sign in to comment.