@opra/elastic
Version:
Opra Elastic Search adapter package
311 lines (310 loc) • 12.5 kB
TypeScript
import type { estypes } from '@elastic/elasticsearch';
import type { TransportRequestOptions } from '@elastic/transport';
import { ComplexType } from '@opra/common';
import type { PartialDTO, PatchDTO, RequiredSome, StrictOmit, Type } from 'ts-gems';
import { type IsObject } from 'valgen';
import { ElasticAdapter } from './elastic-adapter.js';
import { ElasticService } from './elastic-service.js';
/**
*
* @namespace ElasticEntityService
*/
export declare namespace ElasticEntityService {
/**
* The constructor options of ElasticEntityService.
*
* @interface Options
* @extends ElasticService.Options
*/
interface Options extends ElasticService.Options {
indexName?: ElasticEntityService['indexName'];
resourceName?: ElasticEntityService['resourceName'];
idGenerator?: ElasticEntityService['idGenerator'];
scope?: ElasticEntityService['scope'];
}
interface CommandInfo extends ElasticService.CommandInfo {
}
/**
* Represents options for "create" operation
*
* @interface
*/
interface CreateOptions {
request?: StrictOmit<CreateRequest, 'id' | 'index' | 'document'>;
transport?: TransportRequestOptions;
replaceIfExists?: boolean;
}
/**
* Represents options for "count" operation
*
* @interface
* @template T - The type of the document.
*/
interface CountOptions {
filter?: ElasticAdapter.FilterInput;
request?: StrictOmit<CountRequest, 'index'>;
transport?: TransportRequestOptions;
}
/**
* Represents options for "delete" operation
*
* @interface
* @template T - The type of the document.
*/
interface DeleteOptions {
filter?: ElasticAdapter.FilterInput;
request?: StrictOmit<DeleteByQueryRequest, 'index'>;
transport?: TransportRequestOptions;
}
/**
* Represents options for "deleteMany" operation
*
* @interface
* @template T - The type of the document.
*/
interface DeleteManyOptions {
filter?: ElasticAdapter.FilterInput;
request?: StrictOmit<DeleteByQueryRequest, 'index'>;
transport?: TransportRequestOptions;
}
/**
* Represents options for "findOne" operation
*
* @interface
* @template T - The type of the document.
*/
interface FindOneOptions extends StrictOmit<FindManyOptions, 'limit'> {
}
/**
* Represents options for "findMany" operation
*
* @interface
* @template T - The type of the document.
*/
interface FindManyOptions {
filter?: ElasticAdapter.FilterInput;
projection?: string | string[];
sort?: string[];
limit?: number;
skip?: number;
request?: StrictOmit<SearchRequest, 'index' | 'from' | 'size' | 'sort' | '_source'>;
transport?: TransportRequestOptions;
noDecode?: boolean;
}
/**
* Represents options for "search" operation
*
* @interface
*/
interface SearchOptions {
transport?: TransportRequestOptions;
noDecode?: boolean;
}
/**
* Represents options for "update" operation
*
* @interface
* @template T - The type of the document.
*/
interface UpdateOneOptions {
filter?: ElasticAdapter.FilterInput;
request?: StrictOmit<UpdateByQueryRequest, 'index'>;
transport?: TransportRequestOptions;
}
/**
* Represents options for "updateMany" operation
*
* @interface
* @template T - The type of the document.
*/
interface UpdateManyOptions {
filter?: ElasticAdapter.FilterInput;
request?: StrictOmit<UpdateByQueryRequest, 'index'>;
transport?: TransportRequestOptions;
}
interface CreateCommand extends StrictOmit<RequiredSome<CommandInfo, 'input'>, 'documentId'> {
crud: 'create';
options?: CreateOptions;
}
interface CountCommand extends StrictOmit<CommandInfo, 'documentId' | 'input'> {
crud: 'read';
options?: CountOptions;
}
interface DeleteCommand extends StrictOmit<CommandInfo, 'input'> {
crud: 'delete';
options?: DeleteOptions;
}
interface DeleteManyCommand extends StrictOmit<CommandInfo, 'input'> {
crud: 'delete';
options?: DeleteManyOptions;
}
interface FindManyCommand extends StrictOmit<CommandInfo, 'input'> {
crud: 'read';
options?: FindManyOptions;
}
interface SearchCommand extends StrictOmit<CommandInfo, 'input'> {
crud: 'read';
request: SearchRequest;
options?: SearchOptions;
}
interface UpdateCommand<T> extends CommandInfo {
crud: 'update';
input: PatchDTO<T>;
options?: UpdateOneOptions;
}
type CreateRequest = estypes.CreateRequest;
type CreateResponse = estypes.CreateResponse;
type CountRequest = estypes.CountRequest;
type CountResponse = estypes.CountResponse;
type DeleteByQueryRequest = estypes.DeleteByQueryRequest;
type DeleteByQueryResponse = estypes.DeleteByQueryResponse;
type SearchRequest = estypes.SearchRequest;
type SearchResponse<T> = estypes.SearchResponse<T>;
type UpdateByQueryRequest = estypes.UpdateByQueryRequest;
type UpdateByQueryResponse = estypes.UpdateByQueryResponse;
type QueryDslQueryContainer = estypes.QueryDslQueryContainer;
type Script = estypes.Script;
type ScriptSource = estypes.ScriptSource;
}
/**
* @class ElasticEntityService
* @template T - The type of the documents in the collection.
*/
export declare class ElasticEntityService<T extends object = any> extends ElasticService {
protected _dataTypeScope?: string;
protected _dataType_: Type | string;
protected _dataType?: ComplexType;
protected _inputCodecs: Record<string, IsObject.Validator<T>>;
protected _outputCodecs: Record<string, IsObject.Validator<T>>;
/**
* Defines comma delimited scopes for api document
*/
scope?: string;
/**
* Represents the name of a index in ElasticDB
*/
indexName?: string | ((_this: any) => string);
/**
* Represents the name of a resource.
* @type {string}
*/
resourceName?: string | ((_this: any) => string);
/**
* Generates a new id for new inserting Document.
*
*/
idGenerator?: (command: ElasticEntityService.CommandInfo, _this: any) => any;
/**
* Constructs a new instance
*
* @param {Type | string} dataType - The data type of the array elements.
* @param {ElasticEntityService.Options} [options] - The options for the array service.
* @constructor
*/
constructor(dataType: Type | string, options?: ElasticEntityService.Options);
/**
* Retrieves the index name.
*
* @protected
* @returns The index name.
* @throws {Error} If the index name is not defined.
*/
getIndexName(): string;
/**
* Retrieves the resource name.
*
* @protected
* @returns {string} The resource name.
* @throws {Error} If the resource name is not defined.
*/
getResourceName(): string;
/**
* Retrieves the OPRA data type
*
* @throws {NotAcceptableError} If the data type is not a ComplexType.
*/
get dataType(): ComplexType;
/**
* Adds a JSON document to the specified data stream or index and makes it searchable.
* If the target is an index and the document already exists,
* the request updates the document and increments its version.
*
* @param {ElasticEntityService.CreateCommand} command
* @protected
*/
protected _create(command: ElasticEntityService.CreateCommand): Promise<ElasticEntityService.CreateResponse>;
protected __create(request: ElasticEntityService.CreateRequest, options?: ElasticEntityService.CreateOptions): Promise<estypes.WriteResponseBase>;
/**
* Returns the count of documents in the collection based on the provided options.
*
* @param {ElasticEntityService.CountCommand} command
* @protected
*/
protected _count(command: ElasticEntityService.CountCommand): Promise<ElasticEntityService.CountResponse>;
protected __count(request: ElasticEntityService.CountRequest, options?: ElasticEntityService.CountOptions): Promise<estypes.CountResponse>;
/**
* Deletes a document from the collection.
*
* @param {ElasticEntityService.DeleteCommand} command
* @protected
*/
protected _delete(command: ElasticEntityService.DeleteCommand): Promise<ElasticEntityService.DeleteByQueryResponse>;
/**
* Deletes multiple documents from the collection that meet the specified filter criteria.
*
* @param {ElasticEntityService.DeleteManyCommand} command
* @protected
*/
protected _deleteMany(command: ElasticEntityService.DeleteManyCommand): Promise<ElasticEntityService.DeleteByQueryResponse>;
protected __delete(request: ElasticEntityService.DeleteByQueryRequest, options?: ElasticEntityService.DeleteOptions): Promise<estypes.DeleteByQueryResponse>;
/**
* Returns search hits that match the query defined in the request
*
* @param {ElasticEntityService.FindManyCommand} command
*/
protected _findMany(command: ElasticEntityService.FindManyCommand): Promise<ElasticEntityService.SearchResponse<PartialDTO<T>>>;
protected __findMany(request: ElasticEntityService.SearchRequest, options?: ElasticEntityService.FindManyOptions): Promise<estypes.SearchResponse<T, Record<string, estypes.AggregationsAggregate>>>;
/**
* Executes a search operation on the Elasticsearch index using the provided search command.
*
* @param {ElasticEntityService.SearchCommand} command - The search command containing the request configuration and optional transport settings.
* @return {Promise<ElasticEntityService.SearchResponse>} A promise resolving to the search response from Elasticsearch.
*/
protected _searchRaw(command: ElasticEntityService.SearchCommand): Promise<ElasticEntityService.SearchResponse<PartialDTO<T>>>;
/**
* Updates multiple documents in the collection based on the specified input and options.
*
* @param {ElasticEntityService.UpdateCommand<T>} command
*/
protected _updateMany(command: ElasticEntityService.UpdateCommand<T>): Promise<ElasticEntityService.UpdateByQueryResponse>;
protected __update(request: ElasticEntityService.UpdateByQueryRequest, options?: ElasticEntityService.UpdateManyOptions): Promise<estypes.UpdateByQueryResponse>;
/**
* Generates an ID.
*
* @protected
* @returns The generated ID.
*/
protected _generateId(command: ElasticEntityService.CommandInfo): any;
/**
* Retrieves the codec for the specified operation.
*
* @param operation - The operation to retrieve the encoder for. Valid values are 'create' and 'update'.
*/
protected _getInputCodec(operation: string): IsObject.Validator<T>;
/**
* Retrieves the codec.
*/
getOutputCodec(operation: string): IsObject.Validator<T>;
protected _executeCommand(command: ElasticEntityService.CommandInfo, commandFn: () => any): Promise<any>;
protected _mergeQueries(requestQuery?: ElasticEntityService.QueryDslQueryContainer, filterQuery?: ElasticEntityService.QueryDslQueryContainer): ElasticEntityService.QueryDslQueryContainer | undefined;
protected _beforeCreate(command: ElasticEntityService.CreateCommand): Promise<void>;
protected _beforeUpdate(command: ElasticEntityService.UpdateCommand<T>): Promise<void>;
protected _beforeUpdateMany(command: ElasticEntityService.UpdateCommand<T>): Promise<void>;
protected _beforeDelete(command: ElasticEntityService.DeleteCommand): Promise<void>;
protected _beforeDeleteMany(command: ElasticEntityService.DeleteCommand): Promise<void>;
protected _afterCreate(command: ElasticEntityService.CreateCommand, result: PartialDTO<T>): Promise<void>;
protected _afterUpdate(command: ElasticEntityService.UpdateCommand<T>, result?: PartialDTO<T>): Promise<void>;
protected _afterUpdateMany(command: ElasticEntityService.UpdateCommand<T>, affected: number): Promise<void>;
protected _afterDelete(command: ElasticEntityService.DeleteCommand, affected: number): Promise<void>;
protected _afterDeleteMany(command: ElasticEntityService.DeleteCommand, affected: number): Promise<void>;
}