@mondart/nestjs-common-module 1.1.72 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +0 -2
- package/dist/index.js +0 -2
- package/dist/interfaces/index.d.ts +0 -1
- package/dist/interfaces/index.js +0 -1
- package/dist/lib/core-crud/core-crud.controller.d.ts +24 -0
- package/dist/lib/core-crud/core-crud.controller.js +52 -0
- package/dist/lib/core-crud/core-crud.service.d.ts +36 -0
- package/dist/{services → lib/core-crud}/core-crud.service.js +125 -103
- package/dist/lib/core-crud/crud-service-options.interface.d.ts +27 -0
- package/dist/lib/core-crud/crud-service-options.interface.js +2 -0
- package/dist/lib/core-crud/index.d.ts +3 -0
- package/dist/{controllers → lib/core-crud}/index.js +2 -0
- package/dist/lib/index.d.ts +1 -0
- package/dist/lib/index.js +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/dist/controllers/core-crud.controller.d.ts +0 -18
- package/dist/controllers/core-crud.controller.js +0 -71
- package/dist/controllers/index.d.ts +0 -1
- package/dist/interfaces/crud-service.option.d.ts +0 -7
- package/dist/interfaces/crud-service.option.js +0 -9
- package/dist/services/core-crud.service.d.ts +0 -35
- package/dist/services/index.d.ts +0 -1
- package/dist/services/index.js +0 -17
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import 'reflect-metadata';
|
|
2
2
|
export * from './constants';
|
|
3
|
-
export * from './controllers';
|
|
4
3
|
export * from './decorators';
|
|
5
4
|
export * from './dto';
|
|
6
5
|
export * from './entities';
|
|
@@ -10,6 +9,5 @@ export * from './helpers';
|
|
|
10
9
|
export * from './interceptors';
|
|
11
10
|
export * from './interfaces';
|
|
12
11
|
export * from './lib';
|
|
13
|
-
export * from './services';
|
|
14
12
|
export * from './strategy';
|
|
15
13
|
export * from './validators';
|
package/dist/index.js
CHANGED
|
@@ -16,7 +16,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
require("reflect-metadata");
|
|
18
18
|
__exportStar(require("./constants"), exports);
|
|
19
|
-
__exportStar(require("./controllers"), exports);
|
|
20
19
|
__exportStar(require("./decorators"), exports);
|
|
21
20
|
__exportStar(require("./dto"), exports);
|
|
22
21
|
__exportStar(require("./entities"), exports);
|
|
@@ -26,6 +25,5 @@ __exportStar(require("./helpers"), exports);
|
|
|
26
25
|
__exportStar(require("./interceptors"), exports);
|
|
27
26
|
__exportStar(require("./interfaces"), exports);
|
|
28
27
|
__exportStar(require("./lib"), exports);
|
|
29
|
-
__exportStar(require("./services"), exports);
|
|
30
28
|
__exportStar(require("./strategy"), exports);
|
|
31
29
|
__exportStar(require("./validators"), exports);
|
package/dist/interfaces/index.js
CHANGED
|
@@ -14,7 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./crud-service.option"), exports);
|
|
18
17
|
__exportStar(require("./custom-validation-arguments.interface"), exports);
|
|
19
18
|
__exportStar(require("./get-agent.interface"), exports);
|
|
20
19
|
__exportStar(require("./get-user.interface"), exports);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { PaginateConfig } from 'nestjs-paginate/lib/paginate';
|
|
2
|
+
import { CoreCrudService } from './core-crud.service';
|
|
3
|
+
import { CoreBaseServiceOption, CoreFindAllWithPaginationServiceOption, CoreFindOneByIdServiceOption, CoreUpdateServiceOption } from './crud-service-options.interface';
|
|
4
|
+
import { BaseModelEntity } from '../../entities';
|
|
5
|
+
import { BaseResponse, IdDto, SuccessResponse } from '../../dto';
|
|
6
|
+
import { PaginationQueryCustom } from '../../interfaces';
|
|
7
|
+
export declare class CoreCrudController<T extends BaseModelEntity, CreateDto, UpdateDto, ResponseDto extends BaseResponse> {
|
|
8
|
+
protected readonly coreService: CoreCrudService<T, CreateDto, UpdateDto>;
|
|
9
|
+
protected readonly responseDto: {
|
|
10
|
+
new (init?: Partial<BaseResponse>): ResponseDto;
|
|
11
|
+
};
|
|
12
|
+
static responseDto: {
|
|
13
|
+
new (init?: Partial<BaseResponse>): any;
|
|
14
|
+
};
|
|
15
|
+
protected constructor(coreService: CoreCrudService<T, CreateDto, UpdateDto>, responseDto: {
|
|
16
|
+
new (init?: Partial<BaseResponse>): ResponseDto;
|
|
17
|
+
});
|
|
18
|
+
findAllWithPagination(query: PaginationQueryCustom, paginateConfig: PaginateConfig<T>, options?: CoreFindAllWithPaginationServiceOption<T>, ...args: any[]): Promise<SuccessResponse<ResponseDto[]>>;
|
|
19
|
+
findOneById(id: number, options?: CoreFindOneByIdServiceOption<T>, ...args: any[]): Promise<SuccessResponse<ResponseDto>>;
|
|
20
|
+
create(createDto: CreateDto, options?: CoreBaseServiceOption, ...args: any[]): Promise<SuccessResponse<ResponseDto>>;
|
|
21
|
+
update({ id }: IdDto, updateDto: UpdateDto, options?: CoreUpdateServiceOption<T>, ...args: any[]): Promise<SuccessResponse<ResponseDto>>;
|
|
22
|
+
deleteById({ id }: IdDto, options?: CoreBaseServiceOption, ...args: any[]): Promise<SuccessResponse<void>>;
|
|
23
|
+
softDeleteById({ id }: IdDto, options?: CoreBaseServiceOption, ...args: any[]): Promise<SuccessResponse<void>>;
|
|
24
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CoreCrudController = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
const dto_1 = require("../../dto");
|
|
6
|
+
const enums_1 = require("../../enums");
|
|
7
|
+
class CoreCrudController {
|
|
8
|
+
constructor(coreService, responseDto) {
|
|
9
|
+
this.coreService = coreService;
|
|
10
|
+
this.responseDto = responseDto;
|
|
11
|
+
CoreCrudController.responseDto = responseDto;
|
|
12
|
+
}
|
|
13
|
+
async findAllWithPagination(query, paginateConfig, options, ...args) {
|
|
14
|
+
const response = await this.coreService.findAllWithPagination(query, paginateConfig, options);
|
|
15
|
+
if (!response)
|
|
16
|
+
throw new common_1.NotFoundException(enums_1.SharedMessages.FETCH_FAILED);
|
|
17
|
+
const serializedData = response.data.map((item) => new this.responseDto(item));
|
|
18
|
+
return new dto_1.SuccessResponse(serializedData, enums_1.SharedMessages.SUCCESSFUL, common_1.HttpStatus.OK, { ...response.meta, links: response.links });
|
|
19
|
+
}
|
|
20
|
+
async findOneById(id, options, ...args) {
|
|
21
|
+
const response = await this.coreService.findOneById(id, options);
|
|
22
|
+
if (!response)
|
|
23
|
+
throw new common_1.NotFoundException(enums_1.SharedMessages.RESOURCE_NOT_FOUND);
|
|
24
|
+
return new dto_1.SuccessResponse(new this.responseDto(response), enums_1.SharedMessages.SUCCESSFUL, common_1.HttpStatus.OK);
|
|
25
|
+
}
|
|
26
|
+
async create(createDto, options, ...args) {
|
|
27
|
+
const response = await this.coreService.create(createDto, options);
|
|
28
|
+
if (!response)
|
|
29
|
+
throw new common_1.BadRequestException(enums_1.SharedMessages.CREATE_FAILED);
|
|
30
|
+
return new dto_1.SuccessResponse(new this.responseDto(response), enums_1.SharedMessages.SUCCESSFUL, common_1.HttpStatus.CREATED);
|
|
31
|
+
}
|
|
32
|
+
async update({ id }, updateDto, options, ...args) {
|
|
33
|
+
const response = await this.coreService.update(id, updateDto, options);
|
|
34
|
+
if (!response)
|
|
35
|
+
throw new common_1.BadRequestException(enums_1.SharedMessages.UPDATE_FAILED);
|
|
36
|
+
const foundItem = await this.coreService.findOneById(id, options);
|
|
37
|
+
return new dto_1.SuccessResponse(new this.responseDto(foundItem), enums_1.SharedMessages.SUCCESSFUL, common_1.HttpStatus.OK);
|
|
38
|
+
}
|
|
39
|
+
async deleteById({ id }, options, ...args) {
|
|
40
|
+
const result = await this.coreService.deleteById(id, options);
|
|
41
|
+
if (!result)
|
|
42
|
+
throw new common_1.BadRequestException(enums_1.SharedMessages.DELETE_FAILED);
|
|
43
|
+
return new dto_1.SuccessResponse(undefined, enums_1.SharedMessages.SUCCESSFUL, common_1.HttpStatus.OK);
|
|
44
|
+
}
|
|
45
|
+
async softDeleteById({ id }, options, ...args) {
|
|
46
|
+
const result = await this.coreService.softDeleteById(id, options);
|
|
47
|
+
if (!result)
|
|
48
|
+
throw new common_1.BadRequestException(enums_1.SharedMessages.DELETE_FAILED);
|
|
49
|
+
return new dto_1.SuccessResponse(undefined, enums_1.SharedMessages.SUCCESSFUL, common_1.HttpStatus.OK);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
exports.CoreCrudController = CoreCrudController;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { DeepPartial, DeleteResult, FindManyOptions, FindOptionsWhere, Repository, UpdateResult } from 'typeorm';
|
|
2
|
+
import { QueryDeepPartialEntity } from 'typeorm/query-builder/QueryPartialEntity';
|
|
3
|
+
import { Paginated } from 'nestjs-paginate';
|
|
4
|
+
import { PaginateConfig } from 'nestjs-paginate/lib/paginate';
|
|
5
|
+
import { InsertResult } from 'typeorm/query-builder/result/InsertResult';
|
|
6
|
+
import { UpsertOptions } from 'typeorm/repository/UpsertOptions';
|
|
7
|
+
import { CoreBaseServiceOption, CoreFindAllServiceOption, CoreFindAllWithPaginationServiceOption, CoreFindOneByIdServiceOption, CoreFindOneServiceOption, CoreUpdateServiceOption } from './crud-service-options.interface';
|
|
8
|
+
import { BaseModelEntity } from '../../entities';
|
|
9
|
+
import { PaginationQueryCustom } from '../../interfaces';
|
|
10
|
+
export declare abstract class CoreCrudService<T extends BaseModelEntity, CreateDto, UpdateDto> {
|
|
11
|
+
protected readonly repository: Repository<T>;
|
|
12
|
+
private readonly relationsPath;
|
|
13
|
+
protected constructor(repository: Repository<T>);
|
|
14
|
+
create(createDto: CreateDto, options?: CoreBaseServiceOption): Promise<T>;
|
|
15
|
+
update(id: number, updateDto: Partial<UpdateDto> | DeepPartial<T> | QueryDeepPartialEntity<T>, options?: CoreUpdateServiceOption<T>): Promise<UpdateResult>;
|
|
16
|
+
updateMany(query: FindManyOptions<T>, updateDto: Partial<UpdateDto> | DeepPartial<T> | QueryDeepPartialEntity<T>, options?: CoreUpdateServiceOption<T>): Promise<void>;
|
|
17
|
+
upsert(upsertDto: QueryDeepPartialEntity<T>, upsertOptions: UpsertOptions<T>, options?: CoreBaseServiceOption): Promise<InsertResult>;
|
|
18
|
+
findAllWithPagination(query: PaginationQueryCustom, paginateConfig: PaginateConfig<T>, options?: CoreFindAllWithPaginationServiceOption<T>): Promise<Paginated<T>>;
|
|
19
|
+
findAll(query: FindManyOptions<T>, options?: CoreFindAllServiceOption): Promise<T[]>;
|
|
20
|
+
findOneById(id: number, options?: CoreFindOneByIdServiceOption<T>): Promise<T>;
|
|
21
|
+
findOne(query: FindManyOptions<T>, options?: CoreFindOneServiceOption): Promise<T>;
|
|
22
|
+
isExists(query: FindManyOptions<T>, options?: CoreBaseServiceOption): Promise<boolean>;
|
|
23
|
+
count(query: FindManyOptions<T>, options?: CoreBaseServiceOption): Promise<number>;
|
|
24
|
+
softDelete(query: FindOptionsWhere<T>, options?: CoreBaseServiceOption): Promise<UpdateResult>;
|
|
25
|
+
softDeleteById(id: string | string[] | number | number[], options?: CoreBaseServiceOption): Promise<UpdateResult>;
|
|
26
|
+
restore(query: FindOptionsWhere<T>, options?: CoreBaseServiceOption): Promise<UpdateResult>;
|
|
27
|
+
restoreById(id: string | string[] | number | number[], options?: CoreBaseServiceOption): Promise<UpdateResult>;
|
|
28
|
+
delete(query: FindOptionsWhere<T>, options?: CoreBaseServiceOption): Promise<DeleteResult>;
|
|
29
|
+
deleteById(id: string | string[] | number | number[], options?: CoreBaseServiceOption): Promise<DeleteResult>;
|
|
30
|
+
private relatedPropertyTransformer;
|
|
31
|
+
private whereQueryTransformer;
|
|
32
|
+
private isObject;
|
|
33
|
+
private isArray;
|
|
34
|
+
private isArrayOfObjectId;
|
|
35
|
+
private isArrayOfNumbers;
|
|
36
|
+
}
|
|
@@ -4,8 +4,8 @@ exports.CoreCrudService = void 0;
|
|
|
4
4
|
const typeorm_1 = require("typeorm");
|
|
5
5
|
const common_1 = require("@nestjs/common");
|
|
6
6
|
const nestjs_paginate_1 = require("nestjs-paginate");
|
|
7
|
-
const
|
|
8
|
-
const
|
|
7
|
+
const helpers_1 = require("../../helpers");
|
|
8
|
+
const enums_1 = require("../../enums");
|
|
9
9
|
class CoreCrudService {
|
|
10
10
|
constructor(repository) {
|
|
11
11
|
this.repository = repository;
|
|
@@ -22,10 +22,104 @@ class CoreCrudService {
|
|
|
22
22
|
return await this.repository.save(entity);
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
|
+
async update(id, updateDto, options) {
|
|
26
|
+
updateDto = this.relatedPropertyTransformer(updateDto);
|
|
27
|
+
if (!options?.entityManager) {
|
|
28
|
+
const selectFields = Object.keys(updateDto);
|
|
29
|
+
const fetchedItem = await this.repository.findOne({
|
|
30
|
+
where: { id },
|
|
31
|
+
select: ['id', ...selectFields],
|
|
32
|
+
relations: options?.relations
|
|
33
|
+
? options?.relations
|
|
34
|
+
: options?.enableFirstLevelRelation
|
|
35
|
+
? this.relationsPath
|
|
36
|
+
: undefined,
|
|
37
|
+
});
|
|
38
|
+
if (!fetchedItem)
|
|
39
|
+
throw new common_1.NotFoundException(helpers_1.MessageFormatter.replace(enums_1.SharedMessages.RESOURCE_NOT_FOUND, `${helpers_1.ConvertStringCaseHelper.toKebabCase(this.repository.metadata?.tableName)}`));
|
|
40
|
+
const merged = this.repository.merge(fetchedItem, updateDto);
|
|
41
|
+
await this.repository.save(merged);
|
|
42
|
+
return {
|
|
43
|
+
generatedMaps: [],
|
|
44
|
+
raw: merged,
|
|
45
|
+
affected: 1,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
const selectFields = Object.keys(updateDto);
|
|
50
|
+
const fetchedItem = await options?.entityManager.findOne(this.repository.target, {
|
|
51
|
+
where: { id },
|
|
52
|
+
select: ['id', ...selectFields],
|
|
53
|
+
relations: options?.relations
|
|
54
|
+
? options?.relations
|
|
55
|
+
: options?.enableFirstLevelRelation
|
|
56
|
+
? this.relationsPath
|
|
57
|
+
: undefined,
|
|
58
|
+
});
|
|
59
|
+
if (!fetchedItem)
|
|
60
|
+
throw new common_1.NotFoundException(helpers_1.MessageFormatter.replace(enums_1.SharedMessages.RESOURCE_NOT_FOUND, `${helpers_1.ConvertStringCaseHelper.toKebabCase(this.repository.metadata?.tableName)}`));
|
|
61
|
+
const merged = options?.entityManager.merge(this.repository.target, fetchedItem, updateDto);
|
|
62
|
+
await options?.entityManager.save(merged);
|
|
63
|
+
return {
|
|
64
|
+
generatedMaps: [],
|
|
65
|
+
raw: merged,
|
|
66
|
+
affected: 1,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
async updateMany(query, updateDto, options) {
|
|
71
|
+
const foundEntities = await this.findAll(query, options);
|
|
72
|
+
for await (const entity of foundEntities) {
|
|
73
|
+
await this.update(entity.id, updateDto, options).catch((err) => console.log(err));
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
async upsert(upsertDto, upsertOptions, options) {
|
|
77
|
+
try {
|
|
78
|
+
upsertDto = this.relatedPropertyTransformer(upsertDto);
|
|
79
|
+
let conflictPaths = [];
|
|
80
|
+
let findWhereQuery = {};
|
|
81
|
+
if (this.isArray(upsertOptions.conflictPaths)) {
|
|
82
|
+
conflictPaths = upsertOptions.conflictPaths ?? [];
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
conflictPaths = Object.keys(upsertOptions.conflictPaths);
|
|
86
|
+
}
|
|
87
|
+
conflictPaths.map((path) => {
|
|
88
|
+
findWhereQuery = {
|
|
89
|
+
...findWhereQuery,
|
|
90
|
+
[path]: upsertDto[path],
|
|
91
|
+
};
|
|
92
|
+
});
|
|
93
|
+
const formatedQuery = this.whereQueryTransformer(findWhereQuery) ?? undefined;
|
|
94
|
+
if (!options?.entityManager) {
|
|
95
|
+
const existEntity = await this.repository.findOne({
|
|
96
|
+
where: formatedQuery,
|
|
97
|
+
select: ['id'],
|
|
98
|
+
});
|
|
99
|
+
return await this.repository.upsert({ id: existEntity.id, ...upsertDto }, upsertOptions);
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
const existEntity = await options?.entityManager.findOne(this.repository.target, {
|
|
103
|
+
where: formatedQuery,
|
|
104
|
+
select: ['id'],
|
|
105
|
+
});
|
|
106
|
+
return await options?.entityManager.upsert(this.repository.target, { id: existEntity.id, ...upsertDto }, upsertOptions);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
catch (error) {
|
|
110
|
+
throw new common_1.BadRequestException(helpers_1.MessageFormatter.replace(enums_1.SharedMessages.UPSERT_FAILED, error));
|
|
111
|
+
}
|
|
112
|
+
}
|
|
25
113
|
async findAllWithPagination(query, paginateConfig, options) {
|
|
26
|
-
return await (0, nestjs_paginate_1.paginate)(query,
|
|
114
|
+
return await (0, nestjs_paginate_1.paginate)(query, options?.selectQueryBuilder
|
|
115
|
+
? options.selectQueryBuilder
|
|
116
|
+
: this.repository, {
|
|
27
117
|
...paginateConfig,
|
|
28
|
-
relations: options?.relations
|
|
118
|
+
relations: options?.relations
|
|
119
|
+
? options?.relations
|
|
120
|
+
: options?.enableFirstLevelRelation
|
|
121
|
+
? this.relationsPath
|
|
122
|
+
: undefined,
|
|
29
123
|
});
|
|
30
124
|
}
|
|
31
125
|
async findAll(query, options) {
|
|
@@ -33,15 +127,19 @@ class CoreCrudService {
|
|
|
33
127
|
query.where = this.whereQueryTransformer(query.where) ?? undefined;
|
|
34
128
|
if (!options?.entityManager)
|
|
35
129
|
result = await this.repository.find({
|
|
36
|
-
relations: options?.
|
|
130
|
+
relations: options?.enableFirstLevelRelation
|
|
131
|
+
? this.relationsPath
|
|
132
|
+
: undefined,
|
|
37
133
|
...query,
|
|
38
134
|
});
|
|
39
135
|
else
|
|
40
136
|
result = await options.entityManager.find(this.repository.target, {
|
|
41
|
-
relations: options?.
|
|
137
|
+
relations: options?.enableFirstLevelRelation
|
|
138
|
+
? this.relationsPath
|
|
139
|
+
: undefined,
|
|
42
140
|
...query,
|
|
43
141
|
});
|
|
44
|
-
if (options?.existsCheck && !result) {
|
|
142
|
+
if (options?.existsCheck && !result.length) {
|
|
45
143
|
throw new common_1.NotFoundException(helpers_1.MessageFormatter.replace(enums_1.SharedMessages.RESOURCE_NOT_FOUND, `${helpers_1.ConvertStringCaseHelper.toKebabCase(this.repository.metadata?.tableName)}`));
|
|
46
144
|
}
|
|
47
145
|
return result;
|
|
@@ -53,7 +151,11 @@ class CoreCrudService {
|
|
|
53
151
|
where: {
|
|
54
152
|
id,
|
|
55
153
|
},
|
|
56
|
-
relations: options?.relations
|
|
154
|
+
relations: options?.relations
|
|
155
|
+
? options?.relations
|
|
156
|
+
: options?.enableFirstLevelRelation
|
|
157
|
+
? this.relationsPath
|
|
158
|
+
: undefined,
|
|
57
159
|
});
|
|
58
160
|
}
|
|
59
161
|
else {
|
|
@@ -61,7 +163,11 @@ class CoreCrudService {
|
|
|
61
163
|
where: {
|
|
62
164
|
id,
|
|
63
165
|
},
|
|
64
|
-
relations: options?.relations
|
|
166
|
+
relations: options?.relations
|
|
167
|
+
? options?.relations
|
|
168
|
+
: options?.enableFirstLevelRelation
|
|
169
|
+
? this.relationsPath
|
|
170
|
+
: undefined,
|
|
65
171
|
});
|
|
66
172
|
}
|
|
67
173
|
if (options?.existsCheck && !result) {
|
|
@@ -74,13 +180,17 @@ class CoreCrudService {
|
|
|
74
180
|
query.where = this.whereQueryTransformer(query.where) ?? undefined;
|
|
75
181
|
if (options?.entityManager) {
|
|
76
182
|
result = await options?.entityManager.findOne(this.repository.target, {
|
|
77
|
-
relations: options?.
|
|
183
|
+
relations: options?.enableFirstLevelRelation
|
|
184
|
+
? this.relationsPath
|
|
185
|
+
: undefined,
|
|
78
186
|
...query,
|
|
79
187
|
});
|
|
80
188
|
}
|
|
81
189
|
else {
|
|
82
190
|
result = await this.repository.findOne({
|
|
83
|
-
relations: options?.
|
|
191
|
+
relations: options?.enableFirstLevelRelation
|
|
192
|
+
? this.relationsPath
|
|
193
|
+
: undefined,
|
|
84
194
|
...query,
|
|
85
195
|
});
|
|
86
196
|
}
|
|
@@ -93,16 +203,10 @@ class CoreCrudService {
|
|
|
93
203
|
let result;
|
|
94
204
|
query.where = this.whereQueryTransformer(query.where) ?? undefined;
|
|
95
205
|
if (options?.entityManager) {
|
|
96
|
-
result = await options?.entityManager.exists(this.repository.target,
|
|
97
|
-
relations: options?.relations ? options?.relations : this.relationsPath,
|
|
98
|
-
...query,
|
|
99
|
-
});
|
|
206
|
+
result = await options?.entityManager.exists(this.repository.target, query);
|
|
100
207
|
}
|
|
101
208
|
else {
|
|
102
|
-
result = await this.repository.exists(
|
|
103
|
-
relations: options?.relations ? options?.relations : this.relationsPath,
|
|
104
|
-
...query,
|
|
105
|
-
});
|
|
209
|
+
result = await this.repository.exists(query);
|
|
106
210
|
}
|
|
107
211
|
return result;
|
|
108
212
|
}
|
|
@@ -110,95 +214,13 @@ class CoreCrudService {
|
|
|
110
214
|
let result;
|
|
111
215
|
query.where = this.whereQueryTransformer(query.where) ?? undefined;
|
|
112
216
|
if (options?.entityManager) {
|
|
113
|
-
result = await options?.entityManager.count(this.repository.target,
|
|
114
|
-
relations: options?.relations ? options?.relations : this.relationsPath,
|
|
115
|
-
...query,
|
|
116
|
-
});
|
|
217
|
+
result = await options?.entityManager.count(this.repository.target, query);
|
|
117
218
|
}
|
|
118
219
|
else {
|
|
119
|
-
result = await this.repository.count(
|
|
120
|
-
relations: options?.relations ? options?.relations : this.relationsPath,
|
|
121
|
-
...query,
|
|
122
|
-
});
|
|
220
|
+
result = await this.repository.count(query);
|
|
123
221
|
}
|
|
124
222
|
return result;
|
|
125
223
|
}
|
|
126
|
-
async upsert(upsertDto, upsertOptions, options) {
|
|
127
|
-
try {
|
|
128
|
-
upsertDto = this.relatedPropertyTransformer(upsertDto);
|
|
129
|
-
let conflictPaths = [];
|
|
130
|
-
let findWhereQuery = {};
|
|
131
|
-
if (this.isArray(upsertOptions.conflictPaths)) {
|
|
132
|
-
conflictPaths = upsertOptions.conflictPaths ?? [];
|
|
133
|
-
}
|
|
134
|
-
else {
|
|
135
|
-
conflictPaths = Object.keys(upsertOptions.conflictPaths);
|
|
136
|
-
}
|
|
137
|
-
conflictPaths.map((path) => {
|
|
138
|
-
findWhereQuery = {
|
|
139
|
-
...findWhereQuery,
|
|
140
|
-
[path]: upsertDto[path],
|
|
141
|
-
};
|
|
142
|
-
});
|
|
143
|
-
const formatedQuery = this.whereQueryTransformer(findWhereQuery) ?? undefined;
|
|
144
|
-
if (!options?.entityManager) {
|
|
145
|
-
const existEntity = await this.repository.findOne({
|
|
146
|
-
where: formatedQuery,
|
|
147
|
-
select: ['id'],
|
|
148
|
-
});
|
|
149
|
-
return await this.repository.upsert({ id: existEntity.id, ...upsertDto }, upsertOptions);
|
|
150
|
-
}
|
|
151
|
-
else {
|
|
152
|
-
const existEntity = await options?.entityManager.findOne(this.repository.target, {
|
|
153
|
-
where: formatedQuery,
|
|
154
|
-
select: ['id'],
|
|
155
|
-
});
|
|
156
|
-
return await options?.entityManager.upsert(this.repository.target, { id: existEntity.id, ...upsertDto }, upsertOptions);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
catch (error) {
|
|
160
|
-
throw new common_1.BadRequestException(helpers_1.MessageFormatter.replace(enums_1.SharedMessages.UPSERT_FAILED, error));
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
async update(id, updateDto, options) {
|
|
164
|
-
updateDto = this.relatedPropertyTransformer(updateDto);
|
|
165
|
-
if (!options?.entityManager) {
|
|
166
|
-
const selectFields = Object.keys(updateDto);
|
|
167
|
-
const fetchedItem = await this.repository.findOne({
|
|
168
|
-
where: { id },
|
|
169
|
-
select: ['id', ...selectFields],
|
|
170
|
-
relations: options?.relations ? options?.relations : this.relationsPath,
|
|
171
|
-
});
|
|
172
|
-
if (!fetchedItem)
|
|
173
|
-
throw new common_1.NotFoundException(helpers_1.MessageFormatter.replace(enums_1.SharedMessages.RESOURCE_NOT_FOUND, `${helpers_1.ConvertStringCaseHelper.toKebabCase(this.repository.metadata?.tableName)}`));
|
|
174
|
-
const merged = this.repository.merge(fetchedItem, updateDto);
|
|
175
|
-
await this.repository.save(merged);
|
|
176
|
-
return {
|
|
177
|
-
generatedMaps: [],
|
|
178
|
-
raw: merged,
|
|
179
|
-
affected: 1,
|
|
180
|
-
};
|
|
181
|
-
}
|
|
182
|
-
else {
|
|
183
|
-
const selectFields = Object.keys(updateDto);
|
|
184
|
-
const fetchedItem = await options?.entityManager.findOne(this.repository.target, {
|
|
185
|
-
where: { id },
|
|
186
|
-
select: ['id', ...selectFields],
|
|
187
|
-
relations: options?.relations
|
|
188
|
-
? options?.relations
|
|
189
|
-
: this.relationsPath,
|
|
190
|
-
});
|
|
191
|
-
if (!fetchedItem)
|
|
192
|
-
throw new common_1.NotFoundException(helpers_1.MessageFormatter.replace(enums_1.SharedMessages.RESOURCE_NOT_FOUND, `${helpers_1.ConvertStringCaseHelper.toKebabCase(this.repository.metadata?.tableName)}`));
|
|
193
|
-
const merged = options?.entityManager.merge(this.repository.target, fetchedItem, updateDto);
|
|
194
|
-
await options?.entityManager.save(merged);
|
|
195
|
-
return {
|
|
196
|
-
generatedMaps: [],
|
|
197
|
-
raw: merged,
|
|
198
|
-
affected: 1,
|
|
199
|
-
};
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
224
|
async softDelete(query, options) {
|
|
203
225
|
const formatedQuery = this.whereQueryTransformer(query) ?? undefined;
|
|
204
226
|
if (options?.entityManager) {
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { EntityManager, SelectQueryBuilder } from 'typeorm';
|
|
2
|
+
import { FindOptionsRelations } from 'typeorm/find-options/FindOptionsRelations';
|
|
3
|
+
export interface CoreFindOneServiceOption extends CoreBaseServiceOption {
|
|
4
|
+
enableFirstLevelRelation?: boolean;
|
|
5
|
+
existsCheck?: boolean;
|
|
6
|
+
}
|
|
7
|
+
export interface CoreFindOneByIdServiceOption<T> extends CoreBaseServiceOption {
|
|
8
|
+
enableFirstLevelRelation?: boolean;
|
|
9
|
+
relations?: FindOptionsRelations<T>;
|
|
10
|
+
existsCheck?: boolean;
|
|
11
|
+
}
|
|
12
|
+
export interface CoreFindAllServiceOption extends CoreBaseServiceOption {
|
|
13
|
+
enableFirstLevelRelation?: boolean;
|
|
14
|
+
existsCheck?: boolean;
|
|
15
|
+
}
|
|
16
|
+
export interface CoreFindAllWithPaginationServiceOption<T> {
|
|
17
|
+
selectQueryBuilder?: SelectQueryBuilder<T>;
|
|
18
|
+
relations?: FindOptionsRelations<T>;
|
|
19
|
+
enableFirstLevelRelation?: boolean;
|
|
20
|
+
}
|
|
21
|
+
export interface CoreUpdateServiceOption<T> extends CoreBaseServiceOption {
|
|
22
|
+
relations?: FindOptionsRelations<T>;
|
|
23
|
+
enableFirstLevelRelation?: boolean;
|
|
24
|
+
}
|
|
25
|
+
export interface CoreBaseServiceOption {
|
|
26
|
+
entityManager?: EntityManager;
|
|
27
|
+
}
|
|
@@ -15,3 +15,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./core-crud.controller"), exports);
|
|
18
|
+
__exportStar(require("./core-crud.service"), exports);
|
|
19
|
+
__exportStar(require("./crud-service-options.interface"), exports);
|
package/dist/lib/index.d.ts
CHANGED
package/dist/lib/index.js
CHANGED