@sdflc/backend-helpers 1.0.77 → 1.0.79

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 (210) hide show
  1. package/README.md +178 -178
  2. package/build/BaseCore.d.ts +88 -88
  3. package/build/BaseCore.js +816 -819
  4. package/build/BaseGateway.d.ts +49 -49
  5. package/build/BaseGateway.js +482 -484
  6. package/build/DataCache.d.ts +14 -14
  7. package/build/DataCache.js +104 -104
  8. package/build/Database.d.ts +32 -32
  9. package/build/Database.js +198 -198
  10. package/build/DbMigratorAndSeeder.d.ts +49 -49
  11. package/build/DbMigratorAndSeeder.js +238 -228
  12. package/build/RedisDataLoader.d.ts +41 -41
  13. package/build/RedisDataLoader.js +243 -248
  14. package/build/apiContext.d.ts +11 -11
  15. package/build/apiContext.js +143 -143
  16. package/build/{BaseController.d.ts → baseController.d.ts} +17 -17
  17. package/build/{BaseController.js → baseController.js} +66 -66
  18. package/build/checkItHelpers.d.ts +3 -3
  19. package/build/checkItHelpers.js +28 -28
  20. package/build/commonValidators.d.ts +17 -17
  21. package/build/commonValidators.js +292 -298
  22. package/build/configBulder.d.ts +2 -2
  23. package/build/configBulder.js +32 -32
  24. package/build/constants.d.ts +30 -30
  25. package/build/constants.js +80 -81
  26. package/build/dbHelpers.d.ts +43 -43
  27. package/build/dbHelpers.js +166 -166
  28. package/build/dbSeeder.d.ts +2 -2
  29. package/build/dbSeeder.js +92 -82
  30. package/build/dbTableHelpers.d.ts +11 -11
  31. package/build/dbTableHelpers.js +110 -110
  32. package/build/downloadHelpers.d.ts +2 -2
  33. package/build/downloadHelpers.js +83 -83
  34. package/build/esm/BaseCore.d.ts +88 -0
  35. package/build/esm/BaseCore.js +810 -0
  36. package/build/esm/BaseGateway.d.ts +49 -0
  37. package/build/esm/BaseGateway.js +476 -0
  38. package/build/esm/DataCache.d.ts +14 -0
  39. package/build/esm/DataCache.js +98 -0
  40. package/build/esm/Database.d.ts +32 -0
  41. package/build/esm/Database.js +192 -0
  42. package/build/esm/DbMigratorAndSeeder.d.ts +49 -0
  43. package/build/esm/DbMigratorAndSeeder.js +199 -0
  44. package/build/esm/RedisDataLoader.d.ts +41 -0
  45. package/build/esm/RedisDataLoader.js +237 -0
  46. package/build/esm/apiContext.d.ts +11 -0
  47. package/build/esm/apiContext.js +135 -0
  48. package/build/esm/baseController.d.ts +17 -0
  49. package/build/esm/baseController.js +60 -0
  50. package/build/esm/checkItHelpers.d.ts +3 -0
  51. package/build/esm/checkItHelpers.js +23 -0
  52. package/build/esm/commonValidators.d.ts +17 -0
  53. package/build/esm/commonValidators.js +273 -0
  54. package/build/esm/configBulder.d.ts +2 -0
  55. package/build/esm/configBulder.js +28 -0
  56. package/build/esm/constants.d.ts +30 -0
  57. package/build/esm/constants.js +69 -0
  58. package/build/esm/dbHelpers.d.ts +43 -0
  59. package/build/esm/dbHelpers.js +154 -0
  60. package/build/esm/dbSeeder.d.ts +2 -0
  61. package/build/esm/dbSeeder.js +53 -0
  62. package/build/esm/dbTableHelpers.d.ts +11 -0
  63. package/build/esm/dbTableHelpers.js +99 -0
  64. package/build/esm/downloadHelpers.d.ts +2 -0
  65. package/build/esm/downloadHelpers.js +76 -0
  66. package/build/esm/index.d.ts +21 -0
  67. package/build/esm/index.js +21 -0
  68. package/build/esm/interfaces/ApiContext.d.ts +24 -0
  69. package/build/esm/interfaces/ApiContext.js +1 -0
  70. package/build/esm/interfaces/ApiContextInterface.d.ts +12 -0
  71. package/build/esm/interfaces/ApiContextInterface.js +1 -0
  72. package/build/esm/interfaces/BaseCoreActionsInterface.d.ts +38 -0
  73. package/build/esm/interfaces/BaseCoreActionsInterface.js +1 -0
  74. package/build/esm/interfaces/BaseCoreDoAuthOnActionInterface.d.ts +12 -0
  75. package/build/esm/interfaces/BaseCoreDoAuthOnActionInterface.js +1 -0
  76. package/build/esm/interfaces/BaseCoreDoingWhatInterface.d.ts +13 -0
  77. package/build/esm/interfaces/BaseCoreDoingWhatInterface.js +1 -0
  78. package/build/esm/interfaces/BaseCorePropsInterface.d.ts +36 -0
  79. package/build/esm/interfaces/BaseCorePropsInterface.js +1 -0
  80. package/build/esm/interfaces/BaseCoreValidatorsInterface.d.ts +13 -0
  81. package/build/esm/interfaces/BaseCoreValidatorsInterface.js +1 -0
  82. package/build/esm/interfaces/BaseGatewayPropsInterface.d.ts +39 -0
  83. package/build/esm/interfaces/BaseGatewayPropsInterface.js +1 -0
  84. package/build/esm/interfaces/BuildConfigOptions.d.ts +6 -0
  85. package/build/esm/interfaces/BuildConfigOptions.js +1 -0
  86. package/build/esm/interfaces/BuildDefaultResolversOptions.d.ts +17 -0
  87. package/build/esm/interfaces/BuildDefaultResolversOptions.js +1 -0
  88. package/build/esm/interfaces/BuildListQueryArgsInterface.d.ts +6 -0
  89. package/build/esm/interfaces/BuildListQueryArgsInterface.js +1 -0
  90. package/build/esm/interfaces/CacheInterface.d.ts +15 -0
  91. package/build/esm/interfaces/CacheInterface.js +1 -0
  92. package/build/esm/interfaces/CommonValidatorArgs.d.ts +3 -0
  93. package/build/esm/interfaces/CommonValidatorArgs.js +1 -0
  94. package/build/esm/interfaces/CoreActionInterface.d.ts +14 -0
  95. package/build/esm/interfaces/CoreActionInterface.js +1 -0
  96. package/build/esm/interfaces/CreateUserTokenArgs.d.ts +11 -0
  97. package/build/esm/interfaces/CreateUserTokenArgs.js +1 -0
  98. package/build/esm/interfaces/CreateUserTokenResult.d.ts +4 -0
  99. package/build/esm/interfaces/CreateUserTokenResult.js +1 -0
  100. package/build/esm/interfaces/DatabaseOptions.d.ts +22 -0
  101. package/build/esm/interfaces/DatabaseOptions.js +1 -0
  102. package/build/esm/interfaces/DatabaseReadInterface.d.ts +14 -0
  103. package/build/esm/interfaces/DatabaseReadInterface.js +1 -0
  104. package/build/esm/interfaces/DbFieldAddDefaultInterface.d.ts +9 -0
  105. package/build/esm/interfaces/DbFieldAddDefaultInterface.js +1 -0
  106. package/build/esm/interfaces/DownloadFileArgs.d.ts +6 -0
  107. package/build/esm/interfaces/DownloadFileArgs.js +1 -0
  108. package/build/esm/interfaces/ExtractCredentialsArgs.d.ts +19 -0
  109. package/build/esm/interfaces/ExtractCredentialsArgs.js +1 -0
  110. package/build/esm/interfaces/JwtTokenSecretAndSettings.d.ts +5 -0
  111. package/build/esm/interfaces/JwtTokenSecretAndSettings.js +1 -0
  112. package/build/esm/interfaces/ListArgsInterface.d.ts +5 -0
  113. package/build/esm/interfaces/ListArgsInterface.js +1 -0
  114. package/build/esm/interfaces/LoaderInterface.d.ts +4 -0
  115. package/build/esm/interfaces/LoaderInterface.js +1 -0
  116. package/build/esm/interfaces/PaginationInterface.d.ts +7 -0
  117. package/build/esm/interfaces/PaginationInterface.js +1 -0
  118. package/build/esm/interfaces/SortItemInterface.d.ts +4 -0
  119. package/build/esm/interfaces/SortItemInterface.js +1 -0
  120. package/build/esm/interfaces/UserTokenParams.d.ts +8 -0
  121. package/build/esm/interfaces/UserTokenParams.js +1 -0
  122. package/build/esm/interfaces/VerifyUserTokenArgs.d.ts +13 -0
  123. package/build/esm/interfaces/VerifyUserTokenArgs.js +1 -0
  124. package/build/esm/interfaces/index.d.ts +28 -0
  125. package/build/esm/interfaces/index.js +27 -0
  126. package/build/esm/knexConfigBuilder.d.ts +1 -0
  127. package/build/esm/knexConfigBuilder.js +73 -0
  128. package/build/esm/passwordHelpers.d.ts +5 -0
  129. package/build/esm/passwordHelpers.js +22 -0
  130. package/build/esm/redisClient.d.ts +17 -0
  131. package/build/esm/redisClient.js +94 -0
  132. package/build/esm/resolversHelpers.d.ts +3 -0
  133. package/build/esm/resolversHelpers.js +59 -0
  134. package/build/esm/userToken.d.ts +8 -0
  135. package/build/esm/userToken.js +74 -0
  136. package/build/esm/utils.d.ts +2 -0
  137. package/build/esm/utils.js +13 -0
  138. package/build/index.d.ts +21 -21
  139. package/build/index.js +37 -37
  140. package/build/interfaces/ApiContext.d.ts +24 -24
  141. package/build/interfaces/ApiContext.js +2 -2
  142. package/build/interfaces/ApiContextInterface.d.ts +12 -12
  143. package/build/interfaces/ApiContextInterface.js +2 -2
  144. package/build/interfaces/BaseCoreActionsInterface.d.ts +38 -38
  145. package/build/interfaces/BaseCoreActionsInterface.js +2 -2
  146. package/build/interfaces/BaseCoreDoAuthOnActionInterface.d.ts +12 -12
  147. package/build/interfaces/BaseCoreDoAuthOnActionInterface.js +2 -2
  148. package/build/interfaces/BaseCoreDoingWhatInterface.d.ts +13 -13
  149. package/build/interfaces/BaseCoreDoingWhatInterface.js +2 -2
  150. package/build/interfaces/BaseCorePropsInterface.d.ts +36 -36
  151. package/build/interfaces/BaseCorePropsInterface.js +2 -2
  152. package/build/interfaces/BaseCoreValidatorsInterface.d.ts +13 -13
  153. package/build/interfaces/BaseCoreValidatorsInterface.js +2 -2
  154. package/build/interfaces/BaseGatewayPropsInterface.d.ts +39 -39
  155. package/build/interfaces/BaseGatewayPropsInterface.js +2 -2
  156. package/build/interfaces/BuildConfigOptions.d.ts +6 -6
  157. package/build/interfaces/BuildConfigOptions.js +2 -2
  158. package/build/interfaces/BuildDefaultResolversOptions.d.ts +17 -17
  159. package/build/interfaces/BuildDefaultResolversOptions.js +2 -2
  160. package/build/interfaces/BuildListQueryArgsInterface.d.ts +6 -6
  161. package/build/interfaces/BuildListQueryArgsInterface.js +2 -2
  162. package/build/interfaces/CacheInterface.d.ts +15 -15
  163. package/build/interfaces/CacheInterface.js +2 -2
  164. package/build/interfaces/CommonValidatorArgs.d.ts +3 -3
  165. package/build/interfaces/CommonValidatorArgs.js +2 -2
  166. package/build/interfaces/CoreActionInterface.d.ts +14 -14
  167. package/build/interfaces/CoreActionInterface.js +2 -2
  168. package/build/interfaces/CreateUserTokenArgs.d.ts +11 -11
  169. package/build/interfaces/CreateUserTokenArgs.js +2 -2
  170. package/build/interfaces/CreateUserTokenResult.d.ts +4 -4
  171. package/build/interfaces/CreateUserTokenResult.js +2 -2
  172. package/build/interfaces/DatabaseOptions.d.ts +22 -22
  173. package/build/interfaces/DatabaseOptions.js +2 -2
  174. package/build/interfaces/DatabaseReadInterface.d.ts +14 -14
  175. package/build/interfaces/DatabaseReadInterface.js +2 -2
  176. package/build/interfaces/DbFieldAddDefaultInterface.d.ts +9 -9
  177. package/build/interfaces/DbFieldAddDefaultInterface.js +2 -2
  178. package/build/interfaces/DownloadFileArgs.d.ts +6 -6
  179. package/build/interfaces/DownloadFileArgs.js +2 -2
  180. package/build/interfaces/ExtractCredentialsArgs.d.ts +19 -19
  181. package/build/interfaces/ExtractCredentialsArgs.js +2 -2
  182. package/build/interfaces/JwtTokenSecretAndSettings.d.ts +5 -5
  183. package/build/interfaces/JwtTokenSecretAndSettings.js +2 -2
  184. package/build/interfaces/ListArgsInterface.d.ts +5 -5
  185. package/build/interfaces/ListArgsInterface.js +2 -2
  186. package/build/interfaces/LoaderInterface.d.ts +4 -4
  187. package/build/interfaces/LoaderInterface.js +2 -2
  188. package/build/interfaces/PaginationInterface.d.ts +7 -7
  189. package/build/interfaces/PaginationInterface.js +2 -2
  190. package/build/interfaces/SortItemInterface.d.ts +4 -4
  191. package/build/interfaces/SortItemInterface.js +2 -2
  192. package/build/interfaces/UserTokenParams.d.ts +8 -8
  193. package/build/interfaces/UserTokenParams.js +2 -2
  194. package/build/interfaces/VerifyUserTokenArgs.d.ts +13 -13
  195. package/build/interfaces/VerifyUserTokenArgs.js +2 -2
  196. package/build/interfaces/index.d.ts +28 -28
  197. package/build/interfaces/index.js +43 -43
  198. package/build/knexConfigBuilder.d.ts +1 -1
  199. package/build/knexConfigBuilder.js +80 -80
  200. package/build/passwordHelpers.d.ts +5 -5
  201. package/build/passwordHelpers.js +30 -30
  202. package/build/redisClient.d.ts +17 -17
  203. package/build/redisClient.js +97 -97
  204. package/build/resolversHelpers.d.ts +3 -3
  205. package/build/resolversHelpers.js +62 -62
  206. package/build/userToken.d.ts +8 -8
  207. package/build/userToken.js +86 -86
  208. package/build/utils.d.ts +2 -2
  209. package/build/utils.js +21 -21
  210. package/package.json +79 -71
@@ -1,248 +1,243 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.RedisDataLoader = void 0;
16
- const lodash_1 = require("lodash");
17
- const dataloader_1 = __importDefault(require("dataloader"));
18
- const json_stable_stringify_1 = __importDefault(require("json-stable-stringify"));
19
- const customOptions = ['expire', 'serialize', 'deserialize', 'cacheKeyFn'];
20
- const mapPromise = (promise, fn) => Promise.all(promise.map(fn));
21
- class RedisDataLoader {
22
- constructor(redisClient, keySpace, userLoader, opt) {
23
- this.redisClient = redisClient;
24
- this.opt = (0, lodash_1.pick)(opt, customOptions) || {};
25
- this.dataLoaderOpt = Object.assign(Object.assign({}, (0, lodash_1.omit)(opt, customOptions)), { cacheKeyFn: this.opt.cacheKeyFn });
26
- this.opt.cacheKeyFn =
27
- typeof this.opt.cacheKeyFn === 'function'
28
- ? this.opt.cacheKeyFn
29
- : (key) => ((0, lodash_1.isObject)(key) ? (0, json_stable_stringify_1.default)(key) : key);
30
- this.keySpace = keySpace ? keySpace + ':' : '';
31
- this.loader = new dataloader_1.default((keys) => __awaiter(this, void 0, void 0, function* () {
32
- const results = yield this.rMGet(keys);
33
- const keysToRead = [];
34
- const keysIdxsToRead = [];
35
- keys.forEach((key, idx) => {
36
- if (results[idx] == null) {
37
- keysToRead.push(key);
38
- keysIdxsToRead.push(idx);
39
- }
40
- });
41
- if (keysToRead.length > 0) {
42
- const loadedData = yield userLoader.load(keysToRead);
43
- const data = yield this.rMSetAndGet(keysToRead, loadedData);
44
- keysIdxsToRead.forEach((keyIdx, idx) => {
45
- results[keyIdx] = data[idx];
46
- });
47
- }
48
- return results;
49
- }), this.dataLoaderOpt);
50
- }
51
- /**
52
- * Adds a prefix to the passed key
53
- * @param key Key value
54
- * @returns Trasnformed key value that includes key space
55
- */
56
- makeKey(key) {
57
- return `${this.keySpace}${this.opt.cacheKeyFn(key)}`;
58
- }
59
- /**
60
- * Deserializes response from Readis
61
- * @param str String to deserialize
62
- * @returns
63
- */
64
- parse(str) {
65
- return __awaiter(this, void 0, void 0, function* () {
66
- try {
67
- if (str === '' || str === null) {
68
- return str;
69
- }
70
- else if (typeof this.opt.deserialize === 'function') {
71
- return yield this.opt.deserialize(str);
72
- }
73
- else {
74
- return JSON.parse(str);
75
- }
76
- }
77
- catch (ex) {
78
- throw ex;
79
- }
80
- });
81
- }
82
- /**
83
- * Serializes passed value to a string
84
- * @param value A value to be serialized
85
- * @returns serialized value in the form of string
86
- */
87
- toString(value) {
88
- return __awaiter(this, void 0, void 0, function* () {
89
- if (value === null) {
90
- return '';
91
- }
92
- else if (typeof this.opt.serialize === 'function') {
93
- return yield this.opt.serialize(value);
94
- }
95
- else if ((0, lodash_1.isObject)(value)) {
96
- return JSON.stringify(value);
97
- }
98
- else {
99
- throw new Error(RedisDataLoader.messages.valueObjectOrNull);
100
- }
101
- });
102
- }
103
- rSetAndGet(key, rawValue) {
104
- return __awaiter(this, void 0, void 0, function* () {
105
- const value = yield this.toString(rawValue);
106
- const fullKey = this.makeKey(key);
107
- const multi = this.redisClient.multi();
108
- multi.set(fullKey, value);
109
- if (this.opt.expire) {
110
- multi.expire(fullKey, this.opt.expire);
111
- }
112
- multi.get(fullKey);
113
- const replies = yield multi.exec();
114
- const lastReply = (0, lodash_1.last)(replies);
115
- const result = yield this.parse(lastReply);
116
- return result;
117
- });
118
- }
119
- rMSetAndGet(keys, rawValues) {
120
- return __awaiter(this, void 0, void 0, function* () {
121
- const multi = this.redisClient.multi();
122
- const fullKeys = yield mapPromise(keys, (key) => this.makeKey(key));
123
- const values = yield mapPromise(rawValues, (rawValue) => this.toString(rawValue));
124
- multi.mSet(fullKeys.reduce((acc, fullKey, idx) => {
125
- acc.push(fullKey);
126
- acc.push(values[idx]);
127
- return acc;
128
- }, []));
129
- if (this.opt.expire) {
130
- for (const fullKey of fullKeys) {
131
- multi.expire(fullKey, this.opt.expire);
132
- }
133
- }
134
- multi.mGet(fullKeys);
135
- const replies = yield multi.exec();
136
- const lastReplies = (0, lodash_1.last)(replies);
137
- const result = yield mapPromise(lastReplies, (lastReply) => this.parse(lastReply));
138
- return result;
139
- });
140
- }
141
- rGet(key) {
142
- return __awaiter(this, void 0, void 0, function* () {
143
- const reply = yield this.redisClient.get(this.makeKey(key));
144
- const result = yield this.parse(reply);
145
- return result;
146
- });
147
- }
148
- rMGet(keys) {
149
- return __awaiter(this, void 0, void 0, function* () {
150
- const replies = yield this.redisClient.mGet((0, lodash_1.map)(keys, (key) => this.makeKey(key)));
151
- const results = yield mapPromise(replies, (reply) => this.parse(reply));
152
- return results;
153
- });
154
- }
155
- rSet(key, rawValue) {
156
- return __awaiter(this, void 0, void 0, function* () {
157
- const value = yield this.toString(rawValue);
158
- const reply = yield this.redisClient.set(this.makeKey(key), value);
159
- const result = yield this.parse(reply);
160
- // result is expected to be OK
161
- return result;
162
- });
163
- }
164
- rMSet(keys, values) {
165
- return __awaiter(this, void 0, void 0, function* () {
166
- if (keys.length != values.length) {
167
- throw new Error(RedisDataLoader.messages.keysValuesLengths);
168
- }
169
- const multi = this.redisClient.multi();
170
- multi.mSet(keys.reduce((acc, key, idx) => {
171
- acc.push(this.makeKey(key));
172
- acc.push(values[idx]);
173
- return acc;
174
- }, []));
175
- if (this.opt.expire) {
176
- for (const key of keys) {
177
- multi.expire(key, this.opt.expire);
178
- }
179
- }
180
- // The reply is expected to be "OK"
181
- const reply = yield multi.exec();
182
- return reply;
183
- });
184
- }
185
- rDel(key) {
186
- return __awaiter(this, void 0, void 0, function* () {
187
- return yield this.redisClient.del(this.makeKey(key));
188
- });
189
- }
190
- load(key) {
191
- return __awaiter(this, void 0, void 0, function* () {
192
- if (!key) {
193
- throw new TypeError(RedisDataLoader.messages.keyRequired);
194
- }
195
- return yield this.loader.load(key);
196
- });
197
- }
198
- loadMany(keys) {
199
- return __awaiter(this, void 0, void 0, function* () {
200
- if (!Array.isArray(keys)) {
201
- throw new TypeError(RedisDataLoader.messages.keysArrayRequired);
202
- }
203
- return yield this.loader.loadMany(keys);
204
- });
205
- }
206
- prime(key, value) {
207
- return __awaiter(this, void 0, void 0, function* () {
208
- if (!key) {
209
- throw new TypeError(RedisDataLoader.messages.keyRequired);
210
- }
211
- else if (value === undefined) {
212
- throw new TypeError(RedisDataLoader.messages.valueRequired);
213
- }
214
- else {
215
- const result = yield this.rSetAndGet(key, value);
216
- return yield this.loader.clear(key).prime(key, result === '' ? null : result);
217
- }
218
- });
219
- }
220
- clear(key) {
221
- return __awaiter(this, void 0, void 0, function* () {
222
- if (!key) {
223
- throw new TypeError(RedisDataLoader.messages.keyRequired);
224
- }
225
- const resp = yield this.rDel(key);
226
- yield this.loader.clear(key);
227
- return resp;
228
- });
229
- }
230
- clearAllLocal() {
231
- return __awaiter(this, void 0, void 0, function* () {
232
- return yield this.loader.clearAll();
233
- });
234
- }
235
- clearLocal(key) {
236
- return __awaiter(this, void 0, void 0, function* () {
237
- return yield this.loader.clear(key);
238
- });
239
- }
240
- }
241
- exports.RedisDataLoader = RedisDataLoader;
242
- RedisDataLoader.messages = {
243
- keyRequired: 'The "key" parameter is required',
244
- keysArrayRequired: 'The "keys" parameter is required and should be an array',
245
- valueRequired: 'The "value" parameter is required',
246
- valueObjectOrNull: 'The "value" parameter must be an object or null',
247
- keysValuesLengths: 'The "keys" and "values" are expected to be the same',
248
- };
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.RedisDataLoader = void 0;
16
+ const lodash_1 = require("lodash");
17
+ const dataloader_1 = __importDefault(require("dataloader"));
18
+ const json_stable_stringify_1 = __importDefault(require("json-stable-stringify"));
19
+ const customOptions = ['expire', 'serialize', 'deserialize', 'cacheKeyFn'];
20
+ const mapPromise = (promise, fn) => Promise.all(promise.map(fn));
21
+ class RedisDataLoader {
22
+ constructor(redisClient, keySpace, userLoader, opt) {
23
+ this.redisClient = redisClient;
24
+ this.opt = (0, lodash_1.pick)(opt, customOptions) || {};
25
+ this.dataLoaderOpt = Object.assign(Object.assign({}, (0, lodash_1.omit)(opt, customOptions)), { cacheKeyFn: this.opt.cacheKeyFn });
26
+ this.opt.cacheKeyFn =
27
+ typeof this.opt.cacheKeyFn === 'function'
28
+ ? this.opt.cacheKeyFn
29
+ : (key) => ((0, lodash_1.isObject)(key) ? (0, json_stable_stringify_1.default)(key) : key);
30
+ this.keySpace = keySpace ? keySpace + ':' : '';
31
+ this.loader = new dataloader_1.default((keys) => __awaiter(this, void 0, void 0, function* () {
32
+ const results = yield this.rMGet(keys);
33
+ const keysToRead = [];
34
+ const keysIdxsToRead = [];
35
+ keys.forEach((key, idx) => {
36
+ if (results[idx] == null) {
37
+ keysToRead.push(key);
38
+ keysIdxsToRead.push(idx);
39
+ }
40
+ });
41
+ if (keysToRead.length > 0) {
42
+ const loadedData = yield userLoader.load(keysToRead);
43
+ const data = yield this.rMSetAndGet(keysToRead, loadedData);
44
+ keysIdxsToRead.forEach((keyIdx, idx) => {
45
+ results[keyIdx] = data[idx];
46
+ });
47
+ }
48
+ return results;
49
+ }), this.dataLoaderOpt);
50
+ }
51
+ /**
52
+ * Adds a prefix to the passed key
53
+ * @param key Key value
54
+ * @returns Trasnformed key value that includes key space
55
+ */
56
+ makeKey(key) {
57
+ return `${this.keySpace}${this.opt.cacheKeyFn(key)}`;
58
+ }
59
+ /**
60
+ * Deserializes response from Readis
61
+ * @param str String to deserialize
62
+ * @returns
63
+ */
64
+ parse(str) {
65
+ return __awaiter(this, void 0, void 0, function* () {
66
+ if (str === '' || str === null) {
67
+ return str;
68
+ }
69
+ else if (typeof this.opt.deserialize === 'function') {
70
+ return yield this.opt.deserialize(str);
71
+ }
72
+ else {
73
+ return JSON.parse(str);
74
+ }
75
+ });
76
+ }
77
+ /**
78
+ * Serializes passed value to a string
79
+ * @param value A value to be serialized
80
+ * @returns serialized value in the form of string
81
+ */
82
+ toString(value) {
83
+ return __awaiter(this, void 0, void 0, function* () {
84
+ if (value === null) {
85
+ return '';
86
+ }
87
+ else if (typeof this.opt.serialize === 'function') {
88
+ return yield this.opt.serialize(value);
89
+ }
90
+ else if ((0, lodash_1.isObject)(value)) {
91
+ return JSON.stringify(value);
92
+ }
93
+ else {
94
+ throw new Error(RedisDataLoader.messages.valueObjectOrNull);
95
+ }
96
+ });
97
+ }
98
+ rSetAndGet(key, rawValue) {
99
+ return __awaiter(this, void 0, void 0, function* () {
100
+ const value = yield this.toString(rawValue);
101
+ const fullKey = this.makeKey(key);
102
+ const multi = this.redisClient.multi();
103
+ multi.set(fullKey, value);
104
+ if (this.opt.expire) {
105
+ multi.expire(fullKey, this.opt.expire);
106
+ }
107
+ multi.get(fullKey);
108
+ const replies = yield multi.exec();
109
+ const lastReply = (0, lodash_1.last)(replies);
110
+ const result = yield this.parse(lastReply);
111
+ return result;
112
+ });
113
+ }
114
+ rMSetAndGet(keys, rawValues) {
115
+ return __awaiter(this, void 0, void 0, function* () {
116
+ const multi = this.redisClient.multi();
117
+ const fullKeys = yield mapPromise(keys, (key) => this.makeKey(key));
118
+ const values = yield mapPromise(rawValues, (rawValue) => this.toString(rawValue));
119
+ multi.mSet(fullKeys.reduce((acc, fullKey, idx) => {
120
+ acc.push(fullKey);
121
+ acc.push(values[idx]);
122
+ return acc;
123
+ }, []));
124
+ if (this.opt.expire) {
125
+ for (const fullKey of fullKeys) {
126
+ multi.expire(fullKey, this.opt.expire);
127
+ }
128
+ }
129
+ multi.mGet(fullKeys);
130
+ const replies = yield multi.exec();
131
+ const lastReplies = (0, lodash_1.last)(replies);
132
+ const result = yield mapPromise(lastReplies, (lastReply) => this.parse(lastReply));
133
+ return result;
134
+ });
135
+ }
136
+ rGet(key) {
137
+ return __awaiter(this, void 0, void 0, function* () {
138
+ const reply = yield this.redisClient.get(this.makeKey(key));
139
+ const result = yield this.parse(reply);
140
+ return result;
141
+ });
142
+ }
143
+ rMGet(keys) {
144
+ return __awaiter(this, void 0, void 0, function* () {
145
+ const replies = yield this.redisClient.mGet((0, lodash_1.map)(keys, (key) => this.makeKey(key)));
146
+ const results = yield mapPromise(replies, (reply) => this.parse(reply));
147
+ return results;
148
+ });
149
+ }
150
+ rSet(key, rawValue) {
151
+ return __awaiter(this, void 0, void 0, function* () {
152
+ const value = yield this.toString(rawValue);
153
+ const reply = yield this.redisClient.set(this.makeKey(key), value);
154
+ const result = yield this.parse(reply);
155
+ // result is expected to be OK
156
+ return result;
157
+ });
158
+ }
159
+ rMSet(keys, values) {
160
+ return __awaiter(this, void 0, void 0, function* () {
161
+ if (keys.length != values.length) {
162
+ throw new Error(RedisDataLoader.messages.keysValuesLengths);
163
+ }
164
+ const multi = this.redisClient.multi();
165
+ multi.mSet(keys.reduce((acc, key, idx) => {
166
+ acc.push(this.makeKey(key));
167
+ acc.push(values[idx]);
168
+ return acc;
169
+ }, []));
170
+ if (this.opt.expire) {
171
+ for (const key of keys) {
172
+ multi.expire(key, this.opt.expire);
173
+ }
174
+ }
175
+ // The reply is expected to be "OK"
176
+ const reply = yield multi.exec();
177
+ return reply;
178
+ });
179
+ }
180
+ rDel(key) {
181
+ return __awaiter(this, void 0, void 0, function* () {
182
+ return yield this.redisClient.del(this.makeKey(key));
183
+ });
184
+ }
185
+ load(key) {
186
+ return __awaiter(this, void 0, void 0, function* () {
187
+ if (!key) {
188
+ throw new TypeError(RedisDataLoader.messages.keyRequired);
189
+ }
190
+ return yield this.loader.load(key);
191
+ });
192
+ }
193
+ loadMany(keys) {
194
+ return __awaiter(this, void 0, void 0, function* () {
195
+ if (!Array.isArray(keys)) {
196
+ throw new TypeError(RedisDataLoader.messages.keysArrayRequired);
197
+ }
198
+ return yield this.loader.loadMany(keys);
199
+ });
200
+ }
201
+ prime(key, value) {
202
+ return __awaiter(this, void 0, void 0, function* () {
203
+ if (!key) {
204
+ throw new TypeError(RedisDataLoader.messages.keyRequired);
205
+ }
206
+ else if (value === undefined) {
207
+ throw new TypeError(RedisDataLoader.messages.valueRequired);
208
+ }
209
+ else {
210
+ const result = yield this.rSetAndGet(key, value);
211
+ return yield this.loader.clear(key).prime(key, result === '' ? null : result);
212
+ }
213
+ });
214
+ }
215
+ clear(key) {
216
+ return __awaiter(this, void 0, void 0, function* () {
217
+ if (!key) {
218
+ throw new TypeError(RedisDataLoader.messages.keyRequired);
219
+ }
220
+ const resp = yield this.rDel(key);
221
+ yield this.loader.clear(key);
222
+ return resp;
223
+ });
224
+ }
225
+ clearAllLocal() {
226
+ return __awaiter(this, void 0, void 0, function* () {
227
+ return yield this.loader.clearAll();
228
+ });
229
+ }
230
+ clearLocal(key) {
231
+ return __awaiter(this, void 0, void 0, function* () {
232
+ return yield this.loader.clear(key);
233
+ });
234
+ }
235
+ }
236
+ exports.RedisDataLoader = RedisDataLoader;
237
+ RedisDataLoader.messages = {
238
+ keyRequired: 'The "key" parameter is required',
239
+ keysArrayRequired: 'The "keys" parameter is required and should be an array',
240
+ valueRequired: 'The "value" parameter is required',
241
+ valueObjectOrNull: 'The "value" parameter must be an object or null',
242
+ keysValuesLengths: 'The "keys" and "values" are expected to be the same',
243
+ };
@@ -1,11 +1,11 @@
1
- import { ApiContext, ApiContextInterface } from './interfaces';
2
- /**
3
- * Creates a GraphQL context object with all available cores.
4
- * @returns {object} A context object with all available cores
5
- */
6
- export declare const createApiContext: (args: ApiContextInterface) => Promise<ApiContext>;
7
- /**
8
- * Creates default a GraphQL context object with all avilable cores.
9
- * @returns {object} A context object with all available cores
10
- */
11
- export declare const createContextDefault: (args: any) => Promise<ApiContext>;
1
+ import { ApiContext, ApiContextInterface } from './interfaces';
2
+ /**
3
+ * Creates a GraphQL context object with all available cores.
4
+ * @returns {object} A context object with all available cores
5
+ */
6
+ export declare const createApiContext: (args: ApiContextInterface) => Promise<ApiContext>;
7
+ /**
8
+ * Creates default a GraphQL context object with all avilable cores.
9
+ * @returns {object} A context object with all available cores
10
+ */
11
+ export declare const createContextDefault: (args: any) => Promise<ApiContext>;