@futdevpro/nts-dynamo 1.9.15 → 1.9.16
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/.copilot/patterns.json +7 -7
- package/.github/workflows/main.yml +206 -0
- package/HOWTO.md +15 -15
- package/README.md +140 -140
- package/build/_models/control-models/endpoint-params.control-model.d.ts.map +1 -1
- package/build/_models/control-models/endpoint-params.control-model.js +2 -0
- package/build/_models/control-models/endpoint-params.control-model.js.map +1 -1
- package/build/_models/control-models/socket-event.control-model.js +1 -1
- package/build/_services/core/global.service.d.ts.map +1 -1
- package/build/_services/core/global.service.js +1 -0
- package/build/_services/core/global.service.js.map +1 -1
- package/build/_services/route/routing-module.service.d.ts +1 -0
- package/build/_services/route/routing-module.service.d.ts.map +1 -1
- package/build/_services/route/routing-module.service.js +17 -23
- package/build/_services/route/routing-module.service.js.map +1 -1
- package/build/_services/server/app.server.d.ts.map +1 -1
- package/build/_services/server/app.server.js +4 -1
- package/build/_services/server/app.server.js.map +1 -1
- package/build/_services/socket/socket-client.service.d.ts.map +1 -1
- package/build/_services/socket/socket-client.service.js +1 -0
- package/build/_services/socket/socket-client.service.js.map +1 -1
- package/nodemon.json +17 -15
- package/package.json +5 -5
- package/src/_constants/global-settings.const.ts +27 -27
- package/src/_constants/index.ts +2 -2
- package/src/_constants/mocks/app-extended-server.mock.ts +198 -198
- package/src/_constants/mocks/app-params.mock.ts +9 -9
- package/src/_constants/mocks/app-server.mock.ts +185 -185
- package/src/_constants/mocks/auth-service.mock.ts +28 -28
- package/src/_constants/mocks/controller.mock.ts +16 -16
- package/src/_constants/mocks/data-model.mock.ts +83 -83
- package/src/_constants/mocks/email-service-collection.mock.ts +13 -13
- package/src/_constants/mocks/email-service.mock.ts +19 -19
- package/src/_constants/mocks/email-template.mock.html +14 -14
- package/src/_constants/mocks/endpoint.mock.ts +90 -90
- package/src/_constants/mocks/socket-client.mock.ts +43 -43
- package/src/_constants/mocks/socket-server.mock.ts +43 -43
- package/src/_enums/data-model-type.enum.ts +14 -14
- package/src/_enums/data-service-function.enum.ts +15 -15
- package/src/_enums/http/http-call-type.enum.ts +12 -12
- package/src/_enums/http/http-response-type.enum.ts +7 -7
- package/src/_enums/http/socket-event-type.enum.ts +18 -18
- package/src/_enums/index.ts +13 -13
- package/src/_enums/predefined-data-types.enum.ts +27 -27
- package/src/_enums/route-security.enum.ts +12 -12
- package/src/_enums/socket-security.enum.ts +11 -11
- package/src/_models/control-models/api-call-params.control-model.ts +126 -126
- package/src/_models/control-models/app-ext-system-controls.control-model.ts +9 -9
- package/src/_models/control-models/app-params.control-model.ts +45 -45
- package/src/_models/control-models/app-system-controls.control-model.ts +9 -9
- package/src/_models/control-models/endpoint-params.control-model.ts +309 -307
- package/src/_models/control-models/http-settings.control-model.ts +29 -29
- package/src/_models/control-models/index.ts +13 -13
- package/src/_models/control-models/socket-client-service-params.control-model.ts +28 -28
- package/src/_models/control-models/socket-event.control-model.ts +150 -150
- package/src/_models/control-models/socket-presence.control-model.ts +207 -207
- package/src/_models/control-models/socket-server-service-params.control-model.ts +20 -20
- package/src/_models/control-models/system-control.control-model.ts +12 -12
- package/src/_models/index.ts +9 -9
- package/src/_models/interfaces/certification-settings.interface.ts +7 -7
- package/src/_models/interfaces/global-service-settings.interface.ts +45 -45
- package/src/_models/interfaces/global-settings.interface.ts +83 -83
- package/src/_models/interfaces/index.ts +7 -7
- package/src/_models/interfaces/routing-module-settings.interface.ts +20 -20
- package/src/_models/types/db-filter.type.ts +108 -108
- package/src/_models/types/db-update.type.ts +100 -100
- package/src/_models/types/index.ts +5 -5
- package/src/_modules/api-service.index.ts +12 -12
- package/src/_modules/app-extended.index.ts +28 -28
- package/src/_modules/app.index.ts +24 -24
- package/src/_modules/auth.index.ts +7 -7
- package/src/_modules/constants.index.ts +2 -2
- package/src/_modules/controller.index.ts +10 -10
- package/src/_modules/custom-data/custom-data.controller.ts +69 -69
- package/src/_modules/custom-data/custom-data.data-service.ts +20 -20
- package/src/_modules/custom-data/get-custom-data-routing-module.util.ts +23 -23
- package/src/_modules/custom-data/index.ts +6 -6
- package/src/_modules/custom-data-module.index.ts +2 -2
- package/src/_modules/data-service.index.ts +9 -9
- package/src/_modules/email.index.ts +8 -8
- package/src/_modules/enums.index.ts +2 -2
- package/src/_modules/extended.index.ts +8 -8
- package/src/_modules/models.index.ts +2 -2
- package/src/_modules/services.index.ts +2 -2
- package/src/_modules/test/get-test-routing-module.util.ts +23 -23
- package/src/_modules/test/index.ts +5 -5
- package/src/_modules/test/test.controller.ts +115 -115
- package/src/_modules/test-module.index.ts +2 -2
- package/src/_modules/usage/get-usage-routing-module.util.ts +22 -22
- package/src/_modules/usage/index.ts +7 -7
- package/src/_modules/usage/usage.controller.ts +120 -120
- package/src/_modules/usage/usage.data-service.ts +172 -172
- package/src/_modules/usage-module.index.ts +2 -2
- package/src/_services/base/data.service.ts +921 -921
- package/src/_services/base/db.service.spec.ts +32 -32
- package/src/_services/base/db.service.ts +1063 -1063
- package/src/_services/base/singleton.service.ts +21 -21
- package/src/_services/core/api.service.ts +453 -453
- package/src/_services/core/auth.service.ts +172 -172
- package/src/_services/core/email.service.ts +678 -678
- package/src/_services/core/global.service.ts +270 -269
- package/src/_services/core/service-collection.service.ts +5 -5
- package/src/_services/index.ts +23 -23
- package/src/_services/route/controller.service.ts +129 -129
- package/src/_services/route/routing-module.service.ts +293 -273
- package/src/_services/server/app-extended.server.spec.ts +76 -76
- package/src/_services/server/app-extended.server.ts +520 -520
- package/src/_services/server/app.server.spec.ts +67 -67
- package/src/_services/server/app.server.ts +1181 -1179
- package/src/_services/shared.service.spec.ts +19 -19
- package/src/_services/shared.static-service.ts +73 -73
- package/src/_services/socket/socket-client.service.ts +236 -235
- package/src/_services/socket/socket-server.service.spec.ts +11 -11
- package/src/_services/socket/socket-server.service.ts +761 -761
- package/src/index.ts +18 -18
- package/tsconfig.json +41 -41
- package/build/tsconfig.tsbuildinfo +0 -1
|
@@ -1,1063 +1,1063 @@
|
|
|
1
|
-
|
|
2
|
-
import * as mongoose from 'mongoose';
|
|
3
|
-
import { Schema } from 'mongoose';
|
|
4
|
-
|
|
5
|
-
import {
|
|
6
|
-
DynamoFM_Metadata, DynamoFM_DataModel_Params, DynamoFM_DataProperty_Params, DynamoFM_Error,
|
|
7
|
-
DynamoFM_Log, DynamoFM_AnyError, DynamoFM_Error_Settings, DynamoFM_ErrorLevel
|
|
8
|
-
} from '@futdevpro/fsm-dynamo';
|
|
9
|
-
import { DynamoNTS_DBFilter } from '../../_models/types/db-filter.type';
|
|
10
|
-
import { DynamoNTS_DBUpdate } from '../../_models/types/db-update.type';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* DB Service for MongoDB
|
|
14
|
-
*/
|
|
15
|
-
export class DynamoNTS_DBService<T extends DynamoFM_Metadata> {
|
|
16
|
-
|
|
17
|
-
serviceName: string;
|
|
18
|
-
|
|
19
|
-
dataModel/* : mongoose.Model<T> */; // = mongoose.model(this.dataParams.dbName, this.getSchema());
|
|
20
|
-
|
|
21
|
-
private depDataName: string;
|
|
22
|
-
|
|
23
|
-
defaultErrorUserMsg =
|
|
24
|
-
`We encountered an unhandled DB Error, ` +
|
|
25
|
-
`\nplease contact the responsible development team.`;
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* @param dataName name the model
|
|
29
|
-
* @param typeSample sample data for scheme creation (dont include DynamoFM_Metadata!)
|
|
30
|
-
* @param schemaSettings additional settings for specific parameters as unique, required, minlength or maxlength
|
|
31
|
-
* schemaSettings also MUST contain specific types that differs from the listed above (Array, Date)
|
|
32
|
-
*/
|
|
33
|
-
constructor(
|
|
34
|
-
public dataParams: DynamoFM_DataModel_Params
|
|
35
|
-
){
|
|
36
|
-
try {
|
|
37
|
-
this.serviceName = this.constructor?.name;
|
|
38
|
-
|
|
39
|
-
try {
|
|
40
|
-
this.dataModel = mongoose.model(this.dataParams.dbName, this.getSchema());
|
|
41
|
-
} catch (error) {
|
|
42
|
-
if (mongoose.models[this.dataParams.dbName]) {
|
|
43
|
-
this.dataModel = mongoose.models[this.dataParams.dbName];
|
|
44
|
-
} else {
|
|
45
|
-
throw error;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
this.lookForDependencyDataSettings();
|
|
50
|
-
} catch (error) {
|
|
51
|
-
throw new DynamoFM_Error({
|
|
52
|
-
message:
|
|
53
|
-
`DynamoNTS_DBService construction failed! ${dataParams.dataName} - ${this.serviceName}`,
|
|
54
|
-
errorCode: 'NTS-DBS-C00',
|
|
55
|
-
level: DynamoFM_ErrorLevel.critical,
|
|
56
|
-
additionalContent: { dataParams: this.dataParams },
|
|
57
|
-
error,
|
|
58
|
-
});
|
|
59
|
-
/* DynamoFM_Log.error(
|
|
60
|
-
`\nDynamoNTS_DBService ERROR, ` +
|
|
61
|
-
`The dbService construction failed for ${dataParams.dataName}. ${this.serviceName}` +
|
|
62
|
-
`\nERROR: `, error,
|
|
63
|
-
new Error()
|
|
64
|
-
); */
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* save new data
|
|
70
|
-
* @param data data
|
|
71
|
-
* @returns data
|
|
72
|
-
*/
|
|
73
|
-
async createData(data: T, issuer: string): Promise<T> {
|
|
74
|
-
data.__created = new Date();
|
|
75
|
-
data.__lastModified = new Date();
|
|
76
|
-
data.__createdBy = issuer;
|
|
77
|
-
data.__lastModifiedBy = issuer;
|
|
78
|
-
|
|
79
|
-
const dataModel = new this.dataModel(data);
|
|
80
|
-
let newData: T = await dataModel.save().then((res): T => {
|
|
81
|
-
if (res) {
|
|
82
|
-
return res?.toObject() as T;
|
|
83
|
-
|
|
84
|
-
} else {
|
|
85
|
-
throw new DynamoFM_Error({
|
|
86
|
-
...this._getDefaultErrorSettings(
|
|
87
|
-
'createData',
|
|
88
|
-
new Error(
|
|
89
|
-
`createData failed, save ${this.dataParams.dbName} result not found! (NTS DB)`
|
|
90
|
-
),
|
|
91
|
-
issuer
|
|
92
|
-
),
|
|
93
|
-
|
|
94
|
-
status: 204,
|
|
95
|
-
errorCode: 'NTS-DBS-CD1',
|
|
96
|
-
additionalContent: { data },
|
|
97
|
-
issuer,
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
}).catch((error): void => {
|
|
101
|
-
if (error?.flag?.includes('DYNAMO')) {
|
|
102
|
-
throw error;
|
|
103
|
-
|
|
104
|
-
} else {
|
|
105
|
-
throw new DynamoFM_Error({
|
|
106
|
-
...this._getDefaultErrorSettings('createData', error, issuer),
|
|
107
|
-
|
|
108
|
-
status: 422,
|
|
109
|
-
errorCode: 'NTS-DBS-CD0',
|
|
110
|
-
additionalContent: { data, dataModel },
|
|
111
|
-
message:
|
|
112
|
-
`createData failed, Create new ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
|
|
113
|
-
issuer,
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
newData = this.stringifyDataId(newData, 'createData');
|
|
119
|
-
|
|
120
|
-
return newData;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Find data by _id and update
|
|
125
|
-
* @param data data
|
|
126
|
-
* @returns data
|
|
127
|
-
*/
|
|
128
|
-
async modifyData(data: T, issuer: string): Promise<T> {
|
|
129
|
-
data.__lastModified = new Date();
|
|
130
|
-
data.__lastModifiedBy = issuer;
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* EZ A SZAR TELJESEN SZAR, nem friss, nem a db-be mentett adatokat ad vissza,
|
|
134
|
-
* átír random value-kat össze vissza, WTF
|
|
135
|
-
* */
|
|
136
|
-
/* let newData: T = */ await this.dataModel.findByIdAndUpdate(data._id, data).then((res) => {
|
|
137
|
-
/* if (res) {
|
|
138
|
-
//return res?.toObject() as T;
|
|
139
|
-
} else {
|
|
140
|
-
throw new DynamoFM_Error({
|
|
141
|
-
...this._getDefaultErrorSettings(
|
|
142
|
-
'modifyData',
|
|
143
|
-
new Error(`modifyData failed, modify ${this.dataParams.dbName} result not found! (NTS DB)`),
|
|
144
|
-
issuer,
|
|
145
|
-
),
|
|
146
|
-
|
|
147
|
-
status: 204,
|
|
148
|
-
errorCode: 'NTS-DBS-FU1',
|
|
149
|
-
additionalContent: { data },
|
|
150
|
-
issuer,
|
|
151
|
-
});
|
|
152
|
-
} */
|
|
153
|
-
}).catch((error): void => {
|
|
154
|
-
throw new DynamoFM_Error({
|
|
155
|
-
...this._getDefaultErrorSettings('modifyData', error, issuer),
|
|
156
|
-
|
|
157
|
-
errorCode: 'NTS-DBS-FU0',
|
|
158
|
-
additionalContent: { data },
|
|
159
|
-
message: `modifyData ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
|
|
160
|
-
issuer,
|
|
161
|
-
});
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
/* newData = this.stringifyDataId(newData, 'modifyData'); */
|
|
165
|
-
|
|
166
|
-
return data;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* returns data by _id,
|
|
171
|
-
* @param id id
|
|
172
|
-
* @returns data
|
|
173
|
-
*/
|
|
174
|
-
async getDataById(id: string): Promise<T> {
|
|
175
|
-
if (!id) {
|
|
176
|
-
throw new DynamoFM_Error({
|
|
177
|
-
...this._getDefaultErrorSettings('getDataById', new Error(`No ID provided! (NTS DB)`)),
|
|
178
|
-
|
|
179
|
-
errorCode: 'NTS-DBS-GI1',
|
|
180
|
-
message: `get ${this.dataParams.dbName} by ID was unsuccessful (NTS DB)`,
|
|
181
|
-
});
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
let data: T = await this.dataModel
|
|
185
|
-
.findById(id)
|
|
186
|
-
.then((res): T => res?.toObject() as T ?? null)
|
|
187
|
-
.catch((error): void => {
|
|
188
|
-
throw new DynamoFM_Error({
|
|
189
|
-
...this._getDefaultErrorSettings('getDataById', error),
|
|
190
|
-
|
|
191
|
-
errorCode: 'NTS-DBS-GI0',
|
|
192
|
-
additionalContent: { id },
|
|
193
|
-
message: `get ${this.dataParams.dbName} by ID was unsuccessful (NTS DB)`,
|
|
194
|
-
});
|
|
195
|
-
});
|
|
196
|
-
|
|
197
|
-
data = this.stringifyDataId(data, 'getDataById');
|
|
198
|
-
|
|
199
|
-
return data;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
/**
|
|
203
|
-
* get data by dependency data id,
|
|
204
|
-
* !!!: throws error if not found (errorCode on not found: NTS-DBS-GD2)
|
|
205
|
-
*
|
|
206
|
-
* @param dependencyId id
|
|
207
|
-
* @returns data
|
|
208
|
-
*/
|
|
209
|
-
async getDataByDependencyId(dependencyId: string): Promise<T> {
|
|
210
|
-
if (!this.depDataName) {
|
|
211
|
-
throw new DynamoFM_Error({
|
|
212
|
-
...this._getDefaultErrorSettings(
|
|
213
|
-
'getDataByDependencyId',
|
|
214
|
-
new Error(
|
|
215
|
-
`dependencyDataIdKey not setted up for this db-service (${this.dataParams.dbName}) ` +
|
|
216
|
-
`(NTS DB)`
|
|
217
|
-
)
|
|
218
|
-
),
|
|
219
|
-
|
|
220
|
-
status: 501,
|
|
221
|
-
errorCode: 'NTS-DBS-GD0',
|
|
222
|
-
});
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
let data: T = await this.dataModel
|
|
226
|
-
.findOne({ [this.depDataName]: dependencyId })
|
|
227
|
-
.then((res): T => res?.toObject() as T ?? null)
|
|
228
|
-
.catch((error): void => {
|
|
229
|
-
throw new DynamoFM_Error({
|
|
230
|
-
...this._getDefaultErrorSettings('getDataByDependencyId', error),
|
|
231
|
-
|
|
232
|
-
errorCode: 'NTS-DBS-GD1',
|
|
233
|
-
additionalContent: { dependencyId },
|
|
234
|
-
message:
|
|
235
|
-
`get ${this.dataParams.dbName} by ${this.depDataName} was unsuccessful (NTS DB)`,
|
|
236
|
-
});
|
|
237
|
-
});
|
|
238
|
-
|
|
239
|
-
data = this.stringifyDataId(data, 'getDataByDependencyId');
|
|
240
|
-
|
|
241
|
-
return data;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
/**
|
|
245
|
-
* get data by dependency data id,
|
|
246
|
-
* !!!: throws error if not found (errorCode on not found: NTS-DBS-GLD2)
|
|
247
|
-
*
|
|
248
|
-
* @param dependencyId id
|
|
249
|
-
* @returns dataList
|
|
250
|
-
*/
|
|
251
|
-
async getDataListByDependencyId(dependencyId: string): Promise<T[]> {
|
|
252
|
-
if (!this.depDataName) {
|
|
253
|
-
throw new DynamoFM_Error({
|
|
254
|
-
...this._getDefaultErrorSettings(
|
|
255
|
-
'getDataListByDependencyId',
|
|
256
|
-
new Error(
|
|
257
|
-
`isDependencyHook not setted up for this dataModel (${this.dataParams.dbName}) ` +
|
|
258
|
-
`(NTS DB)`
|
|
259
|
-
)
|
|
260
|
-
),
|
|
261
|
-
|
|
262
|
-
status: 501,
|
|
263
|
-
errorCode: 'NTS-DBS-GLD0',
|
|
264
|
-
additionalContent: {
|
|
265
|
-
dataModel: this.dataModel,
|
|
266
|
-
},
|
|
267
|
-
});
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
const dataList: T[] = await this.dataModel
|
|
271
|
-
.find({ [this.depDataName]: dependencyId })
|
|
272
|
-
.then((res): T[] => res as T[] ?? [])
|
|
273
|
-
.catch((error): void => {
|
|
274
|
-
throw new DynamoFM_Error({
|
|
275
|
-
...this._getDefaultErrorSettings('getDataListByDependencyId', error),
|
|
276
|
-
|
|
277
|
-
errorCode: 'NTS-DBS-GLD1',
|
|
278
|
-
additionalContent: { dependencyId },
|
|
279
|
-
message:
|
|
280
|
-
`get ${this.dataParams.dbName} by ${this.depDataName} was unsuccessful (NTS DB)`,
|
|
281
|
-
});
|
|
282
|
-
});
|
|
283
|
-
|
|
284
|
-
dataList.forEach((data: T): void => {
|
|
285
|
-
data = this.stringifyDataId(data, 'getDataListByDependencyId');
|
|
286
|
-
});
|
|
287
|
-
|
|
288
|
-
return dataList;
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
async getDatasByDependencyIds(dependencyIds: string[]): Promise<T[]> {
|
|
292
|
-
if (!this.depDataName) {
|
|
293
|
-
throw new DynamoFM_Error({
|
|
294
|
-
...this._getDefaultErrorSettings(
|
|
295
|
-
'getDatasByDependencyIds',
|
|
296
|
-
new Error(
|
|
297
|
-
`dependencyDataIdKey not setted up for this db-service (${this.dataParams.dbName}) ` +
|
|
298
|
-
`(NTS DB)`
|
|
299
|
-
)
|
|
300
|
-
),
|
|
301
|
-
|
|
302
|
-
status: 501,
|
|
303
|
-
errorCode: 'NTS-DBS-GLDS0',
|
|
304
|
-
});
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
const dataList: T[] = await this.dataModel
|
|
308
|
-
.find({ [this.depDataName]: { $in: dependencyIds }})
|
|
309
|
-
.then((res): T[] => res as T[] ?? [])
|
|
310
|
-
.catch((error): void => {
|
|
311
|
-
throw new DynamoFM_Error({
|
|
312
|
-
...this._getDefaultErrorSettings('getDatasByDependencyIds', error),
|
|
313
|
-
|
|
314
|
-
errorCode: 'NTS-DBS-GLDS1',
|
|
315
|
-
additionalContent: { dependencyIds },
|
|
316
|
-
message: `get ${this.dataParams.dbName} by ${this.depDataName} was unsuccessful (NTS DB)`,
|
|
317
|
-
});
|
|
318
|
-
});
|
|
319
|
-
|
|
320
|
-
dataList.forEach((data: T): void => {
|
|
321
|
-
data = this.stringifyDataId(data, 'getDatasByDependencyIds');
|
|
322
|
-
});
|
|
323
|
-
|
|
324
|
-
return dataList;
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
/**
|
|
328
|
-
* get multiple data objects by a list of DependencyIDs,
|
|
329
|
-
* !!!: throws error if not found (errorCode on not found: NTS-DBS-GLDS2)
|
|
330
|
-
*
|
|
331
|
-
* @param ids ids
|
|
332
|
-
* @returns dataList
|
|
333
|
-
*/
|
|
334
|
-
async getDataListByDependencyIds(ids: string[]): Promise<T[]> {
|
|
335
|
-
if (!this.depDataName) {
|
|
336
|
-
throw new DynamoFM_Error({
|
|
337
|
-
...this._getDefaultErrorSettings(
|
|
338
|
-
'getDataListByDependencyIds',
|
|
339
|
-
new Error(
|
|
340
|
-
`dependencyDataIdKey not setted up for this db-service (${this.dataParams.dbName}) ` +
|
|
341
|
-
`(NTS DB)`
|
|
342
|
-
)
|
|
343
|
-
),
|
|
344
|
-
|
|
345
|
-
status: 501,
|
|
346
|
-
errorCode: 'NTS-DBS-GLDS0',
|
|
347
|
-
});
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
const dataList: T[] = await this.dataModel
|
|
351
|
-
.find({ [this.depDataName]: { $in: ids } })
|
|
352
|
-
.then((res): T[] => res as T[] ?? [])
|
|
353
|
-
.catch((error): void => {
|
|
354
|
-
throw new DynamoFM_Error({
|
|
355
|
-
...this._getDefaultErrorSettings('getDataListByDependencyIds', error),
|
|
356
|
-
|
|
357
|
-
errorCode: 'NTS-DBS-GLDS1',
|
|
358
|
-
additionalContent: { ids },
|
|
359
|
-
message:
|
|
360
|
-
`get ${this.dataParams.dbName}s by ${this.depDataName}s was unsuccessful (NTS DB)`,
|
|
361
|
-
});
|
|
362
|
-
});
|
|
363
|
-
|
|
364
|
-
dataList.forEach((data: T): void => {
|
|
365
|
-
data = this.stringifyDataId(data, 'getDataListByDependencyIds');
|
|
366
|
-
});
|
|
367
|
-
|
|
368
|
-
return dataList;
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
/**
|
|
372
|
-
* returns all data from database,
|
|
373
|
-
* !!!: throws error if not found (errorCode on not found: NTS-DBS-GA1)
|
|
374
|
-
*
|
|
375
|
-
* @returns dataList
|
|
376
|
-
*/
|
|
377
|
-
async getAll(): Promise<T[]> {
|
|
378
|
-
const dataList: T[] = await this.dataModel
|
|
379
|
-
.find({})
|
|
380
|
-
.then((res): T[] => res as T[] ?? [])
|
|
381
|
-
.catch((error): void => {
|
|
382
|
-
throw new DynamoFM_Error({
|
|
383
|
-
...this._getDefaultErrorSettings('getAll', error),
|
|
384
|
-
|
|
385
|
-
errorCode: 'NTS-DBS-GA0',
|
|
386
|
-
message: `get all ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
|
|
387
|
-
});
|
|
388
|
-
});
|
|
389
|
-
|
|
390
|
-
dataList.forEach((data: T): void => {
|
|
391
|
-
data = this.stringifyDataId(data, 'getAll');
|
|
392
|
-
});
|
|
393
|
-
|
|
394
|
-
return dataList as T[];
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
/**
|
|
398
|
-
* deleted data by id
|
|
399
|
-
* @param id id
|
|
400
|
-
*/
|
|
401
|
-
async deleteDataById(id: string): Promise<void> {
|
|
402
|
-
await this.dataModel
|
|
403
|
-
.findByIdAndDelete(id)
|
|
404
|
-
.catch((error): void => {
|
|
405
|
-
throw new DynamoFM_Error({
|
|
406
|
-
...this._getDefaultErrorSettings('deleteDataById', error),
|
|
407
|
-
|
|
408
|
-
errorCode: 'NTS-DBS-DD0',
|
|
409
|
-
additionalContent: { id },
|
|
410
|
-
message: `delete ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
|
|
411
|
-
});
|
|
412
|
-
});
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
/**
|
|
416
|
-
* deleted data by id
|
|
417
|
-
* @param dependencyId id
|
|
418
|
-
*/
|
|
419
|
-
async deleteDataByDependencyId(dependencyId: string): Promise<void> {
|
|
420
|
-
if (!this.depDataName) {
|
|
421
|
-
throw new DynamoFM_Error({
|
|
422
|
-
...this._getDefaultErrorSettings(
|
|
423
|
-
'deleteDataByDependencyId',
|
|
424
|
-
new Error(
|
|
425
|
-
`dependencyDataIdKey not setted up for this db-service (${this.dataParams.dbName}) ` +
|
|
426
|
-
`(NTS DB)`
|
|
427
|
-
)
|
|
428
|
-
),
|
|
429
|
-
|
|
430
|
-
status: 501,
|
|
431
|
-
errorCode: 'NTS-DBS-DDD0',
|
|
432
|
-
});
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
await this.dataModel
|
|
436
|
-
.deleteMany({ [this.depDataName]: dependencyId })
|
|
437
|
-
.catch((error): void => {
|
|
438
|
-
throw new DynamoFM_Error({
|
|
439
|
-
...this._getDefaultErrorSettings('deleteDataByDependencyId', error),
|
|
440
|
-
|
|
441
|
-
errorCode: 'NTS-DBS-DDD1',
|
|
442
|
-
additionalContent: { dependencyId },
|
|
443
|
-
message:
|
|
444
|
-
`delete ${this.dataParams.dbName} by ${this.depDataName} was unsuccessful (NTS DB)`,
|
|
445
|
-
});
|
|
446
|
-
});
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
/**
|
|
450
|
-
* returns search result for searchBy object params
|
|
451
|
-
* can use lists or xRange values for searchBy obj properties
|
|
452
|
-
*
|
|
453
|
-
* @param filterBy filter
|
|
454
|
-
* @param narrowByDependencyIds id
|
|
455
|
-
* @returns dataList
|
|
456
|
-
*/
|
|
457
|
-
async searchData(
|
|
458
|
-
filterBy: DynamoNTS_DBFilter<T>,
|
|
459
|
-
narrowByDependencyIds?: string[]
|
|
460
|
-
): Promise<T[]> {
|
|
461
|
-
const filter = {};
|
|
462
|
-
|
|
463
|
-
if (0 < narrowByDependencyIds.length) {
|
|
464
|
-
if (!this.depDataName) {
|
|
465
|
-
throw new DynamoFM_Error({
|
|
466
|
-
...this._getDefaultErrorSettings(
|
|
467
|
-
'searchData',
|
|
468
|
-
new Error(
|
|
469
|
-
`dependencyDataIdKey not setted up for this db-service (${this.dataParams.dbName}) ` +
|
|
470
|
-
`(NTS DB)`
|
|
471
|
-
)
|
|
472
|
-
),
|
|
473
|
-
|
|
474
|
-
status: 501,
|
|
475
|
-
errorCode: 'NTS-DBS-SD0',
|
|
476
|
-
});
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
filter[this.depDataName] = { $in: narrowByDependencyIds };
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
await this.dataParams.properties.forEach((modelParam: DynamoFM_DataProperty_Params): void => {
|
|
483
|
-
if (
|
|
484
|
-
(filterBy[modelParam.key] !== null && filterBy[modelParam.key] !== undefined) ||
|
|
485
|
-
filterBy[modelParam.key + 'Range']
|
|
486
|
-
) {
|
|
487
|
-
if (modelParam.key.includes('Range') || modelParam.type.includes('[]')) {
|
|
488
|
-
// inverz search filter (for Range and Array functions)
|
|
489
|
-
if (modelParam.key.includes('Range')) {
|
|
490
|
-
const searchParamKeyWithoutRange = modelParam.key.split('Range')[0];
|
|
491
|
-
|
|
492
|
-
if (
|
|
493
|
-
filterBy[searchParamKeyWithoutRange] !== null &&
|
|
494
|
-
filterBy[searchParamKeyWithoutRange] !== undefined
|
|
495
|
-
) {
|
|
496
|
-
filter[modelParam.key] = {
|
|
497
|
-
from: { $lte: filterBy[searchParamKeyWithoutRange] },
|
|
498
|
-
to: { $gte: filterBy[searchParamKeyWithoutRange] }
|
|
499
|
-
};
|
|
500
|
-
}
|
|
501
|
-
} else {
|
|
502
|
-
if (
|
|
503
|
-
filterBy[modelParam.key] !== null &&
|
|
504
|
-
filterBy[modelParam.key] !== undefined
|
|
505
|
-
) {
|
|
506
|
-
filter[modelParam.key] = { $in: filterBy[modelParam.key] };
|
|
507
|
-
}
|
|
508
|
-
}
|
|
509
|
-
} else {
|
|
510
|
-
// basic search filter
|
|
511
|
-
if (filterBy[modelParam.key + 'Range']) {
|
|
512
|
-
if (filterBy[modelParam.key + 'Range'].from || filterBy[modelParam.key + 'Range'].to) {
|
|
513
|
-
filter[modelParam.key] = {};
|
|
514
|
-
|
|
515
|
-
if (filterBy[modelParam.key + 'Range'].from) {
|
|
516
|
-
filter[modelParam.key].$gte = filterBy[modelParam.key + 'Range'].from;
|
|
517
|
-
}
|
|
518
|
-
|
|
519
|
-
if (filterBy[modelParam.key + 'Range'].to) {
|
|
520
|
-
filter[modelParam.key].$lte = filterBy[modelParam.key + 'Range'].to;
|
|
521
|
-
}
|
|
522
|
-
}
|
|
523
|
-
} else if (filterBy[modelParam.key] !== null && filterBy[modelParam.key] !== undefined) {
|
|
524
|
-
if (filterBy[modelParam.key].lenght > 0) {
|
|
525
|
-
filter[modelParam.key] = { $in: filterBy[modelParam.key] };
|
|
526
|
-
}
|
|
527
|
-
}
|
|
528
|
-
}
|
|
529
|
-
}
|
|
530
|
-
});
|
|
531
|
-
|
|
532
|
-
const dataList: T[] = await this.dataModel
|
|
533
|
-
.find(filter)
|
|
534
|
-
.then((res): T[] => res as T[] ?? [])
|
|
535
|
-
.catch((error): void => {
|
|
536
|
-
throw new DynamoFM_Error({
|
|
537
|
-
...this._getDefaultErrorSettings('searchData', error),
|
|
538
|
-
|
|
539
|
-
errorCode: 'NTS-DBS-SD1',
|
|
540
|
-
additionalContent: { filterBy, narrowByDependencyIds },
|
|
541
|
-
message: `search ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
|
|
542
|
-
});
|
|
543
|
-
});
|
|
544
|
-
|
|
545
|
-
dataList.forEach((data: T): void => {
|
|
546
|
-
data = this.stringifyDataId(data, 'searchData');
|
|
547
|
-
});
|
|
548
|
-
|
|
549
|
-
return dataList;
|
|
550
|
-
}
|
|
551
|
-
|
|
552
|
-
// ----------------------------------------------------------------------------------
|
|
553
|
-
// ----------------------------------------------------------------------------------
|
|
554
|
-
// ----------------------------------------------------------------------------------
|
|
555
|
-
// DIRECT Basic MONGOOSE FUNCTIONS (with error handlings)
|
|
556
|
-
|
|
557
|
-
/**
|
|
558
|
-
* Find the data first by any of its parameters,
|
|
559
|
-
* !!!: throws error if not found (errorCode on not found: NTS-DBS-FO1)
|
|
560
|
-
*
|
|
561
|
-
* @param filterBy if you can, use unique parameters for find!
|
|
562
|
-
*
|
|
563
|
-
* @example
|
|
564
|
-
* // by email:
|
|
565
|
-
* { email: email }
|
|
566
|
-
* //
|
|
567
|
-
* @example
|
|
568
|
-
* // or by id that is in list:
|
|
569
|
-
* { userIds: { $in: this.userId } }
|
|
570
|
-
* //
|
|
571
|
-
* @example
|
|
572
|
-
* // or by number or Date that is Greater Than AND Less Than:
|
|
573
|
-
* { points: { $gt: 2, $lt: 14 } }
|
|
574
|
-
* // further tools (syntax matches with $gt):
|
|
575
|
-
* $eq: // Matches values that are EQual to a specified value.
|
|
576
|
-
* $gte: // Matches values that are Greater Than OR Equal to a specified value.
|
|
577
|
-
* $lte: // Matches values that are Less Than or Equal to a specified value.
|
|
578
|
-
* $ne: // Matches all values that are Not Equal to a specified value.
|
|
579
|
-
* $nin: // Matches None of the values specified IN an array.
|
|
580
|
-
* //
|
|
581
|
-
* @returns {T} data: T
|
|
582
|
-
*/
|
|
583
|
-
async findOne(filterBy: DynamoNTS_DBFilter<T>): Promise<T> {
|
|
584
|
-
let data: T = await this.dataModel
|
|
585
|
-
.findOne(filterBy)
|
|
586
|
-
.then((res): T => res as T ?? null)
|
|
587
|
-
.catch((error): void => {
|
|
588
|
-
throw new DynamoFM_Error({
|
|
589
|
-
...this._getDefaultErrorSettings('findOne', error),
|
|
590
|
-
|
|
591
|
-
errorCode: 'NTS-DBS-FO0',
|
|
592
|
-
additionalContent: { filterBy },
|
|
593
|
-
message: `findOne ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
|
|
594
|
-
});
|
|
595
|
-
});
|
|
596
|
-
|
|
597
|
-
data = this.stringifyDataId(data, 'findOne');
|
|
598
|
-
|
|
599
|
-
return data;
|
|
600
|
-
}
|
|
601
|
-
|
|
602
|
-
/**
|
|
603
|
-
* #MONGOOSE FUNCTION
|
|
604
|
-
* Find the data first by any of its parameters,
|
|
605
|
-
* !!!: throws error if not found (errorCode on not found: NTS-DBS-F1)
|
|
606
|
-
*
|
|
607
|
-
* @param filterBy if you can, use unique parameters for find!
|
|
608
|
-
*
|
|
609
|
-
* @example
|
|
610
|
-
* // by email:
|
|
611
|
-
* { email: email }
|
|
612
|
-
* //
|
|
613
|
-
* @example
|
|
614
|
-
* // or by id that is in list:
|
|
615
|
-
* { userIds: { $in: this.userId } }
|
|
616
|
-
* //
|
|
617
|
-
* @example
|
|
618
|
-
* // or by number or Date that is Greater Than AND Less Than:
|
|
619
|
-
* { points: { $gt: 2, $lt: 14 } }
|
|
620
|
-
* // further tools (syntax matches with $gt):
|
|
621
|
-
* $eq: // Matches values that are EQual to a specified value.
|
|
622
|
-
* $gte: // Matches values that are Greater Than OR Equal to a specified value.
|
|
623
|
-
* $lte: // Matches values that are Less Than or Equal to a specified value.
|
|
624
|
-
* $ne: // Matches all values that are Not Equal to a specified value.
|
|
625
|
-
* $nin: // Matches None of the values specified IN an array.
|
|
626
|
-
* //
|
|
627
|
-
* @returns {T[]} dataList: T[]
|
|
628
|
-
*/
|
|
629
|
-
async find(filterBy: DynamoNTS_DBFilter<T>): Promise<T[]> {
|
|
630
|
-
const dataList: T[] = await this.dataModel
|
|
631
|
-
.find(filterBy)
|
|
632
|
-
.then((res): T[] => res as T[] ?? [])
|
|
633
|
-
.catch((error): void => {
|
|
634
|
-
throw new DynamoFM_Error({
|
|
635
|
-
...this._getDefaultErrorSettings('find', error),
|
|
636
|
-
|
|
637
|
-
errorCode: 'NTS-DBS-F0',
|
|
638
|
-
additionalContent: { filterBy },
|
|
639
|
-
message: `find ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
|
|
640
|
-
});
|
|
641
|
-
});
|
|
642
|
-
|
|
643
|
-
dataList.forEach((data: T): void => {
|
|
644
|
-
data = this.stringifyDataId(data, 'find');
|
|
645
|
-
});
|
|
646
|
-
|
|
647
|
-
return dataList as T[];
|
|
648
|
-
}
|
|
649
|
-
|
|
650
|
-
/**
|
|
651
|
-
* #MONGOOSE FUNCTION
|
|
652
|
-
* Find the data first by any of its parameters
|
|
653
|
-
*
|
|
654
|
-
* @param filterBy if you can, use unique parameters for find!
|
|
655
|
-
*
|
|
656
|
-
* @example
|
|
657
|
-
* // by email:
|
|
658
|
-
* { email: email }
|
|
659
|
-
* //
|
|
660
|
-
* @example
|
|
661
|
-
* // or by id that is in list:
|
|
662
|
-
* { userIds: { $in: this.userId } }
|
|
663
|
-
* //
|
|
664
|
-
* @example
|
|
665
|
-
* // or by number or Date that is Greater Than AND Less Than:
|
|
666
|
-
* { points: { $gt: 2, $lt: 14 } }
|
|
667
|
-
* // further tools (syntax matches with $gt):
|
|
668
|
-
* $eq: // Matches values that are EQual to a specified value.
|
|
669
|
-
* $gte: // Matches values that are Greater Than OR Equal to a specified value.
|
|
670
|
-
* $lte: // Matches values that are Less Than or Equal to a specified value.
|
|
671
|
-
* $ne: // Matches all values that are Not Equal to a specified value.
|
|
672
|
-
* $nin: // Matches None of the values specified IN an array.
|
|
673
|
-
* //
|
|
674
|
-
* @param page page
|
|
675
|
-
* @param pageSize pageSize
|
|
676
|
-
* @param sort
|
|
677
|
-
* @example
|
|
678
|
-
* // by dateTime (this uses the basic sort function):
|
|
679
|
-
* { dateTime: -1 }
|
|
680
|
-
* //
|
|
681
|
-
* @returns {T[]} dataList: T[]
|
|
682
|
-
*/
|
|
683
|
-
async findWithPaging(
|
|
684
|
-
filterBy: DynamoNTS_DBFilter<T>,
|
|
685
|
-
page: number,
|
|
686
|
-
pageSize: number,
|
|
687
|
-
sort?: any
|
|
688
|
-
): Promise<T[]> {
|
|
689
|
-
const dataList: T[] = await this.dataModel
|
|
690
|
-
.find(filterBy)
|
|
691
|
-
.sort(sort)
|
|
692
|
-
.skip(page * pageSize)
|
|
693
|
-
.limit(pageSize)
|
|
694
|
-
.then((res): T => res ?? [])
|
|
695
|
-
.catch((error): void => {
|
|
696
|
-
throw new DynamoFM_Error({
|
|
697
|
-
...this._getDefaultErrorSettings('findWithPaging', error),
|
|
698
|
-
|
|
699
|
-
errorCode: 'NTS-DBS-WP0',
|
|
700
|
-
additionalContent: { filterBy, page, pageSize, sort },
|
|
701
|
-
message: `findWithPaging ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
|
|
702
|
-
});
|
|
703
|
-
});
|
|
704
|
-
|
|
705
|
-
dataList.forEach((data: T): void => {
|
|
706
|
-
data = this.stringifyDataId(data, 'findWithPaging');
|
|
707
|
-
});
|
|
708
|
-
|
|
709
|
-
return dataList;
|
|
710
|
-
}
|
|
711
|
-
|
|
712
|
-
/**
|
|
713
|
-
* #MONGOOSE FUNCTION
|
|
714
|
-
* Find data by _id and update
|
|
715
|
-
* !!!: throws error if not found (errorCode on not found: )
|
|
716
|
-
*
|
|
717
|
-
* @param id id
|
|
718
|
-
* @param update update
|
|
719
|
-
* @returns data
|
|
720
|
-
*/
|
|
721
|
-
async findByIdAndUpdate(id: string, update: DynamoNTS_DBUpdate<T>, issuer: string): Promise<T> {
|
|
722
|
-
update.__lastModified = new Date();
|
|
723
|
-
update.__lastModifiedBy = issuer;
|
|
724
|
-
|
|
725
|
-
let newData: T = await this.dataModel
|
|
726
|
-
.findByIdAndUpdate(id, update)
|
|
727
|
-
.then((res): T => res?.toObject() as T ?? null)
|
|
728
|
-
.catch((error): void => {
|
|
729
|
-
throw new DynamoFM_Error({
|
|
730
|
-
...this._getDefaultErrorSettings('findByIdAndUpdate', error, issuer),
|
|
731
|
-
|
|
732
|
-
errorCode: 'NTS-DBS-FIU0',
|
|
733
|
-
additionalContent: { id, update },
|
|
734
|
-
message: `findByIdAndUpdate ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
|
|
735
|
-
issuer,
|
|
736
|
-
});
|
|
737
|
-
});
|
|
738
|
-
|
|
739
|
-
newData = this.stringifyDataId(newData, 'findByIdAndUpdate');
|
|
740
|
-
|
|
741
|
-
return newData;
|
|
742
|
-
}
|
|
743
|
-
|
|
744
|
-
/**
|
|
745
|
-
* #MONGOOSE FUNCTION
|
|
746
|
-
* Find the data first by any of its parameters
|
|
747
|
-
*
|
|
748
|
-
* @param filter This uses the basic Mongoose updateOne.
|
|
749
|
-
* If you can, use unique parameters for find!
|
|
750
|
-
* @example
|
|
751
|
-
* // by email:
|
|
752
|
-
* { email: email }
|
|
753
|
-
* //
|
|
754
|
-
* @example
|
|
755
|
-
* // or by id that is in list:
|
|
756
|
-
* { userIds: { $in: this.userId } }
|
|
757
|
-
* //
|
|
758
|
-
* @example
|
|
759
|
-
* // or by number or Date that is Greater Than AND Less Than:
|
|
760
|
-
* { points: { $gt: 2, $lt: 14 } }
|
|
761
|
-
* // further tools (syntax matches with $gt):
|
|
762
|
-
* $eq: // Matches values that are EQual to a specified value.
|
|
763
|
-
* $gte: // Matches values that are Greater Than OR Equal to a specified value.
|
|
764
|
-
* $lte: // Matches values that are Less Than or Equal to a specified value.
|
|
765
|
-
* $ne: // Matches all values that are Not Equal to a specified value.
|
|
766
|
-
* $nin: // Matches None of the values specified IN an array.
|
|
767
|
-
* //
|
|
768
|
-
*
|
|
769
|
-
* @param update this uses the basic Mongoose updateOne
|
|
770
|
-
* @example
|
|
771
|
-
* // increase a specific value (here by 15):
|
|
772
|
-
* { $inc: { popularity: 15 } }
|
|
773
|
-
* //
|
|
774
|
-
* @example
|
|
775
|
-
* // or add element to a list:
|
|
776
|
-
* { $push: { reactions: this.newReaction }
|
|
777
|
-
* // or add multiple elements to a list
|
|
778
|
-
* { $push: { schedule: {$each: [ monday, tuesday, wednesday ] } } }
|
|
779
|
-
* //
|
|
780
|
-
* @example
|
|
781
|
-
* // or all at once
|
|
782
|
-
* {
|
|
783
|
-
* $inc: { popularity: this.newVote.amount },
|
|
784
|
-
* emailVerified: true,
|
|
785
|
-
* $push: { reactions: this.newReaction }
|
|
786
|
-
* }
|
|
787
|
-
* // further tools (syntax matches with $inc):
|
|
788
|
-
* $currentDate: // Sets the value of a field to current date, either as a Date or a Timestamp.
|
|
789
|
-
* $min: // Only updates the field if the specified value is less than the existing field value.
|
|
790
|
-
* $max: // Only updates the field if the specified value is greater than the existing field value.
|
|
791
|
-
* $mul: // Multiplies the value of the field by the specified amount.
|
|
792
|
-
* $rename: // Renames a field.
|
|
793
|
-
* $unset: // Removes the specified field from a document. (set: "" to value)
|
|
794
|
-
* //
|
|
795
|
-
*/
|
|
796
|
-
async updateOne(
|
|
797
|
-
filterBy: DynamoNTS_DBFilter<T>,
|
|
798
|
-
update: DynamoNTS_DBUpdate<T>,
|
|
799
|
-
issuer: string
|
|
800
|
-
): Promise<void> {
|
|
801
|
-
update.__lastModified = new Date();
|
|
802
|
-
update.__lastModifiedBy = issuer;
|
|
803
|
-
|
|
804
|
-
await this.dataModel
|
|
805
|
-
.updateOne(filterBy, update)
|
|
806
|
-
.catch((error): void => {
|
|
807
|
-
throw new DynamoFM_Error({
|
|
808
|
-
...this._getDefaultErrorSettings('updateOne', error, issuer),
|
|
809
|
-
|
|
810
|
-
errorCode: 'NTS-DBS-UO0',
|
|
811
|
-
additionalContent: { filterBy, update },
|
|
812
|
-
message: `updateOne ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
|
|
813
|
-
issuer,
|
|
814
|
-
});
|
|
815
|
-
});
|
|
816
|
-
}
|
|
817
|
-
|
|
818
|
-
/**
|
|
819
|
-
* #MONGOOSE FUNCTION
|
|
820
|
-
* update one parameter by a specific
|
|
821
|
-
*
|
|
822
|
-
* @param filter This uses the basic Mongoose updateMany.
|
|
823
|
-
* @example
|
|
824
|
-
* // by email:
|
|
825
|
-
* { email: email }
|
|
826
|
-
* //
|
|
827
|
-
* @example
|
|
828
|
-
* // or by id that is in list:
|
|
829
|
-
* { userIds: { $in: this.userId } }
|
|
830
|
-
* //
|
|
831
|
-
* @example
|
|
832
|
-
* // or by number or Date that is Greater Than AND Less Than:
|
|
833
|
-
* { points: { $gt: 2, $lt: 14 } }
|
|
834
|
-
* // further tools (syntax matches with $gt):
|
|
835
|
-
* $eq: // Matches values that are EQual to a specified value.
|
|
836
|
-
* $gte: // Matches values that are Greater Than OR Equal to a specified value.
|
|
837
|
-
* $lte: // Matches values that are Less Than or Equal to a specified value.
|
|
838
|
-
* $ne: // Matches all values that are Not Equal to a specified value.
|
|
839
|
-
* $nin: // Matches None of the values specified IN an array.
|
|
840
|
-
* //
|
|
841
|
-
*
|
|
842
|
-
* @param update this uses the basic Mongoose updateOne
|
|
843
|
-
* @example
|
|
844
|
-
* // increase a specific value (here by 15):
|
|
845
|
-
* { $inc: { popularity: 15 } }
|
|
846
|
-
* //
|
|
847
|
-
* @example
|
|
848
|
-
* // or add element to a list:
|
|
849
|
-
* { $push: { reactions: this.newReaction }
|
|
850
|
-
* // or add multiple elements to a list
|
|
851
|
-
* { $push: { schedule: {$each: [ monday, tuesday, wednesday ] } } }
|
|
852
|
-
* //
|
|
853
|
-
* @example
|
|
854
|
-
* // or all at once
|
|
855
|
-
* {
|
|
856
|
-
* $inc: { popularity: this.newVote.amount },
|
|
857
|
-
* emailVerified: true,
|
|
858
|
-
* $push: { reactions: this.newReaction }
|
|
859
|
-
* }
|
|
860
|
-
* // further tools (syntax matches with $inc):
|
|
861
|
-
* $currentDate: // Sets the value of a field to current date, either as a Date or a Timestamp.
|
|
862
|
-
* $min: // Only updates the field if the specified value is less than the existing field value.
|
|
863
|
-
* $max: // Only updates the field if the specified value is greater than the existing field value.
|
|
864
|
-
* $mul: // Multiplies the value of the field by the specified amount.
|
|
865
|
-
* $rename: // Renames a field.
|
|
866
|
-
* $unset: // Removes the specified field from a document. (set: "" to value)
|
|
867
|
-
* //
|
|
868
|
-
*/
|
|
869
|
-
async updateMany(
|
|
870
|
-
filterBy: DynamoNTS_DBFilter<T>,
|
|
871
|
-
update: DynamoNTS_DBUpdate<T>,
|
|
872
|
-
issuer: string
|
|
873
|
-
): Promise<void> {
|
|
874
|
-
update.__lastModified = new Date();
|
|
875
|
-
update.__lastModifiedBy = issuer;
|
|
876
|
-
|
|
877
|
-
await this.dataModel
|
|
878
|
-
.updateMany(filterBy, update)
|
|
879
|
-
.catch((error): void => {
|
|
880
|
-
throw new DynamoFM_Error({
|
|
881
|
-
...this._getDefaultErrorSettings('updateMany', error, issuer),
|
|
882
|
-
|
|
883
|
-
errorCode: 'NTS-DBS-UM0',
|
|
884
|
-
additionalContent: { filterBy, update },
|
|
885
|
-
message: `updateMany ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
|
|
886
|
-
issuer,
|
|
887
|
-
});
|
|
888
|
-
});
|
|
889
|
-
}
|
|
890
|
-
|
|
891
|
-
// ----------------------------------------------------------------------------------
|
|
892
|
-
// ----------------------------------------------------------------------------------
|
|
893
|
-
// ----------------------------------------------------------------------------------
|
|
894
|
-
// PRIVATE FUNCTIONS
|
|
895
|
-
|
|
896
|
-
private stringifyDataId(data: T, fnName: string): T {
|
|
897
|
-
if (data?._id && (typeof data._id !== 'string' || typeof data._id === 'object')) {
|
|
898
|
-
data._id = `${data._id}`;
|
|
899
|
-
|
|
900
|
-
if (typeof data._id !== 'string' || typeof data._id === 'object') {
|
|
901
|
-
data = JSON.parse(JSON.stringify(data));
|
|
902
|
-
|
|
903
|
-
if (typeof data._id !== 'string' || typeof data._id === 'object') {
|
|
904
|
-
DynamoFM_Log.error(
|
|
905
|
-
`data._id stringifying failed! Please notfiy the DynamoNTS developers! ` +
|
|
906
|
-
`(${fnName})`,
|
|
907
|
-
new Error()
|
|
908
|
-
);
|
|
909
|
-
}
|
|
910
|
-
}
|
|
911
|
-
}
|
|
912
|
-
|
|
913
|
-
return data;
|
|
914
|
-
}
|
|
915
|
-
|
|
916
|
-
private _getDefaultErrorSettings(
|
|
917
|
-
fnName: string,
|
|
918
|
-
error: DynamoFM_AnyError,
|
|
919
|
-
issuer?: string
|
|
920
|
-
): DynamoFM_Error_Settings {
|
|
921
|
-
return {
|
|
922
|
-
status: (error as DynamoFM_Error)?.___status ?? 500,
|
|
923
|
-
message: (error as Error)?.message ?? `${fnName} was UNSUCCESFUL (NTS-DB)`,
|
|
924
|
-
addECToUserMsg: !(error as DynamoFM_Error)?.__userMessage,
|
|
925
|
-
userMessage: (error as DynamoFM_Error)?.__userMessage ?? this.defaultErrorUserMsg,
|
|
926
|
-
issuer: issuer,
|
|
927
|
-
issuerService: this.serviceName,
|
|
928
|
-
error: error,
|
|
929
|
-
};
|
|
930
|
-
}
|
|
931
|
-
|
|
932
|
-
// ----------------------------------------------------------------------------------
|
|
933
|
-
// PRIVATE BUILD FUNCTIONS
|
|
934
|
-
|
|
935
|
-
/**
|
|
936
|
-
* builds and returns mongoose schema
|
|
937
|
-
* @returns schema
|
|
938
|
-
*/
|
|
939
|
-
private getSchema(): mongoose.Schema {
|
|
940
|
-
let schema: any = this.buildMongooseSchemaByModelParams();
|
|
941
|
-
|
|
942
|
-
schema = this.addDynamoFM_MetadataToSchema(schema);
|
|
943
|
-
|
|
944
|
-
// tslint:disable-next-line: no-string-literal
|
|
945
|
-
if (schema['_id']) {
|
|
946
|
-
// tslint:disable-next-line: no-string-literal
|
|
947
|
-
delete schema['_id'];
|
|
948
|
-
}
|
|
949
|
-
|
|
950
|
-
return new Schema(schema);
|
|
951
|
-
}
|
|
952
|
-
|
|
953
|
-
/**
|
|
954
|
-
* builds mongoose schema building settings object by dynamoDataModelParamsList
|
|
955
|
-
* @param params DynamoBEDataPropertyParams
|
|
956
|
-
* @returns mongoose schema object
|
|
957
|
-
*/
|
|
958
|
-
private buildMongooseSchemaByModelParams(params?: DynamoFM_DataProperty_Params[]): any {
|
|
959
|
-
const schemaSettingsObj = {};
|
|
960
|
-
|
|
961
|
-
if (!params) {
|
|
962
|
-
params = this.dataParams.properties;
|
|
963
|
-
}
|
|
964
|
-
|
|
965
|
-
params.forEach((property: DynamoFM_DataProperty_Params): void => {
|
|
966
|
-
const beType = this.getBEType(property.type);
|
|
967
|
-
|
|
968
|
-
if (
|
|
969
|
-
beType !== Object ||
|
|
970
|
-
!property?.subObjectParams ||
|
|
971
|
-
property?.subObjectParams?.length == 0
|
|
972
|
-
) {
|
|
973
|
-
schemaSettingsObj[property.key] = {
|
|
974
|
-
type: beType,
|
|
975
|
-
};
|
|
976
|
-
|
|
977
|
-
if (property.index) {
|
|
978
|
-
schemaSettingsObj[property.key].index = true;
|
|
979
|
-
}
|
|
980
|
-
|
|
981
|
-
if (property.unique) {
|
|
982
|
-
schemaSettingsObj[property.key].unique = true;
|
|
983
|
-
}
|
|
984
|
-
|
|
985
|
-
if (property.required) {
|
|
986
|
-
schemaSettingsObj[property.key].required = true;
|
|
987
|
-
}
|
|
988
|
-
|
|
989
|
-
if (property.minlength) {
|
|
990
|
-
schemaSettingsObj[property.key].minlength = property.minlength;
|
|
991
|
-
}
|
|
992
|
-
|
|
993
|
-
if (property.maxlength) {
|
|
994
|
-
schemaSettingsObj[property.key].maxlength = property.maxlength;
|
|
995
|
-
}
|
|
996
|
-
} else {
|
|
997
|
-
schemaSettingsObj[property.key] = this.buildMongooseSchemaByModelParams(
|
|
998
|
-
property.subObjectParams
|
|
999
|
-
);
|
|
1000
|
-
}
|
|
1001
|
-
});
|
|
1002
|
-
|
|
1003
|
-
return schemaSettingsObj;
|
|
1004
|
-
}
|
|
1005
|
-
|
|
1006
|
-
private getBEType(
|
|
1007
|
-
type: string
|
|
1008
|
-
): String | Number | Boolean | Object | Function | Array<any> | Date {
|
|
1009
|
-
switch (type.toLocaleLowerCase()) {
|
|
1010
|
-
|
|
1011
|
-
case 'string':
|
|
1012
|
-
return String;
|
|
1013
|
-
|
|
1014
|
-
case 'number':
|
|
1015
|
-
return Number;
|
|
1016
|
-
|
|
1017
|
-
case 'boolean':
|
|
1018
|
-
return Boolean;
|
|
1019
|
-
|
|
1020
|
-
case 'date':
|
|
1021
|
-
return Date;
|
|
1022
|
-
|
|
1023
|
-
case 'object':
|
|
1024
|
-
return Object;
|
|
1025
|
-
|
|
1026
|
-
default:
|
|
1027
|
-
if (type.includes('[]') || type.includes('array')) {
|
|
1028
|
-
return Array;
|
|
1029
|
-
} else if (type.includes('=>') || type.includes('function')) {
|
|
1030
|
-
return Function;
|
|
1031
|
-
} else {
|
|
1032
|
-
return Object;
|
|
1033
|
-
}
|
|
1034
|
-
}
|
|
1035
|
-
}
|
|
1036
|
-
|
|
1037
|
-
/**
|
|
1038
|
-
* adds dynamo metadata settings to any mongoose schema building settings object
|
|
1039
|
-
* @param schema schema to update
|
|
1040
|
-
* @returns updated schema
|
|
1041
|
-
*/
|
|
1042
|
-
private addDynamoFM_MetadataToSchema(schema: any): any {
|
|
1043
|
-
schema.__created = { type: Date };
|
|
1044
|
-
schema.__createdBy = { type: String };
|
|
1045
|
-
schema.__lastModified = { type: Date };
|
|
1046
|
-
schema.__lastModifiedBy = { type: String };
|
|
1047
|
-
|
|
1048
|
-
return schema;
|
|
1049
|
-
}
|
|
1050
|
-
|
|
1051
|
-
/**
|
|
1052
|
-
* sets depDataKey
|
|
1053
|
-
*/
|
|
1054
|
-
private lookForDependencyDataSettings(): void {
|
|
1055
|
-
const dependencyParam: DynamoFM_DataProperty_Params = this.dataParams.properties.find(
|
|
1056
|
-
(modelParams: DynamoFM_DataProperty_Params): boolean => modelParams.isDependencyHook
|
|
1057
|
-
);
|
|
1058
|
-
|
|
1059
|
-
if (dependencyParam) {
|
|
1060
|
-
this.depDataName = dependencyParam.key;
|
|
1061
|
-
}
|
|
1062
|
-
}
|
|
1063
|
-
}
|
|
1
|
+
|
|
2
|
+
import * as mongoose from 'mongoose';
|
|
3
|
+
import { Schema } from 'mongoose';
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
DynamoFM_Metadata, DynamoFM_DataModel_Params, DynamoFM_DataProperty_Params, DynamoFM_Error,
|
|
7
|
+
DynamoFM_Log, DynamoFM_AnyError, DynamoFM_Error_Settings, DynamoFM_ErrorLevel
|
|
8
|
+
} from '@futdevpro/fsm-dynamo';
|
|
9
|
+
import { DynamoNTS_DBFilter } from '../../_models/types/db-filter.type';
|
|
10
|
+
import { DynamoNTS_DBUpdate } from '../../_models/types/db-update.type';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* DB Service for MongoDB
|
|
14
|
+
*/
|
|
15
|
+
export class DynamoNTS_DBService<T extends DynamoFM_Metadata> {
|
|
16
|
+
|
|
17
|
+
serviceName: string;
|
|
18
|
+
|
|
19
|
+
dataModel/* : mongoose.Model<T> */; // = mongoose.model(this.dataParams.dbName, this.getSchema());
|
|
20
|
+
|
|
21
|
+
private depDataName: string;
|
|
22
|
+
|
|
23
|
+
defaultErrorUserMsg =
|
|
24
|
+
`We encountered an unhandled DB Error, ` +
|
|
25
|
+
`\nplease contact the responsible development team.`;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @param dataName name the model
|
|
29
|
+
* @param typeSample sample data for scheme creation (dont include DynamoFM_Metadata!)
|
|
30
|
+
* @param schemaSettings additional settings for specific parameters as unique, required, minlength or maxlength
|
|
31
|
+
* schemaSettings also MUST contain specific types that differs from the listed above (Array, Date)
|
|
32
|
+
*/
|
|
33
|
+
constructor(
|
|
34
|
+
public dataParams: DynamoFM_DataModel_Params
|
|
35
|
+
){
|
|
36
|
+
try {
|
|
37
|
+
this.serviceName = this.constructor?.name;
|
|
38
|
+
|
|
39
|
+
try {
|
|
40
|
+
this.dataModel = mongoose.model(this.dataParams.dbName, this.getSchema());
|
|
41
|
+
} catch (error) {
|
|
42
|
+
if (mongoose.models[this.dataParams.dbName]) {
|
|
43
|
+
this.dataModel = mongoose.models[this.dataParams.dbName];
|
|
44
|
+
} else {
|
|
45
|
+
throw error;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
this.lookForDependencyDataSettings();
|
|
50
|
+
} catch (error) {
|
|
51
|
+
throw new DynamoFM_Error({
|
|
52
|
+
message:
|
|
53
|
+
`DynamoNTS_DBService construction failed! ${dataParams.dataName} - ${this.serviceName}`,
|
|
54
|
+
errorCode: 'NTS-DBS-C00',
|
|
55
|
+
level: DynamoFM_ErrorLevel.critical,
|
|
56
|
+
additionalContent: { dataParams: this.dataParams },
|
|
57
|
+
error,
|
|
58
|
+
});
|
|
59
|
+
/* DynamoFM_Log.error(
|
|
60
|
+
`\nDynamoNTS_DBService ERROR, ` +
|
|
61
|
+
`The dbService construction failed for ${dataParams.dataName}. ${this.serviceName}` +
|
|
62
|
+
`\nERROR: `, error,
|
|
63
|
+
new Error()
|
|
64
|
+
); */
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* save new data
|
|
70
|
+
* @param data data
|
|
71
|
+
* @returns data
|
|
72
|
+
*/
|
|
73
|
+
async createData(data: T, issuer: string): Promise<T> {
|
|
74
|
+
data.__created = new Date();
|
|
75
|
+
data.__lastModified = new Date();
|
|
76
|
+
data.__createdBy = issuer;
|
|
77
|
+
data.__lastModifiedBy = issuer;
|
|
78
|
+
|
|
79
|
+
const dataModel = new this.dataModel(data);
|
|
80
|
+
let newData: T = await dataModel.save().then((res): T => {
|
|
81
|
+
if (res) {
|
|
82
|
+
return res?.toObject() as T;
|
|
83
|
+
|
|
84
|
+
} else {
|
|
85
|
+
throw new DynamoFM_Error({
|
|
86
|
+
...this._getDefaultErrorSettings(
|
|
87
|
+
'createData',
|
|
88
|
+
new Error(
|
|
89
|
+
`createData failed, save ${this.dataParams.dbName} result not found! (NTS DB)`
|
|
90
|
+
),
|
|
91
|
+
issuer
|
|
92
|
+
),
|
|
93
|
+
|
|
94
|
+
status: 204,
|
|
95
|
+
errorCode: 'NTS-DBS-CD1',
|
|
96
|
+
additionalContent: { data },
|
|
97
|
+
issuer,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
}).catch((error): void => {
|
|
101
|
+
if (error?.flag?.includes('DYNAMO')) {
|
|
102
|
+
throw error;
|
|
103
|
+
|
|
104
|
+
} else {
|
|
105
|
+
throw new DynamoFM_Error({
|
|
106
|
+
...this._getDefaultErrorSettings('createData', error, issuer),
|
|
107
|
+
|
|
108
|
+
status: 422,
|
|
109
|
+
errorCode: 'NTS-DBS-CD0',
|
|
110
|
+
additionalContent: { data, dataModel },
|
|
111
|
+
message:
|
|
112
|
+
`createData failed, Create new ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
|
|
113
|
+
issuer,
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
newData = this.stringifyDataId(newData, 'createData');
|
|
119
|
+
|
|
120
|
+
return newData;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Find data by _id and update
|
|
125
|
+
* @param data data
|
|
126
|
+
* @returns data
|
|
127
|
+
*/
|
|
128
|
+
async modifyData(data: T, issuer: string): Promise<T> {
|
|
129
|
+
data.__lastModified = new Date();
|
|
130
|
+
data.__lastModifiedBy = issuer;
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* EZ A SZAR TELJESEN SZAR, nem friss, nem a db-be mentett adatokat ad vissza,
|
|
134
|
+
* átír random value-kat össze vissza, WTF
|
|
135
|
+
* */
|
|
136
|
+
/* let newData: T = */ await this.dataModel.findByIdAndUpdate(data._id, data).then((res) => {
|
|
137
|
+
/* if (res) {
|
|
138
|
+
//return res?.toObject() as T;
|
|
139
|
+
} else {
|
|
140
|
+
throw new DynamoFM_Error({
|
|
141
|
+
...this._getDefaultErrorSettings(
|
|
142
|
+
'modifyData',
|
|
143
|
+
new Error(`modifyData failed, modify ${this.dataParams.dbName} result not found! (NTS DB)`),
|
|
144
|
+
issuer,
|
|
145
|
+
),
|
|
146
|
+
|
|
147
|
+
status: 204,
|
|
148
|
+
errorCode: 'NTS-DBS-FU1',
|
|
149
|
+
additionalContent: { data },
|
|
150
|
+
issuer,
|
|
151
|
+
});
|
|
152
|
+
} */
|
|
153
|
+
}).catch((error): void => {
|
|
154
|
+
throw new DynamoFM_Error({
|
|
155
|
+
...this._getDefaultErrorSettings('modifyData', error, issuer),
|
|
156
|
+
|
|
157
|
+
errorCode: 'NTS-DBS-FU0',
|
|
158
|
+
additionalContent: { data },
|
|
159
|
+
message: `modifyData ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
|
|
160
|
+
issuer,
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
/* newData = this.stringifyDataId(newData, 'modifyData'); */
|
|
165
|
+
|
|
166
|
+
return data;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* returns data by _id,
|
|
171
|
+
* @param id id
|
|
172
|
+
* @returns data
|
|
173
|
+
*/
|
|
174
|
+
async getDataById(id: string): Promise<T> {
|
|
175
|
+
if (!id) {
|
|
176
|
+
throw new DynamoFM_Error({
|
|
177
|
+
...this._getDefaultErrorSettings('getDataById', new Error(`No ID provided! (NTS DB)`)),
|
|
178
|
+
|
|
179
|
+
errorCode: 'NTS-DBS-GI1',
|
|
180
|
+
message: `get ${this.dataParams.dbName} by ID was unsuccessful (NTS DB)`,
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
let data: T = await this.dataModel
|
|
185
|
+
.findById(id)
|
|
186
|
+
.then((res): T => res?.toObject() as T ?? null)
|
|
187
|
+
.catch((error): void => {
|
|
188
|
+
throw new DynamoFM_Error({
|
|
189
|
+
...this._getDefaultErrorSettings('getDataById', error),
|
|
190
|
+
|
|
191
|
+
errorCode: 'NTS-DBS-GI0',
|
|
192
|
+
additionalContent: { id },
|
|
193
|
+
message: `get ${this.dataParams.dbName} by ID was unsuccessful (NTS DB)`,
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
data = this.stringifyDataId(data, 'getDataById');
|
|
198
|
+
|
|
199
|
+
return data;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* get data by dependency data id,
|
|
204
|
+
* !!!: throws error if not found (errorCode on not found: NTS-DBS-GD2)
|
|
205
|
+
*
|
|
206
|
+
* @param dependencyId id
|
|
207
|
+
* @returns data
|
|
208
|
+
*/
|
|
209
|
+
async getDataByDependencyId(dependencyId: string): Promise<T> {
|
|
210
|
+
if (!this.depDataName) {
|
|
211
|
+
throw new DynamoFM_Error({
|
|
212
|
+
...this._getDefaultErrorSettings(
|
|
213
|
+
'getDataByDependencyId',
|
|
214
|
+
new Error(
|
|
215
|
+
`dependencyDataIdKey not setted up for this db-service (${this.dataParams.dbName}) ` +
|
|
216
|
+
`(NTS DB)`
|
|
217
|
+
)
|
|
218
|
+
),
|
|
219
|
+
|
|
220
|
+
status: 501,
|
|
221
|
+
errorCode: 'NTS-DBS-GD0',
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
let data: T = await this.dataModel
|
|
226
|
+
.findOne({ [this.depDataName]: dependencyId })
|
|
227
|
+
.then((res): T => res?.toObject() as T ?? null)
|
|
228
|
+
.catch((error): void => {
|
|
229
|
+
throw new DynamoFM_Error({
|
|
230
|
+
...this._getDefaultErrorSettings('getDataByDependencyId', error),
|
|
231
|
+
|
|
232
|
+
errorCode: 'NTS-DBS-GD1',
|
|
233
|
+
additionalContent: { dependencyId },
|
|
234
|
+
message:
|
|
235
|
+
`get ${this.dataParams.dbName} by ${this.depDataName} was unsuccessful (NTS DB)`,
|
|
236
|
+
});
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
data = this.stringifyDataId(data, 'getDataByDependencyId');
|
|
240
|
+
|
|
241
|
+
return data;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* get data by dependency data id,
|
|
246
|
+
* !!!: throws error if not found (errorCode on not found: NTS-DBS-GLD2)
|
|
247
|
+
*
|
|
248
|
+
* @param dependencyId id
|
|
249
|
+
* @returns dataList
|
|
250
|
+
*/
|
|
251
|
+
async getDataListByDependencyId(dependencyId: string): Promise<T[]> {
|
|
252
|
+
if (!this.depDataName) {
|
|
253
|
+
throw new DynamoFM_Error({
|
|
254
|
+
...this._getDefaultErrorSettings(
|
|
255
|
+
'getDataListByDependencyId',
|
|
256
|
+
new Error(
|
|
257
|
+
`isDependencyHook not setted up for this dataModel (${this.dataParams.dbName}) ` +
|
|
258
|
+
`(NTS DB)`
|
|
259
|
+
)
|
|
260
|
+
),
|
|
261
|
+
|
|
262
|
+
status: 501,
|
|
263
|
+
errorCode: 'NTS-DBS-GLD0',
|
|
264
|
+
additionalContent: {
|
|
265
|
+
dataModel: this.dataModel,
|
|
266
|
+
},
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
const dataList: T[] = await this.dataModel
|
|
271
|
+
.find({ [this.depDataName]: dependencyId })
|
|
272
|
+
.then((res): T[] => res as T[] ?? [])
|
|
273
|
+
.catch((error): void => {
|
|
274
|
+
throw new DynamoFM_Error({
|
|
275
|
+
...this._getDefaultErrorSettings('getDataListByDependencyId', error),
|
|
276
|
+
|
|
277
|
+
errorCode: 'NTS-DBS-GLD1',
|
|
278
|
+
additionalContent: { dependencyId },
|
|
279
|
+
message:
|
|
280
|
+
`get ${this.dataParams.dbName} by ${this.depDataName} was unsuccessful (NTS DB)`,
|
|
281
|
+
});
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
dataList.forEach((data: T): void => {
|
|
285
|
+
data = this.stringifyDataId(data, 'getDataListByDependencyId');
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
return dataList;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
async getDatasByDependencyIds(dependencyIds: string[]): Promise<T[]> {
|
|
292
|
+
if (!this.depDataName) {
|
|
293
|
+
throw new DynamoFM_Error({
|
|
294
|
+
...this._getDefaultErrorSettings(
|
|
295
|
+
'getDatasByDependencyIds',
|
|
296
|
+
new Error(
|
|
297
|
+
`dependencyDataIdKey not setted up for this db-service (${this.dataParams.dbName}) ` +
|
|
298
|
+
`(NTS DB)`
|
|
299
|
+
)
|
|
300
|
+
),
|
|
301
|
+
|
|
302
|
+
status: 501,
|
|
303
|
+
errorCode: 'NTS-DBS-GLDS0',
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const dataList: T[] = await this.dataModel
|
|
308
|
+
.find({ [this.depDataName]: { $in: dependencyIds }})
|
|
309
|
+
.then((res): T[] => res as T[] ?? [])
|
|
310
|
+
.catch((error): void => {
|
|
311
|
+
throw new DynamoFM_Error({
|
|
312
|
+
...this._getDefaultErrorSettings('getDatasByDependencyIds', error),
|
|
313
|
+
|
|
314
|
+
errorCode: 'NTS-DBS-GLDS1',
|
|
315
|
+
additionalContent: { dependencyIds },
|
|
316
|
+
message: `get ${this.dataParams.dbName} by ${this.depDataName} was unsuccessful (NTS DB)`,
|
|
317
|
+
});
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
dataList.forEach((data: T): void => {
|
|
321
|
+
data = this.stringifyDataId(data, 'getDatasByDependencyIds');
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
return dataList;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* get multiple data objects by a list of DependencyIDs,
|
|
329
|
+
* !!!: throws error if not found (errorCode on not found: NTS-DBS-GLDS2)
|
|
330
|
+
*
|
|
331
|
+
* @param ids ids
|
|
332
|
+
* @returns dataList
|
|
333
|
+
*/
|
|
334
|
+
async getDataListByDependencyIds(ids: string[]): Promise<T[]> {
|
|
335
|
+
if (!this.depDataName) {
|
|
336
|
+
throw new DynamoFM_Error({
|
|
337
|
+
...this._getDefaultErrorSettings(
|
|
338
|
+
'getDataListByDependencyIds',
|
|
339
|
+
new Error(
|
|
340
|
+
`dependencyDataIdKey not setted up for this db-service (${this.dataParams.dbName}) ` +
|
|
341
|
+
`(NTS DB)`
|
|
342
|
+
)
|
|
343
|
+
),
|
|
344
|
+
|
|
345
|
+
status: 501,
|
|
346
|
+
errorCode: 'NTS-DBS-GLDS0',
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
const dataList: T[] = await this.dataModel
|
|
351
|
+
.find({ [this.depDataName]: { $in: ids } })
|
|
352
|
+
.then((res): T[] => res as T[] ?? [])
|
|
353
|
+
.catch((error): void => {
|
|
354
|
+
throw new DynamoFM_Error({
|
|
355
|
+
...this._getDefaultErrorSettings('getDataListByDependencyIds', error),
|
|
356
|
+
|
|
357
|
+
errorCode: 'NTS-DBS-GLDS1',
|
|
358
|
+
additionalContent: { ids },
|
|
359
|
+
message:
|
|
360
|
+
`get ${this.dataParams.dbName}s by ${this.depDataName}s was unsuccessful (NTS DB)`,
|
|
361
|
+
});
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
dataList.forEach((data: T): void => {
|
|
365
|
+
data = this.stringifyDataId(data, 'getDataListByDependencyIds');
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
return dataList;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* returns all data from database,
|
|
373
|
+
* !!!: throws error if not found (errorCode on not found: NTS-DBS-GA1)
|
|
374
|
+
*
|
|
375
|
+
* @returns dataList
|
|
376
|
+
*/
|
|
377
|
+
async getAll(): Promise<T[]> {
|
|
378
|
+
const dataList: T[] = await this.dataModel
|
|
379
|
+
.find({})
|
|
380
|
+
.then((res): T[] => res as T[] ?? [])
|
|
381
|
+
.catch((error): void => {
|
|
382
|
+
throw new DynamoFM_Error({
|
|
383
|
+
...this._getDefaultErrorSettings('getAll', error),
|
|
384
|
+
|
|
385
|
+
errorCode: 'NTS-DBS-GA0',
|
|
386
|
+
message: `get all ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
|
|
387
|
+
});
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
dataList.forEach((data: T): void => {
|
|
391
|
+
data = this.stringifyDataId(data, 'getAll');
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
return dataList as T[];
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* deleted data by id
|
|
399
|
+
* @param id id
|
|
400
|
+
*/
|
|
401
|
+
async deleteDataById(id: string): Promise<void> {
|
|
402
|
+
await this.dataModel
|
|
403
|
+
.findByIdAndDelete(id)
|
|
404
|
+
.catch((error): void => {
|
|
405
|
+
throw new DynamoFM_Error({
|
|
406
|
+
...this._getDefaultErrorSettings('deleteDataById', error),
|
|
407
|
+
|
|
408
|
+
errorCode: 'NTS-DBS-DD0',
|
|
409
|
+
additionalContent: { id },
|
|
410
|
+
message: `delete ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
|
|
411
|
+
});
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* deleted data by id
|
|
417
|
+
* @param dependencyId id
|
|
418
|
+
*/
|
|
419
|
+
async deleteDataByDependencyId(dependencyId: string): Promise<void> {
|
|
420
|
+
if (!this.depDataName) {
|
|
421
|
+
throw new DynamoFM_Error({
|
|
422
|
+
...this._getDefaultErrorSettings(
|
|
423
|
+
'deleteDataByDependencyId',
|
|
424
|
+
new Error(
|
|
425
|
+
`dependencyDataIdKey not setted up for this db-service (${this.dataParams.dbName}) ` +
|
|
426
|
+
`(NTS DB)`
|
|
427
|
+
)
|
|
428
|
+
),
|
|
429
|
+
|
|
430
|
+
status: 501,
|
|
431
|
+
errorCode: 'NTS-DBS-DDD0',
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
await this.dataModel
|
|
436
|
+
.deleteMany({ [this.depDataName]: dependencyId })
|
|
437
|
+
.catch((error): void => {
|
|
438
|
+
throw new DynamoFM_Error({
|
|
439
|
+
...this._getDefaultErrorSettings('deleteDataByDependencyId', error),
|
|
440
|
+
|
|
441
|
+
errorCode: 'NTS-DBS-DDD1',
|
|
442
|
+
additionalContent: { dependencyId },
|
|
443
|
+
message:
|
|
444
|
+
`delete ${this.dataParams.dbName} by ${this.depDataName} was unsuccessful (NTS DB)`,
|
|
445
|
+
});
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* returns search result for searchBy object params
|
|
451
|
+
* can use lists or xRange values for searchBy obj properties
|
|
452
|
+
*
|
|
453
|
+
* @param filterBy filter
|
|
454
|
+
* @param narrowByDependencyIds id
|
|
455
|
+
* @returns dataList
|
|
456
|
+
*/
|
|
457
|
+
async searchData(
|
|
458
|
+
filterBy: DynamoNTS_DBFilter<T>,
|
|
459
|
+
narrowByDependencyIds?: string[]
|
|
460
|
+
): Promise<T[]> {
|
|
461
|
+
const filter = {};
|
|
462
|
+
|
|
463
|
+
if (0 < narrowByDependencyIds.length) {
|
|
464
|
+
if (!this.depDataName) {
|
|
465
|
+
throw new DynamoFM_Error({
|
|
466
|
+
...this._getDefaultErrorSettings(
|
|
467
|
+
'searchData',
|
|
468
|
+
new Error(
|
|
469
|
+
`dependencyDataIdKey not setted up for this db-service (${this.dataParams.dbName}) ` +
|
|
470
|
+
`(NTS DB)`
|
|
471
|
+
)
|
|
472
|
+
),
|
|
473
|
+
|
|
474
|
+
status: 501,
|
|
475
|
+
errorCode: 'NTS-DBS-SD0',
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
filter[this.depDataName] = { $in: narrowByDependencyIds };
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
await this.dataParams.properties.forEach((modelParam: DynamoFM_DataProperty_Params): void => {
|
|
483
|
+
if (
|
|
484
|
+
(filterBy[modelParam.key] !== null && filterBy[modelParam.key] !== undefined) ||
|
|
485
|
+
filterBy[modelParam.key + 'Range']
|
|
486
|
+
) {
|
|
487
|
+
if (modelParam.key.includes('Range') || modelParam.type.includes('[]')) {
|
|
488
|
+
// inverz search filter (for Range and Array functions)
|
|
489
|
+
if (modelParam.key.includes('Range')) {
|
|
490
|
+
const searchParamKeyWithoutRange = modelParam.key.split('Range')[0];
|
|
491
|
+
|
|
492
|
+
if (
|
|
493
|
+
filterBy[searchParamKeyWithoutRange] !== null &&
|
|
494
|
+
filterBy[searchParamKeyWithoutRange] !== undefined
|
|
495
|
+
) {
|
|
496
|
+
filter[modelParam.key] = {
|
|
497
|
+
from: { $lte: filterBy[searchParamKeyWithoutRange] },
|
|
498
|
+
to: { $gte: filterBy[searchParamKeyWithoutRange] }
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
} else {
|
|
502
|
+
if (
|
|
503
|
+
filterBy[modelParam.key] !== null &&
|
|
504
|
+
filterBy[modelParam.key] !== undefined
|
|
505
|
+
) {
|
|
506
|
+
filter[modelParam.key] = { $in: filterBy[modelParam.key] };
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
} else {
|
|
510
|
+
// basic search filter
|
|
511
|
+
if (filterBy[modelParam.key + 'Range']) {
|
|
512
|
+
if (filterBy[modelParam.key + 'Range'].from || filterBy[modelParam.key + 'Range'].to) {
|
|
513
|
+
filter[modelParam.key] = {};
|
|
514
|
+
|
|
515
|
+
if (filterBy[modelParam.key + 'Range'].from) {
|
|
516
|
+
filter[modelParam.key].$gte = filterBy[modelParam.key + 'Range'].from;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
if (filterBy[modelParam.key + 'Range'].to) {
|
|
520
|
+
filter[modelParam.key].$lte = filterBy[modelParam.key + 'Range'].to;
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
} else if (filterBy[modelParam.key] !== null && filterBy[modelParam.key] !== undefined) {
|
|
524
|
+
if (filterBy[modelParam.key].lenght > 0) {
|
|
525
|
+
filter[modelParam.key] = { $in: filterBy[modelParam.key] };
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
});
|
|
531
|
+
|
|
532
|
+
const dataList: T[] = await this.dataModel
|
|
533
|
+
.find(filter)
|
|
534
|
+
.then((res): T[] => res as T[] ?? [])
|
|
535
|
+
.catch((error): void => {
|
|
536
|
+
throw new DynamoFM_Error({
|
|
537
|
+
...this._getDefaultErrorSettings('searchData', error),
|
|
538
|
+
|
|
539
|
+
errorCode: 'NTS-DBS-SD1',
|
|
540
|
+
additionalContent: { filterBy, narrowByDependencyIds },
|
|
541
|
+
message: `search ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
|
|
542
|
+
});
|
|
543
|
+
});
|
|
544
|
+
|
|
545
|
+
dataList.forEach((data: T): void => {
|
|
546
|
+
data = this.stringifyDataId(data, 'searchData');
|
|
547
|
+
});
|
|
548
|
+
|
|
549
|
+
return dataList;
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
// ----------------------------------------------------------------------------------
|
|
553
|
+
// ----------------------------------------------------------------------------------
|
|
554
|
+
// ----------------------------------------------------------------------------------
|
|
555
|
+
// DIRECT Basic MONGOOSE FUNCTIONS (with error handlings)
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* Find the data first by any of its parameters,
|
|
559
|
+
* !!!: throws error if not found (errorCode on not found: NTS-DBS-FO1)
|
|
560
|
+
*
|
|
561
|
+
* @param filterBy if you can, use unique parameters for find!
|
|
562
|
+
*
|
|
563
|
+
* @example
|
|
564
|
+
* // by email:
|
|
565
|
+
* { email: email }
|
|
566
|
+
* //
|
|
567
|
+
* @example
|
|
568
|
+
* // or by id that is in list:
|
|
569
|
+
* { userIds: { $in: this.userId } }
|
|
570
|
+
* //
|
|
571
|
+
* @example
|
|
572
|
+
* // or by number or Date that is Greater Than AND Less Than:
|
|
573
|
+
* { points: { $gt: 2, $lt: 14 } }
|
|
574
|
+
* // further tools (syntax matches with $gt):
|
|
575
|
+
* $eq: // Matches values that are EQual to a specified value.
|
|
576
|
+
* $gte: // Matches values that are Greater Than OR Equal to a specified value.
|
|
577
|
+
* $lte: // Matches values that are Less Than or Equal to a specified value.
|
|
578
|
+
* $ne: // Matches all values that are Not Equal to a specified value.
|
|
579
|
+
* $nin: // Matches None of the values specified IN an array.
|
|
580
|
+
* //
|
|
581
|
+
* @returns {T} data: T
|
|
582
|
+
*/
|
|
583
|
+
async findOne(filterBy: DynamoNTS_DBFilter<T>): Promise<T> {
|
|
584
|
+
let data: T = await this.dataModel
|
|
585
|
+
.findOne(filterBy)
|
|
586
|
+
.then((res): T => res as T ?? null)
|
|
587
|
+
.catch((error): void => {
|
|
588
|
+
throw new DynamoFM_Error({
|
|
589
|
+
...this._getDefaultErrorSettings('findOne', error),
|
|
590
|
+
|
|
591
|
+
errorCode: 'NTS-DBS-FO0',
|
|
592
|
+
additionalContent: { filterBy },
|
|
593
|
+
message: `findOne ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
|
|
594
|
+
});
|
|
595
|
+
});
|
|
596
|
+
|
|
597
|
+
data = this.stringifyDataId(data, 'findOne');
|
|
598
|
+
|
|
599
|
+
return data;
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
/**
|
|
603
|
+
* #MONGOOSE FUNCTION
|
|
604
|
+
* Find the data first by any of its parameters,
|
|
605
|
+
* !!!: throws error if not found (errorCode on not found: NTS-DBS-F1)
|
|
606
|
+
*
|
|
607
|
+
* @param filterBy if you can, use unique parameters for find!
|
|
608
|
+
*
|
|
609
|
+
* @example
|
|
610
|
+
* // by email:
|
|
611
|
+
* { email: email }
|
|
612
|
+
* //
|
|
613
|
+
* @example
|
|
614
|
+
* // or by id that is in list:
|
|
615
|
+
* { userIds: { $in: this.userId } }
|
|
616
|
+
* //
|
|
617
|
+
* @example
|
|
618
|
+
* // or by number or Date that is Greater Than AND Less Than:
|
|
619
|
+
* { points: { $gt: 2, $lt: 14 } }
|
|
620
|
+
* // further tools (syntax matches with $gt):
|
|
621
|
+
* $eq: // Matches values that are EQual to a specified value.
|
|
622
|
+
* $gte: // Matches values that are Greater Than OR Equal to a specified value.
|
|
623
|
+
* $lte: // Matches values that are Less Than or Equal to a specified value.
|
|
624
|
+
* $ne: // Matches all values that are Not Equal to a specified value.
|
|
625
|
+
* $nin: // Matches None of the values specified IN an array.
|
|
626
|
+
* //
|
|
627
|
+
* @returns {T[]} dataList: T[]
|
|
628
|
+
*/
|
|
629
|
+
async find(filterBy: DynamoNTS_DBFilter<T>): Promise<T[]> {
|
|
630
|
+
const dataList: T[] = await this.dataModel
|
|
631
|
+
.find(filterBy)
|
|
632
|
+
.then((res): T[] => res as T[] ?? [])
|
|
633
|
+
.catch((error): void => {
|
|
634
|
+
throw new DynamoFM_Error({
|
|
635
|
+
...this._getDefaultErrorSettings('find', error),
|
|
636
|
+
|
|
637
|
+
errorCode: 'NTS-DBS-F0',
|
|
638
|
+
additionalContent: { filterBy },
|
|
639
|
+
message: `find ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
|
|
640
|
+
});
|
|
641
|
+
});
|
|
642
|
+
|
|
643
|
+
dataList.forEach((data: T): void => {
|
|
644
|
+
data = this.stringifyDataId(data, 'find');
|
|
645
|
+
});
|
|
646
|
+
|
|
647
|
+
return dataList as T[];
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
/**
|
|
651
|
+
* #MONGOOSE FUNCTION
|
|
652
|
+
* Find the data first by any of its parameters
|
|
653
|
+
*
|
|
654
|
+
* @param filterBy if you can, use unique parameters for find!
|
|
655
|
+
*
|
|
656
|
+
* @example
|
|
657
|
+
* // by email:
|
|
658
|
+
* { email: email }
|
|
659
|
+
* //
|
|
660
|
+
* @example
|
|
661
|
+
* // or by id that is in list:
|
|
662
|
+
* { userIds: { $in: this.userId } }
|
|
663
|
+
* //
|
|
664
|
+
* @example
|
|
665
|
+
* // or by number or Date that is Greater Than AND Less Than:
|
|
666
|
+
* { points: { $gt: 2, $lt: 14 } }
|
|
667
|
+
* // further tools (syntax matches with $gt):
|
|
668
|
+
* $eq: // Matches values that are EQual to a specified value.
|
|
669
|
+
* $gte: // Matches values that are Greater Than OR Equal to a specified value.
|
|
670
|
+
* $lte: // Matches values that are Less Than or Equal to a specified value.
|
|
671
|
+
* $ne: // Matches all values that are Not Equal to a specified value.
|
|
672
|
+
* $nin: // Matches None of the values specified IN an array.
|
|
673
|
+
* //
|
|
674
|
+
* @param page page
|
|
675
|
+
* @param pageSize pageSize
|
|
676
|
+
* @param sort
|
|
677
|
+
* @example
|
|
678
|
+
* // by dateTime (this uses the basic sort function):
|
|
679
|
+
* { dateTime: -1 }
|
|
680
|
+
* //
|
|
681
|
+
* @returns {T[]} dataList: T[]
|
|
682
|
+
*/
|
|
683
|
+
async findWithPaging(
|
|
684
|
+
filterBy: DynamoNTS_DBFilter<T>,
|
|
685
|
+
page: number,
|
|
686
|
+
pageSize: number,
|
|
687
|
+
sort?: any
|
|
688
|
+
): Promise<T[]> {
|
|
689
|
+
const dataList: T[] = await this.dataModel
|
|
690
|
+
.find(filterBy)
|
|
691
|
+
.sort(sort)
|
|
692
|
+
.skip(page * pageSize)
|
|
693
|
+
.limit(pageSize)
|
|
694
|
+
.then((res): T => res ?? [])
|
|
695
|
+
.catch((error): void => {
|
|
696
|
+
throw new DynamoFM_Error({
|
|
697
|
+
...this._getDefaultErrorSettings('findWithPaging', error),
|
|
698
|
+
|
|
699
|
+
errorCode: 'NTS-DBS-WP0',
|
|
700
|
+
additionalContent: { filterBy, page, pageSize, sort },
|
|
701
|
+
message: `findWithPaging ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
|
|
702
|
+
});
|
|
703
|
+
});
|
|
704
|
+
|
|
705
|
+
dataList.forEach((data: T): void => {
|
|
706
|
+
data = this.stringifyDataId(data, 'findWithPaging');
|
|
707
|
+
});
|
|
708
|
+
|
|
709
|
+
return dataList;
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
/**
|
|
713
|
+
* #MONGOOSE FUNCTION
|
|
714
|
+
* Find data by _id and update
|
|
715
|
+
* !!!: throws error if not found (errorCode on not found: )
|
|
716
|
+
*
|
|
717
|
+
* @param id id
|
|
718
|
+
* @param update update
|
|
719
|
+
* @returns data
|
|
720
|
+
*/
|
|
721
|
+
async findByIdAndUpdate(id: string, update: DynamoNTS_DBUpdate<T>, issuer: string): Promise<T> {
|
|
722
|
+
update.__lastModified = new Date();
|
|
723
|
+
update.__lastModifiedBy = issuer;
|
|
724
|
+
|
|
725
|
+
let newData: T = await this.dataModel
|
|
726
|
+
.findByIdAndUpdate(id, update)
|
|
727
|
+
.then((res): T => res?.toObject() as T ?? null)
|
|
728
|
+
.catch((error): void => {
|
|
729
|
+
throw new DynamoFM_Error({
|
|
730
|
+
...this._getDefaultErrorSettings('findByIdAndUpdate', error, issuer),
|
|
731
|
+
|
|
732
|
+
errorCode: 'NTS-DBS-FIU0',
|
|
733
|
+
additionalContent: { id, update },
|
|
734
|
+
message: `findByIdAndUpdate ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
|
|
735
|
+
issuer,
|
|
736
|
+
});
|
|
737
|
+
});
|
|
738
|
+
|
|
739
|
+
newData = this.stringifyDataId(newData, 'findByIdAndUpdate');
|
|
740
|
+
|
|
741
|
+
return newData;
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
/**
|
|
745
|
+
* #MONGOOSE FUNCTION
|
|
746
|
+
* Find the data first by any of its parameters
|
|
747
|
+
*
|
|
748
|
+
* @param filter This uses the basic Mongoose updateOne.
|
|
749
|
+
* If you can, use unique parameters for find!
|
|
750
|
+
* @example
|
|
751
|
+
* // by email:
|
|
752
|
+
* { email: email }
|
|
753
|
+
* //
|
|
754
|
+
* @example
|
|
755
|
+
* // or by id that is in list:
|
|
756
|
+
* { userIds: { $in: this.userId } }
|
|
757
|
+
* //
|
|
758
|
+
* @example
|
|
759
|
+
* // or by number or Date that is Greater Than AND Less Than:
|
|
760
|
+
* { points: { $gt: 2, $lt: 14 } }
|
|
761
|
+
* // further tools (syntax matches with $gt):
|
|
762
|
+
* $eq: // Matches values that are EQual to a specified value.
|
|
763
|
+
* $gte: // Matches values that are Greater Than OR Equal to a specified value.
|
|
764
|
+
* $lte: // Matches values that are Less Than or Equal to a specified value.
|
|
765
|
+
* $ne: // Matches all values that are Not Equal to a specified value.
|
|
766
|
+
* $nin: // Matches None of the values specified IN an array.
|
|
767
|
+
* //
|
|
768
|
+
*
|
|
769
|
+
* @param update this uses the basic Mongoose updateOne
|
|
770
|
+
* @example
|
|
771
|
+
* // increase a specific value (here by 15):
|
|
772
|
+
* { $inc: { popularity: 15 } }
|
|
773
|
+
* //
|
|
774
|
+
* @example
|
|
775
|
+
* // or add element to a list:
|
|
776
|
+
* { $push: { reactions: this.newReaction }
|
|
777
|
+
* // or add multiple elements to a list
|
|
778
|
+
* { $push: { schedule: {$each: [ monday, tuesday, wednesday ] } } }
|
|
779
|
+
* //
|
|
780
|
+
* @example
|
|
781
|
+
* // or all at once
|
|
782
|
+
* {
|
|
783
|
+
* $inc: { popularity: this.newVote.amount },
|
|
784
|
+
* emailVerified: true,
|
|
785
|
+
* $push: { reactions: this.newReaction }
|
|
786
|
+
* }
|
|
787
|
+
* // further tools (syntax matches with $inc):
|
|
788
|
+
* $currentDate: // Sets the value of a field to current date, either as a Date or a Timestamp.
|
|
789
|
+
* $min: // Only updates the field if the specified value is less than the existing field value.
|
|
790
|
+
* $max: // Only updates the field if the specified value is greater than the existing field value.
|
|
791
|
+
* $mul: // Multiplies the value of the field by the specified amount.
|
|
792
|
+
* $rename: // Renames a field.
|
|
793
|
+
* $unset: // Removes the specified field from a document. (set: "" to value)
|
|
794
|
+
* //
|
|
795
|
+
*/
|
|
796
|
+
async updateOne(
|
|
797
|
+
filterBy: DynamoNTS_DBFilter<T>,
|
|
798
|
+
update: DynamoNTS_DBUpdate<T>,
|
|
799
|
+
issuer: string
|
|
800
|
+
): Promise<void> {
|
|
801
|
+
update.__lastModified = new Date();
|
|
802
|
+
update.__lastModifiedBy = issuer;
|
|
803
|
+
|
|
804
|
+
await this.dataModel
|
|
805
|
+
.updateOne(filterBy, update)
|
|
806
|
+
.catch((error): void => {
|
|
807
|
+
throw new DynamoFM_Error({
|
|
808
|
+
...this._getDefaultErrorSettings('updateOne', error, issuer),
|
|
809
|
+
|
|
810
|
+
errorCode: 'NTS-DBS-UO0',
|
|
811
|
+
additionalContent: { filterBy, update },
|
|
812
|
+
message: `updateOne ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
|
|
813
|
+
issuer,
|
|
814
|
+
});
|
|
815
|
+
});
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
/**
|
|
819
|
+
* #MONGOOSE FUNCTION
|
|
820
|
+
* update one parameter by a specific
|
|
821
|
+
*
|
|
822
|
+
* @param filter This uses the basic Mongoose updateMany.
|
|
823
|
+
* @example
|
|
824
|
+
* // by email:
|
|
825
|
+
* { email: email }
|
|
826
|
+
* //
|
|
827
|
+
* @example
|
|
828
|
+
* // or by id that is in list:
|
|
829
|
+
* { userIds: { $in: this.userId } }
|
|
830
|
+
* //
|
|
831
|
+
* @example
|
|
832
|
+
* // or by number or Date that is Greater Than AND Less Than:
|
|
833
|
+
* { points: { $gt: 2, $lt: 14 } }
|
|
834
|
+
* // further tools (syntax matches with $gt):
|
|
835
|
+
* $eq: // Matches values that are EQual to a specified value.
|
|
836
|
+
* $gte: // Matches values that are Greater Than OR Equal to a specified value.
|
|
837
|
+
* $lte: // Matches values that are Less Than or Equal to a specified value.
|
|
838
|
+
* $ne: // Matches all values that are Not Equal to a specified value.
|
|
839
|
+
* $nin: // Matches None of the values specified IN an array.
|
|
840
|
+
* //
|
|
841
|
+
*
|
|
842
|
+
* @param update this uses the basic Mongoose updateOne
|
|
843
|
+
* @example
|
|
844
|
+
* // increase a specific value (here by 15):
|
|
845
|
+
* { $inc: { popularity: 15 } }
|
|
846
|
+
* //
|
|
847
|
+
* @example
|
|
848
|
+
* // or add element to a list:
|
|
849
|
+
* { $push: { reactions: this.newReaction }
|
|
850
|
+
* // or add multiple elements to a list
|
|
851
|
+
* { $push: { schedule: {$each: [ monday, tuesday, wednesday ] } } }
|
|
852
|
+
* //
|
|
853
|
+
* @example
|
|
854
|
+
* // or all at once
|
|
855
|
+
* {
|
|
856
|
+
* $inc: { popularity: this.newVote.amount },
|
|
857
|
+
* emailVerified: true,
|
|
858
|
+
* $push: { reactions: this.newReaction }
|
|
859
|
+
* }
|
|
860
|
+
* // further tools (syntax matches with $inc):
|
|
861
|
+
* $currentDate: // Sets the value of a field to current date, either as a Date or a Timestamp.
|
|
862
|
+
* $min: // Only updates the field if the specified value is less than the existing field value.
|
|
863
|
+
* $max: // Only updates the field if the specified value is greater than the existing field value.
|
|
864
|
+
* $mul: // Multiplies the value of the field by the specified amount.
|
|
865
|
+
* $rename: // Renames a field.
|
|
866
|
+
* $unset: // Removes the specified field from a document. (set: "" to value)
|
|
867
|
+
* //
|
|
868
|
+
*/
|
|
869
|
+
async updateMany(
|
|
870
|
+
filterBy: DynamoNTS_DBFilter<T>,
|
|
871
|
+
update: DynamoNTS_DBUpdate<T>,
|
|
872
|
+
issuer: string
|
|
873
|
+
): Promise<void> {
|
|
874
|
+
update.__lastModified = new Date();
|
|
875
|
+
update.__lastModifiedBy = issuer;
|
|
876
|
+
|
|
877
|
+
await this.dataModel
|
|
878
|
+
.updateMany(filterBy, update)
|
|
879
|
+
.catch((error): void => {
|
|
880
|
+
throw new DynamoFM_Error({
|
|
881
|
+
...this._getDefaultErrorSettings('updateMany', error, issuer),
|
|
882
|
+
|
|
883
|
+
errorCode: 'NTS-DBS-UM0',
|
|
884
|
+
additionalContent: { filterBy, update },
|
|
885
|
+
message: `updateMany ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
|
|
886
|
+
issuer,
|
|
887
|
+
});
|
|
888
|
+
});
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
// ----------------------------------------------------------------------------------
|
|
892
|
+
// ----------------------------------------------------------------------------------
|
|
893
|
+
// ----------------------------------------------------------------------------------
|
|
894
|
+
// PRIVATE FUNCTIONS
|
|
895
|
+
|
|
896
|
+
private stringifyDataId(data: T, fnName: string): T {
|
|
897
|
+
if (data?._id && (typeof data._id !== 'string' || typeof data._id === 'object')) {
|
|
898
|
+
data._id = `${data._id}`;
|
|
899
|
+
|
|
900
|
+
if (typeof data._id !== 'string' || typeof data._id === 'object') {
|
|
901
|
+
data = JSON.parse(JSON.stringify(data));
|
|
902
|
+
|
|
903
|
+
if (typeof data._id !== 'string' || typeof data._id === 'object') {
|
|
904
|
+
DynamoFM_Log.error(
|
|
905
|
+
`data._id stringifying failed! Please notfiy the DynamoNTS developers! ` +
|
|
906
|
+
`(${fnName})`,
|
|
907
|
+
new Error()
|
|
908
|
+
);
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
return data;
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
private _getDefaultErrorSettings(
|
|
917
|
+
fnName: string,
|
|
918
|
+
error: DynamoFM_AnyError,
|
|
919
|
+
issuer?: string
|
|
920
|
+
): DynamoFM_Error_Settings {
|
|
921
|
+
return {
|
|
922
|
+
status: (error as DynamoFM_Error)?.___status ?? 500,
|
|
923
|
+
message: (error as Error)?.message ?? `${fnName} was UNSUCCESFUL (NTS-DB)`,
|
|
924
|
+
addECToUserMsg: !(error as DynamoFM_Error)?.__userMessage,
|
|
925
|
+
userMessage: (error as DynamoFM_Error)?.__userMessage ?? this.defaultErrorUserMsg,
|
|
926
|
+
issuer: issuer,
|
|
927
|
+
issuerService: this.serviceName,
|
|
928
|
+
error: error,
|
|
929
|
+
};
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
// ----------------------------------------------------------------------------------
|
|
933
|
+
// PRIVATE BUILD FUNCTIONS
|
|
934
|
+
|
|
935
|
+
/**
|
|
936
|
+
* builds and returns mongoose schema
|
|
937
|
+
* @returns schema
|
|
938
|
+
*/
|
|
939
|
+
private getSchema(): mongoose.Schema {
|
|
940
|
+
let schema: any = this.buildMongooseSchemaByModelParams();
|
|
941
|
+
|
|
942
|
+
schema = this.addDynamoFM_MetadataToSchema(schema);
|
|
943
|
+
|
|
944
|
+
// tslint:disable-next-line: no-string-literal
|
|
945
|
+
if (schema['_id']) {
|
|
946
|
+
// tslint:disable-next-line: no-string-literal
|
|
947
|
+
delete schema['_id'];
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
return new Schema(schema);
|
|
951
|
+
}
|
|
952
|
+
|
|
953
|
+
/**
|
|
954
|
+
* builds mongoose schema building settings object by dynamoDataModelParamsList
|
|
955
|
+
* @param params DynamoBEDataPropertyParams
|
|
956
|
+
* @returns mongoose schema object
|
|
957
|
+
*/
|
|
958
|
+
private buildMongooseSchemaByModelParams(params?: DynamoFM_DataProperty_Params[]): any {
|
|
959
|
+
const schemaSettingsObj = {};
|
|
960
|
+
|
|
961
|
+
if (!params) {
|
|
962
|
+
params = this.dataParams.properties;
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
params.forEach((property: DynamoFM_DataProperty_Params): void => {
|
|
966
|
+
const beType = this.getBEType(property.type);
|
|
967
|
+
|
|
968
|
+
if (
|
|
969
|
+
beType !== Object ||
|
|
970
|
+
!property?.subObjectParams ||
|
|
971
|
+
property?.subObjectParams?.length == 0
|
|
972
|
+
) {
|
|
973
|
+
schemaSettingsObj[property.key] = {
|
|
974
|
+
type: beType,
|
|
975
|
+
};
|
|
976
|
+
|
|
977
|
+
if (property.index) {
|
|
978
|
+
schemaSettingsObj[property.key].index = true;
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
if (property.unique) {
|
|
982
|
+
schemaSettingsObj[property.key].unique = true;
|
|
983
|
+
}
|
|
984
|
+
|
|
985
|
+
if (property.required) {
|
|
986
|
+
schemaSettingsObj[property.key].required = true;
|
|
987
|
+
}
|
|
988
|
+
|
|
989
|
+
if (property.minlength) {
|
|
990
|
+
schemaSettingsObj[property.key].minlength = property.minlength;
|
|
991
|
+
}
|
|
992
|
+
|
|
993
|
+
if (property.maxlength) {
|
|
994
|
+
schemaSettingsObj[property.key].maxlength = property.maxlength;
|
|
995
|
+
}
|
|
996
|
+
} else {
|
|
997
|
+
schemaSettingsObj[property.key] = this.buildMongooseSchemaByModelParams(
|
|
998
|
+
property.subObjectParams
|
|
999
|
+
);
|
|
1000
|
+
}
|
|
1001
|
+
});
|
|
1002
|
+
|
|
1003
|
+
return schemaSettingsObj;
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
private getBEType(
|
|
1007
|
+
type: string
|
|
1008
|
+
): String | Number | Boolean | Object | Function | Array<any> | Date {
|
|
1009
|
+
switch (type.toLocaleLowerCase()) {
|
|
1010
|
+
|
|
1011
|
+
case 'string':
|
|
1012
|
+
return String;
|
|
1013
|
+
|
|
1014
|
+
case 'number':
|
|
1015
|
+
return Number;
|
|
1016
|
+
|
|
1017
|
+
case 'boolean':
|
|
1018
|
+
return Boolean;
|
|
1019
|
+
|
|
1020
|
+
case 'date':
|
|
1021
|
+
return Date;
|
|
1022
|
+
|
|
1023
|
+
case 'object':
|
|
1024
|
+
return Object;
|
|
1025
|
+
|
|
1026
|
+
default:
|
|
1027
|
+
if (type.includes('[]') || type.includes('array')) {
|
|
1028
|
+
return Array;
|
|
1029
|
+
} else if (type.includes('=>') || type.includes('function')) {
|
|
1030
|
+
return Function;
|
|
1031
|
+
} else {
|
|
1032
|
+
return Object;
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
/**
|
|
1038
|
+
* adds dynamo metadata settings to any mongoose schema building settings object
|
|
1039
|
+
* @param schema schema to update
|
|
1040
|
+
* @returns updated schema
|
|
1041
|
+
*/
|
|
1042
|
+
private addDynamoFM_MetadataToSchema(schema: any): any {
|
|
1043
|
+
schema.__created = { type: Date };
|
|
1044
|
+
schema.__createdBy = { type: String };
|
|
1045
|
+
schema.__lastModified = { type: Date };
|
|
1046
|
+
schema.__lastModifiedBy = { type: String };
|
|
1047
|
+
|
|
1048
|
+
return schema;
|
|
1049
|
+
}
|
|
1050
|
+
|
|
1051
|
+
/**
|
|
1052
|
+
* sets depDataKey
|
|
1053
|
+
*/
|
|
1054
|
+
private lookForDependencyDataSettings(): void {
|
|
1055
|
+
const dependencyParam: DynamoFM_DataProperty_Params = this.dataParams.properties.find(
|
|
1056
|
+
(modelParams: DynamoFM_DataProperty_Params): boolean => modelParams.isDependencyHook
|
|
1057
|
+
);
|
|
1058
|
+
|
|
1059
|
+
if (dependencyParam) {
|
|
1060
|
+
this.depDataName = dependencyParam.key;
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
}
|