@opra/elastic 1.21.0 → 1.22.1
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/{esm/elastic-collection-service.js → elastic-collection-service.js} +12 -0
- package/{esm/elastic-entity-service.js → elastic-entity-service.js} +23 -2
- package/{esm/elastic-service.js → elastic-service.js} +11 -0
- package/package.json +10 -26
- package/cjs/adapter-utils/prepare-filter.js +0 -130
- package/cjs/adapter-utils/prepare-patch.js +0 -35
- package/cjs/adapter-utils/prepare-projection.js +0 -69
- package/cjs/adapter-utils/prepare-sort.js +0 -16
- package/cjs/elastic-adapter.js +0 -126
- package/cjs/elastic-collection-service.js +0 -409
- package/cjs/elastic-entity-service.js +0 -536
- package/cjs/elastic-service.js +0 -64
- package/cjs/index.js +0 -7
- package/cjs/package.json +0 -3
- package/esm/package.json +0 -3
- package/types/index.d.cts +0 -4
- /package/{types/adapter-utils → adapter-utils}/prepare-filter.d.ts +0 -0
- /package/{esm/adapter-utils → adapter-utils}/prepare-filter.js +0 -0
- /package/{types/adapter-utils → adapter-utils}/prepare-patch.d.ts +0 -0
- /package/{esm/adapter-utils → adapter-utils}/prepare-patch.js +0 -0
- /package/{types/adapter-utils → adapter-utils}/prepare-projection.d.ts +0 -0
- /package/{esm/adapter-utils → adapter-utils}/prepare-projection.js +0 -0
- /package/{types/adapter-utils → adapter-utils}/prepare-sort.d.ts +0 -0
- /package/{esm/adapter-utils → adapter-utils}/prepare-sort.js +0 -0
- /package/{types/elastic-adapter.d.ts → elastic-adapter.d.ts} +0 -0
- /package/{esm/elastic-adapter.js → elastic-adapter.js} +0 -0
- /package/{types/elastic-collection-service.d.ts → elastic-collection-service.d.ts} +0 -0
- /package/{types/elastic-entity-service.d.ts → elastic-entity-service.d.ts} +0 -0
- /package/{types/elastic-service.d.ts → elastic-service.d.ts} +0 -0
- /package/{types/index.d.ts → index.d.ts} +0 -0
- /package/{esm/index.js → index.js} +0 -0
|
@@ -1,409 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ElasticCollectionService = void 0;
|
|
4
|
-
const common_1 = require("@opra/common");
|
|
5
|
-
const elastic_adapter_js_1 = require("./elastic-adapter.js");
|
|
6
|
-
const elastic_entity_service_js_1 = require("./elastic-entity-service.js");
|
|
7
|
-
/**
|
|
8
|
-
* @class ElasticCollectionService
|
|
9
|
-
* @template T - The type of the documents in the collection.
|
|
10
|
-
*/
|
|
11
|
-
class ElasticCollectionService extends elastic_entity_service_js_1.ElasticEntityService {
|
|
12
|
-
/**
|
|
13
|
-
* Constructs a new instance
|
|
14
|
-
*
|
|
15
|
-
* @param {Type | string} dataType - The data type of the array elements.
|
|
16
|
-
* @param {ElasticCollectionService.Options} [options] - The options for the array service.
|
|
17
|
-
* @constructor
|
|
18
|
-
*/
|
|
19
|
-
constructor(dataType, options) {
|
|
20
|
-
super(dataType, options);
|
|
21
|
-
this.documentFilter = options?.documentFilter;
|
|
22
|
-
this.defaultLimit = options?.defaultLimit || 10;
|
|
23
|
-
}
|
|
24
|
-
for(context, overwriteProperties, overwriteContext) {
|
|
25
|
-
if (overwriteProperties?.documentFilter && this.documentFilter) {
|
|
26
|
-
overwriteProperties.documentFilter = [
|
|
27
|
-
...(Array.isArray(this.documentFilter)
|
|
28
|
-
? this.documentFilter
|
|
29
|
-
: [this.documentFilter]),
|
|
30
|
-
...(Array.isArray(overwriteProperties?.documentFilter)
|
|
31
|
-
? overwriteProperties.documentFilter
|
|
32
|
-
: [overwriteProperties.documentFilter]),
|
|
33
|
-
];
|
|
34
|
-
}
|
|
35
|
-
return super.for(context, overwriteProperties, overwriteContext);
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Asserts the existence of a resource with the given ID.
|
|
39
|
-
* Throws a ResourceNotFoundError if the resource does not exist.
|
|
40
|
-
*
|
|
41
|
-
* @param {string} id - The ID of the resource to assert.
|
|
42
|
-
* @param {ElasticEntityService.FindOneOptions} [options] - Optional options for checking the existence.
|
|
43
|
-
* @returns {Promise<void>} - A Promise that resolves when the resource exists.
|
|
44
|
-
* @throws {ResourceNotAvailableError} - If the resource does not exist.
|
|
45
|
-
*/
|
|
46
|
-
async assert(id, options) {
|
|
47
|
-
if (!(await this.exists(id, options)))
|
|
48
|
-
throw new common_1.ResourceNotAvailableError(this.getResourceName(), id);
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Adds a document to the specified index
|
|
52
|
-
*
|
|
53
|
-
* @param {PartialDTO<T>} input - The input data for creating the document.
|
|
54
|
-
* @param {ElasticEntityService.CreateOptions} [options] - The options for creating the document.
|
|
55
|
-
* @returns {Promise<PartialDTO<T>>} A promise that resolves to the created document.
|
|
56
|
-
* @throws {Error} if an unknown error occurs while creating the document.
|
|
57
|
-
*/
|
|
58
|
-
async create(input, options) {
|
|
59
|
-
const command = {
|
|
60
|
-
crud: 'create',
|
|
61
|
-
method: 'createOnly',
|
|
62
|
-
byId: false,
|
|
63
|
-
input,
|
|
64
|
-
options,
|
|
65
|
-
};
|
|
66
|
-
command.input._id =
|
|
67
|
-
command.input._id == null || command.input._id === ''
|
|
68
|
-
? this._generateId(command)
|
|
69
|
-
: command.input._id;
|
|
70
|
-
return this._executeCommand(command, () => this._create(command));
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* Returns the count of documents in the collection based on the provided options.
|
|
74
|
-
*
|
|
75
|
-
* @param {ElasticEntityService.CountOptions<T>} options - The options for the count operation.
|
|
76
|
-
* @return {Promise<number>} - A promise that resolves to the count of documents in the collection.
|
|
77
|
-
*/
|
|
78
|
-
async count(options) {
|
|
79
|
-
const command = {
|
|
80
|
-
crud: 'read',
|
|
81
|
-
method: 'count',
|
|
82
|
-
byId: false,
|
|
83
|
-
options,
|
|
84
|
-
};
|
|
85
|
-
return this._executeCommand(command, async () => {
|
|
86
|
-
const filter = elastic_adapter_js_1.ElasticAdapter.prepareFilter([
|
|
87
|
-
await this._getDocumentFilter(command),
|
|
88
|
-
command.options?.filter,
|
|
89
|
-
]);
|
|
90
|
-
command.options = { ...command.options, filter };
|
|
91
|
-
const r = await this._count(command);
|
|
92
|
-
return r.count;
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Deletes a document from the collection.
|
|
97
|
-
*
|
|
98
|
-
* @param {string} id - The ID of the document to delete.
|
|
99
|
-
* @param {ElasticEntityService.DeleteOptions} [options] - Optional delete options.
|
|
100
|
-
* @return {Promise<number>} - A Promise that resolves to the number of documents deleted.
|
|
101
|
-
*/
|
|
102
|
-
async delete(id, options) {
|
|
103
|
-
const command = {
|
|
104
|
-
crud: 'delete',
|
|
105
|
-
method: 'delete',
|
|
106
|
-
byId: true,
|
|
107
|
-
documentId: id,
|
|
108
|
-
options,
|
|
109
|
-
};
|
|
110
|
-
return this._executeCommand(command, async () => {
|
|
111
|
-
const filter = elastic_adapter_js_1.ElasticAdapter.prepareFilter([
|
|
112
|
-
await this._getDocumentFilter(command),
|
|
113
|
-
command.options?.filter,
|
|
114
|
-
]);
|
|
115
|
-
command.options = { ...command.options, filter };
|
|
116
|
-
return this._delete(command);
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* Deletes multiple documents from the collection that meet the specified filter criteria.
|
|
121
|
-
*
|
|
122
|
-
* @param {ElasticEntityService.DeleteManyOptions} options - The options for the delete operation.
|
|
123
|
-
* @return {Promise<number>} - A promise that resolves to the number of documents deleted.
|
|
124
|
-
*/
|
|
125
|
-
async deleteMany(options) {
|
|
126
|
-
const command = {
|
|
127
|
-
crud: 'delete',
|
|
128
|
-
method: 'deleteMany',
|
|
129
|
-
byId: false,
|
|
130
|
-
options,
|
|
131
|
-
};
|
|
132
|
-
return this._executeCommand(command, async () => {
|
|
133
|
-
const filter = elastic_adapter_js_1.ElasticAdapter.prepareFilter([
|
|
134
|
-
await this._getDocumentFilter(command),
|
|
135
|
-
command.options?.filter,
|
|
136
|
-
]);
|
|
137
|
-
command.options = { ...command.options, filter };
|
|
138
|
-
return this._deleteMany(command);
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* Checks if an object with the given id exists.
|
|
143
|
-
*
|
|
144
|
-
* @param {string} id - The id of the object to check.
|
|
145
|
-
* @param {ElasticEntityService.FindOneOptions} [options] - The options for the query (optional).
|
|
146
|
-
* @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the object exists or not.
|
|
147
|
-
*/
|
|
148
|
-
async exists(id, options) {
|
|
149
|
-
const command = {
|
|
150
|
-
crud: 'read',
|
|
151
|
-
method: 'exists',
|
|
152
|
-
byId: true,
|
|
153
|
-
documentId: id,
|
|
154
|
-
options,
|
|
155
|
-
};
|
|
156
|
-
return this._executeCommand(command, async () => {
|
|
157
|
-
const request = {
|
|
158
|
-
...command.options?.request,
|
|
159
|
-
terminate_after: 1,
|
|
160
|
-
size: 0,
|
|
161
|
-
};
|
|
162
|
-
const filter = elastic_adapter_js_1.ElasticAdapter.prepareFilter([
|
|
163
|
-
await this._getDocumentFilter(command),
|
|
164
|
-
command.options?.filter,
|
|
165
|
-
]);
|
|
166
|
-
command.options = { ...options, request, filter };
|
|
167
|
-
const r = await this._findMany(command);
|
|
168
|
-
return !!(typeof r.hits.total === 'number'
|
|
169
|
-
? r.hits.total
|
|
170
|
-
: r.hits.total?.value);
|
|
171
|
-
});
|
|
172
|
-
}
|
|
173
|
-
/**
|
|
174
|
-
* Checks if an object with the given arguments exists.
|
|
175
|
-
*
|
|
176
|
-
* @param {ElasticEntityService.FindOneOptions} [options] - The options for the query (optional).
|
|
177
|
-
* @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the object exists or not.
|
|
178
|
-
*/
|
|
179
|
-
async existsOne(options) {
|
|
180
|
-
const command = {
|
|
181
|
-
crud: 'read',
|
|
182
|
-
method: 'exists',
|
|
183
|
-
byId: false,
|
|
184
|
-
options,
|
|
185
|
-
};
|
|
186
|
-
return this._executeCommand(command, async () => {
|
|
187
|
-
const request = {
|
|
188
|
-
...command.options?.request,
|
|
189
|
-
terminate_after: 1,
|
|
190
|
-
size: 0,
|
|
191
|
-
};
|
|
192
|
-
command.options = { ...options, request };
|
|
193
|
-
const r = await this._findMany(command);
|
|
194
|
-
return !!(typeof r.hits.total === 'number'
|
|
195
|
-
? r.hits.total
|
|
196
|
-
: r.hits.total?.value);
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
/**
|
|
200
|
-
*
|
|
201
|
-
*/
|
|
202
|
-
async findById(id, options) {
|
|
203
|
-
const command = {
|
|
204
|
-
crud: 'read',
|
|
205
|
-
method: 'findById',
|
|
206
|
-
byId: true,
|
|
207
|
-
documentId: id,
|
|
208
|
-
options,
|
|
209
|
-
};
|
|
210
|
-
return this._executeCommand(command, async () => {
|
|
211
|
-
const documentFilter = await this._getDocumentFilter(command);
|
|
212
|
-
const filter = elastic_adapter_js_1.ElasticAdapter.prepareFilter([
|
|
213
|
-
documentFilter,
|
|
214
|
-
command.options?.filter,
|
|
215
|
-
]);
|
|
216
|
-
const newCommand = {
|
|
217
|
-
...command,
|
|
218
|
-
limit: 1,
|
|
219
|
-
options: { ...command.options, filter },
|
|
220
|
-
};
|
|
221
|
-
const r = await this._findMany(newCommand);
|
|
222
|
-
if (r.hits.hits?.length) {
|
|
223
|
-
const outputCodec = this.getOutputCodec('find');
|
|
224
|
-
return {
|
|
225
|
-
_id: r.hits.hits[0]._id,
|
|
226
|
-
...outputCodec(r.hits.hits[0]._source),
|
|
227
|
-
};
|
|
228
|
-
}
|
|
229
|
-
});
|
|
230
|
-
}
|
|
231
|
-
/**
|
|
232
|
-
*
|
|
233
|
-
*/
|
|
234
|
-
async findOne(options) {
|
|
235
|
-
const command = {
|
|
236
|
-
crud: 'read',
|
|
237
|
-
method: 'findOne',
|
|
238
|
-
byId: false,
|
|
239
|
-
options,
|
|
240
|
-
};
|
|
241
|
-
return this._executeCommand(command, async () => {
|
|
242
|
-
const filter = elastic_adapter_js_1.ElasticAdapter.prepareFilter([
|
|
243
|
-
await this._getDocumentFilter(command),
|
|
244
|
-
command.options?.filter,
|
|
245
|
-
]);
|
|
246
|
-
const newCommand = {
|
|
247
|
-
...command,
|
|
248
|
-
limit: 1,
|
|
249
|
-
options: { ...command.options, filter },
|
|
250
|
-
};
|
|
251
|
-
const r = await this._findMany(newCommand);
|
|
252
|
-
if (r.hits.hits?.length) {
|
|
253
|
-
const outputCodec = this.getOutputCodec('find');
|
|
254
|
-
return {
|
|
255
|
-
_id: r.hits.hits[0]._id,
|
|
256
|
-
...outputCodec(r.hits.hits[0]._source),
|
|
257
|
-
};
|
|
258
|
-
}
|
|
259
|
-
});
|
|
260
|
-
}
|
|
261
|
-
/**
|
|
262
|
-
*
|
|
263
|
-
*/
|
|
264
|
-
async findMany(options) {
|
|
265
|
-
const command = {
|
|
266
|
-
crud: 'read',
|
|
267
|
-
method: 'findMany',
|
|
268
|
-
byId: false,
|
|
269
|
-
options,
|
|
270
|
-
};
|
|
271
|
-
return this._executeCommand(command, async () => {
|
|
272
|
-
const filter = elastic_adapter_js_1.ElasticAdapter.prepareFilter([
|
|
273
|
-
await this._getDocumentFilter(command),
|
|
274
|
-
command.options?.filter,
|
|
275
|
-
]);
|
|
276
|
-
const limit = command.options?.limit || this.defaultLimit;
|
|
277
|
-
command.options = { ...command.options, filter, limit };
|
|
278
|
-
const r = await this._findMany(command);
|
|
279
|
-
return r.hits?.hits.map((x) => x._source) || [];
|
|
280
|
-
});
|
|
281
|
-
}
|
|
282
|
-
async searchRaw(request, options) {
|
|
283
|
-
const command = {
|
|
284
|
-
crud: 'read',
|
|
285
|
-
method: 'searchRaw',
|
|
286
|
-
byId: false,
|
|
287
|
-
request,
|
|
288
|
-
options,
|
|
289
|
-
};
|
|
290
|
-
return this._executeCommand(command, async () => this._searchRaw(command));
|
|
291
|
-
}
|
|
292
|
-
/**
|
|
293
|
-
*
|
|
294
|
-
*/
|
|
295
|
-
async findManyWithCount(options) {
|
|
296
|
-
const command = {
|
|
297
|
-
crud: 'read',
|
|
298
|
-
method: 'findManyWithCount',
|
|
299
|
-
byId: false,
|
|
300
|
-
options,
|
|
301
|
-
};
|
|
302
|
-
return this._executeCommand(command, async () => {
|
|
303
|
-
const filter = elastic_adapter_js_1.ElasticAdapter.prepareFilter([
|
|
304
|
-
await this._getDocumentFilter(command),
|
|
305
|
-
command.options?.filter,
|
|
306
|
-
]);
|
|
307
|
-
const limit = command.options?.limit || this.defaultLimit;
|
|
308
|
-
command.options = {
|
|
309
|
-
...command.options,
|
|
310
|
-
filter,
|
|
311
|
-
limit,
|
|
312
|
-
request: { ...command.options?.request, track_total_hits: true },
|
|
313
|
-
};
|
|
314
|
-
const r = await this._findMany(command);
|
|
315
|
-
const out = {};
|
|
316
|
-
if (r.hits.hits?.length) {
|
|
317
|
-
const outputCodec = this.getOutputCodec('find');
|
|
318
|
-
out.items = r.hits.hits.map((x) => ({
|
|
319
|
-
_id: x._id,
|
|
320
|
-
...outputCodec(x._source),
|
|
321
|
-
}));
|
|
322
|
-
}
|
|
323
|
-
else
|
|
324
|
-
out.items = [];
|
|
325
|
-
if (typeof r.hits.total === 'object') {
|
|
326
|
-
out.count = r.hits.total.value;
|
|
327
|
-
out.relation = r.hits.total.relation;
|
|
328
|
-
}
|
|
329
|
-
else
|
|
330
|
-
out.count = r.hits.total || 0;
|
|
331
|
-
return out;
|
|
332
|
-
});
|
|
333
|
-
}
|
|
334
|
-
/**
|
|
335
|
-
*
|
|
336
|
-
*/
|
|
337
|
-
async get(id, options) {
|
|
338
|
-
const out = await this.findById(id, options);
|
|
339
|
-
if (!out)
|
|
340
|
-
throw new common_1.ResourceNotAvailableError(this.getResourceName(), id);
|
|
341
|
-
return out;
|
|
342
|
-
}
|
|
343
|
-
/**
|
|
344
|
-
* Updates a document in the collection with the specified ID.
|
|
345
|
-
*
|
|
346
|
-
* @param {string} id - The ID of the document to update.
|
|
347
|
-
* @param {PatchDTO<T>} input - The partial input data to update the document with.
|
|
348
|
-
* @param {ElasticEntityService.UpdateOneOptions} [options] - The options for updating the document.
|
|
349
|
-
* @returns {Promise<estypes.UpdateResponse>} - A promise that resolves to the number of documents modified.
|
|
350
|
-
*/
|
|
351
|
-
async update(id, input, options) {
|
|
352
|
-
const command = {
|
|
353
|
-
crud: 'update',
|
|
354
|
-
method: 'update',
|
|
355
|
-
documentId: id,
|
|
356
|
-
byId: true,
|
|
357
|
-
input,
|
|
358
|
-
options,
|
|
359
|
-
};
|
|
360
|
-
return this._executeCommand(command, async () => {
|
|
361
|
-
const filter = elastic_adapter_js_1.ElasticAdapter.prepareFilter([
|
|
362
|
-
await this._getDocumentFilter(command),
|
|
363
|
-
command.options?.filter,
|
|
364
|
-
]);
|
|
365
|
-
command.options = { ...command.options, filter };
|
|
366
|
-
return await this._updateMany(command);
|
|
367
|
-
});
|
|
368
|
-
}
|
|
369
|
-
/**
|
|
370
|
-
* Updates multiple documents in the collection based on the specified input and options.
|
|
371
|
-
*
|
|
372
|
-
* @param {PatchDTO<T>} input - The partial input to update the documents with.
|
|
373
|
-
* @param {ElasticEntityService.UpdateManyOptions} options - The options for updating the documents.
|
|
374
|
-
* @return {Promise<number>} - A promise that resolves to the number of documents matched and modified.
|
|
375
|
-
*/
|
|
376
|
-
async updateMany(input, options) {
|
|
377
|
-
const command = {
|
|
378
|
-
crud: 'update',
|
|
379
|
-
method: 'updateMany',
|
|
380
|
-
byId: false,
|
|
381
|
-
input,
|
|
382
|
-
options,
|
|
383
|
-
};
|
|
384
|
-
return this._executeCommand(command, async () => {
|
|
385
|
-
const filter = elastic_adapter_js_1.ElasticAdapter.prepareFilter([
|
|
386
|
-
await this._getDocumentFilter(command),
|
|
387
|
-
command.options?.filter,
|
|
388
|
-
]);
|
|
389
|
-
command.options = { ...command.options, filter };
|
|
390
|
-
return this._updateMany(command);
|
|
391
|
-
});
|
|
392
|
-
}
|
|
393
|
-
/**
|
|
394
|
-
* Retrieves the common filter used for querying documents.
|
|
395
|
-
* This method is mostly used for security issues like securing multi-tenant applications.
|
|
396
|
-
*
|
|
397
|
-
* @protected
|
|
398
|
-
* @returns {FilterInput | Promise<FilterInput> | undefined} The common filter or a Promise
|
|
399
|
-
* that resolves to the common filter, or undefined if not available.
|
|
400
|
-
*/
|
|
401
|
-
_getDocumentFilter(command) {
|
|
402
|
-
const commonFilter = Array.isArray(this.documentFilter)
|
|
403
|
-
? this.documentFilter
|
|
404
|
-
: [this.documentFilter];
|
|
405
|
-
const mapped = commonFilter.map(f => typeof f === 'function' ? f(command, this) : f);
|
|
406
|
-
return mapped.length > 1 ? elastic_adapter_js_1.ElasticAdapter.prepareFilter(mapped) : mapped[0];
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
exports.ElasticCollectionService = ElasticCollectionService;
|