lemon-core 4.1.14 → 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.
- package/dist/common/test-helper.d.ts +2 -2
- package/dist/common/test-helper.js +24 -26
- package/dist/common/test-helper.js.map +1 -1
- package/dist/controllers/dummy-controller.js +39 -46
- package/dist/controllers/dummy-controller.js.map +1 -1
- package/dist/controllers/general-api-controller.js +95 -100
- package/dist/controllers/general-api-controller.js.map +1 -1
- package/dist/controllers/general-controller.js +81 -82
- package/dist/controllers/general-controller.js.map +1 -1
- package/dist/cores/api/api-service.d.ts +1 -1
- package/dist/cores/api/api-service.js +228 -269
- package/dist/cores/api/api-service.js.map +1 -1
- package/dist/cores/aws/aws-kms-service.d.ts +1 -2
- package/dist/cores/aws/aws-kms-service.js +143 -153
- package/dist/cores/aws/aws-kms-service.js.map +1 -1
- package/dist/cores/aws/aws-s3-service.d.ts +2 -4
- package/dist/cores/aws/aws-s3-service.js +306 -330
- package/dist/cores/aws/aws-s3-service.js.map +1 -1
- package/dist/cores/aws/aws-sns-service.js +147 -153
- package/dist/cores/aws/aws-sns-service.js.map +1 -1
- package/dist/cores/aws/aws-sqs-service.js +149 -170
- package/dist/cores/aws/aws-sqs-service.js.map +1 -1
- package/dist/cores/aws/index.js +10 -20
- package/dist/cores/aws/index.js.map +1 -1
- package/dist/cores/cache/cache-service.d.ts +2 -2
- package/dist/cores/cache/cache-service.js +435 -499
- package/dist/cores/cache/cache-service.js.map +1 -1
- package/dist/cores/config/config-service.d.ts +1 -1
- package/dist/cores/config/config-service.js +56 -63
- package/dist/cores/config/config-service.js.map +1 -1
- package/dist/cores/config/index.js +14 -24
- package/dist/cores/config/index.js.map +1 -1
- package/dist/cores/core-services.d.ts +1 -1
- package/dist/cores/dynamo/dynamo-query-service.js +37 -51
- package/dist/cores/dynamo/dynamo-query-service.js.map +1 -1
- package/dist/cores/dynamo/dynamo-scan-service.d.ts +2 -2
- package/dist/cores/dynamo/dynamo-scan-service.js +29 -40
- package/dist/cores/dynamo/dynamo-scan-service.js.map +1 -1
- package/dist/cores/dynamo/dynamo-service.d.ts +2 -2
- package/dist/cores/dynamo/dynamo-service.js +528 -601
- package/dist/cores/dynamo/dynamo-service.js.map +1 -1
- package/dist/cores/dynamo/tools/expressions.js +17 -7
- package/dist/cores/dynamo/tools/expressions.js.map +1 -1
- package/dist/cores/dynamo/tools/query.js +142 -127
- package/dist/cores/dynamo/tools/query.js.map +1 -1
- package/dist/cores/dynamo/tools/scan.js +111 -97
- package/dist/cores/dynamo/tools/scan.js.map +1 -1
- package/dist/cores/dynamo/tools/serializer.js +17 -7
- package/dist/cores/dynamo/tools/serializer.js.map +1 -1
- package/dist/cores/dynamo/tools/utils.d.ts +0 -2
- package/dist/cores/dynamo/tools/utils.js.map +1 -1
- package/dist/cores/elastic/elastic6-query-service.js +307 -324
- package/dist/cores/elastic/elastic6-query-service.js.map +1 -1
- package/dist/cores/elastic/elastic6-service.d.ts +3 -3
- package/dist/cores/elastic/elastic6-service.js +568 -647
- package/dist/cores/elastic/elastic6-service.js.map +1 -1
- package/dist/cores/elastic/hangul-service.js +52 -54
- package/dist/cores/elastic/hangul-service.js.map +1 -1
- package/dist/cores/lambda/index.js +42 -36
- package/dist/cores/lambda/index.js.map +1 -1
- package/dist/cores/lambda/lambda-alb-handler.d.ts +2 -2
- package/dist/cores/lambda/lambda-alb-handler.js +59 -72
- package/dist/cores/lambda/lambda-alb-handler.js.map +1 -1
- package/dist/cores/lambda/lambda-cognito-handler.js +10 -19
- package/dist/cores/lambda/lambda-cognito-handler.js.map +1 -1
- package/dist/cores/lambda/lambda-cron-handler.d.ts +1 -1
- package/dist/cores/lambda/lambda-cron-handler.js +14 -23
- package/dist/cores/lambda/lambda-cron-handler.js.map +1 -1
- package/dist/cores/lambda/lambda-dynamo-stream-handler.d.ts +2 -2
- package/dist/cores/lambda/lambda-dynamo-stream-handler.js +57 -67
- package/dist/cores/lambda/lambda-dynamo-stream-handler.js.map +1 -1
- package/dist/cores/lambda/lambda-handler.d.ts +22 -22
- package/dist/cores/lambda/lambda-handler.js +93 -106
- package/dist/cores/lambda/lambda-handler.js.map +1 -1
- package/dist/cores/lambda/lambda-notification-handler.d.ts +1 -1
- package/dist/cores/lambda/lambda-notification-handler.js +39 -50
- package/dist/cores/lambda/lambda-notification-handler.js.map +1 -1
- package/dist/cores/lambda/lambda-sns-handler.js +79 -88
- package/dist/cores/lambda/lambda-sns-handler.js.map +1 -1
- package/dist/cores/lambda/lambda-sqs-handler.js +79 -88
- package/dist/cores/lambda/lambda-sqs-handler.js.map +1 -1
- package/dist/cores/lambda/lambda-web-handler.d.ts +21 -43
- package/dist/cores/lambda/lambda-web-handler.js +392 -482
- package/dist/cores/lambda/lambda-web-handler.js.map +1 -1
- package/dist/cores/lambda/lambda-wss-handler.js +23 -32
- package/dist/cores/lambda/lambda-wss-handler.js.map +1 -1
- package/dist/cores/protocol/index.js +10 -20
- package/dist/cores/protocol/index.js.map +1 -1
- package/dist/cores/protocol/protocol-service.d.ts +3 -3
- package/dist/cores/protocol/protocol-service.js +235 -227
- package/dist/cores/protocol/protocol-service.js.map +1 -1
- package/dist/cores/storage/http-storage-service.js +65 -85
- package/dist/cores/storage/http-storage-service.js.map +1 -1
- package/dist/cores/storage/model-manager.js +66 -85
- package/dist/cores/storage/model-manager.js.map +1 -1
- package/dist/cores/storage/proxy-storage-service.d.ts +2 -2
- package/dist/cores/storage/proxy-storage-service.js +562 -599
- package/dist/cores/storage/proxy-storage-service.js.map +1 -1
- package/dist/cores/storage/redis-storage-service.js +163 -177
- package/dist/cores/storage/redis-storage-service.js.map +1 -1
- package/dist/cores/storage/storage-service.js +281 -322
- package/dist/cores/storage/storage-service.js.map +1 -1
- package/dist/engine/builder.d.ts +1 -1
- package/dist/engine/builder.js +59 -63
- package/dist/engine/builder.js.map +1 -1
- package/dist/engine/engine.d.ts +4 -4
- package/dist/engine/engine.js +9 -18
- package/dist/engine/engine.js.map +1 -1
- package/dist/engine/types.d.ts +1 -1
- package/dist/engine/utilities.d.ts +14 -10
- package/dist/engine/utilities.js +301 -280
- package/dist/engine/utilities.js.map +1 -1
- package/dist/environ.js +4 -6
- package/dist/environ.js.map +1 -1
- package/dist/extended/abstract-service.js +595 -645
- package/dist/extended/abstract-service.js.map +1 -1
- package/dist/extended/libs/sig-v4.js.map +1 -1
- package/dist/generated/field-registry.d.ts +10 -0
- package/dist/generated/field-registry.js +17 -0
- package/dist/generated/field-registry.js.map +1 -0
- package/dist/helpers/helpers.d.ts +17 -9
- package/dist/helpers/helpers.js +88 -78
- package/dist/helpers/helpers.js.map +1 -1
- package/dist/index.js +17 -7
- package/dist/index.js.map +1 -1
- package/dist/lib/dynamodb-value.js +2 -3
- package/dist/lib/dynamodb-value.js.map +1 -1
- package/dist/tools/express.js +4 -5
- package/dist/tools/express.js.map +1 -1
- package/dist/tools/tools.d.ts +3 -1
- package/dist/tools/tools.js +14 -21
- package/dist/tools/tools.js.map +1 -1
- package/package.json +19 -18
- package/dist/exec-cli.d.ts +0 -2
- package/dist/exec-cli.js +0 -211
- package/dist/exec-cli.js.map +0 -1
- package/dist/lib/dynamo/expressions.d.ts +0 -14
- package/dist/lib/dynamo/expressions.js +0 -212
- package/dist/lib/dynamo/expressions.js.map +0 -1
- package/dist/lib/dynamo/query.d.ts +0 -43
- package/dist/lib/dynamo/query.js +0 -246
- package/dist/lib/dynamo/query.js.map +0 -1
- package/dist/lib/dynamo/scan.d.ts +0 -33
- package/dist/lib/dynamo/scan.js +0 -172
- package/dist/lib/dynamo/scan.js.map +0 -1
- package/dist/lib/dynamo/serializer.d.ts +0 -12
- package/dist/lib/dynamo/serializer.js +0 -243
- package/dist/lib/dynamo/serializer.js.map +0 -1
- package/dist/lib/dynamo/utils.d.ts +0 -15
- package/dist/lib/dynamo/utils.js +0 -129
- package/dist/lib/dynamo/utils.js.map +0 -1
- package/dist/tools/shared.d.ts +0 -37
- package/dist/tools/shared.js +0 -130
- package/dist/tools/shared.js.map +0 -1
|
@@ -1,32 +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
|
-
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
12
|
-
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
13
|
-
var m = o[Symbol.asyncIterator], i;
|
|
14
|
-
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
15
|
-
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
16
|
-
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
17
|
-
};
|
|
18
|
-
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
|
|
19
|
-
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
|
|
20
|
-
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
21
|
-
var g = generator.apply(thisArg, _arguments || []), i, q = [];
|
|
22
|
-
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
|
|
23
|
-
function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
|
|
24
|
-
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
|
|
25
|
-
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
|
|
26
|
-
function fulfill(value) { resume("next", value); }
|
|
27
|
-
function reject(value) { resume("throw", value); }
|
|
28
|
-
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
|
|
29
|
-
};
|
|
30
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
31
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
32
4
|
};
|
|
@@ -68,21 +40,8 @@ const _S = (v, def = '') => typeof v === 'string' ? v : v === undefined || v ===
|
|
|
68
40
|
* TODO - support `Elastic` and `OpenSearch`
|
|
69
41
|
*/
|
|
70
42
|
class ElasticIndexService {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
* @param options { endpoint, indexName } is required.
|
|
74
|
-
*/
|
|
75
|
-
constructor(options) {
|
|
76
|
-
(0, engine_1._inf)(NS, `ElasticIndexService(${options.indexName}/${options.idName})...`);
|
|
77
|
-
if (!options.endpoint)
|
|
78
|
-
throw new Error('.endpoint (URL) is required');
|
|
79
|
-
if (!options.indexName)
|
|
80
|
-
throw new Error('.indexName (string) is required');
|
|
81
|
-
// default option values: docType='_doc', idName='$id'
|
|
82
|
-
const { client } = ElasticIndexService.instance(options.endpoint);
|
|
83
|
-
this._options = Object.assign({ docType: '_doc', idName: '$id', version: '6.8' }, options);
|
|
84
|
-
this._client = client;
|
|
85
|
-
}
|
|
43
|
+
_options;
|
|
44
|
+
_client;
|
|
86
45
|
/**
|
|
87
46
|
* simple instance maker.
|
|
88
47
|
*
|
|
@@ -103,6 +62,21 @@ class ElasticIndexService {
|
|
|
103
62
|
});
|
|
104
63
|
return { client };
|
|
105
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* default constuctor w/ options.
|
|
67
|
+
* @param options { endpoint, indexName } is required.
|
|
68
|
+
*/
|
|
69
|
+
constructor(options) {
|
|
70
|
+
(0, engine_1._inf)(NS, `ElasticIndexService(${options.indexName}/${options.idName})...`);
|
|
71
|
+
if (!options.endpoint)
|
|
72
|
+
throw new Error('.endpoint (URL) is required');
|
|
73
|
+
if (!options.indexName)
|
|
74
|
+
throw new Error('.indexName (string) is required');
|
|
75
|
+
// default option values: docType='_doc', idName='$id'
|
|
76
|
+
const { client } = ElasticIndexService.instance(options.endpoint);
|
|
77
|
+
this._options = { docType: '_doc', idName: '$id', version: '6.8', ...options };
|
|
78
|
+
this._client = client;
|
|
79
|
+
}
|
|
106
80
|
/**
|
|
107
81
|
* get the client instance.
|
|
108
82
|
*/
|
|
@@ -129,18 +103,21 @@ exports.ElasticIndexService = ElasticIndexService;
|
|
|
129
103
|
* - extends `ElasticIndexService` and adds version-specific implementation
|
|
130
104
|
*/
|
|
131
105
|
class Elastic6Service extends ElasticIndexService {
|
|
106
|
+
// internal field name to store analyzed strings for autocomplete search
|
|
107
|
+
static DECOMPOSED_FIELD = '_decomposed';
|
|
108
|
+
static QWERTY_FIELD = '_qwerty';
|
|
132
109
|
/**
|
|
133
110
|
* default constuctor w/ options.
|
|
134
111
|
* @param options { endpoint, indexName } is required.
|
|
135
112
|
*/
|
|
136
113
|
constructor(options) {
|
|
137
114
|
super(options);
|
|
138
|
-
/**
|
|
139
|
-
* say hello of identity.
|
|
140
|
-
*/
|
|
141
|
-
this.hello = () => `elastic6-service:${this.options.indexName}:${this.options.version}`;
|
|
142
115
|
(0, engine_1._inf)('Elastic6Service', `Elastic6Service(${options.indexName}/${options.idName})...`);
|
|
143
116
|
}
|
|
117
|
+
/**
|
|
118
|
+
* say hello of identity.
|
|
119
|
+
*/
|
|
120
|
+
hello = () => `elastic6-service:${this.options.indexName}:${this.options.version}`;
|
|
144
121
|
/**
|
|
145
122
|
* get isOldES6
|
|
146
123
|
* - used when setting doctype
|
|
@@ -174,45 +151,43 @@ class Elastic6Service extends ElasticIndexService {
|
|
|
174
151
|
*
|
|
175
152
|
* @protected only for internal test.
|
|
176
153
|
*/
|
|
177
|
-
getVersion(options) {
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
154
|
+
async getVersion(options) {
|
|
155
|
+
const isDump = options?.dump ?? false;
|
|
156
|
+
// it consumes about >20ms
|
|
157
|
+
const info = await this.client.info();
|
|
158
|
+
const rootVersion = engine_1.$U.S(info.body.version.number);
|
|
159
|
+
const parsedVersion = this.parseVersion(rootVersion, { throwable: true });
|
|
160
|
+
if (isDump) {
|
|
161
|
+
//* save into `info.json`.
|
|
162
|
+
const description = {
|
|
163
|
+
'!': `${this.parsedVersion?.engine}${this.options.version} client info`,
|
|
164
|
+
...info,
|
|
165
|
+
};
|
|
166
|
+
const filePath = path_1.default.resolve(__dirname, `../../../data/samples/${this.parsedVersion?.engine}${this.options.version}/info.json`);
|
|
167
|
+
await this.saveInfoToFile(description, filePath);
|
|
168
|
+
}
|
|
169
|
+
return parsedVersion;
|
|
193
170
|
}
|
|
194
171
|
/**
|
|
195
172
|
* check whether the service version matches the version provided in the options.
|
|
196
173
|
*
|
|
197
174
|
* @protected only for internal test.
|
|
198
175
|
*/
|
|
199
|
-
executeSelfTest() {
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
};
|
|
215
|
-
});
|
|
176
|
+
async executeSelfTest() {
|
|
177
|
+
// STEP.1 read the parsed-version.
|
|
178
|
+
const optionVersion = this.parsedVersion;
|
|
179
|
+
// STEP.2 get the real version via `getVersion()`
|
|
180
|
+
const rootVersion = await this.getVersion();
|
|
181
|
+
// STEP.3 validate version
|
|
182
|
+
const isEqual = optionVersion.engine === rootVersion.engine &&
|
|
183
|
+
optionVersion.major === rootVersion.major &&
|
|
184
|
+
optionVersion.minor === rootVersion.minor;
|
|
185
|
+
// Return the comparison result
|
|
186
|
+
return {
|
|
187
|
+
isEqual: isEqual,
|
|
188
|
+
optionVersion: optionVersion,
|
|
189
|
+
rootVersion: rootVersion,
|
|
190
|
+
};
|
|
216
191
|
}
|
|
217
192
|
/**
|
|
218
193
|
* parse version according to Semantic Versioning (SemVer) rules.
|
|
@@ -222,18 +197,24 @@ class Elastic6Service extends ElasticIndexService {
|
|
|
222
197
|
* @returns A ParsedVersion object or null if parsing fails and throwable is false.
|
|
223
198
|
*/
|
|
224
199
|
parseVersion(version, options) {
|
|
225
|
-
|
|
226
|
-
const isThrowable = (_a = options === null || options === void 0 ? void 0 : options.throwable) !== null && _a !== void 0 ? _a : true;
|
|
200
|
+
const isThrowable = options?.throwable ?? true;
|
|
227
201
|
if (!version && isThrowable)
|
|
228
202
|
throw new Error(`@version (string) is required!`);
|
|
229
203
|
// RegEx to match Semantic Versioning patterns
|
|
230
|
-
const match = version
|
|
204
|
+
const match = version?.match(/^(\d{1,2})(?:\.(\d{1,2}))?(?:\.(\d{1,2}))?(?:-([a-zA-Z0-9-.]+))?(?:\+([a-zA-Z0-9-.]+))?$/);
|
|
231
205
|
if (!match) {
|
|
232
206
|
if (isThrowable)
|
|
233
207
|
throw new Error(`@version[${version}] is invalid - fail to parse`);
|
|
234
208
|
return null;
|
|
235
209
|
}
|
|
236
|
-
const res =
|
|
210
|
+
const res = {
|
|
211
|
+
engine: engine_1.$U.N(match[1], 10) < 6 ? 'os' : 'es',
|
|
212
|
+
major: engine_1.$U.N(match[1], 10),
|
|
213
|
+
minor: match[2] !== undefined ? engine_1.$U.N(match[2], 10) : 0,
|
|
214
|
+
patch: match[3] !== undefined ? engine_1.$U.N(match[3], 10) : 0,
|
|
215
|
+
...(match[4] !== undefined ? { prerelease: match[4] } : {}),
|
|
216
|
+
...(match[5] !== undefined ? { build: match[5] } : {}),
|
|
217
|
+
};
|
|
237
218
|
return res;
|
|
238
219
|
}
|
|
239
220
|
/**
|
|
@@ -241,234 +222,218 @@ class Elastic6Service extends ElasticIndexService {
|
|
|
241
222
|
* @param info - The information to be saved
|
|
242
223
|
* @param filePath - The file path where should be saved.
|
|
243
224
|
*/
|
|
244
|
-
saveInfoToFile(info, filePath) {
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
fs_1.default.mkdirSync(directory, { recursive: true });
|
|
251
|
-
}
|
|
252
|
-
// write info to file
|
|
253
|
-
fs_1.default.writeFileSync(filePath, JSON.stringify(info, null, 2));
|
|
225
|
+
async saveInfoToFile(info, filePath) {
|
|
226
|
+
try {
|
|
227
|
+
const directory = path_1.default.dirname(filePath);
|
|
228
|
+
// check whether directory exists
|
|
229
|
+
if (!fs_1.default.existsSync(directory)) {
|
|
230
|
+
fs_1.default.mkdirSync(directory, { recursive: true });
|
|
254
231
|
}
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
232
|
+
// write info to file
|
|
233
|
+
fs_1.default.writeFileSync(filePath, JSON.stringify(info, null, 2));
|
|
234
|
+
}
|
|
235
|
+
catch {
|
|
236
|
+
exports.$ERROR.handler('saveIntoFile', e => {
|
|
237
|
+
throw e;
|
|
238
|
+
});
|
|
239
|
+
}
|
|
261
240
|
}
|
|
262
241
|
/**
|
|
263
242
|
* list of index
|
|
264
243
|
*/
|
|
265
|
-
listIndices() {
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
return { list };
|
|
290
|
-
});
|
|
244
|
+
async listIndices() {
|
|
245
|
+
(0, engine_1._log)(NS, `- listIndices()`);
|
|
246
|
+
//* prepare client..
|
|
247
|
+
const client = this.client;
|
|
248
|
+
const res = await client.cat.indices({ format: 'json' });
|
|
249
|
+
(0, engine_1._log)(NS, `> indices =`, engine_1.$U.json(res));
|
|
250
|
+
// eslint-disable-next-line prettier/prettier
|
|
251
|
+
const list0 = Array.isArray(res) ? res : res?.body && Array.isArray(res?.body) ? res?.body : null;
|
|
252
|
+
if (!list0)
|
|
253
|
+
throw new Error(`@result<${typeof res}> is invalid - ${engine_1.$U.json(res)}!`);
|
|
254
|
+
// {"docs.count": "84", "docs.deleted": "7", "health": "green", "index": "dev-eureka-alarms-v1", "pri": "5", "pri.store.size": "234.3kb", "rep": "1", "status": "open", "store.size": "468.6kb", "uuid": "xPp-Sx86SgmhAWxT3cGAFw"}
|
|
255
|
+
const list = list0.map(N => ({
|
|
256
|
+
pri: engine_1.$U.N(N['pri']),
|
|
257
|
+
rep: engine_1.$U.N(N['rep']),
|
|
258
|
+
docsCount: engine_1.$U.N(N['docs.count']),
|
|
259
|
+
docsDeleted: engine_1.$U.N(N['docs.deleted']),
|
|
260
|
+
health: _S(N['health']),
|
|
261
|
+
index: _S(N['index']),
|
|
262
|
+
status: _S(N['status']),
|
|
263
|
+
uuid: _S(N['uuid']),
|
|
264
|
+
priStoreSize: _S(N['pri.store.size']),
|
|
265
|
+
storeSize: _S(N['store.size']),
|
|
266
|
+
}));
|
|
267
|
+
return { list };
|
|
291
268
|
}
|
|
292
269
|
/**
|
|
293
270
|
* get mapping of an index
|
|
294
271
|
* @param indexName - name of the index
|
|
295
272
|
*/
|
|
296
|
-
getIndexMapping() {
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
throw new Error(`@indexName[${indexName}] is not found - ${engine_1.$U.json(res)}!`);
|
|
312
|
-
return mapping;
|
|
313
|
-
});
|
|
273
|
+
async getIndexMapping() {
|
|
274
|
+
const client = this.client;
|
|
275
|
+
const indexName = this.options.indexName;
|
|
276
|
+
const res = await client.indices.getMapping({ index: indexName }).catch(
|
|
277
|
+
// $ERROR.throwAsJson,
|
|
278
|
+
exports.$ERROR.handler('getMapping', e => {
|
|
279
|
+
const msg = (0, test_helper_1.GETERR)(e);
|
|
280
|
+
if (msg.startsWith('404 INDEX NOT FOUND'))
|
|
281
|
+
throw new Error(`404 NOT FOUND - index:${indexName}`);
|
|
282
|
+
throw e;
|
|
283
|
+
}));
|
|
284
|
+
const mapping = res?.body ? res.body[indexName]?.mappings : null;
|
|
285
|
+
if (!mapping)
|
|
286
|
+
throw new Error(`@indexName[${indexName}] is not found - ${engine_1.$U.json(res)}!`);
|
|
287
|
+
return mapping;
|
|
314
288
|
}
|
|
315
289
|
/**
|
|
316
290
|
* find the index by name
|
|
317
291
|
* @param indexName - name of the index
|
|
318
292
|
*/
|
|
319
|
-
findIndex(indexName) {
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
return found >= 0 ? list[found] : null;
|
|
326
|
-
});
|
|
293
|
+
async findIndex(indexName) {
|
|
294
|
+
indexName = indexName || this.options.indexName;
|
|
295
|
+
(0, engine_1._log)(NS, `- findIndex(${indexName})`);
|
|
296
|
+
const { list } = await this.listIndices();
|
|
297
|
+
const found = list.findIndex(N => N.index == indexName);
|
|
298
|
+
return found >= 0 ? list[found] : null;
|
|
327
299
|
}
|
|
328
300
|
/**
|
|
329
301
|
* create index by name
|
|
330
302
|
* @param settings - creating settings
|
|
331
303
|
*/
|
|
332
|
-
createIndex(settings) {
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
304
|
+
async createIndex(settings) {
|
|
305
|
+
const { indexName, docType, idName, timeSeries, version } = this.options;
|
|
306
|
+
settings = settings || Elastic6Service.prepareSettings({ docType, idName, timeSeries, version });
|
|
307
|
+
if (!indexName)
|
|
308
|
+
new Error('@index is required!');
|
|
309
|
+
(0, engine_1._log)(NS, `- createIndex(${indexName})`);
|
|
310
|
+
//* prepare payload
|
|
311
|
+
const payload = {
|
|
312
|
+
settings: {
|
|
313
|
+
number_of_shards: 5,
|
|
314
|
+
number_of_replicas: 1,
|
|
315
|
+
},
|
|
316
|
+
...settings,
|
|
317
|
+
};
|
|
318
|
+
(0, engine_1._log)(NS, `> settings[${indexName}] = `, engine_1.$U.json(payload));
|
|
319
|
+
//* call create index..
|
|
320
|
+
const client = this.client;
|
|
321
|
+
const res = await client.indices.create({ index: indexName, body: payload }).catch(
|
|
322
|
+
// $ERROR.throwAsJson,
|
|
323
|
+
exports.$ERROR.handler('create', e => {
|
|
324
|
+
const msg = (0, test_helper_1.GETERR)(e);
|
|
325
|
+
if (msg.startsWith('400 RESOURCE ALREADY EXISTS'))
|
|
326
|
+
throw new Error(`400 IN USE - index:${indexName}`);
|
|
327
|
+
throw e;
|
|
328
|
+
}));
|
|
329
|
+
// if (res) throw res;
|
|
330
|
+
(0, engine_1._log)(NS, `> create[${indexName}] =`, engine_1.$U.json({ ...res, meta: undefined }));
|
|
331
|
+
//* build result.
|
|
332
|
+
return {
|
|
333
|
+
status: res.statusCode,
|
|
334
|
+
index: indexName,
|
|
335
|
+
acknowledged: res.body.shards_acknowledged,
|
|
336
|
+
};
|
|
364
337
|
}
|
|
365
338
|
/**
|
|
366
339
|
* destroy search index
|
|
367
340
|
*/
|
|
368
|
-
destroyIndex() {
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
};
|
|
392
|
-
});
|
|
341
|
+
async destroyIndex() {
|
|
342
|
+
const { indexName } = this.options;
|
|
343
|
+
if (!indexName)
|
|
344
|
+
new Error('@index is required!');
|
|
345
|
+
(0, engine_1._log)(NS, `- destroyIndex(${indexName})`);
|
|
346
|
+
//* call destroy index..
|
|
347
|
+
// const { client } = instance(endpoint);
|
|
348
|
+
const client = this.client;
|
|
349
|
+
const res = await client.indices.delete({ index: indexName }).catch(
|
|
350
|
+
// $ERROR.throwAsJson,
|
|
351
|
+
exports.$ERROR.handler('destroy', e => {
|
|
352
|
+
const msg = (0, test_helper_1.GETERR)(e);
|
|
353
|
+
if (msg.startsWith('404 INDEX NOT FOUND'))
|
|
354
|
+
throw new Error(`404 NOT FOUND - index:${indexName}`);
|
|
355
|
+
throw e;
|
|
356
|
+
}));
|
|
357
|
+
// if (res) throw res;
|
|
358
|
+
(0, engine_1._log)(NS, `> destroy[${indexName}] =`, engine_1.$U.json({ ...res, meta: undefined }));
|
|
359
|
+
return {
|
|
360
|
+
status: res.statusCode,
|
|
361
|
+
index: indexName,
|
|
362
|
+
acknowledged: res.body.acknowledged,
|
|
363
|
+
};
|
|
393
364
|
}
|
|
394
365
|
/**
|
|
395
366
|
* refresh search index - refresh index to make all items searchable
|
|
396
367
|
*/
|
|
397
|
-
refreshIndex() {
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
return res.body;
|
|
416
|
-
});
|
|
368
|
+
async refreshIndex() {
|
|
369
|
+
const { indexName } = this.options;
|
|
370
|
+
if (!indexName)
|
|
371
|
+
throw new Error('.indexName is required!');
|
|
372
|
+
(0, engine_1._log)(NS, `- refreshIndex(${indexName})`);
|
|
373
|
+
//* call refresh index..
|
|
374
|
+
// const { client } = instance(endpoint);
|
|
375
|
+
const client = this.client;
|
|
376
|
+
const res = await client.indices.refresh({ index: indexName }).catch(
|
|
377
|
+
// $ERROR.throwAsJson,
|
|
378
|
+
exports.$ERROR.handler('refresh', e => {
|
|
379
|
+
const msg = (0, test_helper_1.GETERR)(e);
|
|
380
|
+
if (msg.startsWith('404 INDEX NOT FOUND'))
|
|
381
|
+
throw new Error(`404 NOT FOUND - index:${indexName}`);
|
|
382
|
+
throw e;
|
|
383
|
+
}));
|
|
384
|
+
(0, engine_1._log)(NS, `> refresh[${indexName}] =`, engine_1.$U.json({ ...res, meta: undefined }));
|
|
385
|
+
return res.body;
|
|
417
386
|
}
|
|
418
387
|
/**
|
|
419
388
|
* flush search index - force store changes into search index immediately
|
|
420
389
|
*/
|
|
421
|
-
flushIndex() {
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
return res.body;
|
|
440
|
-
});
|
|
390
|
+
async flushIndex() {
|
|
391
|
+
const { indexName } = this.options;
|
|
392
|
+
if (!indexName)
|
|
393
|
+
throw new Error('.indexName is required!');
|
|
394
|
+
(0, engine_1._log)(NS, `- flushIndex(${indexName})`);
|
|
395
|
+
//* call flush index..
|
|
396
|
+
// const { client } = instance(endpoint);
|
|
397
|
+
const client = this.client;
|
|
398
|
+
const res = await client.indices.flush({ index: indexName }).catch(
|
|
399
|
+
// $ERROR.throwAsJson,
|
|
400
|
+
exports.$ERROR.handler('flush', e => {
|
|
401
|
+
const msg = (0, test_helper_1.GETERR)(e);
|
|
402
|
+
if (msg.startsWith('404 INDEX NOT FOUND'))
|
|
403
|
+
throw new Error(`404 NOT FOUND - index:${indexName}`);
|
|
404
|
+
throw e;
|
|
405
|
+
}));
|
|
406
|
+
(0, engine_1._log)(NS, `> flush[${indexName}] =`, engine_1.$U.json({ ...res, meta: undefined }));
|
|
407
|
+
return res.body;
|
|
441
408
|
}
|
|
442
409
|
/**
|
|
443
410
|
* describe `settings` and `mappings` of index.
|
|
444
411
|
*/
|
|
445
|
-
describe() {
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
return { settings, mappings };
|
|
471
|
-
});
|
|
412
|
+
async describe() {
|
|
413
|
+
const { indexName } = this.options;
|
|
414
|
+
//* call create index..
|
|
415
|
+
(0, engine_1._log)(NS, `- describe(${indexName})`);
|
|
416
|
+
//* read settings.
|
|
417
|
+
// const { client } = instance(endpoint);
|
|
418
|
+
const client = this.client;
|
|
419
|
+
const res = await client.indices.getSettings({ index: indexName }).catch(
|
|
420
|
+
// $ERROR.throwAsJson,
|
|
421
|
+
exports.$ERROR.handler('describe', e => {
|
|
422
|
+
const msg = (0, test_helper_1.GETERR)(e);
|
|
423
|
+
if (msg.startsWith('404 INDEX NOT FOUND'))
|
|
424
|
+
throw new Error(`404 NOT FOUND - index:${indexName}`);
|
|
425
|
+
throw e;
|
|
426
|
+
}));
|
|
427
|
+
(0, engine_1._log)(NS, `> settings[${indexName}] =`, engine_1.$U.json({ ...res, meta: undefined }));
|
|
428
|
+
const settings = (res.body && res.body[indexName] && res.body[indexName].settings) || {};
|
|
429
|
+
(0, engine_1._log)(NS, `> number_of_shards =`, settings.index && settings.index.number_of_shards); // 5
|
|
430
|
+
(0, engine_1._log)(NS, `> number_of_replicas =`, settings.index && settings.index.number_of_replicas); // 1
|
|
431
|
+
//* read mappings.
|
|
432
|
+
const res2 = await client.indices.getMapping({ index: indexName });
|
|
433
|
+
(0, engine_1._log)(NS, `> mappings[${indexName}] =`, engine_1.$U.json(res2));
|
|
434
|
+
const mappings = (res2.body && res2.body[indexName] && res2.body[indexName].mappings) || {};
|
|
435
|
+
//* returns
|
|
436
|
+
return { settings, mappings };
|
|
472
437
|
}
|
|
473
438
|
/**
|
|
474
439
|
* save single item
|
|
@@ -477,47 +442,44 @@ class Elastic6Service extends ElasticIndexService {
|
|
|
477
442
|
* @param item - item to save
|
|
478
443
|
* @param type - document type (default: doc-type given at construction time)
|
|
479
444
|
*/
|
|
480
|
-
saveItem(id, item, type) {
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
445
|
+
async saveItem(id, item, type) {
|
|
446
|
+
const { indexName, docType, idName } = this.options;
|
|
447
|
+
(0, engine_1._log)(NS, `- saveItem(${id})`);
|
|
448
|
+
// const { client } = instance(endpoint);
|
|
449
|
+
const client = this.client;
|
|
450
|
+
// prepare item body and autocomplete fields
|
|
451
|
+
const body = { ...item, [idName]: id };
|
|
452
|
+
const body2 = this.popullateAutocompleteFields(body);
|
|
453
|
+
type = `${type || docType}`;
|
|
454
|
+
const params = { index: indexName, id, body: body2 };
|
|
455
|
+
// check version to include 'type' in params
|
|
456
|
+
if (this.isOldES6) {
|
|
457
|
+
params.type = type;
|
|
458
|
+
}
|
|
459
|
+
if (idName === '_id')
|
|
460
|
+
delete params.body[idName]; //WARN! `_id` is reserved in ES6.
|
|
461
|
+
(0, engine_1._log)(NS, `> params[${id}] =`, engine_1.$U.json(params));
|
|
462
|
+
//NOTE - use npm `elasticsearch#13.2.0` for avoiding error.
|
|
463
|
+
const res = await client.create(params).catch(
|
|
464
|
+
// $ERROR.throwAsJson,
|
|
465
|
+
exports.$ERROR.handler('save', e => {
|
|
466
|
+
const msg = (0, test_helper_1.GETERR)(e);
|
|
467
|
+
//* try to overwrite document..
|
|
468
|
+
if (msg.startsWith('409 VERSION CONFLICT ENGINE')) {
|
|
469
|
+
// delete body2[idName]; // do set id while update
|
|
470
|
+
// return this.updateItem(id, body2);
|
|
471
|
+
const param2 = { index: indexName, id, body: { ...body2 } };
|
|
472
|
+
if (this.isOldES6)
|
|
473
|
+
param2.type = type;
|
|
474
|
+
return client.index(param2);
|
|
495
475
|
}
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
const msg = (0, test_helper_1.GETERR)(e);
|
|
504
|
-
//* try to overwrite document..
|
|
505
|
-
if (msg.startsWith('409 VERSION CONFLICT ENGINE')) {
|
|
506
|
-
// delete body2[idName]; // do set id while update
|
|
507
|
-
// return this.updateItem(id, body2);
|
|
508
|
-
const param2 = { index: indexName, id, body: Object.assign({}, body2) };
|
|
509
|
-
if (this.isOldES6)
|
|
510
|
-
param2.type = type;
|
|
511
|
-
return client.index(param2);
|
|
512
|
-
}
|
|
513
|
-
throw e;
|
|
514
|
-
}));
|
|
515
|
-
(0, engine_1._log)(NS, `> create[${id}].res =`, engine_1.$U.json(Object.assign(Object.assign({}, res), { meta: undefined })));
|
|
516
|
-
const _version = engine_1.$U.N((_a = res.body) === null || _a === void 0 ? void 0 : _a._version, 0);
|
|
517
|
-
const _id = (_b = res.body) === null || _b === void 0 ? void 0 : _b._id;
|
|
518
|
-
const res2 = Object.assign(Object.assign({}, body), { _id, _version });
|
|
519
|
-
return res2;
|
|
520
|
-
});
|
|
476
|
+
throw e;
|
|
477
|
+
}));
|
|
478
|
+
(0, engine_1._log)(NS, `> create[${id}].res =`, engine_1.$U.json({ ...res, meta: undefined }));
|
|
479
|
+
const _version = engine_1.$U.N(res.body?._version, 0);
|
|
480
|
+
const _id = res.body?._id;
|
|
481
|
+
const res2 = { ...body, _id, _version };
|
|
482
|
+
return res2;
|
|
521
483
|
}
|
|
522
484
|
/**
|
|
523
485
|
* push item for time-series data.
|
|
@@ -525,33 +487,30 @@ class Elastic6Service extends ElasticIndexService {
|
|
|
525
487
|
* @param item - item to push
|
|
526
488
|
* @param type - document type (default: doc-type given at construction time)
|
|
527
489
|
*/
|
|
528
|
-
pushItem(item, type) {
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
const res2 = Object.assign(Object.assign({}, body), { _id, _version });
|
|
553
|
-
return res2;
|
|
554
|
-
});
|
|
490
|
+
async pushItem(item, type) {
|
|
491
|
+
const { indexName, docType } = this.options;
|
|
492
|
+
const id = '';
|
|
493
|
+
type = `${type || docType}`;
|
|
494
|
+
const body = { ...item };
|
|
495
|
+
const body2 = this.popullateAutocompleteFields(body);
|
|
496
|
+
(0, engine_1._log)(NS, `- pushItem(${id})`);
|
|
497
|
+
const params = { index: indexName, type, body: body2 };
|
|
498
|
+
(0, engine_1._log)(NS, `> params[${id}] =`, engine_1.$U.json(params));
|
|
499
|
+
//NOTE - use npm `elasticsearch#13.2.0` for avoiding error.
|
|
500
|
+
// const { client } = instance(endpoint);
|
|
501
|
+
const client = this.client;
|
|
502
|
+
const res = await client.index(params).catch(
|
|
503
|
+
// $ERROR.throwAsJson,
|
|
504
|
+
exports.$ERROR.handler('index', e => {
|
|
505
|
+
(0, engine_1._err)(NS, `> index[${indexName}].err =`, e instanceof Error ? e : engine_1.$U.json(e));
|
|
506
|
+
throw e;
|
|
507
|
+
}));
|
|
508
|
+
// {"_index":"test-v3","_type":"_doc","_id":"rTeHiW4BPb_liACrA9qa","_version":1,"result":"created","_shards":{"total":2,"successful":2,"failed":0},"_seq_no":2,"_primary_term":1}
|
|
509
|
+
(0, engine_1._log)(NS, `> create[${id}].res =`, engine_1.$U.json({ ...res, meta: undefined }));
|
|
510
|
+
const _id = res.body?._id;
|
|
511
|
+
const _version = res.body?._version;
|
|
512
|
+
const res2 = { ...body, _id, _version };
|
|
513
|
+
return res2;
|
|
555
514
|
}
|
|
556
515
|
/**
|
|
557
516
|
* read item with projections
|
|
@@ -559,76 +518,70 @@ class Elastic6Service extends ElasticIndexService {
|
|
|
559
518
|
* @param id - item-id
|
|
560
519
|
* @param views - projections
|
|
561
520
|
*/
|
|
562
|
-
readItem(id, views) {
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
const
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
const res2 = Object.assign(Object.assign({}, data), { _id, _version });
|
|
598
|
-
return res2;
|
|
599
|
-
});
|
|
521
|
+
async readItem(id, views) {
|
|
522
|
+
const { indexName, docType } = this.options;
|
|
523
|
+
const type = `${docType}`;
|
|
524
|
+
(0, engine_1._log)(NS, `- readItem(${id})`);
|
|
525
|
+
const params = { index: indexName, type, id };
|
|
526
|
+
if (views) {
|
|
527
|
+
const fields = [];
|
|
528
|
+
const keys = Array.isArray(views) ? views : Object.keys(views);
|
|
529
|
+
keys.forEach((k) => {
|
|
530
|
+
fields.push(k);
|
|
531
|
+
});
|
|
532
|
+
params._source = fields;
|
|
533
|
+
}
|
|
534
|
+
(0, engine_1._log)(NS, `> params[${id}] =`, engine_1.$U.json(params));
|
|
535
|
+
// const { client } = instance(endpoint);
|
|
536
|
+
const client = this.client;
|
|
537
|
+
const res = await client.get(params).catch(
|
|
538
|
+
// $ERROR.throwAsJson,
|
|
539
|
+
exports.$ERROR.handler('read', e => {
|
|
540
|
+
const msg = (0, test_helper_1.GETERR)(e);
|
|
541
|
+
if (msg.startsWith('404 NOT FOUND'))
|
|
542
|
+
throw new Error(`404 NOT FOUND - id:${id}`);
|
|
543
|
+
if (msg.startsWith('404 INDEX NOT FOUND'))
|
|
544
|
+
throw new Error(`404 NOT FOUND - index:${indexName}`);
|
|
545
|
+
throw e;
|
|
546
|
+
}));
|
|
547
|
+
(0, engine_1._log)(NS, `> read[${id}].res =`, engine_1.$U.json({ ...res, meta: undefined }));
|
|
548
|
+
const _id = res.body?._id;
|
|
549
|
+
const _version = res.body?._version;
|
|
550
|
+
const data = res?._source || res.body?._source || {};
|
|
551
|
+
// delete internal (analyzed) field
|
|
552
|
+
delete data[Elastic6Service.DECOMPOSED_FIELD];
|
|
553
|
+
delete data[Elastic6Service.QWERTY_FIELD];
|
|
554
|
+
const res2 = { ...data, _id, _version };
|
|
555
|
+
return res2;
|
|
600
556
|
}
|
|
601
557
|
/**
|
|
602
558
|
* delete item with projections
|
|
603
559
|
*
|
|
604
560
|
* @param id - item-id
|
|
605
561
|
*/
|
|
606
|
-
deleteItem(id) {
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
const
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
const res2 = Object.assign(Object.assign({}, data), { _id, _version });
|
|
630
|
-
return res2;
|
|
631
|
-
});
|
|
562
|
+
async deleteItem(id) {
|
|
563
|
+
const { indexName, docType } = this.options;
|
|
564
|
+
const type = `${docType}`;
|
|
565
|
+
(0, engine_1._log)(NS, `- readItem(${id})`);
|
|
566
|
+
const params = { index: indexName, type, id };
|
|
567
|
+
(0, engine_1._log)(NS, `> params[${id}] =`, engine_1.$U.json(params));
|
|
568
|
+
// const { client } = instance(endpoint);
|
|
569
|
+
const client = this.client;
|
|
570
|
+
const res = await client.delete(params).catch(exports.$ERROR.handler('read', e => {
|
|
571
|
+
const msg = (0, test_helper_1.GETERR)(e);
|
|
572
|
+
if (msg.startsWith('404 NOT FOUND'))
|
|
573
|
+
throw new Error(`404 NOT FOUND - id:${id}`);
|
|
574
|
+
if (msg.startsWith('404 INDEX NOT FOUND'))
|
|
575
|
+
throw new Error(`404 NOT FOUND - index:${indexName}`);
|
|
576
|
+
throw e;
|
|
577
|
+
}));
|
|
578
|
+
// {"_index":"test-v3","_type":"_doc","_id":"aaa","_version":3,"result":"deleted","_shards":{"total":2,"successful":2,"failed":0},"_seq_no":4,"_primary_term":1}
|
|
579
|
+
(0, engine_1._log)(NS, `> delete[${id}].res =`, engine_1.$U.json({ ...res, meta: undefined }));
|
|
580
|
+
const _id = res.body?._id;
|
|
581
|
+
const _version = res.body?._version;
|
|
582
|
+
const data = res.body?._source || res.body?._source || {};
|
|
583
|
+
const res2 = { ...data, _id, _version };
|
|
584
|
+
return res2;
|
|
632
585
|
}
|
|
633
586
|
/**
|
|
634
587
|
* update item (throw if not exist)
|
|
@@ -643,122 +596,118 @@ class Elastic6Service extends ElasticIndexService {
|
|
|
643
596
|
* @param increments - item to increase
|
|
644
597
|
* @param options - (optional) request option of client.
|
|
645
598
|
*/
|
|
646
|
-
updateItem(id, item, increments, options) {
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
if
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
scripts.push(`if (ctx._source['${key}'] != null && ctx._source['${key}'] instanceof List) {
|
|
599
|
+
async updateItem(id, item, increments, options) {
|
|
600
|
+
const { indexName, docType, idName } = this.options;
|
|
601
|
+
const type = `${docType}`;
|
|
602
|
+
(0, engine_1._log)(NS, `- updateItem(${id})`);
|
|
603
|
+
item = !item && increments ? undefined : item;
|
|
604
|
+
//* prepare params.
|
|
605
|
+
const params = { index: indexName, id, body: {} };
|
|
606
|
+
// check version to include 'type' in params
|
|
607
|
+
if (this.isOldES6) {
|
|
608
|
+
params.type = type;
|
|
609
|
+
}
|
|
610
|
+
const scripts = [];
|
|
611
|
+
if (increments) {
|
|
612
|
+
//* it will create if not exists.
|
|
613
|
+
params.body.upsert = { ...increments, [idName]: id };
|
|
614
|
+
Object.entries(increments).forEach(([key, val]) => {
|
|
615
|
+
if (Array.isArray(val)) {
|
|
616
|
+
// If the value is an array, append it to the existing array in the source
|
|
617
|
+
scripts.push(`if (ctx._source['${key}'] != null && ctx._source['${key}'] instanceof List) {
|
|
666
618
|
ctx._source['${key}'].addAll(params.increments['${key}']);
|
|
667
619
|
} else {
|
|
668
620
|
ctx._source['${key}'] = params.increments['${key}'];
|
|
669
621
|
}`);
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
622
|
+
}
|
|
623
|
+
else {
|
|
624
|
+
// If the value is a number, increment the existing field
|
|
625
|
+
scripts.push(`if (ctx._source['${key}'] != null) {
|
|
674
626
|
ctx._source['${key}'] += params.increments['${key}'];
|
|
675
627
|
} else {
|
|
676
628
|
ctx._source['${key}'] = params.increments['${key}'];
|
|
677
629
|
}`);
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
});
|
|
630
|
+
}
|
|
631
|
+
});
|
|
632
|
+
}
|
|
633
|
+
if (item) {
|
|
634
|
+
// Handle item updates in the script
|
|
635
|
+
Object.entries(item).forEach(([key]) => {
|
|
636
|
+
scripts.push(`ctx._source['${key}'] = params.item['${key}'];`);
|
|
637
|
+
});
|
|
638
|
+
}
|
|
639
|
+
params.body.script = {
|
|
640
|
+
source: scripts.join(' '),
|
|
641
|
+
lang: 'painless',
|
|
642
|
+
params: { item, increments },
|
|
643
|
+
};
|
|
644
|
+
(0, engine_1._log)(NS, `> params[${id}] =`, engine_1.$U.json(params));
|
|
645
|
+
// const { client } = instance(endpoint);
|
|
646
|
+
const client = this.client;
|
|
647
|
+
const res = await client.update(params, options).catch(exports.$ERROR.handler('update', (e, E) => {
|
|
648
|
+
const msg = (0, test_helper_1.GETERR)(e);
|
|
649
|
+
//* id 아이템이 없을 경우 발생함.
|
|
650
|
+
if (msg.startsWith('404 DOCUMENT MISSING'))
|
|
651
|
+
throw new Error(`404 NOT FOUND - id:${id}`);
|
|
652
|
+
//* 해당 속성이 없을때 업데이트 하려면 생길 수 있음.
|
|
653
|
+
if (msg.startsWith('400 REMOTE TRANSPORT'))
|
|
654
|
+
throw new Error(`400 INVALID FIELD - id:${id}`);
|
|
655
|
+
if (msg.startsWith('404 NOT FOUND'))
|
|
656
|
+
throw new Error(`404 NOT FOUND - id:${id}`);
|
|
657
|
+
if (msg.startsWith('400 ACTION REQUEST VALIDATION'))
|
|
658
|
+
throw e;
|
|
659
|
+
if (msg.startsWith('400 INVALID FIELD'))
|
|
660
|
+
throw e; // at ES6.8
|
|
661
|
+
if (msg.startsWith('400 MAPPER PARSING'))
|
|
662
|
+
throw e;
|
|
663
|
+
if (msg.startsWith('400 ILLEGAL ARGUMENT - Cannot apply') ||
|
|
664
|
+
msg.startsWith('400 ILLEGAL ARGUMENT - class_cast_exception:'))
|
|
665
|
+
throw new Error(`400 ILLEGAL ARGUMENT - failed to update due to type mismatch in item's field`); // at ES7.1
|
|
666
|
+
if (msg.startsWith('400 ILLEGAL ARGUMENT'))
|
|
667
|
+
throw e; // at ES7.1
|
|
668
|
+
throw E;
|
|
669
|
+
}));
|
|
670
|
+
// {"_index":"test-v3","_type":"_doc","_id":"aaa","_version":2,"result":"updated","_shards":{"total":2,"successful":2,"failed":0},"_seq_no":8,"_primary_term":1}
|
|
671
|
+
// {"_index":"test-v3","_type":"_doc","_id":"aaa","_version":2,"result":"noop","_shards":{"total":0,"successful":0,"failed":0}}
|
|
672
|
+
(0, engine_1._log)(NS, `> update[${id}].res =`, engine_1.$U.json({ ...res, meta: undefined }));
|
|
673
|
+
const _id = res.body._id;
|
|
674
|
+
const _version = res.body._version;
|
|
675
|
+
const res2 = { ...item, _id, _version };
|
|
676
|
+
return res2;
|
|
726
677
|
}
|
|
727
678
|
/**
|
|
728
679
|
* run search and get the raw response.
|
|
729
680
|
* @param body - Elasticsearch Query DSL that defines the search request (e.g., size, query, filters).
|
|
730
681
|
* @param searchType - type of search (e.g., 'query_then_fetch', 'dfs_query_then_fetch').
|
|
731
682
|
*/
|
|
732
|
-
searchRaw(body, searchType) {
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
return $res === null || $res === void 0 ? void 0 : $res.body;
|
|
761
|
-
});
|
|
683
|
+
async searchRaw(body, searchType) {
|
|
684
|
+
if (!body)
|
|
685
|
+
throw new Error('@body (SearchBody) is required');
|
|
686
|
+
const { indexName, docType } = this.options;
|
|
687
|
+
(0, engine_1._log)(NS, `- search(${indexName}, ${searchType || ''})....`);
|
|
688
|
+
(0, engine_1._log)(NS, `> body =`, engine_1.$U.json(body));
|
|
689
|
+
const tmp = docType ? docType : '';
|
|
690
|
+
const type = docType ? `${docType}` : undefined;
|
|
691
|
+
const params = { index: indexName, body, searchType };
|
|
692
|
+
// check version to include 'type' in params
|
|
693
|
+
if (this.isOldES6) {
|
|
694
|
+
params.type = type;
|
|
695
|
+
}
|
|
696
|
+
(0, engine_1._log)(NS, `> params[${tmp}] =`, engine_1.$U.json({ ...params, body: undefined }));
|
|
697
|
+
// const { client } = instance(endpoint);
|
|
698
|
+
const client = this.client;
|
|
699
|
+
const $res = await client.search(params).catch(exports.$ERROR.handler('search', e => {
|
|
700
|
+
(0, engine_1._err)(NS, `> search[${indexName}].err =`, e);
|
|
701
|
+
throw e;
|
|
702
|
+
}));
|
|
703
|
+
// {"took":6,"timed_out":false,"_shards":{"total":4,"successful":4,"skipped":0,"failed":0},"hits":{"total":1,"max_score":0.2876821,"hits":[{"_index":"test-v3","_type":"_doc","_id":"aaa","_score":0.2876821,"_source":{"name":"AAA","@id":"aaa","a":-3,"b":-2}}]}}
|
|
704
|
+
// _log(NS, `> search[${id}].res =`, $U.json({ ...res, meta: undefined }));
|
|
705
|
+
// _log(NS, `> search[${tmp}].took =`, $res.took);
|
|
706
|
+
// _log(NS, `> search[${tmp}].hits.total =`, $res.hits?.total);
|
|
707
|
+
// _log(NS, `> search[${tmp}].hits.max_score =`, $res.hits?.max_score);
|
|
708
|
+
// _log(NS, `> search[${tmp}].hits.hits[0] =`, $res.hits && $U.json($res.hits.hits[0]));
|
|
709
|
+
//* return raw results.
|
|
710
|
+
return $res?.body;
|
|
762
711
|
}
|
|
763
712
|
/**
|
|
764
713
|
* run search, and get the formatmted response.
|
|
@@ -766,48 +715,36 @@ class Elastic6Service extends ElasticIndexService {
|
|
|
766
715
|
* @param searchType - type of search (e.g., 'query_then_fetch', 'dfs_query_then_fetch').
|
|
767
716
|
*
|
|
768
717
|
*/
|
|
769
|
-
search(body, searchType) {
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
718
|
+
async search(body, searchType) {
|
|
719
|
+
const size = engine_1.$U.N(body.size, 0);
|
|
720
|
+
const response = await this.searchRaw(body, searchType);
|
|
721
|
+
// return w/ transformed id
|
|
722
|
+
const hits = response.hits;
|
|
723
|
+
if (typeof hits !== 'object')
|
|
724
|
+
throw new Error(`.hits (object) is required - hits:${engine_1.$U.json(hits)}`);
|
|
725
|
+
//NOTE - ES6.8 w/ OS1.1
|
|
726
|
+
return {
|
|
727
|
+
total: typeof hits.total?.value === 'number' ? hits.total?.value : hits.total,
|
|
728
|
+
list: hits.hits.map((hit) => ({
|
|
729
|
+
...hit._source,
|
|
730
|
+
_id: hit._id,
|
|
731
|
+
_score: hit._score,
|
|
732
|
+
})),
|
|
733
|
+
last: hits.hits.length === size && size > 0 ? hits.hits[size - 1]?.sort : undefined,
|
|
734
|
+
aggregations: response.aggregations,
|
|
735
|
+
};
|
|
786
736
|
}
|
|
787
737
|
/**
|
|
788
738
|
* search all until limit (-1 means no-limit)
|
|
789
739
|
* @param body - Elasticsearch Query DSL that defines the search request (e.g., size, query, filters).
|
|
790
740
|
* @param params - parameters including search type, limit, and retry options.
|
|
791
741
|
*/
|
|
792
|
-
searchAll(body, params) {
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
const chunk = _c.value;
|
|
799
|
-
chunk.forEach((N) => list.push(N));
|
|
800
|
-
}
|
|
801
|
-
}
|
|
802
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
803
|
-
finally {
|
|
804
|
-
try {
|
|
805
|
-
if (_c && !_c.done && (_a = _b.return)) yield _a.call(_b);
|
|
806
|
-
}
|
|
807
|
-
finally { if (e_1) throw e_1.error; }
|
|
808
|
-
}
|
|
809
|
-
return list;
|
|
810
|
-
});
|
|
742
|
+
async searchAll(body, params) {
|
|
743
|
+
const list = [];
|
|
744
|
+
for await (const chunk of this.generateSearchResult(body, params)) {
|
|
745
|
+
chunk.forEach((N) => list.push(N));
|
|
746
|
+
}
|
|
747
|
+
return list;
|
|
811
748
|
}
|
|
812
749
|
/**
|
|
813
750
|
* create async generator that yields items queried until last
|
|
@@ -815,38 +752,35 @@ class Elastic6Service extends ElasticIndexService {
|
|
|
815
752
|
* @param body - Elasticsearch Query DSL that defines the search request (e.g., size, query, filters).
|
|
816
753
|
* @param params - parameters including search type, limit, and retry options.
|
|
817
754
|
*/
|
|
818
|
-
generateSearchResult(body, params) {
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
if (list.length === 0 && !body.search_after) {
|
|
833
|
-
break;
|
|
834
|
-
}
|
|
835
|
-
yield yield __await(list);
|
|
755
|
+
async *generateSearchResult(body, params) {
|
|
756
|
+
const doRetry = params?.retryOptions?.do ?? false;
|
|
757
|
+
const t = params?.retryOptions?.t ?? 5000;
|
|
758
|
+
const maxRetries = params?.retryOptions?.maxRetries ?? 3;
|
|
759
|
+
let limit = params?.limit ?? -1;
|
|
760
|
+
let retryCount = 0;
|
|
761
|
+
if (!body.sort)
|
|
762
|
+
body.sort = '_doc';
|
|
763
|
+
do {
|
|
764
|
+
try {
|
|
765
|
+
const { list, last } = await this.search(body, params?.searchType);
|
|
766
|
+
body.search_after = last;
|
|
767
|
+
if (list.length === 0 && !body.search_after) {
|
|
768
|
+
break;
|
|
836
769
|
}
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
throw e;
|
|
846
|
-
}
|
|
770
|
+
yield list;
|
|
771
|
+
}
|
|
772
|
+
catch (e) {
|
|
773
|
+
const msg = (0, test_helper_1.GETERR)(e);
|
|
774
|
+
if (doRetry && msg.startsWith('429 UNKNOWN') && retryCount < maxRetries) {
|
|
775
|
+
retryCount++;
|
|
776
|
+
await (0, test_helper_1.waited)(t);
|
|
777
|
+
continue;
|
|
847
778
|
}
|
|
848
|
-
|
|
849
|
-
|
|
779
|
+
else {
|
|
780
|
+
throw e;
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
} while (body.search_after && (limit === -1 || --limit > 0));
|
|
850
784
|
}
|
|
851
785
|
/**
|
|
852
786
|
* prepare default setting
|
|
@@ -954,9 +888,9 @@ class Elastic6Service extends ElasticIndexService {
|
|
|
954
888
|
tokenizer: {
|
|
955
889
|
hangul: {
|
|
956
890
|
type: 'seunjeon_tokenizer',
|
|
957
|
-
decompound: true,
|
|
958
|
-
deinflect: true,
|
|
959
|
-
index_eojeol: true,
|
|
891
|
+
decompound: true, // 복합명사 분해
|
|
892
|
+
deinflect: true, // 활용어의 원형 추출
|
|
893
|
+
index_eojeol: true, // 어절 추출
|
|
960
894
|
pos_tagging: false, // 품사 태깅
|
|
961
895
|
},
|
|
962
896
|
edge_30grams: {
|
|
@@ -1038,13 +972,10 @@ class Elastic6Service extends ElasticIndexService {
|
|
|
1038
972
|
}
|
|
1039
973
|
}
|
|
1040
974
|
return N;
|
|
1041
|
-
},
|
|
975
|
+
}, { ...body, [Elastic6Service.DECOMPOSED_FIELD]: {}, [Elastic6Service.QWERTY_FIELD]: {} });
|
|
1042
976
|
}
|
|
1043
977
|
}
|
|
1044
978
|
exports.Elastic6Service = Elastic6Service;
|
|
1045
|
-
// internal field name to store analyzed strings for autocomplete search
|
|
1046
|
-
Elastic6Service.DECOMPOSED_FIELD = '_decomposed';
|
|
1047
|
-
Elastic6Service.QWERTY_FIELD = '_qwerty';
|
|
1048
979
|
/**
|
|
1049
980
|
* error samples
|
|
1050
981
|
*/
|
|
@@ -1055,7 +986,7 @@ exports.$ERROR = {
|
|
|
1055
986
|
const err = e;
|
|
1056
987
|
const meta = err.meta && typeof err.meta === 'object' ? err.meta : undefined;
|
|
1057
988
|
const $err = _pack(err);
|
|
1058
|
-
return
|
|
989
|
+
return { ...$err, meta };
|
|
1059
990
|
}
|
|
1060
991
|
return e;
|
|
1061
992
|
},
|
|
@@ -1100,16 +1031,15 @@ exports.$ERROR = {
|
|
|
1100
1031
|
const status = `${E.statusCode || ''}`;
|
|
1101
1032
|
const message = `${E.message || E.msg || ''}`;
|
|
1102
1033
|
const reason = ((E) => {
|
|
1103
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
|
|
1104
1034
|
//* from ES7.1
|
|
1105
1035
|
if (E.meta && typeof E.meta == 'object') {
|
|
1106
|
-
const type = _S(E
|
|
1107
|
-
const status = engine_1.$U.N(
|
|
1036
|
+
const type = _S(E?.message).toUpperCase().split('_').slice(0, -1).join(' ');
|
|
1037
|
+
const status = engine_1.$U.N(E.meta?.statusCode, type.includes('NOT FOUND') ? 404 : 400);
|
|
1108
1038
|
const $res = exports.$ERROR.parseMeta(E.meta);
|
|
1109
1039
|
//* find the reason.
|
|
1110
|
-
const reason =
|
|
1111
|
-
?
|
|
1112
|
-
:
|
|
1040
|
+
const reason = $res.body?.error?.reason === 'failed to execute script'
|
|
1041
|
+
? $res.body?.error?.caused_by?.caused_by?.reason
|
|
1042
|
+
: $res.body?.error?.reason;
|
|
1113
1043
|
const result = { status, type: type || (status === 404 ? 'NOT FOUND' : 'UNKNOWN') };
|
|
1114
1044
|
if (typeof reason !== 'undefined') {
|
|
1115
1045
|
result.reason = reason;
|
|
@@ -1122,29 +1052,28 @@ exports.$ERROR = {
|
|
|
1122
1052
|
const $res = exports.$ERROR.parseMeta(E.response);
|
|
1123
1053
|
//* find the root-cause.
|
|
1124
1054
|
const pic1 = (N, i = 0) => (N && Array.isArray(N) ? N[i] : N);
|
|
1125
|
-
const cause = pic1(
|
|
1126
|
-
const status = engine_1.$U.N(
|
|
1127
|
-
const reason = _S(
|
|
1128
|
-
const type = _S(cause
|
|
1055
|
+
const cause = pic1($res?.error?.root_cause);
|
|
1056
|
+
const status = engine_1.$U.N($res.error?.status || $res.status);
|
|
1057
|
+
const reason = _S($res.error?.reason, $res.found === false || $res.result === 'not_found' ? 'NOT FOUND' : '');
|
|
1058
|
+
const type = _S(cause?.type).toUpperCase().split('_').slice(0, -1).join(' ');
|
|
1129
1059
|
return { status, reason, cause, type: type || reason };
|
|
1130
1060
|
})(E);
|
|
1131
1061
|
//* FINAL. convert to error-object.
|
|
1132
1062
|
return {
|
|
1133
|
-
status: engine_1.$U.N(status,
|
|
1134
|
-
message: message ||
|
|
1063
|
+
status: engine_1.$U.N(status, reason?.status || 0),
|
|
1064
|
+
message: message || reason?.reason,
|
|
1135
1065
|
reason,
|
|
1136
1066
|
};
|
|
1137
1067
|
},
|
|
1138
1068
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1139
1069
|
handler: (name, cb) => (e) => {
|
|
1140
|
-
var _a, _b, _c, _d, _e;
|
|
1141
1070
|
const E = exports.$ERROR.asError(e);
|
|
1142
1071
|
//* unknown error found..
|
|
1143
|
-
if (!
|
|
1072
|
+
if (!E?.status) {
|
|
1144
1073
|
(0, engine_1._err)(NS, `! err[${name}]@handler =`, e instanceof Error, engine_1.$U.json(e));
|
|
1145
1074
|
throw e;
|
|
1146
1075
|
}
|
|
1147
|
-
const $e = new Error(`${E.status} ${
|
|
1076
|
+
const $e = new Error(`${E.status} ${E.reason?.type ?? ''} - ${E.reason?.reason ?? E.message ?? ''}`);
|
|
1148
1077
|
if (cb)
|
|
1149
1078
|
return cb($e, E);
|
|
1150
1079
|
throw $e;
|
|
@@ -1160,17 +1089,13 @@ exports.$ERROR = {
|
|
|
1160
1089
|
class DummyElastic6Service extends Elastic6Service {
|
|
1161
1090
|
constructor(dataFile, options) {
|
|
1162
1091
|
super(options);
|
|
1163
|
-
this.buffer = {};
|
|
1164
|
-
/**
|
|
1165
|
-
* say hello()
|
|
1166
|
-
*/
|
|
1167
|
-
this.hello = () => `dummy-elastic6-service:${this.options.indexName}`;
|
|
1168
1092
|
(0, engine_1._log)(NS, `DummyElastic6Service(${dataFile || ''})...`);
|
|
1169
1093
|
if (!dataFile)
|
|
1170
1094
|
throw new Error('@dataFile(string) is required!');
|
|
1171
1095
|
const dummy = (0, tools_1.loadDataYml)(dataFile);
|
|
1172
1096
|
this.load(dummy.data);
|
|
1173
1097
|
}
|
|
1098
|
+
buffer = {};
|
|
1174
1099
|
load(data) {
|
|
1175
1100
|
const { idName } = this.options;
|
|
1176
1101
|
if (!data || !Array.isArray(data))
|
|
@@ -1180,56 +1105,52 @@ class DummyElastic6Service extends Elastic6Service {
|
|
|
1180
1105
|
this.buffer[id] = item;
|
|
1181
1106
|
});
|
|
1182
1107
|
}
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1108
|
+
/**
|
|
1109
|
+
* say hello()
|
|
1110
|
+
*/
|
|
1111
|
+
hello = () => `dummy-elastic6-service:${this.options.indexName}`;
|
|
1112
|
+
async readItem(id) {
|
|
1113
|
+
const item = this.buffer[id];
|
|
1114
|
+
if (item === undefined)
|
|
1115
|
+
throw new Error(`404 NOT FOUND - id:${id}`);
|
|
1116
|
+
return item;
|
|
1190
1117
|
}
|
|
1191
|
-
saveItem(id, item) {
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
return Object.assign(Object.assign({ [idName]: id }, item), { _version: 1 });
|
|
1196
|
-
});
|
|
1118
|
+
async saveItem(id, item) {
|
|
1119
|
+
const { idName } = this.options;
|
|
1120
|
+
this.buffer[id] = { id, ...item };
|
|
1121
|
+
return { [idName]: id, ...item, _version: 1 };
|
|
1197
1122
|
}
|
|
1198
|
-
deleteItem(id, sort) {
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
return Object.assign({}, org);
|
|
1203
|
-
});
|
|
1123
|
+
async deleteItem(id, sort) {
|
|
1124
|
+
const org = this.buffer[id];
|
|
1125
|
+
delete this.buffer[id];
|
|
1126
|
+
return { ...org };
|
|
1204
1127
|
}
|
|
1205
|
-
updateItem(id, updates, increments) {
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
item[key] = [...item[key], ...value];
|
|
1215
|
-
}
|
|
1216
|
-
else {
|
|
1217
|
-
item[key] = value;
|
|
1218
|
-
}
|
|
1128
|
+
async updateItem(id, updates, increments) {
|
|
1129
|
+
const org = await this.readItem(id);
|
|
1130
|
+
const item = { ...org, ...updates, _version: Number(org._version || 0) + 1 };
|
|
1131
|
+
if (increments) {
|
|
1132
|
+
Object.entries(increments).forEach(([key, value]) => {
|
|
1133
|
+
if (Array.isArray(value)) {
|
|
1134
|
+
// case of arrary increment
|
|
1135
|
+
if (Array.isArray(item[key])) {
|
|
1136
|
+
item[key] = [...item[key], ...value];
|
|
1219
1137
|
}
|
|
1220
1138
|
else {
|
|
1221
|
-
|
|
1222
|
-
item[key] = item[key] + value;
|
|
1223
|
-
}
|
|
1224
|
-
else {
|
|
1225
|
-
item[key] = value;
|
|
1226
|
-
}
|
|
1139
|
+
item[key] = value;
|
|
1227
1140
|
}
|
|
1228
|
-
}
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1141
|
+
}
|
|
1142
|
+
else {
|
|
1143
|
+
if (typeof item[key] === 'number') {
|
|
1144
|
+
item[key] = item[key] + value;
|
|
1145
|
+
}
|
|
1146
|
+
else {
|
|
1147
|
+
item[key] = value;
|
|
1148
|
+
}
|
|
1149
|
+
}
|
|
1150
|
+
});
|
|
1151
|
+
}
|
|
1152
|
+
this.buffer[id] = item;
|
|
1153
|
+
return { id: id, _version: Number(org._version || 0) + 1, ...updates };
|
|
1233
1154
|
}
|
|
1234
1155
|
}
|
|
1235
1156
|
exports.DummyElastic6Service = DummyElastic6Service;
|