@futdevpro/nts-dynamo 1.5.52 → 1.5.54

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.
@@ -3,6 +3,7 @@ import { Dynamo_Metadata, DynamoNTS_DataParams, DynamoNTS_DataPropertyParams, Dy
3
3
 
4
4
  import { DynamoNTS_DBService } from './dynamo-nts-db.service';
5
5
  import { DynamoNTS_GlobalService } from './dynamo-nts-global.service';
6
+ import { DynamoNTS_Shared } from './dynamo-nts-shared.service';
6
7
 
7
8
  /**
8
9
  * Basic Data Service that is connected to the relevant DBServices
@@ -27,13 +28,12 @@ export class DynamoNTS_DataService<T extends Dynamo_Metadata> {
27
28
  dataDBService: DynamoNTS_DBService<T>;
28
29
  data: T;
29
30
  dataList: T[] = [];
30
- issuer?: string;
31
+ issuer: string;
31
32
 
32
33
  depKey?: string;
33
34
  depDBServiceKey?: string;
34
35
  private depDataDBService: DynamoNTS_DBService<any>;
35
36
 
36
- // dataModelParams?: DynamoBEDataPropertyParams[] = [];
37
37
  dataParams: DynamoNTS_DataParams;
38
38
 
39
39
  defaultErrorUserMsg = 'We encountered an unhandled Data Error, please contact the responsible development team.';
@@ -42,12 +42,11 @@ export class DynamoNTS_DataService<T extends Dynamo_Metadata> {
42
42
  constructor(
43
43
  data: T,
44
44
  dataParams: DynamoNTS_DataParams,
45
- issuer?: string,
45
+ issuer: string,
46
46
  ) {
47
47
  this.dataDBService = DynamoNTS_GlobalService.getDBService<T>(dataParams);
48
48
  this.data = data;
49
49
  this.dataParams = dataParams;
50
- // this.dataModelParams = dataParams.modelParams;
51
50
  this.lookForDependencyDataSettings();
52
51
  this.issuer = issuer;
53
52
  }
@@ -57,7 +56,14 @@ export class DynamoNTS_DataService<T extends Dynamo_Metadata> {
57
56
  */
58
57
  async getAll(): Promise<void> {
59
58
  try {
60
- this.dataList = await this.dataDBService.getAll();
59
+ this.dataList = await this.dataDBService.getAll().catch(error => {
60
+ if (error?.errorCodes?.includes('NTS-DBS-GA1')) {
61
+ console.error(error);
62
+ return [];
63
+ } else {
64
+ throw error;
65
+ }
66
+ });
61
67
  } catch (error) {
62
68
  throw new Dynamo_Error({
63
69
  status: 417,
@@ -74,7 +80,7 @@ export class DynamoNTS_DataService<T extends Dynamo_Metadata> {
74
80
  * @description
75
81
  * returns data from database by id
76
82
  * also if dontSetToService is false or not setted,
77
- * the data will be saved to the service
83
+ * the data will be saved to the service, even if its not found
78
84
  *
79
85
  * @remarks
80
86
  * If you need to get-save a data, if possible,
@@ -88,8 +94,15 @@ export class DynamoNTS_DataService<T extends Dynamo_Metadata> {
88
94
  async getDataById(id?: string, dontSetToService?: boolean): Promise<T> {
89
95
  try {
90
96
  if (id || this.data._id) {
91
- let dataExists: T = await this.dataDBService.getDataById(id ? id : this.data._id);
92
- if (!dontSetToService && dataExists) {
97
+ let dataExists: T = await this.dataDBService.getDataById(id ? id : this.data._id).catch(error => {
98
+ if (error?.errorCodes?.includes('NTS-DBS-GI1')) {
99
+ console.error(error);
100
+ return null;
101
+ } else {
102
+ throw error;
103
+ }
104
+ });
105
+ if (!dontSetToService) {
93
106
  this.data = dataExists;
94
107
  }
95
108
  return dataExists;
@@ -122,7 +135,14 @@ export class DynamoNTS_DataService<T extends Dynamo_Metadata> {
122
135
  try {
123
136
  if (this.depKey) {
124
137
  if (dependencyId || this.data[this.depKey]) {
125
- let dataExists: T = await this.dataDBService.getDataByDependencyId(dependencyId ? dependencyId : this.data[this.depKey]);
138
+ let dataExists: T = await this.dataDBService.getDataByDependencyId(dependencyId ? dependencyId : this.data[this.depKey]).catch(error => {
139
+ if (error?.errorCodes?.includes('NTS-DBS-GD2')) {
140
+ console.error(error);
141
+ return null;
142
+ } else {
143
+ throw error;
144
+ }
145
+ });
126
146
  if (!dontSetToService && dataExists) {
127
147
  this.data = dataExists;
128
148
  }
@@ -165,7 +185,14 @@ export class DynamoNTS_DataService<T extends Dynamo_Metadata> {
165
185
  try {
166
186
  if (this.depKey) {
167
187
  if (dependencyId || this.data[this.depKey]) {
168
- this.dataList = await this.dataDBService.getDataListByDependencyId(dependencyId ? dependencyId : this.data[this.depKey]);
188
+ this.dataList = await this.dataDBService.getDataListByDependencyId(dependencyId ? dependencyId : this.data[this.depKey]).catch(error => {
189
+ if (error?.errorCodes?.includes('NTS-DBS-GLD2')) {
190
+ console.error(error);
191
+ return [];
192
+ } else {
193
+ throw error;
194
+ }
195
+ });
169
196
  } else {
170
197
  throw new Dynamo_Error({
171
198
  status: 417,
@@ -196,13 +223,191 @@ export class DynamoNTS_DataService<T extends Dynamo_Metadata> {
196
223
  }
197
224
  }
198
225
 
226
+ /**
227
+ *
228
+ * // findOne desc:
229
+ *
230
+ * Find the data first by any of its parameters,
231
+ * also if dontSetToService is false or not setted,
232
+ * the data will be saved to the service, even if non found
233
+ *
234
+ * @param filter if you can, use unique parameters for find!
235
+ *
236
+ * @example
237
+ * // by email:
238
+ * { email: email }
239
+ * //
240
+ * @example
241
+ * // or by id that is in list:
242
+ * { userIds: { $in: this.userId } }
243
+ * //
244
+ * @example
245
+ * // or by number or Date that is Greater Than AND Less Than:
246
+ * { points: { $gt: 2, $lt: 14 } }
247
+ * // further tools (syntax matches with $gt):
248
+ * $eq: // Matches values that are EQual to a specified value.
249
+ * $gte: // Matches values that are Greater Than OR Equal to a specified value.
250
+ * $lte: // Matches values that are Less Than or Equal to a specified value.
251
+ * $ne: // Matches all values that are Not Equal to a specified value.
252
+ * $nin: // Matches None of the values specified IN an array.
253
+ * //
254
+ * @returns {T} data: T
255
+ */
256
+ async findData(findBy: any, dontSetToService?: boolean): Promise<T> {
257
+ try {
258
+ const dataExists: T = await this.dataDBService.findOne(findBy).catch(error => {
259
+ if (error?.errorCodes?.includes('NTS-DBS-FO1')) {
260
+ console.error(error);
261
+ return null;
262
+ } else {
263
+ throw error;
264
+ }
265
+ });
266
+ if (!dontSetToService) {
267
+ this.data = dataExists;
268
+ }
269
+ return dataExists;
270
+ } catch (error) {
271
+ throw new Dynamo_Error({
272
+ status: 417,
273
+ errorCode: 'NTS-DS0-FD0',
274
+ addECToUserMsg: true,
275
+ message: `findData was unsuccessful (${this.dataParams.dataName})`,
276
+ userMessage: this.defaultErrorUserMsg,
277
+ error: error
278
+ });
279
+ }
280
+ }
281
+
282
+ /**
283
+ *
284
+ * // find desc:
285
+ *
286
+ * Find the data first by any of its parameters,
287
+ * also if dontSetToService is false or not setted,
288
+ * the data will be saved to the service, even if non found
289
+ *
290
+ * @param filter if you can, use unique parameters for find!
291
+ *
292
+ * @example
293
+ * // by email:
294
+ * { email: email }
295
+ * //
296
+ * @example
297
+ * // or by id that is in list:
298
+ * { userIds: { $in: this.userId } }
299
+ * //
300
+ * @example
301
+ * // or by number or Date that is Greater Than AND Less Than:
302
+ * { points: { $gt: 2, $lt: 14 } }
303
+ * // further tools (syntax matches with $gt):
304
+ * $eq: // Matches values that are EQual to a specified value.
305
+ * $gte: // Matches values that are Greater Than OR Equal to a specified value.
306
+ * $lte: // Matches values that are Less Than or Equal to a specified value.
307
+ * $ne: // Matches all values that are Not Equal to a specified value.
308
+ * $nin: // Matches None of the values specified IN an array.
309
+ * //
310
+ * @returns {T[]} dataList: T[]
311
+ */
312
+ async findDatas(findBy: any, dontSetToService?: boolean): Promise<T[]> {
313
+ try {
314
+ const dataListExists: T[] = await this.dataDBService.find(findBy).catch(error => {
315
+ if (error?.errorCodes?.includes('NTS-DBS-F1')) {
316
+ console.error(error);
317
+ return [];
318
+ } else {
319
+ throw error;
320
+ }
321
+ });
322
+ if (!dontSetToService) {
323
+ this.dataList = dataListExists;
324
+ }
325
+ return dataListExists;
326
+ } catch (error) {
327
+ throw new Dynamo_Error({
328
+ status: 417,
329
+ errorCode: 'NTS-DS0-FDS0',
330
+ addECToUserMsg: true,
331
+ message: `findDatas was unsuccessful (${this.dataParams.dataName})`,
332
+ userMessage: this.defaultErrorUserMsg,
333
+ error: error
334
+ });
335
+ }
336
+ }
199
337
 
338
+ /**
339
+ * This function uses the dataDBService.updateOne function.
340
+ * This uses updateBy if setted, or data._id if its setted or this.data[this.dependencyKey]
341
+ * @param set
342
+ *
343
+ * // updateOne desc:
344
+ *
345
+ * Find the data first by any of its parameters, throws error if not found
346
+ * @param filter This uses the basic Mongoose updateOne.
347
+ * If you can, use unique parameters for find!
348
+ * @example
349
+ * // by email:
350
+ * { email: email }
351
+ * //
352
+ * @example
353
+ * // or by id that is in list:
354
+ * { userIds: { $in: this.userId } }
355
+ * //
356
+ * @example
357
+ * // or by number or Date that is Greater Than AND Less Than:
358
+ * { points: { $gt: 2, $lt: 14 } }
359
+ * // further tools (syntax matches with $gt):
360
+ * $eq: // Matches values that are EQual to a specified value.
361
+ * $gte: // Matches values that are Greater Than OR Equal to a specified value.
362
+ * $lte: // Matches values that are Less Than or Equal to a specified value.
363
+ * $ne: // Matches all values that are Not Equal to a specified value.
364
+ * $nin: // Matches None of the values specified IN an array.
365
+ * //
366
+ *
367
+ * @param update this uses the basic Mongoose updateOne
368
+ * @example
369
+ * // increase a specific value (here by 15):
370
+ * { $inc: { popularity: 15 } }
371
+ * //
372
+ * @example
373
+ * // or add element to a list:
374
+ * { $push: { reactions: this.newReaction }
375
+ * // or add multiple elements to a list
376
+ * { $push: { schedule: {$each: [ monday, tuesday, wednesday ] } } }
377
+ * //
378
+ * @example
379
+ * // or all at once
380
+ * {
381
+ * $inc: { popularity: this.newVote.amount },
382
+ * emailVerified: true,
383
+ * $push: { reactions: this.newReaction }
384
+ * }
385
+ * // further tools (syntax matches with $inc):
386
+ * $currentDate: // Sets the value of a field to current date, either as a Date or a Timestamp.
387
+ * $min: // Only updates the field if the specified value is less than the existing field value.
388
+ * $max: // Only updates the field if the specified value is greater than the existing field value.
389
+ * $mul: // Multiplies the value of the field by the specified amount.
390
+ * $rename: // Renames a field.
391
+ * $unset: // Removes the specified field from a document. (set: "" to value)
392
+ * //
393
+ */
200
394
  async updateData(set: { updateBy?: any, update: any }): Promise<void> {
201
395
  try {
202
396
  if (set.updateBy) {
203
- this.data = await this.dataDBService.updateOne(set.updateBy, set.update, this.issuer);
397
+ await this.dataDBService.updateOne(set.updateBy, set.update, this.issuer);
398
+ } else if (this.data._id) {
399
+ await this.dataDBService.updateOne({ _id: this.data._id }, set.update, this.issuer);
400
+ } else if (this.depKey && this.data[this.depKey]) {
401
+ await this.dataDBService.updateOne({ [this.depKey]: this.data[this.depKey] }, set.update, this.issuer);
402
+ } else {
403
+ throw new Dynamo_Error({
404
+ status: 417,
405
+ errorCode: 'NTS-DS0-UD1',
406
+ addECToUserMsg: true,
407
+ message: `no usable parameter provided for updateData; no updateBy, no id, no dependencyId (${this.dataParams.dataName})`,
408
+ userMessage: this.defaultErrorUserMsg,
409
+ });
204
410
  }
205
-
206
411
  } catch (error) {
207
412
  throw new Dynamo_Error({
208
413
  status: 417,
@@ -219,6 +424,7 @@ export class DynamoNTS_DataService<T extends Dynamo_Metadata> {
219
424
  * modifies data if the data have ID and already exists in the DB,
220
425
  * creates new if the ID is not present or cant find in DB,
221
426
  * and if dependency data setted up, will check before creation,
427
+ *
222
428
  * @warning
223
429
  * but the proper way to update data, if you use update method instead,
224
430
  * this way, you can avoid data override errors
@@ -229,36 +435,43 @@ export class DynamoNTS_DataService<T extends Dynamo_Metadata> {
229
435
  try {
230
436
  if (this.data._id) {
231
437
  // check if already exists
232
- let dataExists: T = await this.getDataById(null, true).catch(() => null);
233
- if (!dataExists) {
438
+ const dataExists: T = await this.getDataById(null, true);
439
+ if (dataExists) {
440
+ // if data exists do modify
441
+ this.data = await this.dataDBService.modifyData(this.data, this.issuer);
442
+ } else {
234
443
  // if data not exists check that dependency already exists for this
235
444
  if (this.depKey) {
236
- if (this.data[this.depKey]) {
237
- let dependencyExists = await this.getDependencyDataDBService().getDataById(this.data[this.depKey]);
238
- if (!dependencyExists) {
239
- throw new Dynamo_Error({
240
- status: 417,
241
- errorCode: 'NTS-DS0-SD1',
242
- addECToUserMsg: true,
243
- message: `saveData was unsuccessful: dependency data not exists (key: ${this.depKey}, id: ${this.data[this.depKey]})`,
244
- userMessage: this.defaultErrorUserMsg
245
- });
445
+ if (!this.data[this.depKey]) {
446
+ throw new Dynamo_Error({
447
+ status: 417,
448
+ errorCode: 'NTS-DS0-SD1',
449
+ addECToUserMsg: true,
450
+ message: `saveData was unsuccessful: dependency data id missing from data (key: ${this.depKey})`,
451
+ userMessage: this.defaultErrorUserMsg
452
+ });
453
+ }
454
+
455
+ let dependencyExists = await this.getDependencyDataDBService().getDataById(this.data[this.depKey]).catch(error => {
456
+ if (error?.errorCodes?.includes('NTS-DBS-GI1')) {
457
+ console.error(error);
458
+ return null;
459
+ } else {
460
+ throw error;
246
461
  }
247
- } else {
462
+ });
463
+ if (!dependencyExists) {
248
464
  throw new Dynamo_Error({
249
465
  status: 417,
250
466
  errorCode: 'NTS-DS0-SD2',
251
467
  addECToUserMsg: true,
252
- message: `saveData was unsuccessful: dependency data id missing from data (key: ${this.depKey})`,
468
+ message: `saveData was unsuccessful: dependency data not exists (key: ${this.depKey}, id: ${this.data[this.depKey]})`,
253
469
  userMessage: this.defaultErrorUserMsg
254
470
  });
255
471
  }
256
472
  }
257
473
  // if data not exists create new data
258
474
  this.data = await this.dataDBService.createData(this.data, this.issuer);
259
- } else {
260
- // if data exists do modify
261
- this.data = await this.dataDBService.modifyData(this.data, this.issuer);
262
475
  }
263
476
  } else {
264
477
  // if ID is not present, data not exists, create new data
@@ -281,9 +494,7 @@ export class DynamoNTS_DataService<T extends Dynamo_Metadata> {
281
494
  */
282
495
  async deleteData(): Promise<void> {
283
496
  try {
284
- if (this.data._id) {
285
- await this.dataDBService.deleteDataById(this.data._id);
286
- } else {
497
+ if (!this.data._id) {
287
498
  throw new Dynamo_Error({
288
499
  status: 417,
289
500
  errorCode: 'NTS-DS0-DD1',
@@ -292,6 +503,8 @@ export class DynamoNTS_DataService<T extends Dynamo_Metadata> {
292
503
  userMessage: this.defaultErrorUserMsg,
293
504
  });
294
505
  }
506
+
507
+ await this.dataDBService.deleteDataById(this.data._id);
295
508
  } catch (error) {
296
509
  throw new Dynamo_Error({
297
510
  status: 417,
@@ -321,6 +534,7 @@ export class DynamoNTS_DataService<T extends Dynamo_Metadata> {
321
534
  userMessage: this.defaultValidationErrorUserMsg
322
535
  });
323
536
  }
537
+
324
538
  // specific Date validation
325
539
  if (this.dataParams.modelParams[i].type === 'Date' && !(new Date(this.data[this.dataParams.modelParams[i].key]) instanceof Date)) {
326
540
  throw new Dynamo_Error({
@@ -331,6 +545,7 @@ export class DynamoNTS_DataService<T extends Dynamo_Metadata> {
331
545
  userMessage: this.defaultValidationErrorUserMsg
332
546
  });
333
547
  }
548
+
334
549
  // call additional validators
335
550
  if (this.dataParams.modelParams[i].additionalValidators) {
336
551
  for (let j = 0; j < this.dataParams.modelParams[i].additionalValidators.length; j++) {
@@ -368,14 +583,7 @@ export class DynamoNTS_DataService<T extends Dynamo_Metadata> {
368
583
  * @returns
369
584
  */
370
585
  getDependencyDataDBService(): DynamoNTS_DBService<any> {
371
- if (this.depDBServiceKey) {
372
- if (this.depDataDBService) {
373
- return this.depDataDBService;
374
- } else {
375
- this.depDataDBService = DynamoNTS_GlobalService.getDBServiceByKey(this.depDBServiceKey);
376
- return this.depDataDBService;
377
- }
378
- } else {
586
+ if (!this.depDBServiceKey) {
379
587
  throw new Dynamo_Error({
380
588
  status: 501,
381
589
  errorCode: 'NTS-DS0-GDDB0',
@@ -384,5 +592,12 @@ export class DynamoNTS_DataService<T extends Dynamo_Metadata> {
384
592
  userMessage: this.defaultErrorUserMsg
385
593
  });
386
594
  }
595
+
596
+ if (this.depDataDBService) {
597
+ return this.depDataDBService;
598
+ } else {
599
+ this.depDataDBService = DynamoNTS_GlobalService.getDBServiceByKey(this.depDBServiceKey);
600
+ return this.depDataDBService;
601
+ }
387
602
  }
388
603
  }