@opra/elastic 1.0.0-alpha.32 → 1.0.0-beta.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/cjs/adapter-utils/prepare-filter.js +88 -80
- package/cjs/adapter-utils/prepare-patch.js +35 -0
- package/cjs/adapter-utils/prepare-projection.js +65 -108
- package/cjs/adapter-utils/prepare-sort.js +5 -6
- package/cjs/elastic-adapter.js +4 -7
- package/cjs/elastic-collection-service.js +365 -0
- package/cjs/elastic-entity-service.js +392 -0
- package/cjs/elastic-service.js +20 -103
- package/cjs/index.js +3 -0
- package/esm/adapter-utils/prepare-filter.js +88 -80
- package/esm/adapter-utils/prepare-patch.js +32 -0
- package/esm/adapter-utils/prepare-projection.js +62 -109
- package/esm/adapter-utils/prepare-sort.js +5 -6
- package/esm/elastic-adapter.js +4 -7
- package/esm/elastic-collection-service.js +361 -0
- package/esm/elastic-entity-service.js +388 -0
- package/esm/elastic-service.js +20 -103
- package/esm/index.js +3 -0
- package/esm/package.json +3 -0
- package/package.json +27 -35
- package/types/adapter-utils/prepare-filter.d.ts +3 -2
- package/types/adapter-utils/prepare-patch.d.ts +2 -0
- package/types/adapter-utils/prepare-projection.d.ts +7 -0
- package/types/elastic-adapter.d.ts +7 -2
- package/types/elastic-collection-service.d.ts +217 -0
- package/types/elastic-entity-service.d.ts +262 -0
- package/types/elastic-service.d.ts +20 -99
- package/types/index.d.cts +4 -0
- package/types/index.d.ts +3 -0
- package/cjs/adapter-utils/prepare-key-values.js +0 -22
- package/esm/adapter-utils/prepare-key-values.js +0 -18
- package/types/adapter-utils/prepare-key-values.d.ts +0 -1
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import type * as elastic from '@elastic/elasticsearch/lib/api/types';
|
|
2
|
+
import { HttpContext } from '@opra/core';
|
|
3
|
+
import type { DTO, Nullish, PartialDTO, PatchDTO, RequiredSome, Type } from 'ts-gems';
|
|
4
|
+
import { ElasticAdapter } from './elastic-adapter.js';
|
|
5
|
+
import { ElasticEntityService } from './elastic-entity-service.js';
|
|
6
|
+
import { ElasticService } from './elastic-service.js';
|
|
7
|
+
/**
|
|
8
|
+
*
|
|
9
|
+
* @namespace ElasticCollectionService
|
|
10
|
+
*/
|
|
11
|
+
export declare namespace ElasticCollectionService {
|
|
12
|
+
/**
|
|
13
|
+
* The constructor options of ElasticCollectionService.
|
|
14
|
+
*
|
|
15
|
+
* @interface Options
|
|
16
|
+
* @extends ElasticService.Options
|
|
17
|
+
*/
|
|
18
|
+
interface Options extends ElasticEntityService.Options {
|
|
19
|
+
documentFilter?: ElasticCollectionService['documentFilter'];
|
|
20
|
+
defaultLimit?: number;
|
|
21
|
+
}
|
|
22
|
+
type DocumentFilter = ElasticAdapter.FilterInput | ((args: ElasticEntityService.CommandInfo, _this: ElasticCollectionService) => ElasticAdapter.FilterInput | Promise<ElasticAdapter.FilterInput> | undefined);
|
|
23
|
+
interface FindManyWithCountResult<X> {
|
|
24
|
+
items: X[];
|
|
25
|
+
count: number;
|
|
26
|
+
relation: elastic.SearchTotalHitsRelation;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* @class ElasticCollectionService
|
|
31
|
+
* @template T - The type of the documents in the collection.
|
|
32
|
+
*/
|
|
33
|
+
export declare class ElasticCollectionService<T extends object = any> extends ElasticEntityService<T> {
|
|
34
|
+
/**
|
|
35
|
+
* Represents a common filter function for a ElasticService.
|
|
36
|
+
*
|
|
37
|
+
* @type {FilterInput | Function}
|
|
38
|
+
*/
|
|
39
|
+
documentFilter?: ElasticCollectionService.DocumentFilter | ElasticCollectionService.DocumentFilter[];
|
|
40
|
+
/**
|
|
41
|
+
* Represents the default limit value for a certain operation.
|
|
42
|
+
*
|
|
43
|
+
* @type {number}
|
|
44
|
+
*/
|
|
45
|
+
defaultLimit: number;
|
|
46
|
+
/**
|
|
47
|
+
* Constructs a new instance
|
|
48
|
+
*
|
|
49
|
+
* @param {Type | string} dataType - The data type of the array elements.
|
|
50
|
+
* @param {ElasticCollectionService.Options} [options] - The options for the array service.
|
|
51
|
+
* @constructor
|
|
52
|
+
*/
|
|
53
|
+
constructor(dataType: Type | string, options?: ElasticCollectionService.Options);
|
|
54
|
+
for<C extends HttpContext, P extends Partial<this>>(context: C, overwriteProperties?: Nullish<P>, overwriteContext?: Partial<C>): this & Required<P>;
|
|
55
|
+
/**
|
|
56
|
+
* Asserts the existence of a resource with the given ID.
|
|
57
|
+
* Throws a ResourceNotFoundError if the resource does not exist.
|
|
58
|
+
*
|
|
59
|
+
* @param {string} id - The ID of the resource to assert.
|
|
60
|
+
* @param {ElasticEntityService.FindOneOptions} [options] - Optional options for checking the existence.
|
|
61
|
+
* @returns {Promise<void>} - A Promise that resolves when the resource exists.
|
|
62
|
+
* @throws {ResourceNotAvailableError} - If the resource does not exist.
|
|
63
|
+
*/
|
|
64
|
+
assert(id: string, options?: ElasticEntityService.FindOneOptions): Promise<void>;
|
|
65
|
+
/**
|
|
66
|
+
* Adds a document to the specified index
|
|
67
|
+
*
|
|
68
|
+
* @param {PartialDTO<T>} input - The input data for creating the document.
|
|
69
|
+
* @param {ElasticEntityService.CreateOptions} [options] - The options for creating the document.
|
|
70
|
+
* @returns {Promise<PartialDTO<T>>} A promise that resolves to the created document.
|
|
71
|
+
* @throws {Error} if an unknown error occurs while creating the document.
|
|
72
|
+
*/
|
|
73
|
+
create(input: DTO<T>, options?: ElasticEntityService.CreateOptions): Promise<elastic.CreateResponse>;
|
|
74
|
+
/**
|
|
75
|
+
* Returns the count of documents in the collection based on the provided options.
|
|
76
|
+
*
|
|
77
|
+
* @param {ElasticEntityService.CountOptions<T>} options - The options for the count operation.
|
|
78
|
+
* @return {Promise<number>} - A promise that resolves to the count of documents in the collection.
|
|
79
|
+
*/
|
|
80
|
+
count(options?: ElasticEntityService.CountOptions): Promise<number>;
|
|
81
|
+
/**
|
|
82
|
+
* Deletes a document from the collection.
|
|
83
|
+
*
|
|
84
|
+
* @param {string} id - The ID of the document to delete.
|
|
85
|
+
* @param {ElasticEntityService.DeleteOptions} [options] - Optional delete options.
|
|
86
|
+
* @return {Promise<number>} - A Promise that resolves to the number of documents deleted.
|
|
87
|
+
*/
|
|
88
|
+
delete(id: string, options?: ElasticEntityService.DeleteOptions): Promise<elastic.DeleteByQueryResponse>;
|
|
89
|
+
/**
|
|
90
|
+
* Deletes multiple documents from the collection that meet the specified filter criteria.
|
|
91
|
+
*
|
|
92
|
+
* @param {ElasticEntityService.DeleteManyOptions} options - The options for the delete operation.
|
|
93
|
+
* @return {Promise<number>} - A promise that resolves to the number of documents deleted.
|
|
94
|
+
*/
|
|
95
|
+
deleteMany(options?: ElasticEntityService.DeleteManyOptions): Promise<elastic.DeleteByQueryResponse>;
|
|
96
|
+
/**
|
|
97
|
+
* Checks if an object with the given id exists.
|
|
98
|
+
*
|
|
99
|
+
* @param {string} id - The id of the object to check.
|
|
100
|
+
* @param {ElasticEntityService.FindOneOptions} [options] - The options for the query (optional).
|
|
101
|
+
* @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the object exists or not.
|
|
102
|
+
*/
|
|
103
|
+
exists(id: string, options?: ElasticEntityService.FindOneOptions): Promise<boolean>;
|
|
104
|
+
/**
|
|
105
|
+
* Checks if an object with the given arguments exists.
|
|
106
|
+
*
|
|
107
|
+
* @param {ElasticEntityService.FindOneOptions} [options] - The options for the query (optional).
|
|
108
|
+
* @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the object exists or not.
|
|
109
|
+
*/
|
|
110
|
+
existsOne(options?: ElasticEntityService.FindOneOptions): Promise<boolean>;
|
|
111
|
+
/**
|
|
112
|
+
* Finds a document by its ID.
|
|
113
|
+
*
|
|
114
|
+
* @param {string} id - The ID of the document.
|
|
115
|
+
* @param {ElasticEntityService.FindOneOptions} [options] - The options for the find query.
|
|
116
|
+
* @return {Promise<PartialDTO<T | undefined>>} - A promise resolving to the found document, or undefined if not found.
|
|
117
|
+
*/
|
|
118
|
+
findById(id: string, options: RequiredSome<ElasticEntityService.FindOneOptions, 'projection'>): Promise<PartialDTO<T> | undefined>;
|
|
119
|
+
/**
|
|
120
|
+
* Finds a document by its ID.
|
|
121
|
+
*
|
|
122
|
+
* @param {string} id - The ID of the document.
|
|
123
|
+
* @param {ElasticEntityService.FindOneOptions} [options] - The options for the find query.
|
|
124
|
+
* @return {Promise<T | undefined>} - A promise resolving to the found document, or undefined if not found.
|
|
125
|
+
*/
|
|
126
|
+
findById(id: string, options?: ElasticEntityService.FindOneOptions): Promise<T | undefined>;
|
|
127
|
+
/**
|
|
128
|
+
* Finds a document in the collection that matches the specified options.
|
|
129
|
+
*
|
|
130
|
+
* @param {ElasticEntityService.FindOneOptions} [options] - The options for the query.
|
|
131
|
+
* @return {Promise<PartialDTO<T> | undefined>} A promise that resolves with the found document or undefined if no document is found.
|
|
132
|
+
*/
|
|
133
|
+
findOne(options: RequiredSome<ElasticEntityService.FindOneOptions, 'projection'>): Promise<PartialDTO<T> | undefined>;
|
|
134
|
+
/**
|
|
135
|
+
* Finds a document in the collection that matches the specified options.
|
|
136
|
+
*
|
|
137
|
+
* @param {ElasticEntityService.FindOneOptions} [options] - The options for the query.
|
|
138
|
+
* @return {Promise<T | undefined>} A promise that resolves with the found document or undefined if no document is found.
|
|
139
|
+
*/
|
|
140
|
+
findOne(options?: ElasticEntityService.FindOneOptions): Promise<T | undefined>;
|
|
141
|
+
/**
|
|
142
|
+
* Finds multiple documents in the ElasticDB collection.
|
|
143
|
+
*
|
|
144
|
+
* @param {ElasticEntityService.SearchOptions} options - The options for the find operation.
|
|
145
|
+
* @return {Promise<PartialDTO<T> | undefined>} A Promise that resolves to an array of partial outputs of type T.
|
|
146
|
+
*/
|
|
147
|
+
findMany(options: RequiredSome<ElasticEntityService.SearchOptions, 'projection'>): Promise<PartialDTO<T>[]>;
|
|
148
|
+
/**
|
|
149
|
+
* Finds multiple documents in the ElasticDB collection.
|
|
150
|
+
*
|
|
151
|
+
* @param {ElasticEntityService.SearchOptions} options - The options for the find operation.
|
|
152
|
+
* @return {Promise<T | undefined>} A Promise that resolves to an array of partial outputs of type T.
|
|
153
|
+
*/
|
|
154
|
+
findMany(options?: ElasticEntityService.SearchOptions): Promise<T[]>;
|
|
155
|
+
/**
|
|
156
|
+
* Finds multiple documents in the collection and returns both records (max limit)
|
|
157
|
+
* and total count that matched the given criteria
|
|
158
|
+
*
|
|
159
|
+
* @param {ElasticEntityService.SearchOptions} [options] - The options for the find operation.
|
|
160
|
+
* @return {ElasticCollectionService.FindManyWithCountResult<PartialDTO<T>>} A Promise that resolves to an array of partial outputs of type T.
|
|
161
|
+
*/
|
|
162
|
+
findManyWithCount(options: RequiredSome<ElasticEntityService.SearchOptions, 'projection'>): Promise<ElasticCollectionService.FindManyWithCountResult<PartialDTO<T>>>;
|
|
163
|
+
/**
|
|
164
|
+
* Finds multiple documents in the collection and returns both records (max limit)
|
|
165
|
+
* and total count that matched the given criteria
|
|
166
|
+
*
|
|
167
|
+
* @param {ElasticEntityService.SearchOptions} [options] - The options for the find operation.
|
|
168
|
+
* @return {ElasticCollectionService.FindManyWithCountResult<T>} A Promise that resolves to an array of partial outputs of type T.
|
|
169
|
+
*/
|
|
170
|
+
findManyWithCount(options?: ElasticEntityService.SearchOptions): Promise<ElasticCollectionService.FindManyWithCountResult<T>>;
|
|
171
|
+
/**
|
|
172
|
+
* Retrieves a document from the collection by its ID. Throws error if not found.
|
|
173
|
+
*
|
|
174
|
+
* @param {string} id - The ID of the document to retrieve.
|
|
175
|
+
* @param {ElasticEntityService.FindOneOptions<T>} [options] - Optional options for the findOne operation.
|
|
176
|
+
* @returns {Promise<PartialDTO<T>>} - A promise that resolves to the retrieved document,
|
|
177
|
+
* or rejects with a ResourceNotFoundError if the document does not exist.
|
|
178
|
+
* @throws {ResourceNotAvailableError} - If the document with the specified ID does not exist.
|
|
179
|
+
*/
|
|
180
|
+
get(id: string, options: RequiredSome<ElasticEntityService.FindOneOptions, 'projection'>): Promise<PartialDTO<T>>;
|
|
181
|
+
/**
|
|
182
|
+
* Retrieves a document from the collection by its ID. Throws error if not found.
|
|
183
|
+
*
|
|
184
|
+
* @param {string} id - The ID of the document to retrieve.
|
|
185
|
+
* @param {ElasticEntityService.FindOneOptions<T>} [options] - Optional options for the findOne operation.
|
|
186
|
+
* @returns {Promise<T>} - A promise that resolves to the retrieved document,
|
|
187
|
+
* or rejects with a ResourceNotFoundError if the document does not exist.
|
|
188
|
+
* @throws {ResourceNotAvailableError} - If the document with the specified ID does not exist.
|
|
189
|
+
*/
|
|
190
|
+
get(id: string, options?: ElasticEntityService.FindOneOptions): Promise<T>;
|
|
191
|
+
/**
|
|
192
|
+
* Updates a document in the collection with the specified ID.
|
|
193
|
+
*
|
|
194
|
+
* @param {string} id - The ID of the document to update.
|
|
195
|
+
* @param {PatchDTO<T>} input - The partial input data to update the document with.
|
|
196
|
+
* @param {ElasticEntityService.UpdateOneOptions} [options] - The options for updating the document.
|
|
197
|
+
* @returns {Promise<elastic.UpdateResponse>} - A promise that resolves to the number of documents modified.
|
|
198
|
+
*/
|
|
199
|
+
update(id: string, input: PatchDTO<T>, options?: ElasticEntityService.UpdateOneOptions): Promise<elastic.UpdateByQueryResponse>;
|
|
200
|
+
/**
|
|
201
|
+
* Updates multiple documents in the collection based on the specified input and options.
|
|
202
|
+
*
|
|
203
|
+
* @param {PatchDTO<T>} input - The partial input to update the documents with.
|
|
204
|
+
* @param {ElasticEntityService.UpdateManyOptions} options - The options for updating the documents.
|
|
205
|
+
* @return {Promise<number>} - A promise that resolves to the number of documents matched and modified.
|
|
206
|
+
*/
|
|
207
|
+
updateMany(input: PatchDTO<T>, options?: ElasticEntityService.UpdateManyOptions): Promise<elastic.UpdateByQueryResponse>;
|
|
208
|
+
/**
|
|
209
|
+
* Retrieves the common filter used for querying documents.
|
|
210
|
+
* This method is mostly used for security issues like securing multi-tenant applications.
|
|
211
|
+
*
|
|
212
|
+
* @protected
|
|
213
|
+
* @returns {FilterInput | Promise<FilterInput> | undefined} The common filter or a Promise
|
|
214
|
+
* that resolves to the common filter, or undefined if not available.
|
|
215
|
+
*/
|
|
216
|
+
protected _getDocumentFilter(command: ElasticService.CommandInfo): ElasticAdapter.FilterInput | Promise<ElasticAdapter.FilterInput> | undefined;
|
|
217
|
+
}
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import type * as elastic from '@elastic/elasticsearch/lib/api/types.js';
|
|
2
|
+
import type { TransportRequestOptions } from '@elastic/transport';
|
|
3
|
+
import { ComplexType } from '@opra/common';
|
|
4
|
+
import type { PartialDTO, PatchDTO, RequiredSome, StrictOmit, Type } from 'ts-gems';
|
|
5
|
+
import { type IsObject } from 'valgen';
|
|
6
|
+
import { ElasticAdapter } from './elastic-adapter.js';
|
|
7
|
+
import { ElasticService } from './elastic-service.js';
|
|
8
|
+
/**
|
|
9
|
+
*
|
|
10
|
+
* @namespace ElasticEntityService
|
|
11
|
+
*/
|
|
12
|
+
export declare namespace ElasticEntityService {
|
|
13
|
+
/**
|
|
14
|
+
* The constructor options of ElasticEntityService.
|
|
15
|
+
*
|
|
16
|
+
* @interface Options
|
|
17
|
+
* @extends ElasticService.Options
|
|
18
|
+
*/
|
|
19
|
+
interface Options extends ElasticService.Options {
|
|
20
|
+
indexName?: ElasticEntityService['indexName'];
|
|
21
|
+
resourceName?: ElasticEntityService['resourceName'];
|
|
22
|
+
idGenerator?: ElasticEntityService['idGenerator'];
|
|
23
|
+
}
|
|
24
|
+
interface CommandInfo extends ElasticService.CommandInfo {
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Represents options for "create" operation
|
|
28
|
+
*
|
|
29
|
+
* @interface
|
|
30
|
+
*/
|
|
31
|
+
interface CreateOptions {
|
|
32
|
+
request?: elastic.CreateRequest;
|
|
33
|
+
transport?: TransportRequestOptions;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Represents options for "count" operation
|
|
37
|
+
*
|
|
38
|
+
* @interface
|
|
39
|
+
* @template T - The type of the document.
|
|
40
|
+
*/
|
|
41
|
+
interface CountOptions {
|
|
42
|
+
filter?: ElasticAdapter.FilterInput;
|
|
43
|
+
request?: elastic.CountRequest;
|
|
44
|
+
transport?: TransportRequestOptions;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Represents options for "delete" operation
|
|
48
|
+
*
|
|
49
|
+
* @interface
|
|
50
|
+
* @template T - The type of the document.
|
|
51
|
+
*/
|
|
52
|
+
interface DeleteOptions {
|
|
53
|
+
filter?: ElasticAdapter.FilterInput;
|
|
54
|
+
request?: elastic.DeleteByQueryRequest;
|
|
55
|
+
transport?: TransportRequestOptions;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Represents options for "deleteMany" operation
|
|
59
|
+
*
|
|
60
|
+
* @interface
|
|
61
|
+
* @template T - The type of the document.
|
|
62
|
+
*/
|
|
63
|
+
interface DeleteManyOptions {
|
|
64
|
+
filter?: ElasticAdapter.FilterInput;
|
|
65
|
+
request?: elastic.DeleteByQueryRequest;
|
|
66
|
+
transport?: TransportRequestOptions;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Represents options for "findOne" operation
|
|
70
|
+
*
|
|
71
|
+
* @interface
|
|
72
|
+
* @template T - The type of the document.
|
|
73
|
+
*/
|
|
74
|
+
interface FindOneOptions extends StrictOmit<SearchOptions, 'limit'> {
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Represents options for "findMany" operation
|
|
78
|
+
*
|
|
79
|
+
* @interface
|
|
80
|
+
* @template T - The type of the document.
|
|
81
|
+
*/
|
|
82
|
+
interface SearchOptions {
|
|
83
|
+
filter?: ElasticAdapter.FilterInput;
|
|
84
|
+
projection?: string | string[];
|
|
85
|
+
sort?: string[];
|
|
86
|
+
limit?: number;
|
|
87
|
+
skip?: number;
|
|
88
|
+
request?: elastic.SearchRequest;
|
|
89
|
+
transport?: TransportRequestOptions;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Represents options for "update" operation
|
|
93
|
+
*
|
|
94
|
+
* @interface
|
|
95
|
+
* @template T - The type of the document.
|
|
96
|
+
*/
|
|
97
|
+
interface UpdateOneOptions {
|
|
98
|
+
filter?: ElasticAdapter.FilterInput;
|
|
99
|
+
request?: elastic.UpdateByQueryRequest;
|
|
100
|
+
transport?: TransportRequestOptions;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Represents options for "updateMany" operation
|
|
104
|
+
*
|
|
105
|
+
* @interface
|
|
106
|
+
* @template T - The type of the document.
|
|
107
|
+
*/
|
|
108
|
+
interface UpdateManyOptions {
|
|
109
|
+
filter?: ElasticAdapter.FilterInput;
|
|
110
|
+
request?: elastic.UpdateByQueryRequest;
|
|
111
|
+
transport?: TransportRequestOptions;
|
|
112
|
+
}
|
|
113
|
+
interface CreateCommand extends StrictOmit<RequiredSome<CommandInfo, 'input'>, 'documentId'> {
|
|
114
|
+
crud: 'create';
|
|
115
|
+
options?: CreateOptions;
|
|
116
|
+
}
|
|
117
|
+
interface CountCommand extends StrictOmit<CommandInfo, 'documentId' | 'input'> {
|
|
118
|
+
crud: 'read';
|
|
119
|
+
options?: CountOptions;
|
|
120
|
+
}
|
|
121
|
+
interface DeleteCommand extends StrictOmit<CommandInfo, 'input'> {
|
|
122
|
+
crud: 'delete';
|
|
123
|
+
options?: DeleteOptions;
|
|
124
|
+
}
|
|
125
|
+
interface DeleteManyCommand extends StrictOmit<CommandInfo, 'input'> {
|
|
126
|
+
crud: 'delete';
|
|
127
|
+
options?: DeleteManyOptions;
|
|
128
|
+
}
|
|
129
|
+
interface SearchCommand extends StrictOmit<CommandInfo, 'input'> {
|
|
130
|
+
crud: 'read';
|
|
131
|
+
options?: SearchOptions;
|
|
132
|
+
}
|
|
133
|
+
interface UpdateCommand<T> extends CommandInfo {
|
|
134
|
+
crud: 'update';
|
|
135
|
+
input: PatchDTO<T>;
|
|
136
|
+
options?: UpdateOneOptions;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* @class ElasticEntityService
|
|
141
|
+
* @template T - The type of the documents in the collection.
|
|
142
|
+
*/
|
|
143
|
+
export declare class ElasticEntityService<T extends object = any> extends ElasticService {
|
|
144
|
+
protected _dataType_: Type | string;
|
|
145
|
+
protected _dataType?: ComplexType;
|
|
146
|
+
protected _inputCodecs: Record<string, IsObject.Validator<T>>;
|
|
147
|
+
protected _outputCodecs: Record<string, IsObject.Validator<T>>;
|
|
148
|
+
/**
|
|
149
|
+
* Represents the name of a index in ElasticDB
|
|
150
|
+
*/
|
|
151
|
+
indexName?: string | ((_this: any) => string);
|
|
152
|
+
/**
|
|
153
|
+
* Represents the name of a resource.
|
|
154
|
+
* @type {string}
|
|
155
|
+
*/
|
|
156
|
+
resourceName?: string | ((_this: any) => string);
|
|
157
|
+
/**
|
|
158
|
+
* Generates a new id for new inserting Document.
|
|
159
|
+
*
|
|
160
|
+
*/
|
|
161
|
+
idGenerator?: (command: ElasticEntityService.CommandInfo, _this: any) => any;
|
|
162
|
+
/**
|
|
163
|
+
* Constructs a new instance
|
|
164
|
+
*
|
|
165
|
+
* @param {Type | string} dataType - The data type of the array elements.
|
|
166
|
+
* @param {ElasticEntityService.Options} [options] - The options for the array service.
|
|
167
|
+
* @constructor
|
|
168
|
+
*/
|
|
169
|
+
constructor(dataType: Type | string, options?: ElasticEntityService.Options);
|
|
170
|
+
/**
|
|
171
|
+
* Retrieves the index name.
|
|
172
|
+
*
|
|
173
|
+
* @protected
|
|
174
|
+
* @returns The index name.
|
|
175
|
+
* @throws {Error} If the index name is not defined.
|
|
176
|
+
*/
|
|
177
|
+
getIndexName(): string;
|
|
178
|
+
/**
|
|
179
|
+
* Retrieves the resource name.
|
|
180
|
+
*
|
|
181
|
+
* @protected
|
|
182
|
+
* @returns {string} The resource name.
|
|
183
|
+
* @throws {Error} If the resource name is not defined.
|
|
184
|
+
*/
|
|
185
|
+
getResourceName(): string;
|
|
186
|
+
/**
|
|
187
|
+
* Retrieves the OPRA data type
|
|
188
|
+
*
|
|
189
|
+
* @throws {NotAcceptableError} If the data type is not a ComplexType.
|
|
190
|
+
*/
|
|
191
|
+
get dataType(): ComplexType;
|
|
192
|
+
/**
|
|
193
|
+
* Adds a JSON document to the specified data stream or index and makes it searchable.
|
|
194
|
+
* If the target is an index and the document already exists,
|
|
195
|
+
* the request updates the document and increments its version.
|
|
196
|
+
*
|
|
197
|
+
* @param {ElasticEntityService.CreateCommand} command
|
|
198
|
+
* @protected
|
|
199
|
+
*/
|
|
200
|
+
protected _create(command: ElasticEntityService.CreateCommand): Promise<elastic.CreateResponse>;
|
|
201
|
+
/**
|
|
202
|
+
* Returns the count of documents in the collection based on the provided options.
|
|
203
|
+
*
|
|
204
|
+
* @param {ElasticEntityService.CountCommand} command
|
|
205
|
+
* @protected
|
|
206
|
+
*/
|
|
207
|
+
protected _count(command: ElasticEntityService.CountCommand): Promise<elastic.CountResponse>;
|
|
208
|
+
/**
|
|
209
|
+
* Deletes a document from the collection.
|
|
210
|
+
*
|
|
211
|
+
* @param {ElasticEntityService.DeleteCommand} command
|
|
212
|
+
* @protected
|
|
213
|
+
*/
|
|
214
|
+
protected _delete(command: ElasticEntityService.DeleteCommand): Promise<elastic.DeleteByQueryResponse>;
|
|
215
|
+
/**
|
|
216
|
+
* Deletes multiple documents from the collection that meet the specified filter criteria.
|
|
217
|
+
*
|
|
218
|
+
* @param {ElasticEntityService.DeleteManyCommand} command
|
|
219
|
+
* @protected
|
|
220
|
+
*/
|
|
221
|
+
protected _deleteMany(command: ElasticEntityService.DeleteManyCommand): Promise<elastic.DeleteByQueryResponse>;
|
|
222
|
+
/**
|
|
223
|
+
* Returns search hits that match the query defined in the request
|
|
224
|
+
*
|
|
225
|
+
* @param {ElasticEntityService.SearchCommand} command
|
|
226
|
+
*/
|
|
227
|
+
protected _search(command: ElasticEntityService.SearchCommand): Promise<elastic.SearchResponse>;
|
|
228
|
+
/**
|
|
229
|
+
* Updates multiple documents in the collection based on the specified input and options.
|
|
230
|
+
*
|
|
231
|
+
* @param {ElasticEntityService.UpdateCommand<T>} command
|
|
232
|
+
*/
|
|
233
|
+
protected _updateMany(command: ElasticEntityService.UpdateCommand<T>): Promise<elastic.UpdateByQueryResponse>;
|
|
234
|
+
/**
|
|
235
|
+
* Generates an ID.
|
|
236
|
+
*
|
|
237
|
+
* @protected
|
|
238
|
+
* @returns The generated ID.
|
|
239
|
+
*/
|
|
240
|
+
protected _generateId(command: ElasticEntityService.CommandInfo): any;
|
|
241
|
+
/**
|
|
242
|
+
* Retrieves the codec for the specified operation.
|
|
243
|
+
*
|
|
244
|
+
* @param operation - The operation to retrieve the encoder for. Valid values are 'create' and 'update'.
|
|
245
|
+
*/
|
|
246
|
+
protected _getInputCodec(operation: string): IsObject.Validator<T>;
|
|
247
|
+
/**
|
|
248
|
+
* Retrieves the codec.
|
|
249
|
+
*/
|
|
250
|
+
protected _getOutputCodec(operation: string): IsObject.Validator<T>;
|
|
251
|
+
protected _executeCommand(command: ElasticEntityService.CommandInfo, commandFn: () => any): Promise<any>;
|
|
252
|
+
protected _beforeCreate(command: ElasticEntityService.CreateCommand): Promise<void>;
|
|
253
|
+
protected _beforeUpdate(command: ElasticEntityService.UpdateCommand<T>): Promise<void>;
|
|
254
|
+
protected _beforeUpdateMany(command: ElasticEntityService.UpdateCommand<T>): Promise<void>;
|
|
255
|
+
protected _beforeDelete(command: ElasticEntityService.DeleteCommand): Promise<void>;
|
|
256
|
+
protected _beforeDeleteMany(command: ElasticEntityService.DeleteCommand): Promise<void>;
|
|
257
|
+
protected _afterCreate(command: ElasticEntityService.CreateCommand, result: PartialDTO<T>): Promise<void>;
|
|
258
|
+
protected _afterUpdate(command: ElasticEntityService.UpdateCommand<T>, result?: PartialDTO<T>): Promise<void>;
|
|
259
|
+
protected _afterUpdateMany(command: ElasticEntityService.UpdateCommand<T>, affected: number): Promise<void>;
|
|
260
|
+
protected _afterDelete(command: ElasticEntityService.DeleteCommand, affected: number): Promise<void>;
|
|
261
|
+
protected _afterDeleteMany(command: ElasticEntityService.DeleteCommand, affected: number): Promise<void>;
|
|
262
|
+
}
|
|
@@ -1,9 +1,5 @@
|
|
|
1
1
|
import { Client } from '@elastic/elasticsearch';
|
|
2
|
-
import { QueryDslQueryContainer } from '@elastic/elasticsearch/lib/api/types';
|
|
3
|
-
import { ComplexType } from '@opra/common';
|
|
4
2
|
import { ServiceBase } from '@opra/core';
|
|
5
|
-
import { Type } from 'ts-gems';
|
|
6
|
-
import { IsObject } from 'valgen';
|
|
7
3
|
/**
|
|
8
4
|
* The namespace for the ElasticService.
|
|
9
5
|
*
|
|
@@ -11,12 +7,9 @@ import { IsObject } from 'valgen';
|
|
|
11
7
|
*/
|
|
12
8
|
export declare namespace ElasticService {
|
|
13
9
|
interface Options {
|
|
14
|
-
client?: ElasticService
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
interceptor?: ElasticService<any>['$interceptor'];
|
|
18
|
-
idGenerator?: ElasticService<any>['$idGenerator'];
|
|
19
|
-
onError?: ElasticService<any>['$onError'];
|
|
10
|
+
client?: ElasticService['client'];
|
|
11
|
+
interceptor?: ElasticService['interceptor'];
|
|
12
|
+
onError?: ElasticService['onError'];
|
|
20
13
|
}
|
|
21
14
|
type CrudOp = 'create' | 'read' | 'update' | 'delete';
|
|
22
15
|
interface CommandInfo {
|
|
@@ -24,101 +17,45 @@ export declare namespace ElasticService {
|
|
|
24
17
|
method: string;
|
|
25
18
|
byId: boolean;
|
|
26
19
|
documentId?: any;
|
|
27
|
-
input?:
|
|
28
|
-
options?:
|
|
20
|
+
input?: any;
|
|
21
|
+
options?: any;
|
|
29
22
|
}
|
|
30
23
|
}
|
|
24
|
+
export interface ElasticService {
|
|
25
|
+
/**
|
|
26
|
+
* Interceptor function for handling callback execution with provided arguments.
|
|
27
|
+
* @type Function
|
|
28
|
+
* @param next - The callback function to be intercepted.
|
|
29
|
+
* @param {ElasticService.CommandInfo} command - The arguments object containing the following properties:
|
|
30
|
+
* @param _this - The reference to the current object.
|
|
31
|
+
* @returns - The promise that resolves to the result of the callback execution.
|
|
32
|
+
*/
|
|
33
|
+
interceptor?(next: () => any, command: ElasticService.CommandInfo, _this: any): Promise<any>;
|
|
34
|
+
}
|
|
31
35
|
/**
|
|
32
36
|
* Class representing a ElasticSearch service for interacting with a collection.
|
|
33
37
|
* @extends ServiceBase
|
|
34
38
|
* @template T - The type of the documents in the collection.
|
|
35
39
|
*/
|
|
36
|
-
export declare class ElasticService
|
|
37
|
-
protected _dataType_: Type | string;
|
|
38
|
-
protected _dataType?: ComplexType;
|
|
39
|
-
protected _inputCodecs: Record<string, IsObject.Validator<T>>;
|
|
40
|
-
protected _outputCodecs: Record<string, IsObject.Validator<T>>;
|
|
41
|
-
/**
|
|
42
|
-
* Represents the name of a index in ElasticDB
|
|
43
|
-
*/
|
|
44
|
-
$indexName: string | ((_this: any) => string);
|
|
45
|
-
/**
|
|
46
|
-
* Represents the name of a resource.
|
|
47
|
-
* @type {string}
|
|
48
|
-
*/
|
|
49
|
-
$resourceName?: string | ((_this: any) => string);
|
|
40
|
+
export declare class ElasticService extends ServiceBase {
|
|
50
41
|
/**
|
|
51
42
|
* Represents a ElasticDB database object.
|
|
52
43
|
*/
|
|
53
44
|
client?: Client | ((_this: any) => Client);
|
|
54
|
-
/**
|
|
55
|
-
* Generates a new id for new inserting Document.
|
|
56
|
-
*
|
|
57
|
-
*/
|
|
58
|
-
$idGenerator?: (_this: any) => any;
|
|
59
45
|
/**
|
|
60
46
|
* Callback function for handling errors.
|
|
61
47
|
*
|
|
62
48
|
* @param {unknown} error - The error object.
|
|
63
49
|
* @param _this - The context object.
|
|
64
50
|
*/
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Represents a common filter function for a ElasticService.
|
|
68
|
-
*
|
|
69
|
-
* @type {QueryDslQueryContainer | Function}
|
|
70
|
-
*/
|
|
71
|
-
$commonFilter?: QueryDslQueryContainer | ((args: ElasticService.CommandInfo, _this: this) => QueryDslQueryContainer | Promise<QueryDslQueryContainer> | undefined);
|
|
72
|
-
/**
|
|
73
|
-
* Interceptor function for handling callback execution with provided arguments.
|
|
74
|
-
*
|
|
75
|
-
* @param callback - The callback function to be intercepted.
|
|
76
|
-
* @param {ElasticService.CommandInfo} info - The arguments object containing the following properties:
|
|
77
|
-
* @param _this - The reference to the current object.
|
|
78
|
-
* @returns - The promise that resolves to the result of the callback execution.
|
|
79
|
-
*/
|
|
80
|
-
$interceptor?: (callback: () => any, info: ElasticService.CommandInfo, _this: any) => Promise<any>;
|
|
51
|
+
onError?: (error: unknown, _this: any) => void | Promise<void>;
|
|
81
52
|
/**
|
|
82
53
|
* Constructs a new instance
|
|
83
54
|
*
|
|
84
|
-
* @param dataType - The data type of the returning results
|
|
85
|
-
* @param indexName - The name of the index, or a function that returns the index name
|
|
86
55
|
* @param [options] - The options for the service
|
|
87
56
|
* @constructor
|
|
88
57
|
*/
|
|
89
|
-
constructor(
|
|
90
|
-
/**
|
|
91
|
-
* Retrieves the index name.
|
|
92
|
-
*
|
|
93
|
-
* @protected
|
|
94
|
-
* @returns The index name.
|
|
95
|
-
* @throws {Error} If the index name is not defined.
|
|
96
|
-
*/
|
|
97
|
-
getIndexName(): string;
|
|
98
|
-
/**
|
|
99
|
-
* Retrieves the resource name.
|
|
100
|
-
*
|
|
101
|
-
* @protected
|
|
102
|
-
* @returns {string} The resource name.
|
|
103
|
-
* @throws {Error} If the resource name is not defined.
|
|
104
|
-
*/
|
|
105
|
-
getResourceName(): string;
|
|
106
|
-
/**
|
|
107
|
-
* Retrieves the OPRA data type
|
|
108
|
-
*
|
|
109
|
-
* @throws {NotAcceptableError} If the data type is not a ComplexType.
|
|
110
|
-
*/
|
|
111
|
-
get dataType(): ComplexType;
|
|
112
|
-
/**
|
|
113
|
-
* Retrieves the codec for the specified operation.
|
|
114
|
-
*
|
|
115
|
-
* @param operation - The operation to retrieve the encoder for. Valid values are 'create' and 'update'.
|
|
116
|
-
*/
|
|
117
|
-
getInputCodec(operation: string): IsObject.Validator<T>;
|
|
118
|
-
/**
|
|
119
|
-
* Retrieves the codec.
|
|
120
|
-
*/
|
|
121
|
-
getOutputCodec(operation: string): IsObject.Validator<T>;
|
|
58
|
+
constructor(options?: ElasticService.Options);
|
|
122
59
|
/**
|
|
123
60
|
* Retrieves the ElasticSearch client.
|
|
124
61
|
*
|
|
@@ -127,21 +64,5 @@ export declare class ElasticService<T extends object> extends ServiceBase {
|
|
|
127
64
|
* @throws {Error} If the context or client is not set.
|
|
128
65
|
*/
|
|
129
66
|
getClient(): Client;
|
|
130
|
-
|
|
131
|
-
* Generates an ID.
|
|
132
|
-
*
|
|
133
|
-
* @protected
|
|
134
|
-
* @returns The generated ID.
|
|
135
|
-
*/
|
|
136
|
-
protected _generateId(): any;
|
|
137
|
-
/**
|
|
138
|
-
* Retrieves the common filter used for querying documents.
|
|
139
|
-
* This method is mostly used for security issues like securing multi-tenant applications.
|
|
140
|
-
*
|
|
141
|
-
* @protected
|
|
142
|
-
* @returns {QueryDslQueryContainer | Promise<QueryDslQueryContainer> | undefined} The common filter or a Promise
|
|
143
|
-
* that resolves to the common filter, or undefined if not available.
|
|
144
|
-
*/
|
|
145
|
-
protected _getCommonFilter(info: ElasticService.CommandInfo): QueryDslQueryContainer | Promise<QueryDslQueryContainer> | undefined;
|
|
146
|
-
protected _intercept(callback: (...args: any[]) => any, info: ElasticService.CommandInfo): Promise<any>;
|
|
67
|
+
protected _executeCommand(command: ElasticService.CommandInfo, commandFn: () => any): Promise<any>;
|
|
147
68
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.default = prepareKeyValues;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const putil_isplainobject_1 = tslib_1.__importDefault(require("putil-isplainobject"));
|
|
6
|
-
const defaultPrimaryKey = ['_id'];
|
|
7
|
-
function prepareKeyValues(keyValue, primaryKey) {
|
|
8
|
-
primaryKey = primaryKey || defaultPrimaryKey;
|
|
9
|
-
const b = (0, putil_isplainobject_1.default)(keyValue);
|
|
10
|
-
if (primaryKey.length > 1 && !b) {
|
|
11
|
-
throw new TypeError(`Argument "keyValue" must be an object that contains all key values`);
|
|
12
|
-
}
|
|
13
|
-
if (primaryKey.length > 1 || b) {
|
|
14
|
-
return primaryKey.reduce((o, k) => {
|
|
15
|
-
o[k] = keyValue[k];
|
|
16
|
-
if (o[k] == null)
|
|
17
|
-
throw new Error(`Value of key "${k}" is required`);
|
|
18
|
-
return o;
|
|
19
|
-
}, {});
|
|
20
|
-
}
|
|
21
|
-
return { [primaryKey[0]]: keyValue };
|
|
22
|
-
}
|