lemon-core 4.1.15 → 4.2.0

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 (154) hide show
  1. package/dist/common/test-helper.d.ts +2 -2
  2. package/dist/common/test-helper.js +24 -26
  3. package/dist/common/test-helper.js.map +1 -1
  4. package/dist/controllers/dummy-controller.js +39 -46
  5. package/dist/controllers/dummy-controller.js.map +1 -1
  6. package/dist/controllers/general-api-controller.js +95 -100
  7. package/dist/controllers/general-api-controller.js.map +1 -1
  8. package/dist/controllers/general-controller.js +81 -82
  9. package/dist/controllers/general-controller.js.map +1 -1
  10. package/dist/cores/api/api-service.d.ts +1 -1
  11. package/dist/cores/api/api-service.js +228 -269
  12. package/dist/cores/api/api-service.js.map +1 -1
  13. package/dist/cores/aws/aws-kms-service.d.ts +1 -2
  14. package/dist/cores/aws/aws-kms-service.js +143 -153
  15. package/dist/cores/aws/aws-kms-service.js.map +1 -1
  16. package/dist/cores/aws/aws-s3-service.d.ts +2 -4
  17. package/dist/cores/aws/aws-s3-service.js +306 -330
  18. package/dist/cores/aws/aws-s3-service.js.map +1 -1
  19. package/dist/cores/aws/aws-sns-service.js +147 -153
  20. package/dist/cores/aws/aws-sns-service.js.map +1 -1
  21. package/dist/cores/aws/aws-sqs-service.js +149 -170
  22. package/dist/cores/aws/aws-sqs-service.js.map +1 -1
  23. package/dist/cores/aws/index.js +10 -20
  24. package/dist/cores/aws/index.js.map +1 -1
  25. package/dist/cores/cache/cache-service.d.ts +2 -2
  26. package/dist/cores/cache/cache-service.js +435 -499
  27. package/dist/cores/cache/cache-service.js.map +1 -1
  28. package/dist/cores/config/config-service.d.ts +1 -1
  29. package/dist/cores/config/config-service.js +56 -63
  30. package/dist/cores/config/config-service.js.map +1 -1
  31. package/dist/cores/config/index.js +14 -24
  32. package/dist/cores/config/index.js.map +1 -1
  33. package/dist/cores/core-services.d.ts +1 -1
  34. package/dist/cores/dynamo/dynamo-query-service.js +37 -51
  35. package/dist/cores/dynamo/dynamo-query-service.js.map +1 -1
  36. package/dist/cores/dynamo/dynamo-scan-service.d.ts +2 -2
  37. package/dist/cores/dynamo/dynamo-scan-service.js +29 -40
  38. package/dist/cores/dynamo/dynamo-scan-service.js.map +1 -1
  39. package/dist/cores/dynamo/dynamo-service.d.ts +2 -2
  40. package/dist/cores/dynamo/dynamo-service.js +528 -601
  41. package/dist/cores/dynamo/dynamo-service.js.map +1 -1
  42. package/dist/cores/dynamo/tools/expressions.js +17 -7
  43. package/dist/cores/dynamo/tools/expressions.js.map +1 -1
  44. package/dist/cores/dynamo/tools/query.js +142 -127
  45. package/dist/cores/dynamo/tools/query.js.map +1 -1
  46. package/dist/cores/dynamo/tools/scan.js +111 -97
  47. package/dist/cores/dynamo/tools/scan.js.map +1 -1
  48. package/dist/cores/dynamo/tools/serializer.js +17 -7
  49. package/dist/cores/dynamo/tools/serializer.js.map +1 -1
  50. package/dist/cores/dynamo/tools/utils.d.ts +0 -2
  51. package/dist/cores/dynamo/tools/utils.js.map +1 -1
  52. package/dist/cores/elastic/elastic6-query-service.js +307 -324
  53. package/dist/cores/elastic/elastic6-query-service.js.map +1 -1
  54. package/dist/cores/elastic/elastic6-service.d.ts +3 -3
  55. package/dist/cores/elastic/elastic6-service.js +568 -647
  56. package/dist/cores/elastic/elastic6-service.js.map +1 -1
  57. package/dist/cores/elastic/hangul-service.js +52 -54
  58. package/dist/cores/elastic/hangul-service.js.map +1 -1
  59. package/dist/cores/lambda/index.js +42 -36
  60. package/dist/cores/lambda/index.js.map +1 -1
  61. package/dist/cores/lambda/lambda-alb-handler.d.ts +2 -2
  62. package/dist/cores/lambda/lambda-alb-handler.js +59 -72
  63. package/dist/cores/lambda/lambda-alb-handler.js.map +1 -1
  64. package/dist/cores/lambda/lambda-cognito-handler.js +10 -19
  65. package/dist/cores/lambda/lambda-cognito-handler.js.map +1 -1
  66. package/dist/cores/lambda/lambda-cron-handler.d.ts +1 -1
  67. package/dist/cores/lambda/lambda-cron-handler.js +14 -23
  68. package/dist/cores/lambda/lambda-cron-handler.js.map +1 -1
  69. package/dist/cores/lambda/lambda-dynamo-stream-handler.d.ts +2 -2
  70. package/dist/cores/lambda/lambda-dynamo-stream-handler.js +57 -67
  71. package/dist/cores/lambda/lambda-dynamo-stream-handler.js.map +1 -1
  72. package/dist/cores/lambda/lambda-handler.d.ts +22 -22
  73. package/dist/cores/lambda/lambda-handler.js +93 -106
  74. package/dist/cores/lambda/lambda-handler.js.map +1 -1
  75. package/dist/cores/lambda/lambda-notification-handler.d.ts +1 -1
  76. package/dist/cores/lambda/lambda-notification-handler.js +39 -50
  77. package/dist/cores/lambda/lambda-notification-handler.js.map +1 -1
  78. package/dist/cores/lambda/lambda-sns-handler.js +79 -88
  79. package/dist/cores/lambda/lambda-sns-handler.js.map +1 -1
  80. package/dist/cores/lambda/lambda-sqs-handler.js +79 -88
  81. package/dist/cores/lambda/lambda-sqs-handler.js.map +1 -1
  82. package/dist/cores/lambda/lambda-web-handler.d.ts +6 -6
  83. package/dist/cores/lambda/lambda-web-handler.js +387 -412
  84. package/dist/cores/lambda/lambda-web-handler.js.map +1 -1
  85. package/dist/cores/lambda/lambda-wss-handler.js +23 -32
  86. package/dist/cores/lambda/lambda-wss-handler.js.map +1 -1
  87. package/dist/cores/protocol/index.js +10 -20
  88. package/dist/cores/protocol/index.js.map +1 -1
  89. package/dist/cores/protocol/protocol-service.d.ts +3 -3
  90. package/dist/cores/protocol/protocol-service.js +235 -227
  91. package/dist/cores/protocol/protocol-service.js.map +1 -1
  92. package/dist/cores/storage/http-storage-service.js +65 -85
  93. package/dist/cores/storage/http-storage-service.js.map +1 -1
  94. package/dist/cores/storage/model-manager.js +66 -85
  95. package/dist/cores/storage/model-manager.js.map +1 -1
  96. package/dist/cores/storage/proxy-storage-service.d.ts +2 -2
  97. package/dist/cores/storage/proxy-storage-service.js +562 -599
  98. package/dist/cores/storage/proxy-storage-service.js.map +1 -1
  99. package/dist/cores/storage/redis-storage-service.js +163 -177
  100. package/dist/cores/storage/redis-storage-service.js.map +1 -1
  101. package/dist/cores/storage/storage-service.js +281 -322
  102. package/dist/cores/storage/storage-service.js.map +1 -1
  103. package/dist/engine/builder.d.ts +1 -1
  104. package/dist/engine/builder.js +59 -63
  105. package/dist/engine/builder.js.map +1 -1
  106. package/dist/engine/engine.d.ts +4 -4
  107. package/dist/engine/engine.js +9 -18
  108. package/dist/engine/engine.js.map +1 -1
  109. package/dist/engine/types.d.ts +1 -1
  110. package/dist/engine/utilities.d.ts +5 -5
  111. package/dist/engine/utilities.js +301 -293
  112. package/dist/engine/utilities.js.map +1 -1
  113. package/dist/environ.js +4 -6
  114. package/dist/environ.js.map +1 -1
  115. package/dist/extended/abstract-service.js +595 -645
  116. package/dist/extended/abstract-service.js.map +1 -1
  117. package/dist/extended/libs/sig-v4.js.map +1 -1
  118. package/dist/generated/field-registry.d.ts +10 -0
  119. package/dist/generated/field-registry.js +17 -0
  120. package/dist/generated/field-registry.js.map +1 -0
  121. package/dist/helpers/helpers.d.ts +17 -9
  122. package/dist/helpers/helpers.js +88 -78
  123. package/dist/helpers/helpers.js.map +1 -1
  124. package/dist/index.js +17 -7
  125. package/dist/index.js.map +1 -1
  126. package/dist/lib/dynamodb-value.js +2 -3
  127. package/dist/lib/dynamodb-value.js.map +1 -1
  128. package/dist/tools/express.js +4 -5
  129. package/dist/tools/express.js.map +1 -1
  130. package/dist/tools/tools.d.ts +3 -1
  131. package/dist/tools/tools.js +14 -21
  132. package/dist/tools/tools.js.map +1 -1
  133. package/package.json +19 -18
  134. package/dist/exec-cli.d.ts +0 -2
  135. package/dist/exec-cli.js +0 -211
  136. package/dist/exec-cli.js.map +0 -1
  137. package/dist/lib/dynamo/expressions.d.ts +0 -14
  138. package/dist/lib/dynamo/expressions.js +0 -212
  139. package/dist/lib/dynamo/expressions.js.map +0 -1
  140. package/dist/lib/dynamo/query.d.ts +0 -43
  141. package/dist/lib/dynamo/query.js +0 -246
  142. package/dist/lib/dynamo/query.js.map +0 -1
  143. package/dist/lib/dynamo/scan.d.ts +0 -33
  144. package/dist/lib/dynamo/scan.js +0 -172
  145. package/dist/lib/dynamo/scan.js.map +0 -1
  146. package/dist/lib/dynamo/serializer.d.ts +0 -12
  147. package/dist/lib/dynamo/serializer.js +0 -243
  148. package/dist/lib/dynamo/serializer.js.map +0 -1
  149. package/dist/lib/dynamo/utils.d.ts +0 -15
  150. package/dist/lib/dynamo/utils.js +0 -129
  151. package/dist/lib/dynamo/utils.js.map +0 -1
  152. package/dist/tools/shared.d.ts +0 -28
  153. package/dist/tools/shared.js +0 -143
  154. package/dist/tools/shared.js.map +0 -1
@@ -1,13 +1,4 @@
1
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
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.DummyStorageService = exports.DynamoStorageService = void 0;
13
4
  /**
@@ -39,16 +30,11 @@ const clearDuplicated = (arr) => arr.sort().reduce((L, val) => {
39
30
  * - service via DynamoDB with id + json data.
40
31
  */
41
32
  class DynamoStorageService {
33
+ _table; // target table-name
34
+ _idName; // target table-name
35
+ _fields; // fields set.
36
+ $dynamo;
42
37
  constructor(table, fields, idName = 'id', idType = 'string') {
43
- /**
44
- * say hello()
45
- * @param name (optional) given name
46
- */
47
- this.hello = () => `dynamo-storage-service:${this._table}/${this._idName}/${this._fields.length}`;
48
- /**
49
- * (extended) get copy of fields.
50
- */
51
- this.fields = () => [...this._fields];
52
38
  if (!table)
53
39
  throw new Error(`@table (table-name) is required!`);
54
40
  this._table = table;
@@ -56,52 +42,56 @@ class DynamoStorageService {
56
42
  this._fields = clearDuplicated(['id', 'type', 'stereo', 'meta', idName].concat(fields));
57
43
  this.$dynamo = new dynamo_1.DynamoService({ tableName: this._table, idName, idType });
58
44
  }
45
+ /**
46
+ * say hello()
47
+ * @param name (optional) given name
48
+ */
49
+ hello = () => `dynamo-storage-service:${this._table}/${this._idName}/${this._fields.length}`;
50
+ /**
51
+ * (extended) get copy of fields.
52
+ */
53
+ fields = () => [...this._fields];
59
54
  /**
60
55
  * Read whole model via database.
61
56
  *
62
57
  * @param id id
63
58
  */
64
- read(id) {
65
- return __awaiter(this, void 0, void 0, function* () {
66
- const data = yield this.$dynamo.readItem(id);
67
- const fields = this._fields || [];
68
- const item = fields.reduce((N, key) => {
69
- const val = data[key];
70
- if (val !== undefined)
71
- N[key] = val;
72
- return N;
73
- }, {});
74
- return item;
75
- });
59
+ async read(id) {
60
+ const data = await this.$dynamo.readItem(id);
61
+ const fields = this._fields || [];
62
+ const item = fields.reduce((N, key) => {
63
+ const val = data[key];
64
+ if (val !== undefined)
65
+ N[key] = val;
66
+ return N;
67
+ }, {});
68
+ return item;
76
69
  }
77
70
  /**
78
71
  * Read multiple models via database.
79
72
  *
80
73
  * @param ids ids
81
74
  */
82
- mread(ids) {
83
- var _a;
84
- return __awaiter(this, void 0, void 0, function* () {
85
- const total = (_a = ids === null || ids === void 0 ? void 0 : ids.length) !== null && _a !== void 0 ? _a : 0;
86
- const result = { success: [], failed: [], total };
87
- if (!ids || total === 0)
88
- return result;
89
- const list = ids.map(id => ({ id }));
90
- const res = yield this.$dynamo.mreadItem(list);
91
- const fields = this._fields || [];
92
- const toModel = (data) => fields.reduce((N, key) => {
93
- const val = data[key];
94
- if (val !== undefined)
95
- N[key] = val;
96
- return N;
97
- }, {});
98
- result.success = (res.success || []).map(toModel);
99
- result.failed = (res.failed || []).map(item => {
100
- const model = toModel(item);
101
- return Object.assign(Object.assign({}, model), { error: item.error || 'Unknown error' });
102
- });
75
+ async mread(ids) {
76
+ const total = ids?.length ?? 0;
77
+ const result = { success: [], failed: [], total };
78
+ if (!ids || total === 0)
103
79
  return result;
80
+ const list = ids.map(id => ({ id }));
81
+ const res = await this.$dynamo.mreadItem(list);
82
+ const fields = this._fields || [];
83
+ const toModel = (data) => fields.reduce((N, key) => {
84
+ const val = data[key];
85
+ if (val !== undefined)
86
+ N[key] = val;
87
+ return N;
88
+ }, {});
89
+ result.success = (res.success || []).map(toModel);
90
+ result.failed = (res.failed || []).map(item => {
91
+ const model = toModel(item);
92
+ return { ...model, error: item.error || 'Unknown error' };
104
93
  });
94
+ return result;
105
95
  }
106
96
  /**
107
97
  * auto-create if not found.
@@ -109,13 +99,11 @@ class DynamoStorageService {
109
99
  * @param id
110
100
  * @param model
111
101
  */
112
- readOrCreate(id, model) {
113
- return __awaiter(this, void 0, void 0, function* () {
114
- return this.read(id).catch((e) => {
115
- if (`${e.message}`.startsWith('404 NOT FOUND'))
116
- return this.update(id, model);
117
- throw e;
118
- });
102
+ async readOrCreate(id, model) {
103
+ return this.read(id).catch((e) => {
104
+ if (`${e.message}`.startsWith('404 NOT FOUND'))
105
+ return this.update(id, model);
106
+ throw e;
119
107
  });
120
108
  }
121
109
  /**
@@ -124,19 +112,17 @@ class DynamoStorageService {
124
112
  * @param id id
125
113
  * @param model whole object.
126
114
  */
127
- save(id, model) {
128
- return __awaiter(this, void 0, void 0, function* () {
129
- const fields = this._fields || [];
130
- const data = fields.reduce((N, key) => {
131
- const val = model[key];
132
- if (val !== undefined)
133
- N[key] = val;
134
- return N;
135
- }, {});
136
- yield this.$dynamo.saveItem(id, data); // must be `{}`
137
- const item = Object.assign({ [this._idName]: id }, data);
138
- return item;
139
- });
115
+ async save(id, model) {
116
+ const fields = this._fields || [];
117
+ const data = fields.reduce((N, key) => {
118
+ const val = model[key];
119
+ if (val !== undefined)
120
+ N[key] = val;
121
+ return N;
122
+ }, {});
123
+ await this.$dynamo.saveItem(id, data); // must be `{}`
124
+ const item = Object.assign({ [this._idName]: id }, data);
125
+ return item;
140
126
  }
141
127
  /**
142
128
  * update some attributes
@@ -145,58 +131,53 @@ class DynamoStorageService {
145
131
  * @param model attributes to update
146
132
  * @param incrementals (optional) attributes to increment
147
133
  */
148
- update(id, model, incrementals) {
149
- return __awaiter(this, void 0, void 0, function* () {
150
- const fields = this._fields || [];
151
- const $U = fields.reduce((N, key) => {
152
- const val = model[key];
153
- if (val !== undefined)
154
- N[key] = val;
155
- return N;
156
- }, {});
157
- /* eslint-disable prettier/prettier */
158
- const $I = !incrementals ? null : Object.keys(incrementals).reduce((M, key) => {
159
- const val = incrementals[key];
160
- if (typeof val !== 'number')
161
- throw new Error(`.${key} (${val}) should be number!`);
162
- M[key] = val;
163
- return M;
164
- }, {});
165
- /* eslint-enable prettier/prettier */
166
- const ret = yield this.$dynamo.updateItem(id, undefined, $U, $I);
167
- return ret;
168
- });
134
+ async update(id, model, incrementals) {
135
+ const fields = this._fields || [];
136
+ const $U = fields.reduce((N, key) => {
137
+ const val = model[key];
138
+ if (val !== undefined)
139
+ N[key] = val;
140
+ return N;
141
+ }, {});
142
+ /* eslint-disable prettier/prettier */
143
+ const $I = !incrementals ? null : Object.keys(incrementals).reduce((M, key) => {
144
+ const val = incrementals[key];
145
+ if (typeof val !== 'number')
146
+ throw new Error(`.${key} (${val}) should be number!`);
147
+ M[key] = val;
148
+ return M;
149
+ }, {});
150
+ /* eslint-enable prettier/prettier */
151
+ const ret = await this.$dynamo.updateItem(id, undefined, $U, $I);
152
+ return ret;
169
153
  }
170
154
  /**
171
155
  * update multiple models
172
156
  *
173
157
  * @param list list of models (should include id)
174
158
  */
175
- mupdate(list) {
176
- var _a;
177
- return __awaiter(this, void 0, void 0, function* () {
178
- const total = (_a = list === null || list === void 0 ? void 0 : list.length) !== null && _a !== void 0 ? _a : 0;
179
- const result = { success: [], failed: [], total };
180
- if (!list || total === 0)
181
- return result;
182
- const fields = this._fields || [];
183
- const items = list.map((item) => {
184
- const _id = item[this._idName] || item.id;
185
- if (!_id)
186
- throw new Error('@id is required!');
187
- const data = fields.reduce((N, key) => {
188
- const val = item[key];
189
- if (val !== undefined)
190
- N[key] = val;
191
- return N;
192
- }, {});
193
- return Object.assign({ id: `${_id}` }, data);
194
- });
195
- const res = yield this.$dynamo.mupdateItem(items);
196
- result.success = res.success || [];
197
- result.failed = res.failed || [];
159
+ async mupdate(list) {
160
+ const total = list?.length ?? 0;
161
+ const result = { success: [], failed: [], total };
162
+ if (!list || total === 0)
198
163
  return result;
164
+ const fields = this._fields || [];
165
+ const items = list.map((item) => {
166
+ const _id = item[this._idName] || item.id;
167
+ if (!_id)
168
+ throw new Error('@id is required!');
169
+ const data = fields.reduce((N, key) => {
170
+ const val = item[key];
171
+ if (val !== undefined)
172
+ N[key] = val;
173
+ return N;
174
+ }, {});
175
+ return { id: `${_id}`, ...data };
199
176
  });
177
+ const res = await this.$dynamo.mupdateItem(items);
178
+ result.success = res.success || [];
179
+ result.failed = res.failed || [];
180
+ return result;
200
181
  }
201
182
  /**
202
183
  * increment number attribute (or overwrite string field)
@@ -206,42 +187,40 @@ class DynamoStorageService {
206
187
  * @param model attributes of number.
207
188
  * @param $update (optional) update-set.
208
189
  */
209
- increment(id, model, $update) {
210
- return __awaiter(this, void 0, void 0, function* () {
211
- if (!model && !$update)
212
- throw new Error('@item is required!');
213
- const $org = yield this.read(id).catch(e => {
214
- if (`${e.message || e}`.startsWith('404 NOT FOUND'))
215
- return { id };
216
- throw e;
217
- });
218
- const fields = this._fields || [];
219
- const $U = fields.reduce((N, key) => {
220
- const val = $update ? $update[key] : undefined;
221
- if (val !== undefined)
222
- N[key] = val;
223
- return N;
224
- }, {});
225
- const $I = fields.reduce((N, key) => {
226
- const val = model[key];
227
- if (val !== undefined) {
228
- const org = $org[key];
229
- //* check type matched!
230
- if (org !== undefined && typeof org === 'number' && typeof val !== 'number')
231
- throw new Error(`.${key} (${val}) should be number!`);
232
- //* if not exists, update it.
233
- if (org === undefined && typeof val === 'number')
234
- N[key] = val;
235
- else if (typeof val !== 'number' && !Array.isArray(val))
236
- $U[key] = val;
237
- else
238
- N[key] = val;
239
- }
240
- return N;
241
- }, {});
242
- const ret = yield this.$dynamo.updateItem(id, undefined, $U, $I);
243
- return ret;
190
+ async increment(id, model, $update) {
191
+ if (!model && !$update)
192
+ throw new Error('@item is required!');
193
+ const $org = await this.read(id).catch(e => {
194
+ if (`${e.message || e}`.startsWith('404 NOT FOUND'))
195
+ return { id };
196
+ throw e;
244
197
  });
198
+ const fields = this._fields || [];
199
+ const $U = fields.reduce((N, key) => {
200
+ const val = $update ? $update[key] : undefined;
201
+ if (val !== undefined)
202
+ N[key] = val;
203
+ return N;
204
+ }, {});
205
+ const $I = fields.reduce((N, key) => {
206
+ const val = model[key];
207
+ if (val !== undefined) {
208
+ const org = $org[key];
209
+ //* check type matched!
210
+ if (org !== undefined && typeof org === 'number' && typeof val !== 'number')
211
+ throw new Error(`.${key} (${val}) should be number!`);
212
+ //* if not exists, update it.
213
+ if (org === undefined && typeof val === 'number')
214
+ N[key] = val;
215
+ else if (typeof val !== 'number' && !Array.isArray(val))
216
+ $U[key] = val;
217
+ else
218
+ N[key] = val;
219
+ }
220
+ return N;
221
+ }, {});
222
+ const ret = await this.$dynamo.updateItem(id, undefined, $U, $I);
223
+ return ret;
245
224
  }
246
225
  /**
247
226
  * delete set.
@@ -249,12 +228,10 @@ class DynamoStorageService {
249
228
  *
250
229
  * @param id id
251
230
  */
252
- delete(id) {
253
- return __awaiter(this, void 0, void 0, function* () {
254
- const $org = yield this.read(id);
255
- yield this.$dynamo.deleteItem(id);
256
- return $org;
257
- });
231
+ async delete(id) {
232
+ const $org = await this.read(id);
233
+ await this.$dynamo.deleteItem(id);
234
+ return $org;
258
235
  }
259
236
  }
260
237
  exports.DynamoStorageService = DynamoStorageService;
@@ -269,14 +246,9 @@ exports.DynamoStorageService = DynamoStorageService;
269
246
  * - this dummy service should be replaceable with real service `DynamoStorageService`
270
247
  */
271
248
  class DummyStorageService {
249
+ name;
250
+ idName;
272
251
  constructor(dataFile, name = 'memory', idName) {
273
- this.buffer = {};
274
- /**
275
- * say hello()
276
- * @param name (optional) given name
277
- */
278
- this.hello = () => `dummy-storage-service:${this.name}/${this.idName}`;
279
- this.$locks = {}; //* only for lock.
280
252
  (0, engine_1._log)(NS, `DummyStorageService(${dataFile || ''})...`);
281
253
  if (!dataFile)
282
254
  throw new Error('@dataFile(string) is required!');
@@ -286,6 +258,7 @@ class DummyStorageService {
286
258
  const dummy = (0, tools_1.loadDataYml)(dataFile);
287
259
  this.load(dummy.data);
288
260
  }
261
+ buffer = {};
289
262
  load(data) {
290
263
  if (!data || !Array.isArray(data))
291
264
  throw new Error('@data should be array!');
@@ -294,183 +267,169 @@ class DummyStorageService {
294
267
  this.buffer[id] = item;
295
268
  });
296
269
  }
297
- read(id) {
298
- return __awaiter(this, void 0, void 0, function* () {
299
- if (!id.trim())
270
+ /**
271
+ * say hello()
272
+ * @param name (optional) given name
273
+ */
274
+ hello = () => `dummy-storage-service:${this.name}/${this.idName}`;
275
+ async read(id) {
276
+ if (!id.trim())
277
+ throw new Error('@id (string) is required!');
278
+ const item = this.buffer[id];
279
+ if (!item)
280
+ throw new Error(`404 NOT FOUND - ${this.idName}:${id}`);
281
+ return { ...item, [this.idName]: id };
282
+ }
283
+ async mread(ids) {
284
+ const total = ids?.length ?? 0;
285
+ const result = { success: [], failed: [], total };
286
+ if (!ids || total === 0)
287
+ return result;
288
+ for (const id of ids) {
289
+ if (!id || !`${id}`.trim())
300
290
  throw new Error('@id (string) is required!');
301
291
  const item = this.buffer[id];
302
- if (!item)
303
- throw new Error(`404 NOT FOUND - ${this.idName}:${id}`);
304
- return Object.assign(Object.assign({}, item), { [this.idName]: id });
305
- });
292
+ if (!item) {
293
+ result.failed.push({ [this.idName]: id, error: '404 NOT FOUND' });
294
+ }
295
+ else {
296
+ result.success.push({ ...item, [this.idName]: id });
297
+ }
298
+ }
299
+ return result;
306
300
  }
307
- mread(ids) {
308
- var _a;
309
- return __awaiter(this, void 0, void 0, function* () {
310
- const total = (_a = ids === null || ids === void 0 ? void 0 : ids.length) !== null && _a !== void 0 ? _a : 0;
311
- const result = { success: [], failed: [], total };
312
- if (!ids || total === 0)
313
- return result;
314
- for (const id of ids) {
315
- if (!id || !`${id}`.trim())
316
- throw new Error('@id (string) is required!');
317
- const item = this.buffer[id];
318
- if (!item) {
319
- result.failed.push({ [this.idName]: id, error: '404 NOT FOUND' });
320
- }
321
- else {
322
- result.success.push(Object.assign(Object.assign({}, item), { [this.idName]: id }));
323
- }
301
+ async readSafe(id) {
302
+ return this.read(id).catch(e => {
303
+ if (`${e.message || e}`.startsWith('404 NOT FOUND')) {
304
+ const $org = { [this.idName]: id };
305
+ return $org;
324
306
  }
325
- return result;
307
+ throw e;
326
308
  });
327
309
  }
328
- readSafe(id) {
329
- return __awaiter(this, void 0, void 0, function* () {
330
- return this.read(id).catch(e => {
331
- if (`${e.message || e}`.startsWith('404 NOT FOUND')) {
332
- const $org = { [this.idName]: id };
333
- return $org;
334
- }
335
- throw e;
336
- });
310
+ async readOrCreate(id, model) {
311
+ return this.read(id).catch((e) => {
312
+ if (`${e.message}`.startsWith('404 NOT FOUND'))
313
+ return this.update(id, model);
314
+ throw e;
337
315
  });
338
316
  }
339
- readOrCreate(id, model) {
340
- return __awaiter(this, void 0, void 0, function* () {
341
- return this.read(id).catch((e) => {
342
- if (`${e.message}`.startsWith('404 NOT FOUND'))
343
- return this.update(id, model);
344
- throw e;
345
- });
346
- });
317
+ async save(id, item) {
318
+ if (!id)
319
+ throw new Error('@id is required!');
320
+ if (!item)
321
+ throw new Error('@item is required!');
322
+ if (item && typeof item.lock == 'number')
323
+ this.$locks[id] = item.lock;
324
+ this.buffer[id] = item;
325
+ return Object.assign({ [this.idName]: id }, item);
347
326
  }
348
- save(id, item) {
349
- return __awaiter(this, void 0, void 0, function* () {
350
- if (!id)
351
- throw new Error('@id is required!');
352
- if (!item)
353
- throw new Error('@item is required!');
327
+ $locks = {}; //* only for lock.
328
+ async update(id, item, $inc) {
329
+ if (!id)
330
+ throw new Error('@id is required!');
331
+ if (!item)
332
+ throw new Error('@item is required!');
333
+ //* atomic operation for `.lock`
334
+ const lock = (() => {
335
+ let lock = 0;
354
336
  if (item && typeof item.lock == 'number')
355
- this.$locks[id] = item.lock;
356
- this.buffer[id] = item;
357
- return Object.assign({ [this.idName]: id }, item);
358
- });
337
+ this.$locks[id] = lock = item.lock;
338
+ if ($inc && typeof $inc.lock == 'number')
339
+ this.$locks[id] = lock = $inc.lock + engine_1.$U.N(this.$locks[id], 0);
340
+ return lock;
341
+ })();
342
+ const $org = await this.readSafe(id);
343
+ const $new = Object.assign($org, item);
344
+ /* eslint-disable prettier/prettier */
345
+ const incremented = !$inc ? null : Object.keys($inc).reduce((M, key) => {
346
+ const val = $inc[key];
347
+ if (typeof val !== 'number')
348
+ throw new Error(`.${key} (${val}) should be number!`);
349
+ if (key == 'lock') {
350
+ M[key] = lock;
351
+ }
352
+ else {
353
+ M[key] = engine_1.$U.N($new[key], 0) + val;
354
+ }
355
+ return M;
356
+ }, {});
357
+ if (incremented)
358
+ Object.assign($new, incremented);
359
+ /* eslint-enable prettier/prettier */
360
+ await this.save(id, $new);
361
+ const $set = { ...item, ...incremented };
362
+ if (typeof $set.lock == 'number')
363
+ $set.lock = lock;
364
+ return Object.assign({ [this.idName]: id }, $set);
359
365
  }
360
- update(id, item, $inc) {
361
- return __awaiter(this, void 0, void 0, function* () {
362
- if (!id)
366
+ async mupdate(list) {
367
+ const total = list?.length ?? 0;
368
+ const result = { success: [], failed: [], total };
369
+ if (!list || total === 0)
370
+ return result;
371
+ for (const item of list) {
372
+ const _id = item[this.idName] || item.id;
373
+ if (!_id)
363
374
  throw new Error('@id is required!');
364
- if (!item)
365
- throw new Error('@item is required!');
366
- //* atomic operation for `.lock`
367
- const lock = (() => {
368
- let lock = 0;
369
- if (item && typeof item.lock == 'number')
370
- this.$locks[id] = lock = item.lock;
371
- if ($inc && typeof $inc.lock == 'number')
372
- this.$locks[id] = lock = $inc.lock + engine_1.$U.N(this.$locks[id], 0);
373
- return lock;
374
- })();
375
- const $org = yield this.readSafe(id);
376
- const $new = Object.assign($org, item);
377
- /* eslint-disable prettier/prettier */
378
- const incremented = !$inc ? null : Object.keys($inc).reduce((M, key) => {
379
- const val = $inc[key];
380
- if (typeof val !== 'number')
375
+ if (item && typeof item.lock == 'number')
376
+ this.$locks[_id] = item.lock;
377
+ this.buffer[_id] = item;
378
+ result.success.push({ ...item, [this.idName]: _id });
379
+ }
380
+ return result;
381
+ }
382
+ async increment(id, $inc, $upt) {
383
+ if (!id)
384
+ throw new Error('@id is required!');
385
+ if (!$inc && !$upt)
386
+ throw new Error('@item is required!');
387
+ //* atomic operation for `.lock`
388
+ const lock = (() => {
389
+ let lock = 0;
390
+ if ($upt && typeof $upt.lock == 'number')
391
+ this.$locks[id] = lock = $upt.lock;
392
+ if ($inc && typeof $inc.lock == 'number')
393
+ this.$locks[id] = lock = $inc.lock + engine_1.$U.N(this.$locks[id], 0);
394
+ return lock;
395
+ })();
396
+ const $org = await this.readSafe(id);
397
+ const $set = Object.keys($inc)
398
+ .concat(Object.keys($upt || {}))
399
+ .reduce((N, key) => {
400
+ const val = $inc ? $inc[key] : undefined;
401
+ const upt = $upt ? $upt[key] : undefined;
402
+ const org = $org[key];
403
+ if (upt !== undefined) {
404
+ N[key] = upt;
405
+ }
406
+ else if (val !== undefined) {
407
+ if (org !== undefined && typeof org === 'number' && typeof val !== 'number')
381
408
  throw new Error(`.${key} (${val}) should be number!`);
382
- if (key == 'lock') {
383
- M[key] = lock;
409
+ if (typeof val !== 'number') {
410
+ N[key] = val;
411
+ }
412
+ else if (key == 'lock') {
413
+ N[key] = lock;
414
+ $org[key] = lock;
384
415
  }
385
416
  else {
386
- M[key] = engine_1.$U.N($new[key], 0) + val;
417
+ N[key] = (org === undefined ? 0 : org) + val;
418
+ $org[key] = (org === undefined ? 0 : org) + val;
387
419
  }
388
- return M;
389
- }, {});
390
- if (incremented)
391
- Object.assign($new, incremented);
392
- /* eslint-enable prettier/prettier */
393
- yield this.save(id, $new);
394
- const $set = Object.assign(Object.assign({}, item), incremented);
395
- if (typeof $set.lock == 'number')
396
- $set.lock = lock;
397
- return Object.assign({ [this.idName]: id }, $set);
398
- });
399
- }
400
- mupdate(list) {
401
- var _a;
402
- return __awaiter(this, void 0, void 0, function* () {
403
- const total = (_a = list === null || list === void 0 ? void 0 : list.length) !== null && _a !== void 0 ? _a : 0;
404
- const result = { success: [], failed: [], total };
405
- if (!list || total === 0)
406
- return result;
407
- for (const item of list) {
408
- const _id = item[this.idName] || item.id;
409
- if (!_id)
410
- throw new Error('@id is required!');
411
- if (item && typeof item.lock == 'number')
412
- this.$locks[_id] = item.lock;
413
- this.buffer[_id] = item;
414
- result.success.push(Object.assign(Object.assign({}, item), { [this.idName]: _id }));
415
420
  }
416
- return result;
417
- });
418
- }
419
- increment(id, $inc, $upt) {
420
- return __awaiter(this, void 0, void 0, function* () {
421
- if (!id)
422
- throw new Error('@id is required!');
423
- if (!$inc && !$upt)
424
- throw new Error('@item is required!');
425
- //* atomic operation for `.lock`
426
- const lock = (() => {
427
- let lock = 0;
428
- if ($upt && typeof $upt.lock == 'number')
429
- this.$locks[id] = lock = $upt.lock;
430
- if ($inc && typeof $inc.lock == 'number')
431
- this.$locks[id] = lock = $inc.lock + engine_1.$U.N(this.$locks[id], 0);
432
- return lock;
433
- })();
434
- const $org = yield this.readSafe(id);
435
- const $set = Object.keys($inc)
436
- .concat(Object.keys($upt || {}))
437
- .reduce((N, key) => {
438
- const val = $inc ? $inc[key] : undefined;
439
- const upt = $upt ? $upt[key] : undefined;
440
- const org = $org[key];
441
- if (upt !== undefined) {
442
- N[key] = upt;
443
- }
444
- else if (val !== undefined) {
445
- if (org !== undefined && typeof org === 'number' && typeof val !== 'number')
446
- throw new Error(`.${key} (${val}) should be number!`);
447
- if (typeof val !== 'number') {
448
- N[key] = val;
449
- }
450
- else if (key == 'lock') {
451
- N[key] = lock;
452
- $org[key] = lock;
453
- }
454
- else {
455
- N[key] = (org === undefined ? 0 : org) + val;
456
- $org[key] = (org === undefined ? 0 : org) + val;
457
- }
458
- }
459
- return N;
460
- }, {});
461
- if (typeof $set.lock == 'number')
462
- $set.lock = lock;
463
- yield this.save(id, Object.assign($org, $set));
464
- return Object.assign({ [this.idName]: id }, $set);
465
- });
421
+ return N;
422
+ }, {});
423
+ if (typeof $set.lock == 'number')
424
+ $set.lock = lock;
425
+ await this.save(id, Object.assign($org, $set));
426
+ return Object.assign({ [this.idName]: id }, $set);
466
427
  }
467
- delete(id) {
468
- return __awaiter(this, void 0, void 0, function* () {
469
- const $org = yield this.read(id);
470
- delete this.buffer[id];
471
- delete this.$locks[id];
472
- return $org;
473
- });
428
+ async delete(id) {
429
+ const $org = await this.read(id);
430
+ delete this.buffer[id];
431
+ delete this.$locks[id];
432
+ return $org;
474
433
  }
475
434
  }
476
435
  exports.DummyStorageService = DummyStorageService;