Skip to content

Commit c3b9322

Browse files
committed
feat(nestjs-json-rpc): Add support soft delete
Allow set options for soft delete for DeleteOne
1 parent e405f37 commit c3b9322

File tree

4 files changed

+14
-12
lines changed

4 files changed

+14
-12
lines changed

libs/json-api/json-api-nestjs/src/lib/constants/defaults.ts

+1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ export const ConfigParamDefault: ConfigParam = {
1919
debug: true,
2020
requiredSelectField: true,
2121
pipeForId: ParseIntPipe,
22+
useSoftDelete: false,
2223
};
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
import { Entity, TypeormServiceObject } from '../../../../types';
2+
import { FindOptionsWhere } from 'typeorm';
23

34
export async function deleteOne<E extends Entity>(
45
this: TypeormServiceObject<E>,
56
id: number | string
67
): Promise<void> {
7-
await this.repository
8-
.createQueryBuilder(this.typeormUtilsService.currentAlias)
9-
.delete()
10-
.where(
11-
`${this.typeormUtilsService.currentPrimaryColumn.toString()} = :params`
12-
)
13-
.setParameters({
14-
params: id,
15-
})
16-
.execute();
8+
const data = await this.repository.findOne({
9+
where: {
10+
[this.typeormUtilsService.currentPrimaryColumn.toString()]: id,
11+
} as FindOptionsWhere<E>,
12+
});
13+
if (!data) return void 0;
14+
15+
this.config.useSoftDelete
16+
? await this.repository.softRemove(data)
17+
: await this.repository.remove(data);
18+
1719
return void 0;
1820
}

libs/json-api/json-api-nestjs/src/lib/helper/orm/methods/get-all/get-all.ts

-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
SUB_QUERY_ALIAS_FOR_PAGINATION,
1010
} from '../../../../constants';
1111
import { ResourceObject } from '../../../../types/response';
12-
import { da } from '@faker-js/faker';
1312

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

@@ -250,7 +249,6 @@ export async function getAll<E extends Entity>(
250249
);
251250
}
252251
const resultData = await resultQuery.getMany();
253-
console.log(resultData);
254252
const { included, data } =
255253
this.transformDataService.transformData(resultData);
256254
return {

libs/json-api/json-api-nestjs/src/lib/types/module.types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export type ExtractNestType<ArrayType> =
2020
export interface ConfigParam {
2121
requiredSelectField: boolean;
2222
debug: boolean;
23+
useSoftDelete: boolean;
2324
pipeForId: PipeMixin;
2425
operationUrl?: string;
2526
overrideRoute?: string;

0 commit comments

Comments
 (0)