@sdflc/backend-helpers 1.0.106 → 1.0.107

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 (209) hide show
  1. package/build/BaseCore.d.ts +88 -0
  2. package/build/BaseCore.js +734 -0
  3. package/build/BaseGateway.d.ts +56 -0
  4. package/build/BaseGateway.js +477 -0
  5. package/build/DataCache.d.ts +16 -0
  6. package/build/DataCache.js +113 -0
  7. package/build/Database.d.ts +36 -0
  8. package/build/Database.js +195 -0
  9. package/build/DbMigratorAndSeeder.d.ts +49 -0
  10. package/build/DbMigratorAndSeeder.js +212 -0
  11. package/build/RedisDataLoader.d.ts +41 -0
  12. package/build/RedisDataLoader.js +212 -0
  13. package/build/apiContext.d.ts +11 -0
  14. package/build/apiContext.js +146 -0
  15. package/build/baseController.d.ts +17 -0
  16. package/build/baseController.js +59 -0
  17. package/build/checkItHelpers.d.ts +3 -0
  18. package/build/checkItHelpers.js +28 -0
  19. package/build/commonValidators.d.ts +17 -0
  20. package/build/commonValidators.js +311 -0
  21. package/build/configBulder.d.ts +2 -0
  22. package/build/configBulder.js +36 -0
  23. package/build/constants.d.ts +30 -0
  24. package/build/constants.js +80 -0
  25. package/build/dbHelpers.d.ts +43 -0
  26. package/build/dbHelpers.js +156 -0
  27. package/build/dbSeeder.d.ts +2 -0
  28. package/build/dbSeeder.js +83 -0
  29. package/build/dbTableHelpers.d.ts +14 -0
  30. package/build/dbTableHelpers.js +118 -0
  31. package/build/downloadHelpers.d.ts +2 -0
  32. package/build/downloadHelpers.js +73 -0
  33. package/build/esm/BaseCore.d.ts +88 -0
  34. package/build/esm/BaseCore.js +728 -0
  35. package/build/esm/BaseGateway.d.ts +56 -0
  36. package/build/esm/BaseGateway.js +471 -0
  37. package/build/esm/DataCache.d.ts +16 -0
  38. package/build/esm/DataCache.js +107 -0
  39. package/build/esm/Database.d.ts +36 -0
  40. package/build/esm/Database.js +189 -0
  41. package/build/esm/DbMigratorAndSeeder.d.ts +49 -0
  42. package/build/esm/DbMigratorAndSeeder.js +173 -0
  43. package/build/esm/RedisDataLoader.d.ts +41 -0
  44. package/build/esm/RedisDataLoader.js +206 -0
  45. package/build/esm/apiContext.d.ts +11 -0
  46. package/build/esm/apiContext.js +138 -0
  47. package/build/esm/baseController.d.ts +17 -0
  48. package/build/esm/baseController.js +53 -0
  49. package/build/esm/checkItHelpers.d.ts +3 -0
  50. package/build/esm/checkItHelpers.js +23 -0
  51. package/build/esm/commonValidators.d.ts +17 -0
  52. package/build/esm/commonValidators.js +291 -0
  53. package/build/esm/configBulder.d.ts +2 -0
  54. package/build/esm/configBulder.js +32 -0
  55. package/build/esm/constants.d.ts +30 -0
  56. package/build/esm/constants.js +69 -0
  57. package/build/esm/dbHelpers.d.ts +43 -0
  58. package/build/esm/dbHelpers.js +144 -0
  59. package/build/esm/dbSeeder.d.ts +2 -0
  60. package/build/esm/dbSeeder.js +44 -0
  61. package/build/esm/dbTableHelpers.d.ts +14 -0
  62. package/build/esm/dbTableHelpers.js +106 -0
  63. package/build/esm/downloadHelpers.d.ts +2 -0
  64. package/build/esm/downloadHelpers.js +66 -0
  65. package/build/esm/index.d.ts +21 -0
  66. package/build/esm/index.js +21 -0
  67. package/build/esm/interfaces/ApiContext.d.ts +25 -0
  68. package/build/esm/interfaces/ApiContext.js +1 -0
  69. package/build/esm/interfaces/ApiContextInterface.d.ts +12 -0
  70. package/build/esm/interfaces/ApiContextInterface.js +1 -0
  71. package/build/esm/interfaces/BaseCoreActionsInterface.d.ts +38 -0
  72. package/build/esm/interfaces/BaseCoreActionsInterface.js +1 -0
  73. package/build/esm/interfaces/BaseCoreDoAuthOnActionInterface.d.ts +12 -0
  74. package/build/esm/interfaces/BaseCoreDoAuthOnActionInterface.js +1 -0
  75. package/build/esm/interfaces/BaseCoreDoingWhatInterface.d.ts +13 -0
  76. package/build/esm/interfaces/BaseCoreDoingWhatInterface.js +1 -0
  77. package/build/esm/interfaces/BaseCorePropsInterface.d.ts +37 -0
  78. package/build/esm/interfaces/BaseCorePropsInterface.js +1 -0
  79. package/build/esm/interfaces/BaseCoreValidatorsInterface.d.ts +13 -0
  80. package/build/esm/interfaces/BaseCoreValidatorsInterface.js +1 -0
  81. package/build/esm/interfaces/BaseGatewayPropsInterface.d.ts +39 -0
  82. package/build/esm/interfaces/BaseGatewayPropsInterface.js +1 -0
  83. package/build/esm/interfaces/BuildConfigOptions.d.ts +6 -0
  84. package/build/esm/interfaces/BuildConfigOptions.js +1 -0
  85. package/build/esm/interfaces/BuildDefaultResolversOptions.d.ts +17 -0
  86. package/build/esm/interfaces/BuildDefaultResolversOptions.js +1 -0
  87. package/build/esm/interfaces/BuildListQueryArgsInterface.d.ts +6 -0
  88. package/build/esm/interfaces/BuildListQueryArgsInterface.js +1 -0
  89. package/build/esm/interfaces/CacheInterface.d.ts +15 -0
  90. package/build/esm/interfaces/CacheInterface.js +1 -0
  91. package/build/esm/interfaces/CommonValidatorArgs.d.ts +3 -0
  92. package/build/esm/interfaces/CommonValidatorArgs.js +1 -0
  93. package/build/esm/interfaces/CoreActionInterface.d.ts +15 -0
  94. package/build/esm/interfaces/CoreActionInterface.js +1 -0
  95. package/build/esm/interfaces/CreateUserTokenArgs.d.ts +11 -0
  96. package/build/esm/interfaces/CreateUserTokenArgs.js +1 -0
  97. package/build/esm/interfaces/CreateUserTokenResult.d.ts +4 -0
  98. package/build/esm/interfaces/CreateUserTokenResult.js +1 -0
  99. package/build/esm/interfaces/DatabaseOptions.d.ts +26 -0
  100. package/build/esm/interfaces/DatabaseOptions.js +1 -0
  101. package/build/esm/interfaces/DatabaseReadInterface.d.ts +14 -0
  102. package/build/esm/interfaces/DatabaseReadInterface.js +1 -0
  103. package/build/esm/interfaces/DbFieldAddDefaultInterface.d.ts +9 -0
  104. package/build/esm/interfaces/DbFieldAddDefaultInterface.js +1 -0
  105. package/build/esm/interfaces/DownloadFileArgs.d.ts +6 -0
  106. package/build/esm/interfaces/DownloadFileArgs.js +1 -0
  107. package/build/esm/interfaces/ExtractCredentialsArgs.d.ts +19 -0
  108. package/build/esm/interfaces/ExtractCredentialsArgs.js +1 -0
  109. package/build/esm/interfaces/JwtTokenSecretAndSettings.d.ts +5 -0
  110. package/build/esm/interfaces/JwtTokenSecretAndSettings.js +1 -0
  111. package/build/esm/interfaces/ListArgsInterface.d.ts +7 -0
  112. package/build/esm/interfaces/ListArgsInterface.js +1 -0
  113. package/build/esm/interfaces/LoaderInterface.d.ts +7 -0
  114. package/build/esm/interfaces/LoaderInterface.js +1 -0
  115. package/build/esm/interfaces/PaginationInterface.d.ts +10 -0
  116. package/build/esm/interfaces/PaginationInterface.js +1 -0
  117. package/build/esm/interfaces/SortItemInterface.d.ts +4 -0
  118. package/build/esm/interfaces/SortItemInterface.js +1 -0
  119. package/build/esm/interfaces/UserTokenParams.d.ts +8 -0
  120. package/build/esm/interfaces/UserTokenParams.js +1 -0
  121. package/build/esm/interfaces/VerifyUserTokenArgs.d.ts +13 -0
  122. package/build/esm/interfaces/VerifyUserTokenArgs.js +1 -0
  123. package/build/esm/interfaces/index.d.ts +28 -0
  124. package/build/esm/interfaces/index.js +27 -0
  125. package/build/esm/knexConfigBuilder.d.ts +1 -0
  126. package/build/esm/knexConfigBuilder.js +84 -0
  127. package/build/esm/passwordHelpers.d.ts +5 -0
  128. package/build/esm/passwordHelpers.js +13 -0
  129. package/build/esm/redisClient.d.ts +17 -0
  130. package/build/esm/redisClient.js +97 -0
  131. package/build/esm/resolversHelpers.d.ts +3 -0
  132. package/build/esm/resolversHelpers.js +66 -0
  133. package/build/esm/userToken.d.ts +8 -0
  134. package/build/esm/userToken.js +80 -0
  135. package/build/esm/utils.d.ts +2 -0
  136. package/build/esm/utils.js +13 -0
  137. package/build/index.d.ts +21 -0
  138. package/build/index.js +37 -0
  139. package/build/interfaces/ApiContext.d.ts +25 -0
  140. package/build/interfaces/ApiContext.js +2 -0
  141. package/build/interfaces/ApiContextInterface.d.ts +12 -0
  142. package/build/interfaces/ApiContextInterface.js +2 -0
  143. package/build/interfaces/BaseCoreActionsInterface.d.ts +38 -0
  144. package/build/interfaces/BaseCoreActionsInterface.js +2 -0
  145. package/build/interfaces/BaseCoreDoAuthOnActionInterface.d.ts +12 -0
  146. package/build/interfaces/BaseCoreDoAuthOnActionInterface.js +2 -0
  147. package/build/interfaces/BaseCoreDoingWhatInterface.d.ts +13 -0
  148. package/build/interfaces/BaseCoreDoingWhatInterface.js +2 -0
  149. package/build/interfaces/BaseCorePropsInterface.d.ts +37 -0
  150. package/build/interfaces/BaseCorePropsInterface.js +2 -0
  151. package/build/interfaces/BaseCoreValidatorsInterface.d.ts +13 -0
  152. package/build/interfaces/BaseCoreValidatorsInterface.js +2 -0
  153. package/build/interfaces/BaseGatewayPropsInterface.d.ts +39 -0
  154. package/build/interfaces/BaseGatewayPropsInterface.js +2 -0
  155. package/build/interfaces/BuildConfigOptions.d.ts +6 -0
  156. package/build/interfaces/BuildConfigOptions.js +2 -0
  157. package/build/interfaces/BuildDefaultResolversOptions.d.ts +17 -0
  158. package/build/interfaces/BuildDefaultResolversOptions.js +2 -0
  159. package/build/interfaces/BuildListQueryArgsInterface.d.ts +6 -0
  160. package/build/interfaces/BuildListQueryArgsInterface.js +2 -0
  161. package/build/interfaces/CacheInterface.d.ts +15 -0
  162. package/build/interfaces/CacheInterface.js +2 -0
  163. package/build/interfaces/CommonValidatorArgs.d.ts +3 -0
  164. package/build/interfaces/CommonValidatorArgs.js +2 -0
  165. package/build/interfaces/CoreActionInterface.d.ts +15 -0
  166. package/build/interfaces/CoreActionInterface.js +2 -0
  167. package/build/interfaces/CreateUserTokenArgs.d.ts +11 -0
  168. package/build/interfaces/CreateUserTokenArgs.js +2 -0
  169. package/build/interfaces/CreateUserTokenResult.d.ts +4 -0
  170. package/build/interfaces/CreateUserTokenResult.js +2 -0
  171. package/build/interfaces/DatabaseOptions.d.ts +26 -0
  172. package/build/interfaces/DatabaseOptions.js +2 -0
  173. package/build/interfaces/DatabaseReadInterface.d.ts +14 -0
  174. package/build/interfaces/DatabaseReadInterface.js +2 -0
  175. package/build/interfaces/DbFieldAddDefaultInterface.d.ts +9 -0
  176. package/build/interfaces/DbFieldAddDefaultInterface.js +2 -0
  177. package/build/interfaces/DownloadFileArgs.d.ts +6 -0
  178. package/build/interfaces/DownloadFileArgs.js +2 -0
  179. package/build/interfaces/ExtractCredentialsArgs.d.ts +19 -0
  180. package/build/interfaces/ExtractCredentialsArgs.js +2 -0
  181. package/build/interfaces/JwtTokenSecretAndSettings.d.ts +5 -0
  182. package/build/interfaces/JwtTokenSecretAndSettings.js +2 -0
  183. package/build/interfaces/ListArgsInterface.d.ts +7 -0
  184. package/build/interfaces/ListArgsInterface.js +2 -0
  185. package/build/interfaces/LoaderInterface.d.ts +7 -0
  186. package/build/interfaces/LoaderInterface.js +2 -0
  187. package/build/interfaces/PaginationInterface.d.ts +10 -0
  188. package/build/interfaces/PaginationInterface.js +2 -0
  189. package/build/interfaces/SortItemInterface.d.ts +4 -0
  190. package/build/interfaces/SortItemInterface.js +2 -0
  191. package/build/interfaces/UserTokenParams.d.ts +8 -0
  192. package/build/interfaces/UserTokenParams.js +2 -0
  193. package/build/interfaces/VerifyUserTokenArgs.d.ts +13 -0
  194. package/build/interfaces/VerifyUserTokenArgs.js +2 -0
  195. package/build/interfaces/index.d.ts +28 -0
  196. package/build/interfaces/index.js +43 -0
  197. package/build/knexConfigBuilder.d.ts +1 -0
  198. package/build/knexConfigBuilder.js +91 -0
  199. package/build/passwordHelpers.d.ts +5 -0
  200. package/build/passwordHelpers.js +21 -0
  201. package/build/redisClient.d.ts +17 -0
  202. package/build/redisClient.js +100 -0
  203. package/build/resolversHelpers.d.ts +3 -0
  204. package/build/resolversHelpers.js +69 -0
  205. package/build/userToken.d.ts +8 -0
  206. package/build/userToken.js +92 -0
  207. package/build/utils.d.ts +2 -0
  208. package/build/utils.js +21 -0
  209. package/package.json +1 -1
@@ -0,0 +1,311 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.validateUpdate = exports.validateCreate = exports.validateRemoveMany = exports.validateGetMany = exports.validateRemove = exports.validateGet = exports.validateList = exports.validateMultipleTextIds = exports.validateMultipleUuidIds = exports.validateMultipleIntIds = exports.validateSingleTextId = exports.validateSingleUuidId = exports.validateSingleIntId = exports.shouldHaveId = exports.shouldHaveParams = void 0;
7
+ const lodash_1 = require("lodash");
8
+ const checkit_1 = __importDefault(require("checkit"));
9
+ const check_types_1 = __importDefault(require("check-types"));
10
+ const is_uuid_1 = __importDefault(require("is-uuid"));
11
+ const api_helpers_1 = require("@sdflc/api-helpers");
12
+ const checkItHelpers_1 = require("./checkItHelpers");
13
+ const constants_1 = require("./constants");
14
+ const rulesSingleIntId = new checkit_1.default({
15
+ id: [
16
+ {
17
+ rule: 'required',
18
+ message: 'ID is required',
19
+ },
20
+ {
21
+ rule: 'integer',
22
+ message: 'ID should be an integer',
23
+ },
24
+ ],
25
+ });
26
+ const rulesSingleUuidId = new checkit_1.default({
27
+ id: [
28
+ {
29
+ rule: 'required',
30
+ message: 'ID is required',
31
+ },
32
+ {
33
+ rule: 'uuid',
34
+ message: 'ID should be an integer',
35
+ },
36
+ ],
37
+ });
38
+ const rulesSingleTextId = new checkit_1.default({
39
+ id: [
40
+ {
41
+ rule: 'required',
42
+ message: 'ID is required',
43
+ },
44
+ {
45
+ rule: 'string',
46
+ message: 'ID should be a string',
47
+ },
48
+ ],
49
+ });
50
+ const rulesMultipleIntIds = new checkit_1.default({
51
+ ids: [
52
+ {
53
+ rule: 'required',
54
+ message: 'IDs is required',
55
+ },
56
+ {
57
+ rule: 'array',
58
+ message: 'IDs should be an array of integer',
59
+ },
60
+ {
61
+ rule: (values) => {
62
+ const opResult = new api_helpers_1.OpResult();
63
+ (0, lodash_1.castArray)(values).forEach((value, idx) => {
64
+ if (!check_types_1.default.integer(value)) {
65
+ opResult.addError('ids', `The ids[${idx}] should be an integer`);
66
+ }
67
+ });
68
+ if (opResult.hasErrors()) {
69
+ opResult.setCode(api_helpers_1.OP_RESULT_CODES.VALIDATION_FAILED);
70
+ throw opResult;
71
+ }
72
+ },
73
+ },
74
+ ],
75
+ });
76
+ const rulesMultipleUuidIds = new checkit_1.default({
77
+ ids: [
78
+ {
79
+ rule: 'required',
80
+ message: 'IDs is required',
81
+ },
82
+ {
83
+ rule: 'array',
84
+ message: 'IDs should be an array of UUIDs',
85
+ },
86
+ {
87
+ rule: (values) => {
88
+ const opResult = new api_helpers_1.OpResult();
89
+ (0, lodash_1.castArray)(values).forEach((value, idx) => {
90
+ if (!is_uuid_1.default.v4(value)) {
91
+ opResult.addError('ids', `The ids[${idx}] should be a UUID of version 4`);
92
+ }
93
+ });
94
+ if (opResult.hasErrors()) {
95
+ opResult.setCode(api_helpers_1.OP_RESULT_CODES.VALIDATION_FAILED);
96
+ throw opResult;
97
+ }
98
+ },
99
+ },
100
+ ],
101
+ });
102
+ const rulesMultipleTextIds = new checkit_1.default({
103
+ ids: [
104
+ {
105
+ rule: 'required',
106
+ message: 'IDs is required',
107
+ },
108
+ {
109
+ rule: 'array',
110
+ message: 'IDs should be an array of strings',
111
+ },
112
+ {
113
+ rule: (values) => {
114
+ const opResult = new api_helpers_1.OpResult();
115
+ (0, lodash_1.castArray)(values).forEach((value, idx) => {
116
+ if (!check_types_1.default.string(value)) {
117
+ opResult.addError('ids', `The ids[${idx}] should be a string`);
118
+ }
119
+ });
120
+ if (opResult.hasErrors()) {
121
+ opResult.setCode(api_helpers_1.OP_RESULT_CODES.VALIDATION_FAILED);
122
+ throw opResult;
123
+ }
124
+ },
125
+ },
126
+ ],
127
+ });
128
+ const shouldHaveParams = (args) => {
129
+ const keys = Object.keys(args || {});
130
+ if (keys.length === 0) {
131
+ const result = new api_helpers_1.OpResult({ code: api_helpers_1.OP_RESULT_CODES.VALIDATION_FAILED });
132
+ result.addError('', 'No params received');
133
+ return [result];
134
+ }
135
+ return [true];
136
+ };
137
+ exports.shouldHaveParams = shouldHaveParams;
138
+ const shouldHaveId = (id) => {
139
+ if (!id) {
140
+ const result = new api_helpers_1.OpResult({ code: api_helpers_1.OP_RESULT_CODES.VALIDATION_FAILED });
141
+ result.addError('', 'ID is required');
142
+ return [result];
143
+ }
144
+ return [true];
145
+ };
146
+ exports.shouldHaveId = shouldHaveId;
147
+ const validateSingleIntId = async (args, opt) => {
148
+ const [result] = rulesSingleIntId.validateSync(args);
149
+ if (result) {
150
+ return [(0, checkItHelpers_1.createErrorOnCheckError)(result)];
151
+ }
152
+ return [true, {}];
153
+ };
154
+ exports.validateSingleIntId = validateSingleIntId;
155
+ const validateSingleUuidId = async (args, opt) => {
156
+ const [result] = rulesSingleUuidId.validateSync(args);
157
+ if (result) {
158
+ return [(0, checkItHelpers_1.createErrorOnCheckError)(result)];
159
+ }
160
+ return [true, {}];
161
+ };
162
+ exports.validateSingleUuidId = validateSingleUuidId;
163
+ const validateSingleTextId = async (args, opt) => {
164
+ const [result] = rulesSingleTextId.validateSync(args);
165
+ if (result) {
166
+ return [(0, checkItHelpers_1.createErrorOnCheckError)(result)];
167
+ }
168
+ return [true, {}];
169
+ };
170
+ exports.validateSingleTextId = validateSingleTextId;
171
+ const validateMultipleIntIds = async (args, opt) => {
172
+ const [result] = rulesMultipleIntIds.validateSync(args);
173
+ if (result) {
174
+ return [(0, checkItHelpers_1.createErrorOnCheckError)(result)];
175
+ }
176
+ return [true, {}];
177
+ };
178
+ exports.validateMultipleIntIds = validateMultipleIntIds;
179
+ const validateMultipleUuidIds = async (args, opt) => {
180
+ const [result] = rulesMultipleUuidIds.validateSync(args);
181
+ if (result) {
182
+ return [(0, checkItHelpers_1.createErrorOnCheckError)(result)];
183
+ }
184
+ return [true, {}];
185
+ };
186
+ exports.validateMultipleUuidIds = validateMultipleUuidIds;
187
+ const validateMultipleTextIds = async (args, opt) => {
188
+ const [result] = rulesMultipleTextIds.validateSync(args);
189
+ if (result) {
190
+ return [(0, checkItHelpers_1.createErrorOnCheckError)(result)];
191
+ }
192
+ return [true, {}];
193
+ };
194
+ exports.validateMultipleTextIds = validateMultipleTextIds;
195
+ const validateList = ({ rules }) => async (args, opt) => {
196
+ const { filter } = args || {};
197
+ const [result] = rules.validateSync(filter);
198
+ if (result) {
199
+ return [(0, checkItHelpers_1.createErrorOnCheckError)(result)];
200
+ }
201
+ return [true, {}];
202
+ };
203
+ exports.validateList = validateList;
204
+ const validateGet = async (args, opt) => {
205
+ const idType = opt.core.getIdType();
206
+ let rules = null;
207
+ switch (idType) {
208
+ case constants_1.ID_TYPES.INT:
209
+ rules = rulesSingleIntId;
210
+ break;
211
+ case constants_1.ID_TYPES.UUID:
212
+ rules = rulesSingleUuidId;
213
+ break;
214
+ case constants_1.ID_TYPES.TEXT:
215
+ rules = rulesSingleTextId;
216
+ break;
217
+ }
218
+ if (!rules) {
219
+ return [true, {}];
220
+ }
221
+ const [result] = rules.validateSync(args);
222
+ if (result) {
223
+ return [(0, checkItHelpers_1.createErrorOnCheckError)(result)];
224
+ }
225
+ return [true, {}];
226
+ };
227
+ exports.validateGet = validateGet;
228
+ const validateRemove = async (args, opt) => {
229
+ const { where } = args || {};
230
+ const [idCheck] = (0, exports.shouldHaveParams)(where);
231
+ if (idCheck !== true) {
232
+ return [idCheck, {}];
233
+ }
234
+ const idType = opt.core.getIdType();
235
+ let rules = null;
236
+ switch (idType) {
237
+ case constants_1.ID_TYPES.INT:
238
+ rules = rulesSingleIntId;
239
+ break;
240
+ case constants_1.ID_TYPES.UUID:
241
+ rules = rulesSingleUuidId;
242
+ break;
243
+ case constants_1.ID_TYPES.TEXT:
244
+ rules = rulesSingleTextId;
245
+ break;
246
+ }
247
+ if (!rules) {
248
+ return [true, {}];
249
+ }
250
+ const [result] = rules.validateSync(where);
251
+ if (result) {
252
+ return [(0, checkItHelpers_1.createErrorOnCheckError)(result)];
253
+ }
254
+ return [true, {}];
255
+ };
256
+ exports.validateRemove = validateRemove;
257
+ const validateGetMany = async (args, opt) => {
258
+ const idType = opt.core.getIdType();
259
+ let rules = null;
260
+ switch (idType) {
261
+ case constants_1.ID_TYPES.INT:
262
+ rules = rulesMultipleIntIds;
263
+ break;
264
+ case constants_1.ID_TYPES.UUID:
265
+ rules = rulesMultipleUuidIds;
266
+ break;
267
+ case constants_1.ID_TYPES.TEXT:
268
+ rules = rulesMultipleTextIds;
269
+ break;
270
+ }
271
+ if (!rules) {
272
+ return [true, {}];
273
+ }
274
+ const [result] = rules.validateSync(args);
275
+ if (result) {
276
+ return [(0, checkItHelpers_1.createErrorOnCheckError)(result)];
277
+ }
278
+ return [true, {}];
279
+ };
280
+ exports.validateGetMany = validateGetMany;
281
+ exports.validateRemoveMany = exports.validateGetMany;
282
+ const validateCreate = ({ rules }) => async (args, opt) => {
283
+ const { params } = args || {};
284
+ const [paramsCheck] = (0, exports.shouldHaveParams)(params);
285
+ if (paramsCheck !== true) {
286
+ return [paramsCheck, {}];
287
+ }
288
+ const [result] = rules.validateSync(params);
289
+ if (result) {
290
+ return [(0, checkItHelpers_1.createErrorOnCheckError)(result), {}];
291
+ }
292
+ return [true, {}];
293
+ };
294
+ exports.validateCreate = validateCreate;
295
+ const validateUpdate = ({ rules }) => async (args, opt) => {
296
+ const { where, params } = args || {};
297
+ const [idCheck] = (0, exports.shouldHaveParams)(where);
298
+ if (idCheck !== true) {
299
+ return [idCheck, {}];
300
+ }
301
+ const [paramsCheck] = (0, exports.shouldHaveParams)(params);
302
+ if (paramsCheck !== true) {
303
+ return [paramsCheck, {}];
304
+ }
305
+ const [result] = rules.validateSync(params);
306
+ if (result) {
307
+ return [(0, checkItHelpers_1.createErrorOnCheckError)(result), {}];
308
+ }
309
+ return [true, {}];
310
+ };
311
+ exports.validateUpdate = validateUpdate;
@@ -0,0 +1,2 @@
1
+ import { BuildConfigOptions } from './interfaces';
2
+ export declare const buildConfig: (dirName: string, opt?: BuildConfigOptions) => any;
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildConfig = void 0;
4
+ const lodash_1 = require("lodash");
5
+ const path_1 = require("path");
6
+ const dotenv_safe_1 = require("dotenv-safe");
7
+ const buildConfig = (dirName, opt) => {
8
+ const opt2use = opt || {};
9
+ (0, dotenv_safe_1.config)({
10
+ path: (0, path_1.resolve)(dirName, opt2use.envFile || '.env'),
11
+ allowEmptyValues: opt2use.allowEmptyValues != null ? opt2use.allowEmptyValues : true,
12
+ example: (0, path_1.join)(dirName, opt2use.envFileExample || '.env.example'),
13
+ });
14
+ const env = Object.assign({ NODE_ENV: opt2use.defaultNodeEnv || 'development' }, process.env);
15
+ /**
16
+ * Creates an application settings consisting of environment variables
17
+ */
18
+ const envVars = Object.keys(env).reduce((acc, key) => {
19
+ let value = env[key];
20
+ const tryValue = (value || '').toLowerCase();
21
+ if (tryValue === 'false') {
22
+ value = false;
23
+ }
24
+ else if (tryValue === 'true') {
25
+ value = true;
26
+ }
27
+ acc[(0, lodash_1.camelCase)(key)] = value;
28
+ return acc;
29
+ }, {});
30
+ return {
31
+ ...envVars,
32
+ nodeEnv: env.NODE_ENV,
33
+ isProduction: env.NODE_ENV === 'production',
34
+ };
35
+ };
36
+ exports.buildConfig = buildConfig;
@@ -0,0 +1,30 @@
1
+ declare const FIELDS: any;
2
+ declare const ORDER_BYS: {
3
+ ASC: string;
4
+ DESC: string;
5
+ };
6
+ declare const HEADERS: any;
7
+ declare enum ID_TYPES {
8
+ UNKNOWN = 0,
9
+ INT = 1,
10
+ UUID = 2,
11
+ TEXT = 3
12
+ }
13
+ declare const ID_FIELD: any;
14
+ declare const DEFAULT_ORDER_NO = "10000000";
15
+ declare const DEFAULT_ORDER_NO_STEP = "1000";
16
+ declare const DEFAULT_JWT_ALGORITHM = "HS256";
17
+ declare const DEFAULT_JWT_ISSUER = "microservices-auth";
18
+ declare const ACTIONS: {
19
+ LIST: string;
20
+ GET: string;
21
+ GET_MANY: string;
22
+ CREATE: string;
23
+ CREATE_MANY: string;
24
+ UPDATE: string;
25
+ UPDATE_MANY: string;
26
+ SET: string;
27
+ REMOVE: string;
28
+ REMOVE_MANY: string;
29
+ };
30
+ export { ID_FIELD, FIELDS, ORDER_BYS, HEADERS, DEFAULT_ORDER_NO, DEFAULT_ORDER_NO_STEP, ID_TYPES, DEFAULT_JWT_ALGORITHM, DEFAULT_JWT_ISSUER, ACTIONS, };
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ACTIONS = exports.DEFAULT_JWT_ISSUER = exports.DEFAULT_JWT_ALGORITHM = exports.ID_TYPES = exports.DEFAULT_ORDER_NO_STEP = exports.DEFAULT_ORDER_NO = exports.HEADERS = exports.ORDER_BYS = exports.FIELDS = exports.ID_FIELD = void 0;
4
+ const FIELDS = {
5
+ ID: 'id',
6
+ STATUS: 'status',
7
+ VERSION: 'version',
8
+ CREATED_BY: 'created_by',
9
+ UPDATED_BY: 'updated_by',
10
+ REMOVED_BY: 'removed_by',
11
+ CREATED_AT: 'created_at',
12
+ UPDATED_AT: 'updated_at',
13
+ REMOVED_AT: 'removed_at',
14
+ REMOVED_AT_STR: 'removed_at_str',
15
+ LANG: 'LANG',
16
+ ACCOUNT_ID: 'account_id',
17
+ ROLE_ID: 'role_id',
18
+ USER_ID: 'user_id',
19
+ ORDER_NO: 'order_no',
20
+ SPACE_ID: 'space_id',
21
+ DISPLAY_NAME: 'display_name',
22
+ NAME: 'name',
23
+ USERNAME: 'username',
24
+ EMAIL: 'email',
25
+ PHONE: 'phone',
26
+ };
27
+ exports.FIELDS = FIELDS;
28
+ const ORDER_BYS = {
29
+ ASC: 'asc',
30
+ DESC: 'desc',
31
+ };
32
+ exports.ORDER_BYS = ORDER_BYS;
33
+ const HEADERS = {
34
+ AUTHORIZATION: 'authorization',
35
+ FINGERPRINT: 'x-fingerprint',
36
+ SPACE_ID: 'x-space-id',
37
+ API_KEY: 'x-api-key',
38
+ MANAGEMENT_API_KEY: 'x-management-api-key',
39
+ ACCOUNT_ID: 'x-account-id',
40
+ ROLE_ID: 'x-role-id',
41
+ USER_ID: 'x-user-id',
42
+ USER_AGENT: 'user-agent',
43
+ REFERER: 'referer',
44
+ IP_ADDRESS: 'x-forwarded-for',
45
+ ACCEPT_LANGUAGE: 'accept-language',
46
+ REQUESTED_LANGUAGE: 'x-lang',
47
+ CONTENT_TYPE: 'content-type',
48
+ CONTENT_LENGTH: 'content-length',
49
+ };
50
+ exports.HEADERS = HEADERS;
51
+ var ID_TYPES;
52
+ (function (ID_TYPES) {
53
+ ID_TYPES[ID_TYPES["UNKNOWN"] = 0] = "UNKNOWN";
54
+ ID_TYPES[ID_TYPES["INT"] = 1] = "INT";
55
+ ID_TYPES[ID_TYPES["UUID"] = 2] = "UUID";
56
+ ID_TYPES[ID_TYPES["TEXT"] = 3] = "TEXT";
57
+ })(ID_TYPES || (exports.ID_TYPES = ID_TYPES = {}));
58
+ const ID_FIELD = FIELDS.ID;
59
+ exports.ID_FIELD = ID_FIELD;
60
+ const DEFAULT_ORDER_NO = '10000000';
61
+ exports.DEFAULT_ORDER_NO = DEFAULT_ORDER_NO;
62
+ const DEFAULT_ORDER_NO_STEP = '1000';
63
+ exports.DEFAULT_ORDER_NO_STEP = DEFAULT_ORDER_NO_STEP;
64
+ const DEFAULT_JWT_ALGORITHM = 'HS256';
65
+ exports.DEFAULT_JWT_ALGORITHM = DEFAULT_JWT_ALGORITHM;
66
+ const DEFAULT_JWT_ISSUER = 'microservices-auth';
67
+ exports.DEFAULT_JWT_ISSUER = DEFAULT_JWT_ISSUER;
68
+ const ACTIONS = {
69
+ LIST: 'LIST',
70
+ GET: 'GET',
71
+ GET_MANY: 'GET_MANY',
72
+ CREATE: 'CREATE',
73
+ CREATE_MANY: 'CREATE_MANY',
74
+ UPDATE: 'UPDATE',
75
+ UPDATE_MANY: 'UPDATE_MANY',
76
+ SET: 'SET',
77
+ REMOVE: 'REMOVE',
78
+ REMOVE_MANY: 'REMOVE_MANY',
79
+ };
80
+ exports.ACTIONS = ACTIONS;
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Removes all the records from provided table.
3
+ * If appSettings has cleanTable object with table names then
4
+ * it is used to add extra filters to clear the table to avoid for example users data
5
+ * @param table Name of table from which to remove all the records
6
+ * @param knexHandle Knex handle object with configured connection to DB
7
+ * @param appSettings settings with db related information
8
+ */
9
+ declare const dbCleanTable: (table: string, knexHandle: any, appSettings: any) => Promise<void>;
10
+ /**
11
+ * Returns a list of tables in db schedm specified in settings
12
+ * @param knexHandle Knex handle object with configured connection to DB
13
+ * @param appSettings settings with db related information
14
+ * @returns arrays of table names in the provided db schema
15
+ */
16
+ declare const dbListTables: (knexHandle: any, appSettings: any) => Promise<any>;
17
+ /**
18
+ * Removes all the records from provided list of tables
19
+ * @param tablesToClean array of table names
20
+ * @param knexHandle Knex handle object with configured connection to DB
21
+ * @param appSettings settings with db related information
22
+ * @returns promise
23
+ */
24
+ declare const dbClean: (tablesToClean: any[], knexHandle: any, appSettings: any) => Promise<unknown>;
25
+ /**
26
+ * Selects all the tables in the schema and drops them.
27
+ * This approach does not work in case we have constraints.
28
+ * TODO: Remove all constraints and then drop tables.
29
+ */
30
+ declare const dbWipe: (knexHandle: any, appSettings: any) => any;
31
+ /**
32
+ * Drop all tables, run migrations, then seed
33
+ */
34
+ declare const dbReset: (knexHandle: any, appSettings: any) => any;
35
+ /**
36
+ * Go from array of objects which contain keys of object names which have an array value of seeded objects
37
+ * to a single object with keys of object names and arrays of seeded objects
38
+ * ie from [{table1: [], table2: [] }, { table3: [] }]
39
+ * to { table1: [], table2: [], table3: []}
40
+ */
41
+ declare const dbTransformSeedsToObject: (generatedSeedArrays: any[]) => any;
42
+ declare const dbRunMigrations: (knexHandle: any, appSettings: any) => any;
43
+ export { dbCleanTable, dbListTables, dbClean, dbWipe, dbReset, dbTransformSeedsToObject, dbRunMigrations };
@@ -0,0 +1,156 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.dbRunMigrations = exports.dbTransformSeedsToObject = exports.dbReset = exports.dbWipe = exports.dbClean = exports.dbListTables = exports.dbCleanTable = void 0;
7
+ const bluebird_1 = __importDefault(require("bluebird"));
8
+ const lodash_1 = require("lodash");
9
+ const SQLS = {
10
+ pg: {
11
+ listTablesSchema: (appSettings) => `
12
+ SELECT TABLE_NAME as table_name
13
+ FROM INFORMATION_SCHEMA.TABLES
14
+ WHERE TABLE_SCHEMA = '${appSettings?.dbSchema}';
15
+ `,
16
+ listTables: () => `
17
+ SELECT TABLE_NAME as table_name
18
+ FROM INFORMATION_SCHEMA.TABLES
19
+ `,
20
+ dropTablesSchema: (appSettings) => `
21
+ SELECT 'DROP TABLE IF EXISTS ${appSettings?.dbSchema}.' || TABLE_NAME || ';' as cmd
22
+ FROM INFORMATION_SCHEMA.TABLES
23
+ WHERE TABLE_SCHEMA = '${appSettings?.dbSchema}';
24
+ `,
25
+ dropTables: () => `
26
+ SELECT 'DROP TABLE IF EXISTS ' || TABLE_NAME || ';' as cmd
27
+ FROM INFORMATION_SCHEMA.TABLES';
28
+ `,
29
+ },
30
+ mssql: {
31
+ listTablesSchema: (appSettings) => `
32
+ SELECT TABLE_NAME as table_name
33
+ FROM INFORMATION_SCHEMA.TABLES
34
+ WHERE TABLE_SCHEMA = '${appSettings?.dbSchema}';
35
+ `,
36
+ listTables: () => `
37
+ SELECT TABLE_NAME as table_name
38
+ FROM INFORMATION_SCHEMA.TABLES
39
+ `,
40
+ dropTablesSchema: (appSettings) => `
41
+ SELECT 'DROP TABLE IF EXISTS [${appSettings?.dbSchema}].[' + TABLE_NAME + '];' as cmd
42
+ FROM INFORMATION_SCHEMA.TABLES
43
+ WHERE TABLE_SCHEMA = '${appSettings?.dbSchema}';
44
+ `,
45
+ dropTables: () => `
46
+ SELECT 'DROP TABLE IF EXISTS ' + TABLE_NAME + ';' as cmd
47
+ FROM INFORMATION_SCHEMA.TABLES';
48
+ `,
49
+ },
50
+ };
51
+ /**
52
+ * Removes all the records from provided table.
53
+ * If appSettings has cleanTable object with table names then
54
+ * it is used to add extra filters to clear the table to avoid for example users data
55
+ * @param table Name of table from which to remove all the records
56
+ * @param knexHandle Knex handle object with configured connection to DB
57
+ * @param appSettings settings with db related information
58
+ */
59
+ const dbCleanTable = async (table, knexHandle, appSettings) => {
60
+ const query = knexHandle(table).withSchema(appSettings.dbSchema).delete();
61
+ const cleanTableFilters = appSettings?.cleanTableFilters?.[table];
62
+ if (cleanTableFilters != undefined) {
63
+ if (Array.isArray(cleanTableFilters.where)) {
64
+ cleanTableFilters.where.forEach((whereItem) => {
65
+ query.where(whereItem.fieldName, whereItem.operand, whereItem.value);
66
+ });
67
+ }
68
+ }
69
+ await query;
70
+ };
71
+ exports.dbCleanTable = dbCleanTable;
72
+ /**
73
+ * Returns a list of tables in db schedm specified in settings
74
+ * @param knexHandle Knex handle object with configured connection to DB
75
+ * @param appSettings settings with db related information
76
+ * @returns arrays of table names in the provided db schema
77
+ */
78
+ const dbListTables = async (knexHandle, appSettings) => {
79
+ const skipTables = (appSettings || {}).skipTables || [];
80
+ let query = SQLS[appSettings.dbType].listTablesSchema(appSettings);
81
+ if (!appSettings.dbSchema) {
82
+ query = SQLS[appSettings.dbType].listTables(appSettings);
83
+ }
84
+ const tables = await knexHandle.raw(query);
85
+ return (tables.rows || [])
86
+ .map((row) => row.table_name)
87
+ .filter((table) => skipTables.indexOf(table) === -1);
88
+ };
89
+ exports.dbListTables = dbListTables;
90
+ /**
91
+ * Removes all the records from provided list of tables
92
+ * @param tablesToClean array of table names
93
+ * @param knexHandle Knex handle object with configured connection to DB
94
+ * @param appSettings settings with db related information
95
+ * @returns promise
96
+ */
97
+ const dbClean = async (tablesToClean, knexHandle, appSettings) => {
98
+ for (let idx = 0, len = tablesToClean.length; idx < len; ++idx) {
99
+ await dbCleanTable(tablesToClean[idx], knexHandle, appSettings);
100
+ }
101
+ return new bluebird_1.default((resolve) => {
102
+ resolve(tablesToClean);
103
+ });
104
+ };
105
+ exports.dbClean = dbClean;
106
+ /**
107
+ * Selects all the tables in the schema and drops them.
108
+ * This approach does not work in case we have constraints.
109
+ * TODO: Remove all constraints and then drop tables.
110
+ */
111
+ const dbWipe = (knexHandle, appSettings) => {
112
+ let query = SQLS[appSettings.dbType].dropTablesSchema(appSettings);
113
+ if (!appSettings.dbSchema) {
114
+ query = SQLS[appSettings.dbType].dropTables(appSettings);
115
+ }
116
+ return knexHandle
117
+ .raw(query)
118
+ .then((result) => result.rows.map((r) => r.cmd))
119
+ .then((commands) => bluebird_1.default.each(commands, (cmd) => knexHandle.raw(cmd)));
120
+ };
121
+ exports.dbWipe = dbWipe;
122
+ /**
123
+ * Drop all tables, run migrations, then seed
124
+ */
125
+ const dbReset = (knexHandle, appSettings) => {
126
+ return dbWipe(knexHandle, appSettings)
127
+ .then(() => dbRunMigrations(knexHandle, appSettings))
128
+ .catch((error) => console.log('An exception has occurred on resetting database: ', error));
129
+ };
130
+ exports.dbReset = dbReset;
131
+ /**
132
+ * Go from array of objects which contain keys of object names which have an array value of seeded objects
133
+ * to a single object with keys of object names and arrays of seeded objects
134
+ * ie from [{table1: [], table2: [] }, { table3: [] }]
135
+ * to { table1: [], table2: [], table3: []}
136
+ */
137
+ const dbTransformSeedsToObject = (generatedSeedArrays) => (0, lodash_1.reduce)(generatedSeedArrays, (acc, cur) => {
138
+ for (const key in cur) {
139
+ acc[key] = cur[key];
140
+ }
141
+ return acc;
142
+ }, {});
143
+ exports.dbTransformSeedsToObject = dbTransformSeedsToObject;
144
+ const dbRunMigrations = (knexHandle, appSettings) => {
145
+ return knexHandle.migrate
146
+ .latest()
147
+ .then(async (result) => {
148
+ if (appSettings && appSettings.runDbSeed && result[1].length > 0) {
149
+ await knexHandle.seed.run();
150
+ }
151
+ console.log('Ran', result[1].length, 'migration(s)');
152
+ return knexHandle.migrate.currentVersion();
153
+ })
154
+ .then((version) => console.log('Current version of database: ', version));
155
+ };
156
+ exports.dbRunMigrations = dbRunMigrations;
@@ -0,0 +1,2 @@
1
+ declare const dbSeed: (dirname: string, seedFiles: any, knexHandle: any, config: any) => Promise<any>;
2
+ export { dbSeed };