@futdevpro/nts-dynamo 1.5.77 → 1.5.83

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.
Files changed (227) hide show
  1. package/LICENSE +2 -2
  2. package/lib/_constants/dynamo-nts-global-settings.d.ts +2 -2
  3. package/lib/_constants/dynamo-nts-global-settings.js +17 -17
  4. package/lib/_constants/index.d.ts +1 -1
  5. package/lib/_constants/index.js +4 -4
  6. package/lib/_enums/data-model-type.enum.d.ts +12 -12
  7. package/lib/_enums/data-model-type.enum.js +16 -16
  8. package/lib/_enums/data-model-type.enum.js.map +1 -1
  9. package/lib/_enums/dynamo-nts-data-service-function.enum.d.ts +12 -12
  10. package/lib/_enums/dynamo-nts-data-service-function.enum.js +18 -18
  11. package/lib/_enums/dynamo-nts-data-service-function.enum.js.map +1 -1
  12. package/lib/_enums/dynamo-nts-route-security.enum.d.ts +11 -11
  13. package/lib/_enums/dynamo-nts-route-security.enum.js +15 -15
  14. package/lib/_enums/dynamo-nts-route-security.enum.js.map +1 -1
  15. package/lib/_enums/dynamo-nts-socket-security.enum.d.ts +10 -10
  16. package/lib/_enums/dynamo-nts-socket-security.enum.js +14 -14
  17. package/lib/_enums/dynamo-nts-socket-security.enum.js.map +1 -1
  18. package/lib/_enums/http/http-call-type.enum.d.ts +11 -11
  19. package/lib/_enums/http/http-call-type.enum.js +15 -15
  20. package/lib/_enums/http/http-call-type.enum.js.map +1 -1
  21. package/lib/_enums/http/http-response-type.enum.d.ts +6 -6
  22. package/lib/_enums/http/http-response-type.enum.js +10 -10
  23. package/lib/_enums/http/http-response-type.enum.js.map +1 -1
  24. package/lib/_enums/http/socket-event.enum.d.ts +9 -8
  25. package/lib/_enums/http/socket-event.enum.d.ts.map +1 -1
  26. package/lib/_enums/http/socket-event.enum.js +13 -12
  27. package/lib/_enums/http/socket-event.enum.js.map +1 -1
  28. package/lib/_enums/index.d.ts +8 -8
  29. package/lib/_enums/index.js +13 -13
  30. package/lib/_enums/predefined-data-types.enum.d.ts +10 -10
  31. package/lib/_enums/predefined-data-types.enum.js +14 -14
  32. package/lib/_enums/predefined-data-types.enum.js.map +1 -1
  33. package/lib/_interfaces/certification-settings.interface.d.ts +7 -0
  34. package/lib/_interfaces/certification-settings.interface.d.ts.map +1 -0
  35. package/lib/_interfaces/certification-settings.interface.js +3 -0
  36. package/lib/_interfaces/certification-settings.interface.js.map +1 -0
  37. package/lib/_interfaces/global-service-settings.interface.d.ts +18 -0
  38. package/lib/_interfaces/global-service-settings.interface.d.ts.map +1 -0
  39. package/lib/_interfaces/global-service-settings.interface.js +3 -0
  40. package/lib/_interfaces/global-service-settings.interface.js.map +1 -0
  41. package/lib/_interfaces/index.d.ts +1 -0
  42. package/lib/_interfaces/index.d.ts.map +1 -0
  43. package/lib/_interfaces/index.js +4 -0
  44. package/lib/_interfaces/index.js.map +1 -0
  45. package/lib/_interfaces/port-settings.interface.d.ts +5 -0
  46. package/lib/_interfaces/port-settings.interface.d.ts.map +1 -0
  47. package/lib/_interfaces/port-settings.interface.js +3 -0
  48. package/lib/_interfaces/port-settings.interface.js.map +1 -0
  49. package/lib/_models/dynamo-nts-api-call-params.d.ts +95 -95
  50. package/lib/_models/dynamo-nts-api-call-params.js +47 -47
  51. package/lib/_models/dynamo-nts-app-params.d.ts +49 -49
  52. package/lib/_models/dynamo-nts-app-params.js +17 -17
  53. package/lib/_models/dynamo-nts-endpoint-params.d.ts +80 -80
  54. package/lib/_models/dynamo-nts-endpoint-params.js +139 -139
  55. package/lib/_models/dynamo-nts-global-settings.d.ts +53 -53
  56. package/lib/_models/dynamo-nts-global-settings.js +2 -2
  57. package/lib/_models/dynamo-nts-routing-module-settings.d.ts +19 -19
  58. package/lib/_models/dynamo-nts-routing-module-settings.js +2 -2
  59. package/lib/_models/dynamo-nts-socket-event-params.d.ts +32 -30
  60. package/lib/_models/dynamo-nts-socket-event-params.d.ts.map +1 -1
  61. package/lib/_models/dynamo-nts-socket-event-params.js +80 -79
  62. package/lib/_models/dynamo-nts-socket-event-params.js.map +1 -1
  63. package/lib/_models/dynamo-nts-socket-presence.d.ts +21 -0
  64. package/lib/_models/dynamo-nts-socket-presence.d.ts.map +1 -0
  65. package/lib/_models/dynamo-nts-socket-presence.js +45 -0
  66. package/lib/_models/dynamo-nts-socket-presence.js.map +1 -0
  67. package/lib/_models/dynamo-nts-socket-service-params.d.ts +18 -18
  68. package/lib/_models/dynamo-nts-socket-service-params.d.ts.map +1 -1
  69. package/lib/_models/dynamo-nts-socket-service-params.js +26 -26
  70. package/lib/_models/dynamo-nts-socket-service-params.js.map +1 -1
  71. package/lib/_models/index.d.ts +7 -8
  72. package/lib/_models/index.d.ts.map +1 -1
  73. package/lib/_models/index.js +12 -12
  74. package/lib/_models/index.js.map +1 -1
  75. package/lib/_modules/api-service.index.d.ts +5 -5
  76. package/lib/_modules/api-service.index.js +11 -11
  77. package/lib/_modules/app-extended.index.d.ts +15 -12
  78. package/lib/_modules/app-extended.index.d.ts.map +1 -1
  79. package/lib/_modules/app-extended.index.js +23 -19
  80. package/lib/_modules/app-extended.index.js.map +1 -1
  81. package/lib/_modules/app.index.d.ts +13 -10
  82. package/lib/_modules/app.index.d.ts.map +1 -1
  83. package/lib/_modules/app.index.js +21 -17
  84. package/lib/_modules/app.index.js.map +1 -1
  85. package/lib/_modules/auth.index.d.ts +3 -3
  86. package/lib/_modules/auth.index.js +8 -8
  87. package/lib/_modules/constants.index.d.ts +1 -1
  88. package/lib/_modules/constants.index.js +4 -4
  89. package/lib/_modules/controller.index.d.ts +4 -4
  90. package/lib/_modules/controller.index.js +10 -10
  91. package/lib/_modules/custom-data/custom-data.controller.d.ts +5 -5
  92. package/lib/_modules/custom-data/custom-data.controller.js +55 -55
  93. package/lib/_modules/custom-data/custom-data.service.d.ts +5 -5
  94. package/lib/_modules/custom-data/custom-data.service.js +11 -11
  95. package/lib/_modules/custom-data/get-custom-data-routing-module.d.ts +3 -3
  96. package/lib/_modules/custom-data/get-custom-data-routing-module.js +17 -17
  97. package/lib/_modules/custom-data/index.d.ts +4 -4
  98. package/lib/_modules/custom-data/index.js +8 -8
  99. package/lib/_modules/custom-data-module.index.d.ts +1 -1
  100. package/lib/_modules/custom-data-module.index.js +4 -4
  101. package/lib/_modules/data-service.index.d.ts +5 -5
  102. package/lib/_modules/data-service.index.js +10 -10
  103. package/lib/_modules/email.index.d.ts +3 -3
  104. package/lib/_modules/email.index.js +8 -8
  105. package/lib/_modules/enums.index.d.ts +1 -1
  106. package/lib/_modules/enums.index.js +4 -4
  107. package/lib/_modules/models.index.d.ts +1 -1
  108. package/lib/_modules/models.index.js +4 -4
  109. package/lib/_modules/services.index.d.ts +1 -1
  110. package/lib/_modules/services.index.js +4 -4
  111. package/lib/_modules/test/get-test-routing-module.d.ts +3 -3
  112. package/lib/_modules/test/get-test-routing-module.js +17 -17
  113. package/lib/_modules/test/index.d.ts +3 -3
  114. package/lib/_modules/test/index.js +7 -7
  115. package/lib/_modules/test/test.controller.d.ts +5 -5
  116. package/lib/_modules/test/test.controller.js +104 -104
  117. package/lib/_modules/test-module.index.d.ts +1 -1
  118. package/lib/_modules/test-module.index.js +4 -4
  119. package/lib/_modules/usage/get-usage-routing-module.d.ts +3 -3
  120. package/lib/_modules/usage/get-usage-routing-module.js +17 -17
  121. package/lib/_modules/usage/index.d.ts +4 -4
  122. package/lib/_modules/usage/index.js +8 -8
  123. package/lib/_modules/usage/usage-controller.d.ts +6 -6
  124. package/lib/_modules/usage/usage-controller.js +88 -88
  125. package/lib/_modules/usage/usage.service.d.ts +16 -16
  126. package/lib/_modules/usage/usage.service.js +132 -132
  127. package/lib/_modules/usage-module.index.d.ts +1 -1
  128. package/lib/_modules/usage-module.index.js +4 -4
  129. package/lib/_services/dynamo-nts-api.service.d.ts +34 -34
  130. package/lib/_services/dynamo-nts-api.service.js +188 -188
  131. package/lib/_services/dynamo-nts-app-extended.d.ts +128 -128
  132. package/lib/_services/dynamo-nts-app-extended.d.ts.map +1 -1
  133. package/lib/_services/dynamo-nts-app-extended.js +212 -212
  134. package/lib/_services/dynamo-nts-app-extended.js.map +1 -1
  135. package/lib/_services/dynamo-nts-app.d.ts +270 -278
  136. package/lib/_services/dynamo-nts-app.d.ts.map +1 -1
  137. package/lib/_services/dynamo-nts-app.js +371 -371
  138. package/lib/_services/dynamo-nts-app.js.map +1 -1
  139. package/lib/_services/dynamo-nts-app.spec.d.ts +1 -0
  140. package/lib/_services/dynamo-nts-app.spec.d.ts.map +1 -0
  141. package/lib/_services/dynamo-nts-app.spec.js +10 -0
  142. package/lib/_services/dynamo-nts-app.spec.js.map +1 -0
  143. package/lib/_services/dynamo-nts-auth.service.d.ts +127 -127
  144. package/lib/_services/dynamo-nts-auth.service.d.ts.map +1 -1
  145. package/lib/_services/dynamo-nts-auth.service.js +53 -53
  146. package/lib/_services/dynamo-nts-auth.service.js.map +1 -1
  147. package/lib/_services/dynamo-nts-controller.service.d.ts +105 -105
  148. package/lib/_services/dynamo-nts-controller.service.d.ts.map +1 -1
  149. package/lib/_services/dynamo-nts-controller.service.js +57 -57
  150. package/lib/_services/dynamo-nts-controller.service.js.map +1 -1
  151. package/lib/_services/dynamo-nts-data.service.d.ts +230 -230
  152. package/lib/_services/dynamo-nts-data.service.d.ts.map +1 -1
  153. package/lib/_services/dynamo-nts-data.service.js +689 -689
  154. package/lib/_services/dynamo-nts-data.service.js.map +1 -1
  155. package/lib/_services/dynamo-nts-db-service-collection.service.d.ts +8 -8
  156. package/lib/_services/dynamo-nts-db-service-collection.service.js +10 -10
  157. package/lib/_services/dynamo-nts-db.service.d.ts +386 -312
  158. package/lib/_services/dynamo-nts-db.service.d.ts.map +1 -1
  159. package/lib/_services/dynamo-nts-db.service.js +856 -856
  160. package/lib/_services/dynamo-nts-db.service.js.map +1 -1
  161. package/lib/_services/dynamo-nts-email-service-collection.service.d.ts +18 -18
  162. package/lib/_services/dynamo-nts-email-service-collection.service.js +20 -20
  163. package/lib/_services/dynamo-nts-email.service.d.ts +56 -56
  164. package/lib/_services/dynamo-nts-email.service.d.ts.map +1 -1
  165. package/lib/_services/dynamo-nts-email.service.js +203 -203
  166. package/lib/_services/dynamo-nts-email.service.js.map +1 -1
  167. package/lib/_services/dynamo-nts-global.service.d.ts +71 -84
  168. package/lib/_services/dynamo-nts-global.service.d.ts.map +1 -1
  169. package/lib/_services/dynamo-nts-global.service.js +156 -156
  170. package/lib/_services/dynamo-nts-global.service.js.map +1 -1
  171. package/lib/_services/dynamo-nts-routing-module.service.d.ts +76 -76
  172. package/lib/_services/dynamo-nts-routing-module.service.d.ts.map +1 -1
  173. package/lib/_services/dynamo-nts-routing-module.service.js +191 -191
  174. package/lib/_services/dynamo-nts-routing-module.service.js.map +1 -1
  175. package/lib/_services/dynamo-nts-shared.service.d.ts +31 -31
  176. package/lib/_services/dynamo-nts-shared.service.d.ts.map +1 -1
  177. package/lib/_services/dynamo-nts-shared.service.js +70 -70
  178. package/lib/_services/dynamo-nts-shared.service.js.map +1 -1
  179. package/lib/_services/dynamo-nts-shared.service.spec.d.ts +2 -0
  180. package/lib/_services/dynamo-nts-shared.service.spec.d.ts.map +1 -0
  181. package/lib/_services/dynamo-nts-shared.service.spec.js +12 -0
  182. package/lib/_services/dynamo-nts-shared.service.spec.js.map +1 -0
  183. package/lib/_services/dynamo-nts-singleton.service.d.ts +8 -8
  184. package/lib/_services/dynamo-nts-singleton.service.js +18 -18
  185. package/lib/_services/dynamo-nts-singleton.service.js.map +1 -1
  186. package/lib/_services/dynamo-nts-socket-service-collection.service.d.ts +9 -9
  187. package/lib/_services/dynamo-nts-socket-service-collection.service.js +13 -13
  188. package/lib/_services/dynamo-nts-socket.service.d.ts +57 -45
  189. package/lib/_services/dynamo-nts-socket.service.d.ts.map +1 -1
  190. package/lib/_services/dynamo-nts-socket.service.js +188 -165
  191. package/lib/_services/dynamo-nts-socket.service.js.map +1 -1
  192. package/lib/_services/index.d.ts +14 -16
  193. package/lib/_services/index.d.ts.map +1 -1
  194. package/lib/_services/index.js +20 -20
  195. package/lib/_services/index.js.map +1 -1
  196. package/lib/index.d.ts +6 -6
  197. package/lib/index.js +14 -14
  198. package/lib/tsconfig.tsbuildinfo +1 -1
  199. package/package SAVE.json +216 -0
  200. package/package-NEW with test but not working.json +228 -0
  201. package/package.json +14 -8
  202. package/spec/support/jasmine.json +14 -0
  203. package/src/_enums/http/socket-event.enum.ts +2 -1
  204. package/src/_interfaces/certification-settings.interface.ts +7 -0
  205. package/src/_interfaces/global-service-settings.interface.ts +23 -0
  206. package/src/_interfaces/index.ts +5 -0
  207. package/src/_interfaces/port-settings.interface.ts +5 -0
  208. package/src/_models/dynamo-nts-socket-event-params.ts +23 -21
  209. package/src/_models/{dynamo-nts-socket-dictionary.ts → dynamo-nts-socket-presence.ts} +21 -5
  210. package/src/_models/dynamo-nts-socket-service-params.ts +1 -0
  211. package/src/_models/index.ts +2 -2
  212. package/src/_modules/app-extended.index.ts +7 -2
  213. package/src/_modules/app.index.ts +5 -0
  214. package/src/_services/dynamo-nts-app-extended.ts +6 -2
  215. package/src/_services/dynamo-nts-app.spec.ts +14 -0
  216. package/src/_services/dynamo-nts-app.ts +8 -14
  217. package/src/_services/dynamo-nts-auth.service.ts +1 -0
  218. package/src/_services/dynamo-nts-controller.service.ts +2 -1
  219. package/src/_services/dynamo-nts-data.service.ts +19 -19
  220. package/src/_services/dynamo-nts-db.service.ts +195 -115
  221. package/src/_services/dynamo-nts-email.service.ts +1 -0
  222. package/src/_services/dynamo-nts-global.service.ts +3 -18
  223. package/src/_services/dynamo-nts-routing-module.service.ts +1 -0
  224. package/src/_services/dynamo-nts-shared.service.spec.ts +10 -0
  225. package/src/_services/dynamo-nts-shared.service.ts +2 -1
  226. package/src/_services/dynamo-nts-socket.service.ts +80 -41
  227. package/src/_services/index.ts +2 -2
@@ -1,857 +1,857 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DynamoNTS_DBService = void 0;
4
- const tslib_1 = require("tslib");
5
- const mongoose = tslib_1.__importStar(require("mongoose"));
6
- const mongoose_1 = require("mongoose");
7
- const fsm_dynamo_1 = require("@futdevpro/fsm-dynamo");
8
- /**
9
- *
10
- */
11
- class DynamoNTS_DBService {
12
- /**
13
- * @param dataName name the model
14
- * @param typeSample sample data for scheme creation (dont include Dynamo_Metadata!)
15
- * @param schemaSettings additional settings for specific parameters as unique, required, minlength or maxlength
16
- * schemaSettings also MUST contain specific types that differs from the listed above (Array, Date)
17
- */
18
- constructor(dataParams) {
19
- this.dataParams = dataParams;
20
- this.dataModel = mongoose.model(this.dataParams.dbName, this.getSchema());
21
- this.defaultErrorUserMsg = `We encountered an unhandled DB Error, ` +
22
- `\nplease contact the responsible development team.`;
23
- this.lookForDependencyDataSettings();
24
- }
25
- /**
26
- * save new data
27
- * @param data data
28
- * @returns data
29
- */
30
- createData(data, issuer) {
31
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
32
- data.__created = new Date();
33
- data.__lastModified = new Date();
34
- data.__createdBy = issuer;
35
- data.__lastModifiedBy = issuer;
36
- const dataModel = new this.dataModel(data);
37
- let newData = yield dataModel.save().then(res => {
38
- if (res) {
39
- return res === null || res === void 0 ? void 0 : res.toObject();
40
- }
41
- else {
42
- throw new fsm_dynamo_1.Dynamo_Error({
43
- status: 204,
44
- errorCode: 'NTS-DBS-CD1',
45
- addECToUserMsg: true,
46
- message: `save ${this.dataParams.dbName} result not found! (NTS DB)`,
47
- userMessage: this.defaultErrorUserMsg
48
- });
49
- }
50
- }).catch(error => {
51
- if ((error === null || error === void 0 ? void 0 : error.flag) == 'DYNAMO ERROR OBJECT') {
52
- throw error;
53
- }
54
- else {
55
- throw new fsm_dynamo_1.Dynamo_Error({
56
- status: 422,
57
- errorCode: 'NTS-DBS-CD0',
58
- addECToUserMsg: true,
59
- message: `Create new ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
60
- userMessage: this.defaultErrorUserMsg,
61
- error
62
- });
63
- }
64
- });
65
- if (typeof newData._id === 'object') {
66
- newData._id = `${newData._id}`;
67
- }
68
- data._id = newData._id;
69
- data.__v = newData.__v;
70
- return data;
71
- });
72
- }
73
- /**
74
- * Find data by _id and update
75
- * @param data data
76
- * @returns data
77
- */
78
- modifyData(data, issuer) {
79
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
80
- data.__lastModified = new Date();
81
- data.__lastModifiedBy = issuer;
82
- const dataModel = new this.dataModel(data);
83
- let newData = yield this.dataModel.findByIdAndUpdate(data._id, dataModel).then(res => {
84
- if (res) {
85
- return res === null || res === void 0 ? void 0 : res.toObject();
86
- }
87
- else {
88
- throw new fsm_dynamo_1.Dynamo_Error({
89
- status: 204,
90
- errorCode: 'NTS-DBS-FU1',
91
- addECToUserMsg: true,
92
- message: `modify ${this.dataParams.dbName} result not found! (NTS DB)`,
93
- userMessage: this.defaultErrorUserMsg,
94
- });
95
- }
96
- }).catch(error => {
97
- throw new fsm_dynamo_1.Dynamo_Error({
98
- status: 417,
99
- errorCode: 'NTS-DBS-FU0',
100
- addECToUserMsg: true,
101
- message: `modify ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
102
- userMessage: this.defaultErrorUserMsg,
103
- error
104
- });
105
- });
106
- if (typeof newData._id === 'object') {
107
- newData._id = `${newData._id}`;
108
- }
109
- data._id = newData._id;
110
- data.__v = newData.__v;
111
- return data;
112
- });
113
- }
114
- /**
115
- * returns data by _id,
116
- * @param id id
117
- * @returns data
118
- */
119
- getDataById(id) {
120
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
121
- let data = yield this.dataModel.findById(id).then(res => {
122
- var _a;
123
- return (_a = res === null || res === void 0 ? void 0 : res.toObject()) !== null && _a !== void 0 ? _a : null;
124
- }).catch(error => {
125
- throw new fsm_dynamo_1.Dynamo_Error({
126
- status: 417,
127
- errorCode: 'NTS-DBS-GI0',
128
- addECToUserMsg: true,
129
- message: `get ${this.dataParams.dbName} by ID was unsuccessful (NTS DB)`,
130
- userMessage: this.defaultErrorUserMsg,
131
- error
132
- });
133
- });
134
- if (data && typeof data._id === 'object') {
135
- data._id = `${data._id}`;
136
- }
137
- return data;
138
- });
139
- }
140
- /**
141
- * get data by dependency data id,
142
- * !!!: throws error if not found (errorCode on not found: NTS-DBS-GD2)
143
- *
144
- * @param dependencyId id
145
- * @returns data
146
- */
147
- getDataByDependencyId(dependencyId) {
148
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
149
- if (!this.depDataName) {
150
- throw new fsm_dynamo_1.Dynamo_Error({
151
- status: 501,
152
- errorCode: 'NTS-DBS-GD0',
153
- addECToUserMsg: true,
154
- message: `dependencyDataIdKey not setted up for this db-service (${this.dataParams.dbName}) (NTS DB)`,
155
- userMessage: this.defaultErrorUserMsg
156
- });
157
- }
158
- let data = yield this.dataModel.findOne({ [this.depDataName]: dependencyId }).then(res => {
159
- var _a;
160
- return (_a = res === null || res === void 0 ? void 0 : res.toObject()) !== null && _a !== void 0 ? _a : null;
161
- }).catch(error => {
162
- throw new fsm_dynamo_1.Dynamo_Error({
163
- status: 417,
164
- errorCode: 'NTS-DBS-GD1',
165
- addECToUserMsg: true,
166
- message: `get ${this.dataParams.dbName} by ${this.depDataName} was unsuccessful (NTS DB)`,
167
- userMessage: this.defaultErrorUserMsg,
168
- error
169
- });
170
- });
171
- if (data && typeof data._id === 'object') {
172
- data._id = `${data._id}`;
173
- }
174
- return data;
175
- });
176
- }
177
- /**
178
- * get data by dependency data id,
179
- * !!!: throws error if not found (errorCode on not found: NTS-DBS-GLD2)
180
- *
181
- * @param dependencyId id
182
- * @returns dataList
183
- */
184
- getDataListByDependencyId(dependencyId) {
185
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
186
- if (!this.depDataName) {
187
- throw new fsm_dynamo_1.Dynamo_Error({
188
- status: 501,
189
- errorCode: 'NTS-DBS-GLD0',
190
- addECToUserMsg: true,
191
- message: `dependencyDataIdKey not setted up for this db-service (${this.dataParams.dbName}) (NTS DB)`,
192
- userMessage: this.defaultErrorUserMsg
193
- });
194
- }
195
- let dataList = yield this.dataModel.find({ [this.depDataName]: dependencyId }).then(res => {
196
- var _a;
197
- return (_a = res) !== null && _a !== void 0 ? _a : [];
198
- }).catch(error => {
199
- throw new fsm_dynamo_1.Dynamo_Error({
200
- status: 417,
201
- errorCode: 'NTS-DBS-GLD1',
202
- addECToUserMsg: true,
203
- message: `get ${this.dataParams.dbName} by ${this.depDataName} was unsuccessful (NTS DB)`,
204
- userMessage: this.defaultErrorUserMsg,
205
- error
206
- });
207
- });
208
- if (0 < dataList.length) {
209
- dataList.forEach((data) => {
210
- if (data && typeof data._id === 'object') {
211
- data._id = `${data._id}`;
212
- }
213
- });
214
- }
215
- return dataList;
216
- });
217
- }
218
- /**
219
- * get multiple data objects by a list of DependencyIDs,
220
- * !!!: throws error if not found (errorCode on not found: NTS-DBS-GLDS2)
221
- *
222
- * @param ids ids
223
- * @returns dataList
224
- */
225
- getDataListByDependencyIds(ids) {
226
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
227
- if (!this.depDataName) {
228
- throw new fsm_dynamo_1.Dynamo_Error({
229
- status: 501,
230
- errorCode: 'NTS-DBS-GLDS0',
231
- addECToUserMsg: true,
232
- message: `dependencyDataIdKey not setted up for this db-service (${this.dataParams.dbName}) (NTS DB)`,
233
- userMessage: this.defaultErrorUserMsg
234
- });
235
- }
236
- let dataList = yield this.dataModel.find({ [this.depDataName]: { $in: ids } }).then(res => {
237
- var _a;
238
- return (_a = res) !== null && _a !== void 0 ? _a : [];
239
- }).catch(error => {
240
- throw new fsm_dynamo_1.Dynamo_Error({
241
- status: 417,
242
- errorCode: 'NTS-DBS-GLDS1',
243
- addECToUserMsg: true,
244
- message: `get ${this.dataParams.dbName}s by ${this.depDataName}s was unsuccessful (NTS DB)`,
245
- userMessage: this.defaultErrorUserMsg,
246
- error
247
- });
248
- });
249
- if (0 < dataList.length) {
250
- dataList.forEach((data) => {
251
- if (data && typeof data._id === 'object') {
252
- data._id = `${data._id}`;
253
- }
254
- });
255
- }
256
- return dataList;
257
- });
258
- }
259
- /**
260
- * returns search result for searchBy object params
261
- * can use lists or xRange values for searchBy obj properties
262
- *
263
- * @param searchBy filter
264
- * @param narrowByDependencyIds id
265
- * @returns dataList
266
- */
267
- searchData(searchBy, narrowByDependencyIds) {
268
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
269
- const filter = {};
270
- if (0 < narrowByDependencyIds.length) {
271
- if (!this.depDataName) {
272
- throw new fsm_dynamo_1.Dynamo_Error({
273
- status: 501,
274
- errorCode: 'NTS-DBS-SD0',
275
- addECToUserMsg: true,
276
- message: `dependencyDataIdKey not setted up for this db-service (${this.dataParams.dbName}) (NTS DB)`,
277
- userMessage: this.defaultErrorUserMsg
278
- });
279
- }
280
- filter[this.depDataName] = { $in: narrowByDependencyIds };
281
- }
282
- yield this.dataParams.modelParams.forEach((modelParam) => {
283
- if ((searchBy[modelParam.key] !== null && searchBy[modelParam.key] !== undefined) ||
284
- searchBy[modelParam.key + 'Range']) {
285
- if (modelParam.key.includes('Range') || modelParam.type.includes('[]')) {
286
- // inverz search filter (for Range and Array functions)
287
- if (modelParam.key.includes('Range')) {
288
- const searchParamKeyWithoutRange = modelParam.key.split('Range')[0];
289
- if (searchBy[searchParamKeyWithoutRange] !== null &&
290
- searchBy[searchParamKeyWithoutRange] !== undefined) {
291
- filter[modelParam.key] = {
292
- from: { $lte: searchBy[searchParamKeyWithoutRange] },
293
- to: { $gte: searchBy[searchParamKeyWithoutRange] }
294
- };
295
- }
296
- }
297
- else {
298
- if (searchBy[modelParam.key] !== null &&
299
- searchBy[modelParam.key] !== undefined) {
300
- filter[modelParam.key] = { $in: searchBy[modelParam.key] };
301
- }
302
- }
303
- }
304
- else {
305
- // basic search filter
306
- if (searchBy[modelParam.key + 'Range']) {
307
- if (searchBy[modelParam.key + 'Range'].from || searchBy[modelParam.key + 'Range'].to) {
308
- filter[modelParam.key] = {};
309
- if (searchBy[modelParam.key + 'Range'].from) {
310
- filter[modelParam.key].$gte = searchBy[modelParam.key + 'Range'].from;
311
- }
312
- if (searchBy[modelParam.key + 'Range'].to) {
313
- filter[modelParam.key].$lte = searchBy[modelParam.key + 'Range'].to;
314
- }
315
- }
316
- }
317
- else if (searchBy[modelParam.key] !== null && searchBy[modelParam.key] !== undefined) {
318
- if (searchBy[modelParam.key].lenght > 0) {
319
- filter[modelParam.key] = { $in: searchBy[modelParam.key] };
320
- }
321
- }
322
- }
323
- }
324
- });
325
- let dataList = yield this.dataModel.find(filter)
326
- .then(res => res !== null && res !== void 0 ? res : [])
327
- .catch(error => {
328
- throw new fsm_dynamo_1.Dynamo_Error({
329
- status: 417,
330
- errorCode: 'NTS-DBS-SD1',
331
- addECToUserMsg: true,
332
- message: `search ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
333
- userMessage: this.defaultErrorUserMsg,
334
- error
335
- });
336
- });
337
- if (0 < dataList.length) {
338
- dataList.forEach((data) => {
339
- if (data && typeof data._id === 'object') {
340
- data._id = `${data._id}`;
341
- }
342
- });
343
- }
344
- return dataList;
345
- });
346
- }
347
- /**
348
- * returns all data from database,
349
- * !!!: throws error if not found (errorCode on not found: NTS-DBS-GA1)
350
- *
351
- * @returns dataList
352
- */
353
- getAll() {
354
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
355
- let dataList = yield this.dataModel.find({})
356
- .then(res => {
357
- var _a;
358
- return (_a = res) !== null && _a !== void 0 ? _a : [];
359
- }).catch(error => {
360
- throw new fsm_dynamo_1.Dynamo_Error({
361
- status: 417,
362
- errorCode: 'NTS-DBS-GA0',
363
- addECToUserMsg: true,
364
- message: `get all ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
365
- userMessage: this.defaultErrorUserMsg,
366
- error
367
- });
368
- });
369
- if (0 < dataList.length && typeof dataList[0]._id === 'object') {
370
- dataList.forEach((data) => {
371
- data._id = `˙${data._id}`;
372
- });
373
- }
374
- return dataList;
375
- });
376
- }
377
- /**
378
- * deleted data by id
379
- * @param id id
380
- */
381
- deleteDataById(id) {
382
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
383
- yield this.dataModel.findByIdAndDelete(id).catch(error => {
384
- throw new fsm_dynamo_1.Dynamo_Error({
385
- status: 417,
386
- errorCode: 'NTS-DBS-DD0',
387
- addECToUserMsg: true,
388
- message: `delete ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
389
- userMessage: this.defaultErrorUserMsg,
390
- error
391
- });
392
- });
393
- });
394
- }
395
- /**
396
- * deleted data by id
397
- * @param dependencyId id
398
- */
399
- deleteDataByDependencyId(dependencyId) {
400
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
401
- if (!this.depDataName) {
402
- throw new fsm_dynamo_1.Dynamo_Error({
403
- status: 501,
404
- errorCode: 'NTS-DBS-DDD0',
405
- addECToUserMsg: true,
406
- message: `dependencyDataIdKey not setted up for this db-service (${this.dataParams.dbName}) (NTS DB)`,
407
- userMessage: this.defaultErrorUserMsg
408
- });
409
- }
410
- yield this.dataModel.deleteMany({ [this.depDataName]: dependencyId }).catch(error => {
411
- throw new fsm_dynamo_1.Dynamo_Error({
412
- status: 417,
413
- errorCode: 'NTS-DBS-DDD1',
414
- addECToUserMsg: true,
415
- message: `delete ${this.dataParams.dbName} by ${this.depDataName} was unsuccessful (NTS DB)`,
416
- userMessage: this.defaultErrorUserMsg,
417
- error
418
- });
419
- });
420
- });
421
- }
422
- // ----------------------------------------------------------------------------------
423
- // ----------------------------------------------------------------------------------
424
- // ----------------------------------------------------------------------------------
425
- // DIRECT Basic MONGOOSE FUNCTIONS (with error handlings)
426
- /**
427
- * Find the data first by any of its parameters,
428
- * !!!: throws error if not found (errorCode on not found: NTS-DBS-FO1)
429
- *
430
- * @param filter if you can, use unique parameters for find!
431
- *
432
- * @example
433
- * // by email:
434
- * { email: email }
435
- * //
436
- * @example
437
- * // or by id that is in list:
438
- * { userIds: { $in: this.userId } }
439
- * //
440
- * @example
441
- * // or by number or Date that is Greater Than AND Less Than:
442
- * { points: { $gt: 2, $lt: 14 } }
443
- * // further tools (syntax matches with $gt):
444
- * $eq: // Matches values that are EQual to a specified value.
445
- * $gte: // Matches values that are Greater Than OR Equal to a specified value.
446
- * $lte: // Matches values that are Less Than or Equal to a specified value.
447
- * $ne: // Matches all values that are Not Equal to a specified value.
448
- * $nin: // Matches None of the values specified IN an array.
449
- * //
450
- * @returns {T} data: T
451
- */
452
- findOne(filter) {
453
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
454
- let data = yield this.dataModel.findOne(filter).then(res => {
455
- var _a;
456
- return (_a = res) !== null && _a !== void 0 ? _a : null;
457
- }).catch(error => {
458
- throw new fsm_dynamo_1.Dynamo_Error({
459
- status: 417,
460
- errorCode: 'NTS-DBS-FO0',
461
- addECToUserMsg: true,
462
- message: `findOne ${this.dataParams.dbName} was unsuccessful (NTS DB) fliter: ${filter}`,
463
- userMessage: this.defaultErrorUserMsg,
464
- error
465
- });
466
- });
467
- if (data && typeof data._id === 'object') {
468
- data._id = `${data._id}`;
469
- }
470
- return data;
471
- });
472
- }
473
- /**
474
- * #MONGOOSE FUNCTION
475
- * Find the data first by any of its parameters,
476
- * !!!: throws error if not found (errorCode on not found: NTS-DBS-F1)
477
- *
478
- * @param filter if you can, use unique parameters for find!
479
- *
480
- * @example
481
- * // by email:
482
- * { email: email }
483
- * //
484
- * @example
485
- * // or by id that is in list:
486
- * { userIds: { $in: this.userId } }
487
- * //
488
- * @example
489
- * // or by number or Date that is Greater Than AND Less Than:
490
- * { points: { $gt: 2, $lt: 14 } }
491
- * // further tools (syntax matches with $gt):
492
- * $eq: // Matches values that are EQual to a specified value.
493
- * $gte: // Matches values that are Greater Than OR Equal to a specified value.
494
- * $lte: // Matches values that are Less Than or Equal to a specified value.
495
- * $ne: // Matches all values that are Not Equal to a specified value.
496
- * $nin: // Matches None of the values specified IN an array.
497
- * //
498
- * @returns {T[]} dataList: T[]
499
- */
500
- find(filter) {
501
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
502
- let dataList = yield this.dataModel.find(filter)
503
- .then(res => {
504
- var _a;
505
- return (_a = res) !== null && _a !== void 0 ? _a : [];
506
- }).catch(error => {
507
- throw new fsm_dynamo_1.Dynamo_Error({
508
- status: 417,
509
- errorCode: 'NTS-DBS-F0',
510
- addECToUserMsg: true,
511
- message: `find ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
512
- userMessage: this.defaultErrorUserMsg,
513
- error
514
- });
515
- });
516
- if (0 < dataList.length) {
517
- dataList.forEach((data) => {
518
- if (data && typeof data._id === 'object') {
519
- data._id = `${data._id}`;
520
- }
521
- });
522
- }
523
- return dataList;
524
- });
525
- }
526
- /**
527
- * #MONGOOSE FUNCTION
528
- * Find the data first by any of its parameters
529
- *
530
- * @param filter if you can, use unique parameters for find!
531
- *
532
- * @example
533
- * // by email:
534
- * { email: email }
535
- * //
536
- * @example
537
- * // or by id that is in list:
538
- * { userIds: { $in: this.userId } }
539
- * //
540
- * @example
541
- * // or by number or Date that is Greater Than AND Less Than:
542
- * { points: { $gt: 2, $lt: 14 } }
543
- * // further tools (syntax matches with $gt):
544
- * $eq: // Matches values that are EQual to a specified value.
545
- * $gte: // Matches values that are Greater Than OR Equal to a specified value.
546
- * $lte: // Matches values that are Less Than or Equal to a specified value.
547
- * $ne: // Matches all values that are Not Equal to a specified value.
548
- * $nin: // Matches None of the values specified IN an array.
549
- * //
550
- * @param page page
551
- * @param pageSize pageSize
552
- * @param sort
553
- * @example
554
- * // by dateTime (this uses the basic sort function):
555
- * { dateTime: -1 }
556
- * //
557
- * @returns {T[]} dataList: T[]
558
- */
559
- findWithPaging(filter, page, pageSize, sort) {
560
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
561
- let dataList = yield this.dataModel.find(filter)
562
- .sort(sort)
563
- .skip(page * pageSize)
564
- .limit(pageSize)
565
- .then(res => res !== null && res !== void 0 ? res : [])
566
- .catch(error => {
567
- throw new fsm_dynamo_1.Dynamo_Error({
568
- status: 417,
569
- errorCode: 'NTS-DBS-WP0',
570
- addECToUserMsg: true,
571
- message: `findWithPaging ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
572
- userMessage: this.defaultErrorUserMsg,
573
- error
574
- });
575
- });
576
- if (0 < dataList.length) {
577
- dataList.forEach((data) => {
578
- if (data && typeof data._id === 'object') {
579
- data._id = `${data._id}`;
580
- }
581
- });
582
- }
583
- return dataList;
584
- });
585
- }
586
- /**
587
- * #MONGOOSE FUNCTION
588
- * Find data by _id and update
589
- * !!!: throws error if not found (errorCode on not found: )
590
- *
591
- * @param id id
592
- * @param update update
593
- * @returns data
594
- */
595
- findByIdAndUpdate(id, update, modifier) {
596
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
597
- update.__lastModified = new Date();
598
- update.__lastModifiedBy = modifier;
599
- let newData = yield this.dataModel.findByIdAndUpdate(id, update).then(res => {
600
- var _a;
601
- return (_a = res === null || res === void 0 ? void 0 : res.toObject()) !== null && _a !== void 0 ? _a : null;
602
- }).catch(error => {
603
- throw new fsm_dynamo_1.Dynamo_Error({
604
- status: 417,
605
- errorCode: 'NTS-DBS-FIU0',
606
- addECToUserMsg: true,
607
- message: `findByIdAndUpdate ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
608
- userMessage: this.defaultErrorUserMsg,
609
- error
610
- });
611
- });
612
- if (typeof newData._id === 'object') {
613
- newData._id = `${newData._id}`;
614
- }
615
- return newData;
616
- });
617
- }
618
- /**
619
- * #MONGOOSE FUNCTION
620
- * Find the data first by any of its parameters
621
- *
622
- * @param filter This uses the basic Mongoose updateOne.
623
- * If you can, use unique parameters for find!
624
- * @example
625
- * // by email:
626
- * { email: email }
627
- * //
628
- * @example
629
- * // or by id that is in list:
630
- * { userIds: { $in: this.userId } }
631
- * //
632
- * @example
633
- * // or by number or Date that is Greater Than AND Less Than:
634
- * { points: { $gt: 2, $lt: 14 } }
635
- * // further tools (syntax matches with $gt):
636
- * $eq: // Matches values that are EQual to a specified value.
637
- * $gte: // Matches values that are Greater Than OR Equal to a specified value.
638
- * $lte: // Matches values that are Less Than or Equal to a specified value.
639
- * $ne: // Matches all values that are Not Equal to a specified value.
640
- * $nin: // Matches None of the values specified IN an array.
641
- * //
642
- *
643
- * @param update this uses the basic Mongoose updateOne
644
- * @example
645
- * // increase a specific value (here by 15):
646
- * { $inc: { popularity: 15 } }
647
- * //
648
- * @example
649
- * // or add element to a list:
650
- * { $push: { reactions: this.newReaction }
651
- * // or add multiple elements to a list
652
- * { $push: { schedule: {$each: [ monday, tuesday, wednesday ] } } }
653
- * //
654
- * @example
655
- * // or all at once
656
- * {
657
- * $inc: { popularity: this.newVote.amount },
658
- * emailVerified: true,
659
- * $push: { reactions: this.newReaction }
660
- * }
661
- * // further tools (syntax matches with $inc):
662
- * $currentDate: // Sets the value of a field to current date, either as a Date or a Timestamp.
663
- * $min: // Only updates the field if the specified value is less than the existing field value.
664
- * $max: // Only updates the field if the specified value is greater than the existing field value.
665
- * $mul: // Multiplies the value of the field by the specified amount.
666
- * $rename: // Renames a field.
667
- * $unset: // Removes the specified field from a document. (set: "" to value)
668
- * //
669
- */
670
- updateOne(updateBy, update, issuer) {
671
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
672
- update.__lastModified = new Date();
673
- update.__lastModifiedBy = issuer;
674
- yield this.dataModel.updateOne(updateBy, update).catch(error => {
675
- throw new fsm_dynamo_1.Dynamo_Error({
676
- status: 417,
677
- errorCode: 'NTS-DBS-UO0',
678
- addECToUserMsg: true,
679
- message: `updateOne ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
680
- userMessage: this.defaultErrorUserMsg,
681
- error
682
- });
683
- });
684
- });
685
- }
686
- /**
687
- * #MONGOOSE FUNCTION
688
- * update one parameter by a specific
689
- *
690
- * @param filter This uses the basic Mongoose updateMany.
691
- * @example
692
- * // by email:
693
- * { email: email }
694
- * //
695
- * @example
696
- * // or by id that is in list:
697
- * { userIds: { $in: this.userId } }
698
- * //
699
- * @example
700
- * // or by number or Date that is Greater Than AND Less Than:
701
- * { points: { $gt: 2, $lt: 14 } }
702
- * // further tools (syntax matches with $gt):
703
- * $eq: // Matches values that are EQual to a specified value.
704
- * $gte: // Matches values that are Greater Than OR Equal to a specified value.
705
- * $lte: // Matches values that are Less Than or Equal to a specified value.
706
- * $ne: // Matches all values that are Not Equal to a specified value.
707
- * $nin: // Matches None of the values specified IN an array.
708
- * //
709
- *
710
- * @param update this uses the basic Mongoose updateOne
711
- * @example
712
- * // increase a specific value (here by 15):
713
- * { $inc: { popularity: 15 } }
714
- * //
715
- * @example
716
- * // or add element to a list:
717
- * { $push: { reactions: this.newReaction }
718
- * // or add multiple elements to a list
719
- * { $push: { schedule: {$each: [ monday, tuesday, wednesday ] } } }
720
- * //
721
- * @example
722
- * // or all at once
723
- * {
724
- * $inc: { popularity: this.newVote.amount },
725
- * emailVerified: true,
726
- * $push: { reactions: this.newReaction }
727
- * }
728
- * // further tools (syntax matches with $inc):
729
- * $currentDate: // Sets the value of a field to current date, either as a Date or a Timestamp.
730
- * $min: // Only updates the field if the specified value is less than the existing field value.
731
- * $max: // Only updates the field if the specified value is greater than the existing field value.
732
- * $mul: // Multiplies the value of the field by the specified amount.
733
- * $rename: // Renames a field.
734
- * $unset: // Removes the specified field from a document. (set: "" to value)
735
- * //
736
- */
737
- updateMany(updateBy, update, issuer) {
738
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
739
- update.__lastModified = new Date();
740
- update.__lastModifiedBy = issuer;
741
- yield this.dataModel.updateMany(updateBy, update).catch(error => {
742
- throw new fsm_dynamo_1.Dynamo_Error({
743
- status: 417,
744
- errorCode: 'NTS-DBS-UM0',
745
- addECToUserMsg: true,
746
- message: `updateMany ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
747
- userMessage: this.defaultErrorUserMsg,
748
- error
749
- });
750
- });
751
- });
752
- }
753
- // ----------------------------------------------------------------------------------
754
- // ----------------------------------------------------------------------------------
755
- // ----------------------------------------------------------------------------------
756
- // PRIVATE BUILD FUNCTIONS
757
- /**
758
- * builds and returns mongoose schema
759
- * @returns schema
760
- */
761
- getSchema() {
762
- let schema = this.buildMongooseSchemaByModelParams();
763
- schema = this.addDynamo_MetadataToSchema(schema);
764
- // tslint:disable-next-line: no-string-literal
765
- if (schema['_id']) {
766
- // tslint:disable-next-line: no-string-literal
767
- delete schema['_id'];
768
- }
769
- return new mongoose_1.Schema(schema);
770
- }
771
- /**
772
- * builds mongoose schema building settings object by dynamoDataModelParamsList
773
- * @param params DynamoBEDataPropertyParams
774
- * @returns mongoose schema object
775
- */
776
- buildMongooseSchemaByModelParams(params) {
777
- const schemaSettingsObj = {};
778
- if (!params) {
779
- params = this.dataParams.modelParams;
780
- }
781
- params.forEach((property) => {
782
- var _a;
783
- const beType = this.getBEType(property.type);
784
- if (beType !== Object || !(property === null || property === void 0 ? void 0 : property.subObjectParams) || ((_a = property === null || property === void 0 ? void 0 : property.subObjectParams) === null || _a === void 0 ? void 0 : _a.length) == 0) {
785
- schemaSettingsObj[property.key] = {
786
- type: beType
787
- };
788
- if (property.index) {
789
- schemaSettingsObj[property.key].index = true;
790
- }
791
- if (property.unique) {
792
- schemaSettingsObj[property.key].unique = true;
793
- }
794
- if (property.required) {
795
- schemaSettingsObj[property.key].required = true;
796
- }
797
- if (property.minlength) {
798
- schemaSettingsObj[property.key].minlength = property.minlength;
799
- }
800
- if (property.maxlength) {
801
- schemaSettingsObj[property.key].maxlength = property.maxlength;
802
- }
803
- }
804
- else {
805
- schemaSettingsObj[property.key] = this.buildMongooseSchemaByModelParams(property.subObjectParams);
806
- }
807
- });
808
- return schemaSettingsObj;
809
- }
810
- getBEType(type) {
811
- switch (type) {
812
- case 'string':
813
- return String;
814
- case 'number':
815
- return Number;
816
- case 'boolean':
817
- return Boolean;
818
- case 'Date':
819
- return Date;
820
- case 'object':
821
- return Object;
822
- default:
823
- if (type.includes('[]')) {
824
- return Array;
825
- }
826
- else if (type.includes('=>') || type.includes('function')) {
827
- return Function;
828
- }
829
- else {
830
- return Object;
831
- }
832
- }
833
- }
834
- /**
835
- * adds dynamo metadata settings to any mongoose schema building settings object
836
- * @param schema schema to update
837
- * @returns updated schema
838
- */
839
- addDynamo_MetadataToSchema(schema) {
840
- schema.__created = { type: Date };
841
- schema.__createdBy = { type: String };
842
- schema.__lastModified = { type: Date };
843
- schema.__lastModifiedBy = { type: String };
844
- return schema;
845
- }
846
- /**
847
- * sets depDataKey
848
- */
849
- lookForDependencyDataSettings() {
850
- const dependencyParam = this.dataParams.modelParams.find((modelParams) => modelParams.isDependencyHook);
851
- if (dependencyParam) {
852
- this.depDataName = dependencyParam.key;
853
- }
854
- }
855
- }
856
- exports.DynamoNTS_DBService = DynamoNTS_DBService;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DynamoNTS_DBService = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const mongoose = tslib_1.__importStar(require("mongoose"));
6
+ const mongoose_1 = require("mongoose");
7
+ const fsm_dynamo_1 = require("@futdevpro/fsm-dynamo");
8
+ /**
9
+ *
10
+ */
11
+ class DynamoNTS_DBService {
12
+ /**
13
+ * @param dataName name the model
14
+ * @param typeSample sample data for scheme creation (dont include Dynamo_Metadata!)
15
+ * @param schemaSettings additional settings for specific parameters as unique, required, minlength or maxlength
16
+ * schemaSettings also MUST contain specific types that differs from the listed above (Array, Date)
17
+ */
18
+ constructor(dataParams) {
19
+ this.dataParams = dataParams;
20
+ this.dataModel = mongoose.model(this.dataParams.dbName, this.getSchema());
21
+ this.defaultErrorUserMsg = `We encountered an unhandled DB Error, ` +
22
+ `\nplease contact the responsible development team.`;
23
+ this.lookForDependencyDataSettings();
24
+ }
25
+ /**
26
+ * save new data
27
+ * @param data data
28
+ * @returns data
29
+ */
30
+ createData(data, issuer) {
31
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
32
+ data.__created = new Date();
33
+ data.__lastModified = new Date();
34
+ data.__createdBy = issuer;
35
+ data.__lastModifiedBy = issuer;
36
+ const dataModel = new this.dataModel(data);
37
+ let newData = yield dataModel.save().then(res => {
38
+ if (res) {
39
+ return res === null || res === void 0 ? void 0 : res.toObject();
40
+ }
41
+ else {
42
+ throw new fsm_dynamo_1.Dynamo_Error({
43
+ status: 204,
44
+ errorCode: 'NTS-DBS-CD1',
45
+ addECToUserMsg: true,
46
+ message: `createData failed, save ${this.dataParams.dbName} result not found! (NTS DB)`,
47
+ userMessage: this.defaultErrorUserMsg
48
+ });
49
+ }
50
+ }).catch(error => {
51
+ if ((error === null || error === void 0 ? void 0 : error.flag) == 'DYNAMO ERROR OBJECT') {
52
+ throw error;
53
+ }
54
+ else {
55
+ throw new fsm_dynamo_1.Dynamo_Error({
56
+ status: 422,
57
+ errorCode: 'NTS-DBS-CD0',
58
+ addECToUserMsg: true,
59
+ message: `createData failed, Create new ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
60
+ userMessage: this.defaultErrorUserMsg,
61
+ error
62
+ });
63
+ }
64
+ });
65
+ if (typeof newData._id === 'object') {
66
+ newData._id = `${newData._id}`;
67
+ }
68
+ data._id = newData._id;
69
+ data.__v = newData.__v;
70
+ return data;
71
+ });
72
+ }
73
+ /**
74
+ * Find data by _id and update
75
+ * @param data data
76
+ * @returns data
77
+ */
78
+ modifyData(data, issuer) {
79
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
80
+ data.__lastModified = new Date();
81
+ data.__lastModifiedBy = issuer;
82
+ const dataModel = new this.dataModel(data);
83
+ let newData = yield this.dataModel.findByIdAndUpdate(data._id, dataModel).then(res => {
84
+ if (res) {
85
+ return res === null || res === void 0 ? void 0 : res.toObject();
86
+ }
87
+ else {
88
+ throw new fsm_dynamo_1.Dynamo_Error({
89
+ status: 204,
90
+ errorCode: 'NTS-DBS-FU1',
91
+ addECToUserMsg: true,
92
+ message: `modifyData failed, modify ${this.dataParams.dbName} result not found! (NTS DB)`,
93
+ userMessage: this.defaultErrorUserMsg,
94
+ });
95
+ }
96
+ }).catch(error => {
97
+ throw new fsm_dynamo_1.Dynamo_Error({
98
+ status: 417,
99
+ errorCode: 'NTS-DBS-FU0',
100
+ addECToUserMsg: true,
101
+ message: `modifyData ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
102
+ userMessage: this.defaultErrorUserMsg,
103
+ error
104
+ });
105
+ });
106
+ if (typeof newData._id === 'object') {
107
+ newData._id = `${newData._id}`;
108
+ }
109
+ data._id = newData._id;
110
+ data.__v = newData.__v;
111
+ return data;
112
+ });
113
+ }
114
+ /**
115
+ * returns data by _id,
116
+ * @param id id
117
+ * @returns data
118
+ */
119
+ getDataById(id) {
120
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
121
+ let data = yield this.dataModel.findById(id).then(res => {
122
+ var _a;
123
+ return (_a = res === null || res === void 0 ? void 0 : res.toObject()) !== null && _a !== void 0 ? _a : null;
124
+ }).catch(error => {
125
+ throw new fsm_dynamo_1.Dynamo_Error({
126
+ status: 417,
127
+ errorCode: 'NTS-DBS-GI0',
128
+ addECToUserMsg: true,
129
+ message: `get ${this.dataParams.dbName} by ID was unsuccessful (NTS DB)`,
130
+ userMessage: this.defaultErrorUserMsg,
131
+ error
132
+ });
133
+ });
134
+ if (data && typeof data._id === 'object') {
135
+ data._id = `${data._id}`;
136
+ }
137
+ return data;
138
+ });
139
+ }
140
+ /**
141
+ * get data by dependency data id,
142
+ * !!!: throws error if not found (errorCode on not found: NTS-DBS-GD2)
143
+ *
144
+ * @param dependencyId id
145
+ * @returns data
146
+ */
147
+ getDataByDependencyId(dependencyId) {
148
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
149
+ if (!this.depDataName) {
150
+ throw new fsm_dynamo_1.Dynamo_Error({
151
+ status: 501,
152
+ errorCode: 'NTS-DBS-GD0',
153
+ addECToUserMsg: true,
154
+ message: `dependencyDataIdKey not setted up for this db-service (${this.dataParams.dbName}) (NTS DB)`,
155
+ userMessage: this.defaultErrorUserMsg
156
+ });
157
+ }
158
+ let data = yield this.dataModel.findOne({ [this.depDataName]: dependencyId }).then(res => {
159
+ var _a;
160
+ return (_a = res === null || res === void 0 ? void 0 : res.toObject()) !== null && _a !== void 0 ? _a : null;
161
+ }).catch(error => {
162
+ throw new fsm_dynamo_1.Dynamo_Error({
163
+ status: 417,
164
+ errorCode: 'NTS-DBS-GD1',
165
+ addECToUserMsg: true,
166
+ message: `get ${this.dataParams.dbName} by ${this.depDataName} was unsuccessful (NTS DB)`,
167
+ userMessage: this.defaultErrorUserMsg,
168
+ error
169
+ });
170
+ });
171
+ if (data && typeof data._id === 'object') {
172
+ data._id = `${data._id}`;
173
+ }
174
+ return data;
175
+ });
176
+ }
177
+ /**
178
+ * get data by dependency data id,
179
+ * !!!: throws error if not found (errorCode on not found: NTS-DBS-GLD2)
180
+ *
181
+ * @param dependencyId id
182
+ * @returns dataList
183
+ */
184
+ getDataListByDependencyId(dependencyId) {
185
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
186
+ if (!this.depDataName) {
187
+ throw new fsm_dynamo_1.Dynamo_Error({
188
+ status: 501,
189
+ errorCode: 'NTS-DBS-GLD0',
190
+ addECToUserMsg: true,
191
+ message: `dependencyDataIdKey not setted up for this db-service (${this.dataParams.dbName}) (NTS DB)`,
192
+ userMessage: this.defaultErrorUserMsg
193
+ });
194
+ }
195
+ let dataList = yield this.dataModel.find({ [this.depDataName]: dependencyId }).then(res => {
196
+ var _a;
197
+ return (_a = res) !== null && _a !== void 0 ? _a : [];
198
+ }).catch(error => {
199
+ throw new fsm_dynamo_1.Dynamo_Error({
200
+ status: 417,
201
+ errorCode: 'NTS-DBS-GLD1',
202
+ addECToUserMsg: true,
203
+ message: `get ${this.dataParams.dbName} by ${this.depDataName} was unsuccessful (NTS DB)`,
204
+ userMessage: this.defaultErrorUserMsg,
205
+ error
206
+ });
207
+ });
208
+ if (0 < dataList.length) {
209
+ dataList.forEach((data) => {
210
+ if (data && typeof data._id === 'object') {
211
+ data._id = `${data._id}`;
212
+ }
213
+ });
214
+ }
215
+ return dataList;
216
+ });
217
+ }
218
+ /**
219
+ * get multiple data objects by a list of DependencyIDs,
220
+ * !!!: throws error if not found (errorCode on not found: NTS-DBS-GLDS2)
221
+ *
222
+ * @param ids ids
223
+ * @returns dataList
224
+ */
225
+ getDataListByDependencyIds(ids) {
226
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
227
+ if (!this.depDataName) {
228
+ throw new fsm_dynamo_1.Dynamo_Error({
229
+ status: 501,
230
+ errorCode: 'NTS-DBS-GLDS0',
231
+ addECToUserMsg: true,
232
+ message: `dependencyDataIdKey not setted up for this db-service (${this.dataParams.dbName}) (NTS DB)`,
233
+ userMessage: this.defaultErrorUserMsg
234
+ });
235
+ }
236
+ let dataList = yield this.dataModel.find({ [this.depDataName]: { $in: ids } }).then(res => {
237
+ var _a;
238
+ return (_a = res) !== null && _a !== void 0 ? _a : [];
239
+ }).catch(error => {
240
+ throw new fsm_dynamo_1.Dynamo_Error({
241
+ status: 417,
242
+ errorCode: 'NTS-DBS-GLDS1',
243
+ addECToUserMsg: true,
244
+ message: `get ${this.dataParams.dbName}s by ${this.depDataName}s was unsuccessful (NTS DB)`,
245
+ userMessage: this.defaultErrorUserMsg,
246
+ error
247
+ });
248
+ });
249
+ if (0 < dataList.length) {
250
+ dataList.forEach((data) => {
251
+ if (data && typeof data._id === 'object') {
252
+ data._id = `${data._id}`;
253
+ }
254
+ });
255
+ }
256
+ return dataList;
257
+ });
258
+ }
259
+ /**
260
+ * returns all data from database,
261
+ * !!!: throws error if not found (errorCode on not found: NTS-DBS-GA1)
262
+ *
263
+ * @returns dataList
264
+ */
265
+ getAll() {
266
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
267
+ let dataList = yield this.dataModel.find({})
268
+ .then(res => {
269
+ var _a;
270
+ return (_a = res) !== null && _a !== void 0 ? _a : [];
271
+ }).catch(error => {
272
+ throw new fsm_dynamo_1.Dynamo_Error({
273
+ status: 417,
274
+ errorCode: 'NTS-DBS-GA0',
275
+ addECToUserMsg: true,
276
+ message: `get all ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
277
+ userMessage: this.defaultErrorUserMsg,
278
+ error
279
+ });
280
+ });
281
+ if (0 < dataList.length && typeof dataList[0]._id === 'object') {
282
+ dataList.forEach((data) => {
283
+ data._id = `˙${data._id}`;
284
+ });
285
+ }
286
+ return dataList;
287
+ });
288
+ }
289
+ /**
290
+ * deleted data by id
291
+ * @param id id
292
+ */
293
+ deleteDataById(id) {
294
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
295
+ yield this.dataModel.findByIdAndDelete(id).catch(error => {
296
+ throw new fsm_dynamo_1.Dynamo_Error({
297
+ status: 417,
298
+ errorCode: 'NTS-DBS-DD0',
299
+ addECToUserMsg: true,
300
+ message: `delete ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
301
+ userMessage: this.defaultErrorUserMsg,
302
+ error
303
+ });
304
+ });
305
+ });
306
+ }
307
+ /**
308
+ * deleted data by id
309
+ * @param dependencyId id
310
+ */
311
+ deleteDataByDependencyId(dependencyId) {
312
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
313
+ if (!this.depDataName) {
314
+ throw new fsm_dynamo_1.Dynamo_Error({
315
+ status: 501,
316
+ errorCode: 'NTS-DBS-DDD0',
317
+ addECToUserMsg: true,
318
+ message: `dependencyDataIdKey not setted up for this db-service (${this.dataParams.dbName}) (NTS DB)`,
319
+ userMessage: this.defaultErrorUserMsg
320
+ });
321
+ }
322
+ yield this.dataModel.deleteMany({ [this.depDataName]: dependencyId }).catch(error => {
323
+ throw new fsm_dynamo_1.Dynamo_Error({
324
+ status: 417,
325
+ errorCode: 'NTS-DBS-DDD1',
326
+ addECToUserMsg: true,
327
+ message: `delete ${this.dataParams.dbName} by ${this.depDataName} was unsuccessful (NTS DB)`,
328
+ userMessage: this.defaultErrorUserMsg,
329
+ error
330
+ });
331
+ });
332
+ });
333
+ }
334
+ /**
335
+ * returns search result for searchBy object params
336
+ * can use lists or xRange values for searchBy obj properties
337
+ *
338
+ * @param filterBy filter
339
+ * @param narrowByDependencyIds id
340
+ * @returns dataList
341
+ */
342
+ searchData(filterBy, narrowByDependencyIds) {
343
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
344
+ const filter = {};
345
+ if (0 < narrowByDependencyIds.length) {
346
+ if (!this.depDataName) {
347
+ throw new fsm_dynamo_1.Dynamo_Error({
348
+ status: 501,
349
+ errorCode: 'NTS-DBS-SD0',
350
+ addECToUserMsg: true,
351
+ message: `dependencyDataIdKey not setted up for this db-service (${this.dataParams.dbName}) (NTS DB)`,
352
+ userMessage: this.defaultErrorUserMsg
353
+ });
354
+ }
355
+ filter[this.depDataName] = { $in: narrowByDependencyIds };
356
+ }
357
+ yield this.dataParams.modelParams.forEach((modelParam) => {
358
+ if ((filterBy[modelParam.key] !== null && filterBy[modelParam.key] !== undefined) ||
359
+ filterBy[modelParam.key + 'Range']) {
360
+ if (modelParam.key.includes('Range') || modelParam.type.includes('[]')) {
361
+ // inverz search filter (for Range and Array functions)
362
+ if (modelParam.key.includes('Range')) {
363
+ const searchParamKeyWithoutRange = modelParam.key.split('Range')[0];
364
+ if (filterBy[searchParamKeyWithoutRange] !== null &&
365
+ filterBy[searchParamKeyWithoutRange] !== undefined) {
366
+ filter[modelParam.key] = {
367
+ from: { $lte: filterBy[searchParamKeyWithoutRange] },
368
+ to: { $gte: filterBy[searchParamKeyWithoutRange] }
369
+ };
370
+ }
371
+ }
372
+ else {
373
+ if (filterBy[modelParam.key] !== null &&
374
+ filterBy[modelParam.key] !== undefined) {
375
+ filter[modelParam.key] = { $in: filterBy[modelParam.key] };
376
+ }
377
+ }
378
+ }
379
+ else {
380
+ // basic search filter
381
+ if (filterBy[modelParam.key + 'Range']) {
382
+ if (filterBy[modelParam.key + 'Range'].from || filterBy[modelParam.key + 'Range'].to) {
383
+ filter[modelParam.key] = {};
384
+ if (filterBy[modelParam.key + 'Range'].from) {
385
+ filter[modelParam.key].$gte = filterBy[modelParam.key + 'Range'].from;
386
+ }
387
+ if (filterBy[modelParam.key + 'Range'].to) {
388
+ filter[modelParam.key].$lte = filterBy[modelParam.key + 'Range'].to;
389
+ }
390
+ }
391
+ }
392
+ else if (filterBy[modelParam.key] !== null && filterBy[modelParam.key] !== undefined) {
393
+ if (filterBy[modelParam.key].lenght > 0) {
394
+ filter[modelParam.key] = { $in: filterBy[modelParam.key] };
395
+ }
396
+ }
397
+ }
398
+ }
399
+ });
400
+ let dataList = yield this.dataModel.find(filter)
401
+ .then(res => res !== null && res !== void 0 ? res : [])
402
+ .catch(error => {
403
+ throw new fsm_dynamo_1.Dynamo_Error({
404
+ status: 417,
405
+ errorCode: 'NTS-DBS-SD1',
406
+ addECToUserMsg: true,
407
+ message: `search ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
408
+ userMessage: this.defaultErrorUserMsg,
409
+ error
410
+ });
411
+ });
412
+ if (0 < dataList.length) {
413
+ dataList.forEach((data) => {
414
+ if (data && typeof data._id === 'object') {
415
+ data._id = `${data._id}`;
416
+ }
417
+ });
418
+ }
419
+ return dataList;
420
+ });
421
+ }
422
+ // ----------------------------------------------------------------------------------
423
+ // ----------------------------------------------------------------------------------
424
+ // ----------------------------------------------------------------------------------
425
+ // DIRECT Basic MONGOOSE FUNCTIONS (with error handlings)
426
+ /**
427
+ * Find the data first by any of its parameters,
428
+ * !!!: throws error if not found (errorCode on not found: NTS-DBS-FO1)
429
+ *
430
+ * @param filterBy if you can, use unique parameters for find!
431
+ *
432
+ * @example
433
+ * // by email:
434
+ * { email: email }
435
+ * //
436
+ * @example
437
+ * // or by id that is in list:
438
+ * { userIds: { $in: this.userId } }
439
+ * //
440
+ * @example
441
+ * // or by number or Date that is Greater Than AND Less Than:
442
+ * { points: { $gt: 2, $lt: 14 } }
443
+ * // further tools (syntax matches with $gt):
444
+ * $eq: // Matches values that are EQual to a specified value.
445
+ * $gte: // Matches values that are Greater Than OR Equal to a specified value.
446
+ * $lte: // Matches values that are Less Than or Equal to a specified value.
447
+ * $ne: // Matches all values that are Not Equal to a specified value.
448
+ * $nin: // Matches None of the values specified IN an array.
449
+ * //
450
+ * @returns {T} data: T
451
+ */
452
+ findOne(filterBy) {
453
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
454
+ let data = yield this.dataModel.findOne(filterBy).then(res => {
455
+ var _a;
456
+ return (_a = res) !== null && _a !== void 0 ? _a : null;
457
+ }).catch(error => {
458
+ throw new fsm_dynamo_1.Dynamo_Error({
459
+ status: 417,
460
+ errorCode: 'NTS-DBS-FO0',
461
+ addECToUserMsg: true,
462
+ message: `findOne ${this.dataParams.dbName} was unsuccessful (NTS DB) fliter: ${filterBy}`,
463
+ userMessage: this.defaultErrorUserMsg,
464
+ error
465
+ });
466
+ });
467
+ if (data && typeof data._id === 'object') {
468
+ data._id = `${data._id}`;
469
+ }
470
+ return data;
471
+ });
472
+ }
473
+ /**
474
+ * #MONGOOSE FUNCTION
475
+ * Find the data first by any of its parameters,
476
+ * !!!: throws error if not found (errorCode on not found: NTS-DBS-F1)
477
+ *
478
+ * @param filterBy if you can, use unique parameters for find!
479
+ *
480
+ * @example
481
+ * // by email:
482
+ * { email: email }
483
+ * //
484
+ * @example
485
+ * // or by id that is in list:
486
+ * { userIds: { $in: this.userId } }
487
+ * //
488
+ * @example
489
+ * // or by number or Date that is Greater Than AND Less Than:
490
+ * { points: { $gt: 2, $lt: 14 } }
491
+ * // further tools (syntax matches with $gt):
492
+ * $eq: // Matches values that are EQual to a specified value.
493
+ * $gte: // Matches values that are Greater Than OR Equal to a specified value.
494
+ * $lte: // Matches values that are Less Than or Equal to a specified value.
495
+ * $ne: // Matches all values that are Not Equal to a specified value.
496
+ * $nin: // Matches None of the values specified IN an array.
497
+ * //
498
+ * @returns {T[]} dataList: T[]
499
+ */
500
+ find(filterBy) {
501
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
502
+ let dataList = yield this.dataModel.find(filterBy)
503
+ .then(res => {
504
+ var _a;
505
+ return (_a = res) !== null && _a !== void 0 ? _a : [];
506
+ }).catch(error => {
507
+ throw new fsm_dynamo_1.Dynamo_Error({
508
+ status: 417,
509
+ errorCode: 'NTS-DBS-F0',
510
+ addECToUserMsg: true,
511
+ message: `find ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
512
+ userMessage: this.defaultErrorUserMsg,
513
+ error
514
+ });
515
+ });
516
+ if (0 < dataList.length) {
517
+ dataList.forEach((data) => {
518
+ if (data && typeof data._id === 'object') {
519
+ data._id = `${data._id}`;
520
+ }
521
+ });
522
+ }
523
+ return dataList;
524
+ });
525
+ }
526
+ /**
527
+ * #MONGOOSE FUNCTION
528
+ * Find the data first by any of its parameters
529
+ *
530
+ * @param filterBy if you can, use unique parameters for find!
531
+ *
532
+ * @example
533
+ * // by email:
534
+ * { email: email }
535
+ * //
536
+ * @example
537
+ * // or by id that is in list:
538
+ * { userIds: { $in: this.userId } }
539
+ * //
540
+ * @example
541
+ * // or by number or Date that is Greater Than AND Less Than:
542
+ * { points: { $gt: 2, $lt: 14 } }
543
+ * // further tools (syntax matches with $gt):
544
+ * $eq: // Matches values that are EQual to a specified value.
545
+ * $gte: // Matches values that are Greater Than OR Equal to a specified value.
546
+ * $lte: // Matches values that are Less Than or Equal to a specified value.
547
+ * $ne: // Matches all values that are Not Equal to a specified value.
548
+ * $nin: // Matches None of the values specified IN an array.
549
+ * //
550
+ * @param page page
551
+ * @param pageSize pageSize
552
+ * @param sort
553
+ * @example
554
+ * // by dateTime (this uses the basic sort function):
555
+ * { dateTime: -1 }
556
+ * //
557
+ * @returns {T[]} dataList: T[]
558
+ */
559
+ findWithPaging(filterBy, page, pageSize, sort) {
560
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
561
+ let dataList = yield this.dataModel.find(filterBy)
562
+ .sort(sort)
563
+ .skip(page * pageSize)
564
+ .limit(pageSize)
565
+ .then(res => res !== null && res !== void 0 ? res : [])
566
+ .catch(error => {
567
+ throw new fsm_dynamo_1.Dynamo_Error({
568
+ status: 417,
569
+ errorCode: 'NTS-DBS-WP0',
570
+ addECToUserMsg: true,
571
+ message: `findWithPaging ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
572
+ userMessage: this.defaultErrorUserMsg,
573
+ error
574
+ });
575
+ });
576
+ if (0 < dataList.length) {
577
+ dataList.forEach((data) => {
578
+ if (data && typeof data._id === 'object') {
579
+ data._id = `${data._id}`;
580
+ }
581
+ });
582
+ }
583
+ return dataList;
584
+ });
585
+ }
586
+ /**
587
+ * #MONGOOSE FUNCTION
588
+ * Find data by _id and update
589
+ * !!!: throws error if not found (errorCode on not found: )
590
+ *
591
+ * @param id id
592
+ * @param update update
593
+ * @returns data
594
+ */
595
+ findByIdAndUpdate(id, update, modifier) {
596
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
597
+ update.__lastModified = new Date();
598
+ update.__lastModifiedBy = modifier;
599
+ let newData = yield this.dataModel.findByIdAndUpdate(id, update).then(res => {
600
+ var _a;
601
+ return (_a = res === null || res === void 0 ? void 0 : res.toObject()) !== null && _a !== void 0 ? _a : null;
602
+ }).catch(error => {
603
+ throw new fsm_dynamo_1.Dynamo_Error({
604
+ status: 417,
605
+ errorCode: 'NTS-DBS-FIU0',
606
+ addECToUserMsg: true,
607
+ message: `findByIdAndUpdate ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
608
+ userMessage: this.defaultErrorUserMsg,
609
+ error
610
+ });
611
+ });
612
+ if (typeof newData._id === 'object') {
613
+ newData._id = `${newData._id}`;
614
+ }
615
+ return newData;
616
+ });
617
+ }
618
+ /**
619
+ * #MONGOOSE FUNCTION
620
+ * Find the data first by any of its parameters
621
+ *
622
+ * @param filter This uses the basic Mongoose updateOne.
623
+ * If you can, use unique parameters for find!
624
+ * @example
625
+ * // by email:
626
+ * { email: email }
627
+ * //
628
+ * @example
629
+ * // or by id that is in list:
630
+ * { userIds: { $in: this.userId } }
631
+ * //
632
+ * @example
633
+ * // or by number or Date that is Greater Than AND Less Than:
634
+ * { points: { $gt: 2, $lt: 14 } }
635
+ * // further tools (syntax matches with $gt):
636
+ * $eq: // Matches values that are EQual to a specified value.
637
+ * $gte: // Matches values that are Greater Than OR Equal to a specified value.
638
+ * $lte: // Matches values that are Less Than or Equal to a specified value.
639
+ * $ne: // Matches all values that are Not Equal to a specified value.
640
+ * $nin: // Matches None of the values specified IN an array.
641
+ * //
642
+ *
643
+ * @param update this uses the basic Mongoose updateOne
644
+ * @example
645
+ * // increase a specific value (here by 15):
646
+ * { $inc: { popularity: 15 } }
647
+ * //
648
+ * @example
649
+ * // or add element to a list:
650
+ * { $push: { reactions: this.newReaction }
651
+ * // or add multiple elements to a list
652
+ * { $push: { schedule: {$each: [ monday, tuesday, wednesday ] } } }
653
+ * //
654
+ * @example
655
+ * // or all at once
656
+ * {
657
+ * $inc: { popularity: this.newVote.amount },
658
+ * emailVerified: true,
659
+ * $push: { reactions: this.newReaction }
660
+ * }
661
+ * // further tools (syntax matches with $inc):
662
+ * $currentDate: // Sets the value of a field to current date, either as a Date or a Timestamp.
663
+ * $min: // Only updates the field if the specified value is less than the existing field value.
664
+ * $max: // Only updates the field if the specified value is greater than the existing field value.
665
+ * $mul: // Multiplies the value of the field by the specified amount.
666
+ * $rename: // Renames a field.
667
+ * $unset: // Removes the specified field from a document. (set: "" to value)
668
+ * //
669
+ */
670
+ updateOne(filterBy, update, issuer) {
671
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
672
+ update.__lastModified = new Date();
673
+ update.__lastModifiedBy = issuer;
674
+ yield this.dataModel.updateOne(filterBy, update).catch(error => {
675
+ throw new fsm_dynamo_1.Dynamo_Error({
676
+ status: 417,
677
+ errorCode: 'NTS-DBS-UO0',
678
+ addECToUserMsg: true,
679
+ message: `updateOne ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
680
+ userMessage: this.defaultErrorUserMsg,
681
+ error
682
+ });
683
+ });
684
+ });
685
+ }
686
+ /**
687
+ * #MONGOOSE FUNCTION
688
+ * update one parameter by a specific
689
+ *
690
+ * @param filter This uses the basic Mongoose updateMany.
691
+ * @example
692
+ * // by email:
693
+ * { email: email }
694
+ * //
695
+ * @example
696
+ * // or by id that is in list:
697
+ * { userIds: { $in: this.userId } }
698
+ * //
699
+ * @example
700
+ * // or by number or Date that is Greater Than AND Less Than:
701
+ * { points: { $gt: 2, $lt: 14 } }
702
+ * // further tools (syntax matches with $gt):
703
+ * $eq: // Matches values that are EQual to a specified value.
704
+ * $gte: // Matches values that are Greater Than OR Equal to a specified value.
705
+ * $lte: // Matches values that are Less Than or Equal to a specified value.
706
+ * $ne: // Matches all values that are Not Equal to a specified value.
707
+ * $nin: // Matches None of the values specified IN an array.
708
+ * //
709
+ *
710
+ * @param update this uses the basic Mongoose updateOne
711
+ * @example
712
+ * // increase a specific value (here by 15):
713
+ * { $inc: { popularity: 15 } }
714
+ * //
715
+ * @example
716
+ * // or add element to a list:
717
+ * { $push: { reactions: this.newReaction }
718
+ * // or add multiple elements to a list
719
+ * { $push: { schedule: {$each: [ monday, tuesday, wednesday ] } } }
720
+ * //
721
+ * @example
722
+ * // or all at once
723
+ * {
724
+ * $inc: { popularity: this.newVote.amount },
725
+ * emailVerified: true,
726
+ * $push: { reactions: this.newReaction }
727
+ * }
728
+ * // further tools (syntax matches with $inc):
729
+ * $currentDate: // Sets the value of a field to current date, either as a Date or a Timestamp.
730
+ * $min: // Only updates the field if the specified value is less than the existing field value.
731
+ * $max: // Only updates the field if the specified value is greater than the existing field value.
732
+ * $mul: // Multiplies the value of the field by the specified amount.
733
+ * $rename: // Renames a field.
734
+ * $unset: // Removes the specified field from a document. (set: "" to value)
735
+ * //
736
+ */
737
+ updateMany(filterBy, update, issuer) {
738
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
739
+ update.__lastModified = new Date();
740
+ update.__lastModifiedBy = issuer;
741
+ yield this.dataModel.updateMany(filterBy, update).catch(error => {
742
+ throw new fsm_dynamo_1.Dynamo_Error({
743
+ status: 417,
744
+ errorCode: 'NTS-DBS-UM0',
745
+ addECToUserMsg: true,
746
+ message: `updateMany ${this.dataParams.dbName} was unsuccessful (NTS DB)`,
747
+ userMessage: this.defaultErrorUserMsg,
748
+ error
749
+ });
750
+ });
751
+ });
752
+ }
753
+ // ----------------------------------------------------------------------------------
754
+ // ----------------------------------------------------------------------------------
755
+ // ----------------------------------------------------------------------------------
756
+ // PRIVATE BUILD FUNCTIONS
757
+ /**
758
+ * builds and returns mongoose schema
759
+ * @returns schema
760
+ */
761
+ getSchema() {
762
+ let schema = this.buildMongooseSchemaByModelParams();
763
+ schema = this.addDynamo_MetadataToSchema(schema);
764
+ // tslint:disable-next-line: no-string-literal
765
+ if (schema['_id']) {
766
+ // tslint:disable-next-line: no-string-literal
767
+ delete schema['_id'];
768
+ }
769
+ return new mongoose_1.Schema(schema);
770
+ }
771
+ /**
772
+ * builds mongoose schema building settings object by dynamoDataModelParamsList
773
+ * @param params DynamoBEDataPropertyParams
774
+ * @returns mongoose schema object
775
+ */
776
+ buildMongooseSchemaByModelParams(params) {
777
+ const schemaSettingsObj = {};
778
+ if (!params) {
779
+ params = this.dataParams.modelParams;
780
+ }
781
+ params.forEach((property) => {
782
+ var _a;
783
+ const beType = this.getBEType(property.type);
784
+ if (beType !== Object || !(property === null || property === void 0 ? void 0 : property.subObjectParams) || ((_a = property === null || property === void 0 ? void 0 : property.subObjectParams) === null || _a === void 0 ? void 0 : _a.length) == 0) {
785
+ schemaSettingsObj[property.key] = {
786
+ type: beType
787
+ };
788
+ if (property.index) {
789
+ schemaSettingsObj[property.key].index = true;
790
+ }
791
+ if (property.unique) {
792
+ schemaSettingsObj[property.key].unique = true;
793
+ }
794
+ if (property.required) {
795
+ schemaSettingsObj[property.key].required = true;
796
+ }
797
+ if (property.minlength) {
798
+ schemaSettingsObj[property.key].minlength = property.minlength;
799
+ }
800
+ if (property.maxlength) {
801
+ schemaSettingsObj[property.key].maxlength = property.maxlength;
802
+ }
803
+ }
804
+ else {
805
+ schemaSettingsObj[property.key] = this.buildMongooseSchemaByModelParams(property.subObjectParams);
806
+ }
807
+ });
808
+ return schemaSettingsObj;
809
+ }
810
+ getBEType(type) {
811
+ switch (type) {
812
+ case 'string':
813
+ return String;
814
+ case 'number':
815
+ return Number;
816
+ case 'boolean':
817
+ return Boolean;
818
+ case 'Date':
819
+ return Date;
820
+ case 'object':
821
+ return Object;
822
+ default:
823
+ if (type.includes('[]')) {
824
+ return Array;
825
+ }
826
+ else if (type.includes('=>') || type.includes('function')) {
827
+ return Function;
828
+ }
829
+ else {
830
+ return Object;
831
+ }
832
+ }
833
+ }
834
+ /**
835
+ * adds dynamo metadata settings to any mongoose schema building settings object
836
+ * @param schema schema to update
837
+ * @returns updated schema
838
+ */
839
+ addDynamo_MetadataToSchema(schema) {
840
+ schema.__created = { type: Date };
841
+ schema.__createdBy = { type: String };
842
+ schema.__lastModified = { type: Date };
843
+ schema.__lastModifiedBy = { type: String };
844
+ return schema;
845
+ }
846
+ /**
847
+ * sets depDataKey
848
+ */
849
+ lookForDependencyDataSettings() {
850
+ const dependencyParam = this.dataParams.modelParams.find((modelParams) => modelParams.isDependencyHook);
851
+ if (dependencyParam) {
852
+ this.depDataName = dependencyParam.key;
853
+ }
854
+ }
855
+ }
856
+ exports.DynamoNTS_DBService = DynamoNTS_DBService;
857
857
  //# sourceMappingURL=dynamo-nts-db.service.js.map