@toxplanet/pegasus-sdk 1.0.1 → 1.1.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.
@@ -0,0 +1,404 @@
1
+ class ElasticsearchService {
2
+ constructor(connection) {
3
+ this.connection = connection;
4
+ this.indexRoutes = new Map();
5
+ }
6
+
7
+ /**
8
+ * Returns an Elasticsearch client interface that routes operations through registered handlers.
9
+ * This method provides compatibility with legacy elasticsearch client usage patterns.
10
+ * @returns {ElasticsearchService} The service instance itself, which implements the client interface
11
+ */
12
+ client() {
13
+ return this;
14
+ }
15
+
16
+ registerIndexRoute(indexPattern, handler) {
17
+ this.indexRoutes.set(indexPattern, handler);
18
+ }
19
+
20
+ getRouteHandler(indexName) {
21
+ for (const [pattern, handler] of this.indexRoutes.entries()) {
22
+ if (typeof pattern === 'string' && indexName === pattern) {
23
+ return handler;
24
+ }
25
+ if (pattern instanceof RegExp && pattern.test(indexName)) {
26
+ return handler;
27
+ }
28
+ }
29
+ return null;
30
+ }
31
+
32
+
33
+ // example flow:
34
+ // external application
35
+ // 1. initialize the sdk
36
+ // 2. call elastic search search
37
+ // 3. sdk will call the appropriate handler based on the index name
38
+ // 4. handler will be called with the appropriate parameters
39
+ // 5. handler will return the result
40
+ // 6. sdk will return the result to the external application
41
+
42
+ async search(params) {
43
+ const handler = this.getRouteHandler(params.index);
44
+ if (handler && handler.search) {
45
+ return await handler.search(params);
46
+ }
47
+ throw new Error(`No handler registered for index: ${params.index}`);
48
+ }
49
+
50
+ async msearch(params) {
51
+ const handler = this.getRouteHandler(params.index);
52
+ if (handler && handler.msearch) {
53
+ return await handler.msearch(params);
54
+ }
55
+ throw new Error(`No handler registered for index: ${params.index}`);
56
+ }
57
+
58
+ async index(params) {
59
+ const handler = this.getRouteHandler(params.index);
60
+ if (handler && handler.index) {
61
+ return await handler.index(params);
62
+ }
63
+ throw new Error(`No handler registered for index: ${params.index}`);
64
+ }
65
+
66
+ async bulk(params) {
67
+ const operations = params.body || params.operations;
68
+ if (!Array.isArray(operations) || operations.length === 0) {
69
+ throw new Error('Bulk operations must be a non-empty array');
70
+ }
71
+
72
+ const firstOp = operations[0];
73
+ const indexName = firstOp?.index?._index || firstOp?.index?.index || firstOp?.create?._index || firstOp?.create?.index || params.index;
74
+
75
+ if (!indexName) {
76
+ throw new Error('Could not determine index from bulk operations');
77
+ }
78
+
79
+ const handler = this.getRouteHandler(indexName);
80
+ if (handler && handler.bulk) {
81
+ return await handler.bulk(params);
82
+ }
83
+ throw new Error(`No handler registered for index: ${indexName}`);
84
+ }
85
+
86
+ async get(params) {
87
+ const handler = this.getRouteHandler(params.index);
88
+ if (handler && handler.get) {
89
+ return await handler.get(params);
90
+ }
91
+ throw new Error(`No handler registered for index: ${params.index}`);
92
+ }
93
+
94
+ async mget(params) {
95
+ const handler = this.getRouteHandler(params.index);
96
+ if (handler && handler.mget) {
97
+ return await handler.mget(params);
98
+ }
99
+ throw new Error(`No handler registered for index: ${params.index}`);
100
+ }
101
+
102
+ async update(params) {
103
+ const handler = this.getRouteHandler(params.index);
104
+ if (handler && handler.update) {
105
+ return await handler.update(params);
106
+ }
107
+ throw new Error(`No handler registered for index: ${params.index}`);
108
+ }
109
+
110
+ async updateByQuery(params) {
111
+ const handler = this.getRouteHandler(params.index);
112
+ if (handler && handler.updateByQuery) {
113
+ return await handler.updateByQuery(params);
114
+ }
115
+ throw new Error(`No handler registered for index: ${params.index}`);
116
+ }
117
+
118
+ async delete(params) {
119
+ const handler = this.getRouteHandler(params.index);
120
+ if (handler && handler.delete) {
121
+ return await handler.delete(params);
122
+ }
123
+ throw new Error(`No handler registered for index: ${params.index}`);
124
+ }
125
+
126
+ async deleteByQuery(params) {
127
+ const handler = this.getRouteHandler(params.index);
128
+ if (handler && handler.deleteByQuery) {
129
+ return await handler.deleteByQuery(params);
130
+ }
131
+ throw new Error(`No handler registered for index: ${params.index}`);
132
+ }
133
+
134
+ async exists(params) {
135
+ const handler = this.getRouteHandler(params.index);
136
+ if (handler && handler.exists) {
137
+ return await handler.exists(params);
138
+ }
139
+ throw new Error(`No handler registered for index: ${params.index}`);
140
+ }
141
+
142
+ async count(params) {
143
+ const handler = this.getRouteHandler(params.index);
144
+ if (handler && handler.count) {
145
+ return await handler.count(params);
146
+ }
147
+ throw new Error(`No handler registered for index: ${params.index}`);
148
+ }
149
+
150
+ async scroll(params) {
151
+ const handler = this.getRouteHandler(params.index);
152
+ if (handler && handler.scroll) {
153
+ return await handler.scroll(params);
154
+ }
155
+ throw new Error(`No handler registered for index: ${params.index}`);
156
+ }
157
+
158
+ async clearScroll(params) {
159
+ const handler = this.getRouteHandler(params.index);
160
+ if (handler && handler.clearScroll) {
161
+ return await handler.clearScroll(params);
162
+ }
163
+ throw new Error(`No handler registered for index: ${params.index}`);
164
+ }
165
+
166
+ async reindex(params) {
167
+ const sourceIndex = params.body?.source?.index;
168
+ const destIndex = params.body?.dest?.index;
169
+
170
+ const handler = this.getRouteHandler(destIndex || sourceIndex);
171
+ if (handler && handler.reindex) {
172
+ return await handler.reindex(params);
173
+ }
174
+ throw new Error(`No handler registered for index: ${destIndex || sourceIndex}`);
175
+ }
176
+
177
+ indices() {
178
+ return {
179
+ create: async (params) => {
180
+ const handler = this.getRouteHandler(params.index);
181
+ if (handler && handler.indices && handler.indices.create) {
182
+ return await handler.indices.create(params);
183
+ }
184
+ throw new Error(`No handler registered for index: ${params.index}`);
185
+ },
186
+ delete: async (params) => {
187
+ const handler = this.getRouteHandler(params.index);
188
+ if (handler && handler.indices && handler.indices.delete) {
189
+ return await handler.indices.delete(params);
190
+ }
191
+ throw new Error(`No handler registered for index: ${params.index}`);
192
+ },
193
+ exists: async (params) => {
194
+ const handler = this.getRouteHandler(params.index);
195
+ if (handler && handler.indices && handler.indices.exists) {
196
+ return await handler.indices.exists(params);
197
+ }
198
+ throw new Error(`No handler registered for index: ${params.index}`);
199
+ },
200
+ refresh: async (params) => {
201
+ const handler = this.getRouteHandler(params.index);
202
+ if (handler && handler.indices && handler.indices.refresh) {
203
+ return await handler.indices.refresh(params);
204
+ }
205
+ throw new Error(`No handler registered for index: ${params.index}`);
206
+ },
207
+ flush: async (params) => {
208
+ const handler = this.getRouteHandler(params.index);
209
+ if (handler && handler.indices && handler.indices.flush) {
210
+ return await handler.indices.flush(params);
211
+ }
212
+ throw new Error(`No handler registered for index: ${params.index}`);
213
+ },
214
+ putMapping: async (params) => {
215
+ const handler = this.getRouteHandler(params.index);
216
+ if (handler && handler.indices && handler.indices.putMapping) {
217
+ return await handler.indices.putMapping(params);
218
+ }
219
+ throw new Error(`No handler registered for index: ${params.index}`);
220
+ },
221
+ getMapping: async (params) => {
222
+ const handler = this.getRouteHandler(params.index);
223
+ if (handler && handler.indices && handler.indices.getMapping) {
224
+ return await handler.indices.getMapping(params);
225
+ }
226
+ throw new Error(`No handler registered for index: ${params.index}`);
227
+ },
228
+ putSettings: async (params) => {
229
+ const handler = this.getRouteHandler(params.index);
230
+ if (handler && handler.indices && handler.indices.putSettings) {
231
+ return await handler.indices.putSettings(params);
232
+ }
233
+ throw new Error(`No handler registered for index: ${params.index}`);
234
+ },
235
+ getSettings: async (params) => {
236
+ const handler = this.getRouteHandler(params.index);
237
+ if (handler && handler.indices && handler.indices.getSettings) {
238
+ return await handler.indices.getSettings(params);
239
+ }
240
+ throw new Error(`No handler registered for index: ${params.index}`);
241
+ },
242
+ stats: async (params) => {
243
+ const handler = this.getRouteHandler(params.index);
244
+ if (handler && handler.indices && handler.indices.stats) {
245
+ return await handler.indices.stats(params);
246
+ }
247
+ throw new Error(`No handler registered for index: ${params.index}`);
248
+ },
249
+ get: async (params) => {
250
+ const handler = this.getRouteHandler(params.index);
251
+ if (handler && handler.indices && handler.indices.get) {
252
+ return await handler.indices.get(params);
253
+ }
254
+ throw new Error(`No handler registered for index: ${params.index}`);
255
+ },
256
+ putAlias: async (params) => {
257
+ const handler = this.getRouteHandler(params.index);
258
+ if (handler && handler.indices && handler.indices.putAlias) {
259
+ return await handler.indices.putAlias(params);
260
+ }
261
+ throw new Error(`No handler registered for index: ${params.index}`);
262
+ },
263
+ deleteAlias: async (params) => {
264
+ const handler = this.getRouteHandler(params.index);
265
+ if (handler && handler.indices && handler.indices.deleteAlias) {
266
+ return await handler.indices.deleteAlias(params);
267
+ }
268
+ throw new Error(`No handler registered for index: ${params.index}`);
269
+ },
270
+ getAlias: async (params) => {
271
+ const handler = this.getRouteHandler(params.index);
272
+ if (handler && handler.indices && handler.indices.getAlias) {
273
+ return await handler.indices.getAlias(params);
274
+ }
275
+ throw new Error(`No handler registered for index: ${params.index}`);
276
+ },
277
+ analyze: async (params) => {
278
+ const handler = this.getRouteHandler(params.index);
279
+ if (handler && handler.indices && handler.indices.analyze) {
280
+ return await handler.indices.analyze(params);
281
+ }
282
+ throw new Error(`No handler registered for index: ${params.index}`);
283
+ },
284
+ close: async (params) => {
285
+ const handler = this.getRouteHandler(params.index);
286
+ if (handler && handler.indices && handler.indices.close) {
287
+ return await handler.indices.close(params);
288
+ }
289
+ throw new Error(`No handler registered for index: ${params.index}`);
290
+ },
291
+ open: async (params) => {
292
+ const handler = this.getRouteHandler(params.index);
293
+ if (handler && handler.indices && handler.indices.open) {
294
+ return await handler.indices.open(params);
295
+ }
296
+ throw new Error(`No handler registered for index: ${params.index}`);
297
+ }
298
+ };
299
+ }
300
+
301
+ cat() {
302
+ return {
303
+ indices: async (params) => {
304
+ throw new Error('cat.indices not implemented');
305
+ },
306
+ health: async (params) => {
307
+ throw new Error('cat.health not implemented');
308
+ },
309
+ count: async (params) => {
310
+ throw new Error('cat.count not implemented');
311
+ },
312
+ aliases: async (params) => {
313
+ throw new Error('cat.aliases not implemented');
314
+ }
315
+ };
316
+ }
317
+
318
+ cluster() {
319
+ return {
320
+ health: async (params) => {
321
+ throw new Error('cluster.health not implemented');
322
+ },
323
+ stats: async (params) => {
324
+ throw new Error('cluster.stats not implemented');
325
+ },
326
+ putSettings: async (params) => {
327
+ throw new Error('cluster.putSettings not implemented');
328
+ },
329
+ getSettings: async (params) => {
330
+ throw new Error('cluster.getSettings not implemented');
331
+ }
332
+ };
333
+ }
334
+
335
+ async ping() {
336
+ return true;
337
+ }
338
+
339
+ async info() {
340
+ return {
341
+ name: 'pegasus-sdk',
342
+ version: '1.0.0',
343
+ cluster_name: 'pegasus'
344
+ };
345
+ }
346
+
347
+ async explain(params) {
348
+ const handler = this.getRouteHandler(params.index);
349
+ if (handler && handler.explain) {
350
+ return await handler.explain(params);
351
+ }
352
+ throw new Error(`No handler registered for index: ${params.index}`);
353
+ }
354
+
355
+ async termvectors(params) {
356
+ const handler = this.getRouteHandler(params.index);
357
+ if (handler && handler.termvectors) {
358
+ return await handler.termvectors(params);
359
+ }
360
+ throw new Error(`No handler registered for index: ${params.index}`);
361
+ }
362
+
363
+ async mtermvectors(params) {
364
+ const handler = this.getRouteHandler(params.index);
365
+ if (handler && handler.mtermvectors) {
366
+ return await handler.mtermvectors(params);
367
+ }
368
+ throw new Error(`No handler registered for index: ${params.index}`);
369
+ }
370
+
371
+ async searchTemplate(params) {
372
+ const handler = this.getRouteHandler(params.index);
373
+ if (handler && handler.searchTemplate) {
374
+ return await handler.searchTemplate(params);
375
+ }
376
+ throw new Error(`No handler registered for index: ${params.index}`);
377
+ }
378
+
379
+ async renderSearchTemplate(params) {
380
+ throw new Error('renderSearchTemplate not implemented');
381
+ }
382
+
383
+ async fieldCaps(params) {
384
+ const handler = this.getRouteHandler(params.index);
385
+ if (handler && handler.fieldCaps) {
386
+ return await handler.fieldCaps(params);
387
+ }
388
+ throw new Error(`No handler registered for index: ${params.index}`);
389
+ }
390
+
391
+ async rankEval(params) {
392
+ const handler = this.getRouteHandler(params.index);
393
+ if (handler && handler.rankEval) {
394
+ return await handler.rankEval(params);
395
+ }
396
+ throw new Error(`No handler registered for index: ${params.index}`);
397
+ }
398
+
399
+ async scriptsPainlessExecute(params) {
400
+ throw new Error('scriptsPainlessExecute not implemented');
401
+ }
402
+ }
403
+
404
+ module.exports = ElasticsearchService;