@flusys/nestjs-shared 1.1.0 → 1.2.0-rc.1
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/README.md +139 -1103
- package/cjs/classes/api-controller.class.js +320 -29
- package/cjs/classes/api-service.class.js +219 -64
- package/cjs/classes/handle-custome-db.js +37 -0
- package/cjs/classes/index.js +1 -2
- package/cjs/classes/winston.logger.class.js +103 -70
- package/cjs/constants/index.js +1 -4
- package/cjs/constants/message-keys.js +68 -0
- package/cjs/constants/permissions.js +114 -11
- package/cjs/decorators/api-response.decorator.js +1 -2
- package/cjs/decorators/index.js +1 -1
- package/cjs/decorators/log-action.decorator.js +149 -0
- package/cjs/decorators/require-permission.decorator.js +5 -5
- package/cjs/dtos/get-by-ids.dto.js +65 -0
- package/cjs/dtos/identity-response.dto.js +4 -8
- package/cjs/dtos/index.js +1 -0
- package/cjs/dtos/response-payload.dto.js +72 -0
- package/cjs/entities/identity.js +8 -2
- package/cjs/entities/index.js +1 -0
- package/cjs/entities/raw-type.js +4 -0
- package/cjs/entities/user-root.js +2 -1
- package/cjs/enums/index.js +20 -0
- package/cjs/enums/notification-type.enum.js +17 -0
- package/cjs/enums/participant-status.enum.js +17 -0
- package/cjs/enums/recurrence-type.enum.js +18 -0
- package/cjs/exceptions/base-app.exception.js +151 -0
- package/cjs/exceptions/index.js +1 -0
- package/cjs/exceptions/permission.exception.js +13 -9
- package/cjs/filters/global-exception.filter.js +279 -0
- package/cjs/filters/index.js +18 -0
- package/cjs/guards/jwt-auth.guard.js +4 -1
- package/cjs/guards/permission.guard.js +71 -77
- package/cjs/index.js +2 -0
- package/cjs/interceptors/idempotency.interceptor.js +1 -1
- package/cjs/interceptors/index.js +0 -1
- package/cjs/interfaces/event-manager-adapter.interface.js +11 -0
- package/cjs/interfaces/index.js +2 -1
- package/cjs/interfaces/notification-adapter.interface.js +11 -0
- package/cjs/interfaces/permission.interface.js +1 -1
- package/cjs/middlewares/logger.middleware.js +83 -26
- package/cjs/modules/datasource/index.js +0 -1
- package/cjs/modules/datasource/multi-tenant-datasource.service.js +46 -85
- package/cjs/modules/utils/utils.service.js +4 -20
- package/cjs/utils/date-time.util.js +94 -0
- package/cjs/utils/index.js +1 -1
- package/cjs/utils/query-helpers.util.js +8 -1
- package/cjs/utils/request.util.js +25 -0
- package/classes/api-controller.class.d.ts +14 -6
- package/classes/api-service.class.d.ts +25 -22
- package/classes/handle-custome-db.d.ts +3 -0
- package/classes/index.d.ts +1 -2
- package/classes/winston.logger.class.d.ts +1 -0
- package/constants/index.d.ts +1 -1
- package/constants/message-keys.d.ts +45 -0
- package/constants/permissions.d.ts +127 -0
- package/decorators/api-response.decorator.d.ts +1 -1
- package/decorators/current-user.decorator.d.ts +1 -1
- package/decorators/index.d.ts +1 -1
- package/decorators/log-action.decorator.d.ts +8 -0
- package/decorators/require-permission.decorator.d.ts +2 -2
- package/dtos/get-by-ids.dto.d.ts +4 -0
- package/dtos/index.d.ts +1 -0
- package/dtos/response-payload.dto.d.ts +8 -0
- package/entities/index.d.ts +1 -0
- package/entities/raw-type.d.ts +1 -0
- package/enums/index.d.ts +3 -0
- package/enums/notification-type.enum.d.ts +6 -0
- package/enums/participant-status.enum.d.ts +6 -0
- package/enums/recurrence-type.enum.d.ts +7 -0
- package/exceptions/base-app.exception.d.ts +41 -0
- package/exceptions/index.d.ts +1 -0
- package/exceptions/permission.exception.d.ts +2 -2
- package/fesm/classes/api-controller.class.js +321 -30
- package/fesm/classes/api-service.class.js +222 -67
- package/fesm/classes/handle-custome-db.js +19 -0
- package/fesm/classes/index.js +1 -2
- package/fesm/classes/winston.logger.class.js +100 -68
- package/fesm/constants/index.js +2 -1
- package/fesm/constants/message-keys.js +49 -0
- package/fesm/constants/permissions.js +86 -14
- package/fesm/decorators/api-response.decorator.js +2 -20
- package/fesm/decorators/index.js +1 -1
- package/fesm/decorators/log-action.decorator.js +139 -0
- package/fesm/decorators/require-permission.decorator.js +17 -65
- package/fesm/dtos/get-by-ids.dto.js +55 -0
- package/fesm/dtos/identity-response.dto.js +4 -8
- package/fesm/dtos/index.js +1 -0
- package/fesm/dtos/response-payload.dto.js +72 -0
- package/fesm/entities/identity.js +8 -2
- package/fesm/entities/index.js +1 -0
- package/fesm/entities/raw-type.js +1 -0
- package/fesm/entities/user-root.js +2 -1
- package/fesm/enums/index.js +3 -0
- package/fesm/enums/notification-type.enum.js +7 -0
- package/fesm/enums/participant-status.enum.js +7 -0
- package/fesm/enums/recurrence-type.enum.js +8 -0
- package/fesm/exceptions/base-app.exception.js +115 -0
- package/fesm/exceptions/index.js +1 -0
- package/fesm/exceptions/permission.exception.js +16 -18
- package/fesm/filters/global-exception.filter.js +269 -0
- package/fesm/filters/index.js +1 -0
- package/fesm/guards/jwt-auth.guard.js +5 -2
- package/fesm/guards/permission.guard.js +73 -79
- package/fesm/index.js +2 -0
- package/fesm/interceptors/idempotency.interceptor.js +2 -2
- package/fesm/interceptors/index.js +0 -1
- package/fesm/interfaces/datasource.interface.js +1 -20
- package/fesm/interfaces/event-manager-adapter.interface.js +1 -0
- package/fesm/interfaces/index.js +2 -1
- package/fesm/interfaces/notification-adapter.interface.js +1 -0
- package/fesm/interfaces/permission.interface.js +1 -3
- package/fesm/middlewares/logger.middleware.js +83 -26
- package/fesm/modules/datasource/index.js +0 -1
- package/fesm/modules/datasource/multi-tenant-datasource.service.js +47 -86
- package/fesm/modules/utils/utils.service.js +5 -21
- package/fesm/utils/date-time.util.js +70 -0
- package/fesm/utils/index.js +1 -1
- package/fesm/utils/query-helpers.util.js +8 -1
- package/fesm/utils/request.util.js +22 -0
- package/filters/global-exception.filter.d.ts +9 -0
- package/filters/index.d.ts +1 -0
- package/guards/permission.guard.d.ts +3 -7
- package/index.d.ts +2 -0
- package/interceptors/idempotency.interceptor.d.ts +1 -1
- package/interceptors/index.d.ts +0 -1
- package/interfaces/api.interface.d.ts +2 -0
- package/interfaces/event-manager-adapter.interface.d.ts +43 -0
- package/interfaces/index.d.ts +2 -1
- package/interfaces/notification-adapter.interface.d.ts +21 -0
- package/interfaces/permission.interface.d.ts +13 -8
- package/modules/datasource/index.d.ts +0 -1
- package/modules/datasource/multi-tenant-datasource.service.d.ts +5 -15
- package/modules/utils/utils.service.d.ts +0 -1
- package/package.json +21 -14
- package/utils/date-time.util.d.ts +8 -0
- package/utils/index.d.ts +1 -1
- package/utils/request.util.d.ts +2 -0
- package/cjs/classes/request-scoped-api.service.js +0 -61
- package/cjs/classes/winston-logger-adapter.class.js +0 -104
- package/cjs/decorators/sanitize-html.decorator.js +0 -36
- package/cjs/interceptors/query-performance.interceptor.js +0 -66
- package/cjs/interfaces/logger.interface.js +0 -7
- package/cjs/modules/datasource/datasource.module.js +0 -117
- package/cjs/utils/error-handler.util.js +0 -90
- package/classes/request-scoped-api.service.d.ts +0 -14
- package/classes/winston-logger-adapter.class.d.ts +0 -22
- package/decorators/sanitize-html.decorator.d.ts +0 -2
- package/fesm/classes/request-scoped-api.service.js +0 -51
- package/fesm/classes/winston-logger-adapter.class.js +0 -107
- package/fesm/decorators/sanitize-html.decorator.js +0 -45
- package/fesm/interceptors/query-performance.interceptor.js +0 -56
- package/fesm/interfaces/logger.interface.js +0 -4
- package/fesm/modules/datasource/datasource.module.js +0 -107
- package/fesm/utils/error-handler.util.js +0 -82
- package/interceptors/query-performance.interceptor.d.ts +0 -8
- package/interfaces/logger.interface.d.ts +0 -7
- package/modules/datasource/datasource.module.d.ts +0 -19
- package/utils/error-handler.util.d.ts +0 -19
|
@@ -8,9 +8,14 @@ Object.defineProperty(exports, "ApiService", {
|
|
|
8
8
|
return ApiService;
|
|
9
9
|
}
|
|
10
10
|
});
|
|
11
|
-
const _errorhandlerutil = require("../utils/error-handler.util");
|
|
12
11
|
const _common = require("@nestjs/common");
|
|
13
12
|
const _typeorm = require("typeorm");
|
|
13
|
+
const _constants = require("../constants");
|
|
14
|
+
const _logactiondecorator = require("../decorators/log-action.decorator");
|
|
15
|
+
const _dtos = require("../dtos");
|
|
16
|
+
const _interfaces = require("../interfaces");
|
|
17
|
+
const _utilsservice = require("../modules/utils/utils.service");
|
|
18
|
+
const _hybridcacheclass = require("./hybrid-cache.class");
|
|
14
19
|
function _define_property(obj, key, value) {
|
|
15
20
|
if (key in obj) {
|
|
16
21
|
Object.defineProperty(obj, key, {
|
|
@@ -24,13 +29,47 @@ function _define_property(obj, key, value) {
|
|
|
24
29
|
}
|
|
25
30
|
return obj;
|
|
26
31
|
}
|
|
32
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
33
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
34
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
35
|
+
else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
36
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
37
|
+
}
|
|
38
|
+
function _ts_metadata(k, v) {
|
|
39
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
40
|
+
}
|
|
27
41
|
let ApiService = class ApiService {
|
|
42
|
+
resolveEntity() {
|
|
43
|
+
return this._entity;
|
|
44
|
+
}
|
|
45
|
+
getDataSourceProvider() {
|
|
46
|
+
return this._dataSourceProvider;
|
|
47
|
+
}
|
|
48
|
+
async ensureDataSourceRepository() {
|
|
49
|
+
if (!this.repositoryInitialized) {
|
|
50
|
+
const entity = this.resolveEntity();
|
|
51
|
+
const provider = this.getDataSourceProvider();
|
|
52
|
+
this.repository = await provider.getRepository(entity);
|
|
53
|
+
this.repositoryInitialized = true;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
async resolveRepositories(entities) {
|
|
57
|
+
const provider = this.getDataSourceProvider();
|
|
58
|
+
const repositories = [];
|
|
59
|
+
for (const entity of entities){
|
|
60
|
+
repositories.push(await provider.getRepository(entity));
|
|
61
|
+
}
|
|
62
|
+
return repositories;
|
|
63
|
+
}
|
|
64
|
+
async getDataSourceForService() {
|
|
65
|
+
return this.getDataSourceProvider().getDataSource();
|
|
66
|
+
}
|
|
28
67
|
async insert(dto, user) {
|
|
29
68
|
return this.executeInTransaction('insert', async (qr)=>{
|
|
30
69
|
await this.beforeInsertOperation(dto, user, qr);
|
|
31
70
|
const entities = await this.convertRequestDtoToEntity(dto, user);
|
|
32
71
|
const saved = await qr.manager.save(this.repository.target, entities);
|
|
33
|
-
await this.afterInsertOperation(this.ensureArray(saved), user, qr);
|
|
72
|
+
await this.afterInsertOperation(this.ensureArray(saved), dto, user, qr);
|
|
34
73
|
return {
|
|
35
74
|
saved,
|
|
36
75
|
returnFirst: true
|
|
@@ -42,7 +81,7 @@ let ApiService = class ApiService {
|
|
|
42
81
|
await this.beforeInsertOperation(dtos, user, qr);
|
|
43
82
|
const entities = await this.convertRequestDtoToEntity(dtos, user);
|
|
44
83
|
const saved = await qr.manager.save(this.repository.target, entities);
|
|
45
|
-
await this.afterInsertOperation(this.ensureArray(saved), user, qr);
|
|
84
|
+
await this.afterInsertOperation(this.ensureArray(saved), dtos, user, qr);
|
|
46
85
|
return {
|
|
47
86
|
saved,
|
|
48
87
|
returnFirst: false
|
|
@@ -54,7 +93,7 @@ let ApiService = class ApiService {
|
|
|
54
93
|
await this.beforeUpdateOperation(dto, user, qr);
|
|
55
94
|
const entities = await this.convertRequestDtoToEntity(dto, user);
|
|
56
95
|
const saved = await qr.manager.save(this.repository.target, entities);
|
|
57
|
-
await this.afterUpdateOperation(this.ensureArray(saved), user, qr);
|
|
96
|
+
await this.afterUpdateOperation(this.ensureArray(saved), dto, user, qr);
|
|
58
97
|
return {
|
|
59
98
|
saved,
|
|
60
99
|
returnFirst: true
|
|
@@ -66,21 +105,45 @@ let ApiService = class ApiService {
|
|
|
66
105
|
await this.beforeUpdateOperation(dtos, user, qr);
|
|
67
106
|
const entities = await this.convertRequestDtoToEntity(dtos, user);
|
|
68
107
|
const saved = await qr.manager.save(this.repository.target, entities);
|
|
69
|
-
await this.afterUpdateOperation(this.ensureArray(saved), user, qr);
|
|
108
|
+
await this.afterUpdateOperation(this.ensureArray(saved), dtos, user, qr);
|
|
70
109
|
return {
|
|
71
110
|
saved,
|
|
72
111
|
returnFirst: false
|
|
73
112
|
};
|
|
74
113
|
});
|
|
75
114
|
}
|
|
76
|
-
async
|
|
77
|
-
|
|
115
|
+
async bulkUpsert(toInsert, toUpdate, user) {
|
|
116
|
+
return this.executeInTransaction('bulkUpsert', async (qr)=>{
|
|
117
|
+
const allResults = [];
|
|
118
|
+
if (toInsert.length > 0) {
|
|
119
|
+
await this.beforeInsertOperation(toInsert, user, qr);
|
|
120
|
+
const insertedEntities = await this.convertRequestDtoToEntity(toInsert, user);
|
|
121
|
+
const insertedSaved = await qr.manager.save(this.repository.target, insertedEntities);
|
|
122
|
+
await this.afterInsertOperation(this.ensureArray(insertedSaved), toInsert, user, qr);
|
|
123
|
+
allResults.push(...this.ensureArray(insertedSaved));
|
|
124
|
+
}
|
|
125
|
+
if (toUpdate.length > 0) {
|
|
126
|
+
await this.beforeUpdateOperation(toUpdate, user, qr);
|
|
127
|
+
const updatedEntities = await this.convertRequestDtoToEntity(toUpdate, user);
|
|
128
|
+
const updatedSaved = await qr.manager.save(this.repository.target, updatedEntities);
|
|
129
|
+
await this.afterUpdateOperation(this.ensureArray(updatedSaved), toUpdate, user, qr);
|
|
130
|
+
allResults.push(...this.ensureArray(updatedSaved));
|
|
131
|
+
}
|
|
132
|
+
return {
|
|
133
|
+
saved: allResults,
|
|
134
|
+
returnFirst: false
|
|
135
|
+
};
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
async findByIds(ids, user, select) {
|
|
139
|
+
await this.ensureDataSourceRepository();
|
|
78
140
|
try {
|
|
79
141
|
const query = this.repository.createQueryBuilder(this.entityName);
|
|
80
142
|
query.where({
|
|
81
143
|
id: (0, _typeorm.In)(ids)
|
|
82
144
|
});
|
|
83
|
-
const
|
|
145
|
+
const selectArray = typeof select === 'string' ? select.split(',').map((s)=>s.trim()) : select;
|
|
146
|
+
const { query: finalQuery, isRaw } = await this.getSelectQuery(query, user, selectArray);
|
|
84
147
|
let output;
|
|
85
148
|
if (isRaw) {
|
|
86
149
|
output = await finalQuery.getRawMany();
|
|
@@ -88,7 +151,10 @@ let ApiService = class ApiService {
|
|
|
88
151
|
output = await finalQuery.getMany();
|
|
89
152
|
}
|
|
90
153
|
const result = this.convertEntityListToResponseListDto(output, isRaw);
|
|
91
|
-
if (!result || !result.length) throw new _common.NotFoundException(
|
|
154
|
+
if (!result || !result.length) throw new _common.NotFoundException({
|
|
155
|
+
message: 'No Data Found',
|
|
156
|
+
messageKey: _constants.ERROR_MESSAGES.NOT_FOUND
|
|
157
|
+
});
|
|
92
158
|
return result;
|
|
93
159
|
} catch (error) {
|
|
94
160
|
if (error instanceof _common.NotFoundException) {
|
|
@@ -98,7 +164,7 @@ let ApiService = class ApiService {
|
|
|
98
164
|
}
|
|
99
165
|
}
|
|
100
166
|
async findById(id, user, select) {
|
|
101
|
-
await this.
|
|
167
|
+
await this.ensureDataSourceRepository();
|
|
102
168
|
try {
|
|
103
169
|
const query = this.repository.createQueryBuilder(this.entityName);
|
|
104
170
|
query.where({
|
|
@@ -112,15 +178,17 @@ let ApiService = class ApiService {
|
|
|
112
178
|
result = await this.cacheManager.get(cacheKey);
|
|
113
179
|
}
|
|
114
180
|
if (!result) {
|
|
115
|
-
let output;
|
|
116
181
|
if (isRaw) {
|
|
117
|
-
|
|
118
|
-
const convertResult = this.convertEntityListToResponseListDto(
|
|
182
|
+
const rawOutput = await finalQuery.getRawMany();
|
|
183
|
+
const convertResult = this.convertEntityListToResponseListDto(rawOutput, isRaw);
|
|
119
184
|
if (convertResult.length) result = convertResult[0];
|
|
120
185
|
} else {
|
|
121
|
-
|
|
122
|
-
if (!
|
|
123
|
-
|
|
186
|
+
const singleOutput = await finalQuery.getOne();
|
|
187
|
+
if (!singleOutput) throw new _common.NotFoundException({
|
|
188
|
+
message: 'No Data Found',
|
|
189
|
+
messageKey: _constants.ERROR_MESSAGES.NOT_FOUND
|
|
190
|
+
});
|
|
191
|
+
result = this.convertEntityToResponseDto(singleOutput, false);
|
|
124
192
|
}
|
|
125
193
|
if (this.isCacheable) {
|
|
126
194
|
await this.cacheManager.set(cacheKey, result);
|
|
@@ -136,43 +204,34 @@ let ApiService = class ApiService {
|
|
|
136
204
|
}
|
|
137
205
|
}
|
|
138
206
|
async getAll(search, filterAndPaginationDto, user) {
|
|
139
|
-
await this.
|
|
207
|
+
await this.ensureDataSourceRepository();
|
|
140
208
|
try {
|
|
141
209
|
let data;
|
|
142
210
|
let total;
|
|
143
211
|
let isRaw = false;
|
|
144
212
|
let query = this.repository.createQueryBuilder(this.entityName);
|
|
145
|
-
// Apply filters
|
|
146
213
|
if (filterAndPaginationDto.filter) {
|
|
147
214
|
const { query: filterQuery, isRaw: filterIsRaw } = await this.getFilterQuery(query, filterAndPaginationDto.filter, user);
|
|
148
215
|
query = filterQuery;
|
|
149
216
|
isRaw = isRaw || filterIsRaw;
|
|
150
217
|
}
|
|
151
|
-
// Apply search
|
|
152
218
|
if (search) {
|
|
153
219
|
const { query: searchQuery, isRaw: searchIsRaw } = await this.getGlobalSearchQuery(query, search, user);
|
|
154
220
|
query = searchQuery;
|
|
155
221
|
isRaw = isRaw || searchIsRaw;
|
|
156
222
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
query = selectQuery;
|
|
161
|
-
isRaw = isRaw || selectIsRaw;
|
|
162
|
-
}
|
|
163
|
-
// Apply sort
|
|
223
|
+
const { query: selectQuery, isRaw: selectIsRaw } = await this.getSelectQuery(query, user, filterAndPaginationDto.select);
|
|
224
|
+
query = selectQuery;
|
|
225
|
+
isRaw = isRaw || selectIsRaw;
|
|
164
226
|
if (filterAndPaginationDto.sort) {
|
|
165
227
|
const { query: sortQuery, isRaw: sortIsRaw } = await this.getSortQuery(query, filterAndPaginationDto.sort, user);
|
|
166
228
|
query = sortQuery;
|
|
167
229
|
isRaw = isRaw || sortIsRaw;
|
|
168
230
|
}
|
|
169
|
-
// Include soft-deleted if requested
|
|
170
231
|
if (filterAndPaginationDto.withDeleted) query.withDeleted();
|
|
171
|
-
// Extra manipulation
|
|
172
232
|
const { query: extraQuery, isRaw: extraIsRaw } = await this.getExtraManipulateQuery(query, filterAndPaginationDto, user);
|
|
173
233
|
query = extraQuery;
|
|
174
234
|
isRaw = isRaw || extraIsRaw;
|
|
175
|
-
// Pagination
|
|
176
235
|
if (filterAndPaginationDto.pagination) {
|
|
177
236
|
const offset = filterAndPaginationDto.pagination.currentPage * filterAndPaginationDto.pagination.pageSize;
|
|
178
237
|
const limit = filterAndPaginationDto.pagination.pageSize;
|
|
@@ -182,7 +241,6 @@ let ApiService = class ApiService {
|
|
|
182
241
|
query.skip(offset).take(limit);
|
|
183
242
|
}
|
|
184
243
|
}
|
|
185
|
-
// Generate cache key
|
|
186
244
|
let cacheKey = '';
|
|
187
245
|
if (this.isCacheable) {
|
|
188
246
|
cacheKey = this.utilsService.getCacheKey(this.entityName, {
|
|
@@ -196,8 +254,9 @@ let ApiService = class ApiService {
|
|
|
196
254
|
}
|
|
197
255
|
}
|
|
198
256
|
if (isRaw) {
|
|
199
|
-
|
|
200
|
-
|
|
257
|
+
const rawSubQuery = query.clone().offset(undefined).limit(undefined);
|
|
258
|
+
rawSubQuery.expressionMap.orderBys = {};
|
|
259
|
+
rawSubQuery.distinct(true);
|
|
201
260
|
const countSql = `
|
|
202
261
|
SELECT COUNT(*) as count
|
|
203
262
|
FROM (
|
|
@@ -208,10 +267,20 @@ let ApiService = class ApiService {
|
|
|
208
267
|
`;
|
|
209
268
|
const parameters = rawSubQuery.getParameters();
|
|
210
269
|
const orderedValues = [];
|
|
270
|
+
const isPostgres = this.repository.manager.connection.options.type === 'postgres';
|
|
271
|
+
let paramIndex = 0;
|
|
211
272
|
const convertedQuery = countSql.replace(/:(\w+)/g, (_, key)=>{
|
|
212
|
-
if (!(key in parameters))
|
|
273
|
+
if (!(key in parameters)) {
|
|
274
|
+
throw new _common.InternalServerErrorException({
|
|
275
|
+
message: `Missing parameter value for: ${key}`,
|
|
276
|
+
messageKey: _constants.SYSTEM_MESSAGES.MISSING_PARAMETER,
|
|
277
|
+
messageVariables: {
|
|
278
|
+
key
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
}
|
|
213
282
|
orderedValues.push(parameters[key]);
|
|
214
|
-
return '?';
|
|
283
|
+
return isPostgres ? `$${++paramIndex}` : '?';
|
|
215
284
|
});
|
|
216
285
|
const result = await this.repository.query(convertedQuery, orderedValues);
|
|
217
286
|
total = Number(result[0]?.count || 0);
|
|
@@ -234,7 +303,7 @@ let ApiService = class ApiService {
|
|
|
234
303
|
}
|
|
235
304
|
}
|
|
236
305
|
async delete(option, user) {
|
|
237
|
-
await this.
|
|
306
|
+
await this.ensureDataSourceRepository();
|
|
238
307
|
const queryRunner = this.repository.manager.connection.createQueryRunner();
|
|
239
308
|
await queryRunner.connect();
|
|
240
309
|
await queryRunner.startTransaction();
|
|
@@ -260,13 +329,12 @@ let ApiService = class ApiService {
|
|
|
260
329
|
id
|
|
261
330
|
};
|
|
262
331
|
});
|
|
263
|
-
await this.afterDeleteOperation(ids, user, queryRunner);
|
|
332
|
+
await this.afterDeleteOperation(ids, option, user, queryRunner);
|
|
264
333
|
await queryRunner.commitTransaction();
|
|
265
334
|
await Promise.all([
|
|
266
335
|
this.clearCacheForAll(),
|
|
267
336
|
this.clearCacheForId(ids)
|
|
268
337
|
]);
|
|
269
|
-
// return null per IService contract
|
|
270
338
|
return null;
|
|
271
339
|
} catch (error) {
|
|
272
340
|
await queryRunner.rollbackTransaction();
|
|
@@ -275,28 +343,28 @@ let ApiService = class ApiService {
|
|
|
275
343
|
await queryRunner.release();
|
|
276
344
|
}
|
|
277
345
|
}
|
|
278
|
-
// Caching
|
|
279
346
|
async clearCacheForAll() {
|
|
280
347
|
await this.utilsService.clearCache(this.entityName, this.cacheManager);
|
|
281
348
|
}
|
|
282
349
|
async clearCacheForId(entities) {
|
|
283
350
|
await Promise.all(entities.map((item)=>this.utilsService.clearCache(this.entityName, this.cacheManager, item.id)));
|
|
284
351
|
}
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
352
|
+
handleError(error, _operation) {
|
|
353
|
+
if (error instanceof Error) {
|
|
354
|
+
throw error;
|
|
355
|
+
}
|
|
356
|
+
throw new _common.InternalServerErrorException({
|
|
357
|
+
message: typeof error === 'string' ? error : 'Unknown error',
|
|
358
|
+
messageKey: _constants.ERROR_MESSAGES.INTERNAL
|
|
290
359
|
});
|
|
291
|
-
_errorhandlerutil.ErrorHandler.rethrowError(error);
|
|
292
360
|
}
|
|
293
|
-
|
|
361
|
+
ensureArray(value) {
|
|
294
362
|
return Array.isArray(value) ? value : [
|
|
295
363
|
value
|
|
296
364
|
];
|
|
297
365
|
}
|
|
298
|
-
|
|
299
|
-
await this.
|
|
366
|
+
async executeInTransaction(operation, fn) {
|
|
367
|
+
await this.ensureDataSourceRepository();
|
|
300
368
|
const qr = this.repository.manager.connection.createQueryRunner();
|
|
301
369
|
await qr.connect();
|
|
302
370
|
await qr.startTransaction();
|
|
@@ -321,14 +389,12 @@ let ApiService = class ApiService {
|
|
|
321
389
|
await qr.release();
|
|
322
390
|
}
|
|
323
391
|
}
|
|
324
|
-
// Hooks (override in child classes)
|
|
325
|
-
async ensureRepositoryInitialized() {}
|
|
326
392
|
async beforeInsertOperation(_dto, _user, _queryRunner) {}
|
|
327
|
-
async afterInsertOperation(_entity, _user, _queryRunner) {}
|
|
393
|
+
async afterInsertOperation(_entity, _dto, _user, _queryRunner) {}
|
|
328
394
|
async beforeUpdateOperation(_dto, _user, _queryRunner) {}
|
|
329
|
-
async afterUpdateOperation(_entity, _user, _queryRunner) {}
|
|
395
|
+
async afterUpdateOperation(_entity, _dto, _user, _queryRunner) {}
|
|
330
396
|
async beforeDeleteOperation(_dto, _user, _queryRunner) {}
|
|
331
|
-
async afterDeleteOperation(_entity, _user, _queryRunner) {}
|
|
397
|
+
async afterDeleteOperation(_entity, _dto, _user, _queryRunner) {}
|
|
332
398
|
async getFilterQuery(query, filter, _user) {
|
|
333
399
|
Object.entries(filter).forEach(([key, value])=>{
|
|
334
400
|
query.andWhere(`${this.entityName}.${key} = :value`, {
|
|
@@ -371,7 +437,6 @@ let ApiService = class ApiService {
|
|
|
371
437
|
isRaw: false
|
|
372
438
|
};
|
|
373
439
|
}
|
|
374
|
-
// DTO conversion
|
|
375
440
|
async convertRequestDtoToEntity(dto, user) {
|
|
376
441
|
return Array.isArray(dto) ? await this.convertArrayDtoToEntities(dto, user) : [
|
|
377
442
|
await this.convertSingleDtoToEntity(dto, user)
|
|
@@ -385,7 +450,10 @@ let ApiService = class ApiService {
|
|
|
385
450
|
id: d.id
|
|
386
451
|
}
|
|
387
452
|
});
|
|
388
|
-
if (!existing) throw new _common.NotFoundException(
|
|
453
|
+
if (!existing) throw new _common.NotFoundException({
|
|
454
|
+
message: 'No such entity data found for update! Please, Try Again.',
|
|
455
|
+
messageKey: _constants.ERROR_MESSAGES.NOT_FOUND
|
|
456
|
+
});
|
|
389
457
|
return Object.assign(existing, d);
|
|
390
458
|
}
|
|
391
459
|
delete d.id;
|
|
@@ -398,26 +466,113 @@ let ApiService = class ApiService {
|
|
|
398
466
|
return Object.assign({}, entity);
|
|
399
467
|
}
|
|
400
468
|
convertEntityListToResponseListDto(entities, _isRaw) {
|
|
401
|
-
return entities.map((entity)=>
|
|
469
|
+
return entities.map((entity)=>this.convertEntityToResponseDto(entity, _isRaw));
|
|
402
470
|
}
|
|
403
471
|
async getEntityClass() {
|
|
404
|
-
return
|
|
472
|
+
return this.repository.create();
|
|
405
473
|
}
|
|
406
|
-
constructor(entityName,
|
|
474
|
+
constructor(entityName, cacheManager, utilsService, _loggerName, isCacheable = false, moduleName, entity, dataSourceProvider){
|
|
407
475
|
_define_property(this, "entityName", void 0);
|
|
408
|
-
_define_property(this, "repository", void 0);
|
|
409
476
|
_define_property(this, "cacheManager", void 0);
|
|
410
477
|
_define_property(this, "utilsService", void 0);
|
|
411
|
-
_define_property(this, "
|
|
478
|
+
_define_property(this, "_loggerName", void 0);
|
|
412
479
|
_define_property(this, "isCacheable", void 0);
|
|
413
|
-
_define_property(this, "
|
|
480
|
+
_define_property(this, "moduleName", void 0);
|
|
481
|
+
_define_property(this, "repository", void 0);
|
|
482
|
+
_define_property(this, "repositoryInitialized", void 0);
|
|
483
|
+
_define_property(this, "_entity", void 0);
|
|
484
|
+
_define_property(this, "_dataSourceProvider", void 0);
|
|
414
485
|
this.entityName = entityName;
|
|
415
|
-
this.repository = repository;
|
|
416
486
|
this.cacheManager = cacheManager;
|
|
417
487
|
this.utilsService = utilsService;
|
|
418
|
-
this.
|
|
488
|
+
this._loggerName = _loggerName;
|
|
419
489
|
this.isCacheable = isCacheable;
|
|
420
|
-
this.
|
|
421
|
-
this.
|
|
490
|
+
this.moduleName = moduleName;
|
|
491
|
+
this.repositoryInitialized = false;
|
|
492
|
+
this._entity = entity;
|
|
493
|
+
this._dataSourceProvider = dataSourceProvider;
|
|
422
494
|
}
|
|
423
495
|
};
|
|
496
|
+
_ts_decorate([
|
|
497
|
+
(0, _logactiondecorator.LogAction)({
|
|
498
|
+
action: 'create'
|
|
499
|
+
}),
|
|
500
|
+
_ts_metadata("design:type", Function),
|
|
501
|
+
_ts_metadata("design:paramtypes", [
|
|
502
|
+
typeof CreateDtoT === "undefined" ? Object : CreateDtoT,
|
|
503
|
+
Object
|
|
504
|
+
]),
|
|
505
|
+
_ts_metadata("design:returntype", Promise)
|
|
506
|
+
], ApiService.prototype, "insert", null);
|
|
507
|
+
_ts_decorate([
|
|
508
|
+
(0, _logactiondecorator.LogAction)({
|
|
509
|
+
action: 'createMany'
|
|
510
|
+
}),
|
|
511
|
+
_ts_metadata("design:type", Function),
|
|
512
|
+
_ts_metadata("design:paramtypes", [
|
|
513
|
+
typeof Array === "undefined" ? Object : Array,
|
|
514
|
+
Object
|
|
515
|
+
]),
|
|
516
|
+
_ts_metadata("design:returntype", Promise)
|
|
517
|
+
], ApiService.prototype, "insertMany", null);
|
|
518
|
+
_ts_decorate([
|
|
519
|
+
(0, _logactiondecorator.LogAction)({
|
|
520
|
+
action: 'update'
|
|
521
|
+
}),
|
|
522
|
+
_ts_metadata("design:type", Function),
|
|
523
|
+
_ts_metadata("design:paramtypes", [
|
|
524
|
+
typeof UpdateDtoT === "undefined" ? Object : UpdateDtoT,
|
|
525
|
+
Object
|
|
526
|
+
]),
|
|
527
|
+
_ts_metadata("design:returntype", Promise)
|
|
528
|
+
], ApiService.prototype, "update", null);
|
|
529
|
+
_ts_decorate([
|
|
530
|
+
(0, _logactiondecorator.LogAction)({
|
|
531
|
+
action: 'updateMany'
|
|
532
|
+
}),
|
|
533
|
+
_ts_metadata("design:type", Function),
|
|
534
|
+
_ts_metadata("design:paramtypes", [
|
|
535
|
+
Array,
|
|
536
|
+
Object
|
|
537
|
+
]),
|
|
538
|
+
_ts_metadata("design:returntype", Promise)
|
|
539
|
+
], ApiService.prototype, "updateMany", null);
|
|
540
|
+
_ts_decorate([
|
|
541
|
+
(0, _logactiondecorator.LogAction)({
|
|
542
|
+
action: 'bulkUpsert'
|
|
543
|
+
}),
|
|
544
|
+
_ts_metadata("design:type", Function),
|
|
545
|
+
_ts_metadata("design:paramtypes", [
|
|
546
|
+
Array,
|
|
547
|
+
Array,
|
|
548
|
+
Object
|
|
549
|
+
]),
|
|
550
|
+
_ts_metadata("design:returntype", Promise)
|
|
551
|
+
], ApiService.prototype, "bulkUpsert", null);
|
|
552
|
+
_ts_decorate([
|
|
553
|
+
(0, _logactiondecorator.LogAction)({
|
|
554
|
+
action: 'delete'
|
|
555
|
+
}),
|
|
556
|
+
_ts_metadata("design:type", Function),
|
|
557
|
+
_ts_metadata("design:paramtypes", [
|
|
558
|
+
typeof _dtos.DeleteDto === "undefined" ? Object : _dtos.DeleteDto,
|
|
559
|
+
Object
|
|
560
|
+
]),
|
|
561
|
+
_ts_metadata("design:returntype", Promise)
|
|
562
|
+
], ApiService.prototype, "delete", null);
|
|
563
|
+
ApiService = _ts_decorate([
|
|
564
|
+
(0, _common.Injectable)({
|
|
565
|
+
scope: _common.Scope.REQUEST
|
|
566
|
+
}),
|
|
567
|
+
_ts_metadata("design:type", Function),
|
|
568
|
+
_ts_metadata("design:paramtypes", [
|
|
569
|
+
String,
|
|
570
|
+
typeof _hybridcacheclass.HybridCache === "undefined" ? Object : _hybridcacheclass.HybridCache,
|
|
571
|
+
typeof _utilsservice.UtilsService === "undefined" ? Object : _utilsservice.UtilsService,
|
|
572
|
+
String,
|
|
573
|
+
Boolean,
|
|
574
|
+
String,
|
|
575
|
+
typeof _typeorm.EntityTarget === "undefined" ? Object : _typeorm.EntityTarget,
|
|
576
|
+
typeof _interfaces.IDataSourceProvider === "undefined" ? Object : _interfaces.IDataSourceProvider
|
|
577
|
+
])
|
|
578
|
+
], ApiService);
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: Object.getOwnPropertyDescriptor(all, name).get
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
_export(exports, {
|
|
12
|
+
get buildQuery () {
|
|
13
|
+
return buildQuery;
|
|
14
|
+
},
|
|
15
|
+
get getCastIntType () {
|
|
16
|
+
return getCastIntType;
|
|
17
|
+
},
|
|
18
|
+
get getJsonColumnType () {
|
|
19
|
+
return getJsonColumnType;
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
const _nestjscore = require("@flusys/nestjs-core");
|
|
23
|
+
function getJsonColumnType() {
|
|
24
|
+
const driver = _nestjscore.envConfig.getTypeOrmConfig().type;
|
|
25
|
+
if (driver === 'postgres') return 'jsonb';
|
|
26
|
+
if (driver === 'mysql') return 'json';
|
|
27
|
+
return 'simple-json';
|
|
28
|
+
}
|
|
29
|
+
function getCastIntType() {
|
|
30
|
+
const driver = _nestjscore.envConfig.getTypeOrmConfig().type;
|
|
31
|
+
return driver === 'postgres' ? 'INTEGER' : 'UNSIGNED';
|
|
32
|
+
}
|
|
33
|
+
function buildQuery(sql, dbType) {
|
|
34
|
+
if (dbType !== 'postgres') return sql;
|
|
35
|
+
let index = 0;
|
|
36
|
+
return sql.replace(/\?/g, ()=>`$${++index}`);
|
|
37
|
+
}
|
package/cjs/classes/index.js
CHANGED
|
@@ -4,9 +4,8 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
});
|
|
5
5
|
_export_star(require("./api-controller.class"), exports);
|
|
6
6
|
_export_star(require("./api-service.class"), exports);
|
|
7
|
-
_export_star(require("./
|
|
7
|
+
_export_star(require("./handle-custome-db"), exports);
|
|
8
8
|
_export_star(require("./hybrid-cache.class"), exports);
|
|
9
|
-
_export_star(require("./winston-logger-adapter.class"), exports);
|
|
10
9
|
_export_star(require("./winston.logger.class"), exports);
|
|
11
10
|
_export_star(require("../constants/permissions"), exports);
|
|
12
11
|
function _export_star(from, to) {
|