Skip to content

Commit

Permalink
feat(nestjs-json-rpc): Add support soft delete
Browse files Browse the repository at this point in the history
Allow set options for soft delete for DeleteOne
  • Loading branch information
klerick committed Dec 5, 2024
1 parent e405f37 commit c3b9322
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ export const ConfigParamDefault: ConfigParam = {
debug: true,
requiredSelectField: true,
pipeForId: ParseIntPipe,
useSoftDelete: false,
};
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { Entity, TypeormServiceObject } from '../../../../types';
import { FindOptionsWhere } from 'typeorm';

export async function deleteOne<E extends Entity>(
this: TypeormServiceObject<E>,
id: number | string
): Promise<void> {
await this.repository
.createQueryBuilder(this.typeormUtilsService.currentAlias)
.delete()
.where(
`${this.typeormUtilsService.currentPrimaryColumn.toString()} = :params`
)
.setParameters({
params: id,
})
.execute();
const data = await this.repository.findOne({
where: {
[this.typeormUtilsService.currentPrimaryColumn.toString()]: id,
} as FindOptionsWhere<E>,
});
if (!data) return void 0;

this.config.useSoftDelete
? await this.repository.softRemove(data)
: await this.repository.remove(data);

return void 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
SUB_QUERY_ALIAS_FOR_PAGINATION,
} from '../../../../constants';
import { ResourceObject } from '../../../../types/response';
import { da } from '@faker-js/faker';

type OrderByCondition = Record<string, 'ASC' | 'DESC'>;

Expand Down Expand Up @@ -250,7 +249,6 @@ export async function getAll<E extends Entity>(
);
}
const resultData = await resultQuery.getMany();
console.log(resultData);
const { included, data } =
this.transformDataService.transformData(resultData);
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export type ExtractNestType<ArrayType> =
export interface ConfigParam {
requiredSelectField: boolean;
debug: boolean;
useSoftDelete: boolean;
pipeForId: PipeMixin;
operationUrl?: string;
overrideRoute?: string;
Expand Down

0 comments on commit c3b9322

Please sign in to comment.