@opra/elastic
Version:
Opra Elastic Search adapter package
219 lines (218 loc) • 12.1 kB
TypeScript
import type { estypes } from '@elastic/elasticsearch';
import { ExecutionContext, ServiceBase } from '@opra/core';
import type { Nullish, PartialDTO, PatchDTO, RequiredSome, Type } from 'ts-gems';
import { ElasticAdapter } from './elastic-adapter.js';
import { ElasticEntityService } from './elastic-entity-service.js';
import { ElasticService } from './elastic-service.js';
/**
*
* @namespace ElasticCollectionService
*/
export declare namespace ElasticCollectionService {
/**
* The constructor options of ElasticCollectionService.
*
* @interface Options
* @extends ElasticService.Options
*/
interface Options extends ElasticEntityService.Options {
documentFilter?: ElasticCollectionService['documentFilter'];
defaultLimit?: number;
}
type DocumentFilter = ElasticAdapter.FilterInput | ((args: ElasticEntityService.CommandInfo, _this: ElasticCollectionService) => ElasticAdapter.FilterInput | Promise<ElasticAdapter.FilterInput> | undefined);
interface FindManyWithCountResult<X> {
items: X[];
count: number;
relation: estypes.SearchTotalHitsRelation;
}
}
/**
* @class ElasticCollectionService
* @template T - The type of the documents in the collection.
*/
export declare class ElasticCollectionService<T extends object = any> extends ElasticEntityService<T> {
/**
* Represents a common filter function for a ElasticService.
*
* @type {FilterInput | Function}
*/
documentFilter?: ElasticCollectionService.DocumentFilter | ElasticCollectionService.DocumentFilter[];
/**
* Represents the default limit value for a certain operation.
*
* @type {number}
*/
defaultLimit: number;
/**
* Constructs a new instance
*
* @param {Type | string} dataType - The data type of the array elements.
* @param {ElasticCollectionService.Options} [options] - The options for the array service.
* @constructor
*/
constructor(dataType: Type | string, options?: ElasticCollectionService.Options);
for<C extends ExecutionContext, P extends Partial<this>>(context: C | ServiceBase, overwriteProperties?: Nullish<P>, overwriteContext?: Partial<C>): this & Required<P>;
/**
* Asserts the existence of a resource with the given ID.
* Throws a ResourceNotFoundError if the resource does not exist.
*
* @param {string} id - The ID of the resource to assert.
* @param {ElasticEntityService.FindOneOptions} [options] - Optional options for checking the existence.
* @returns {Promise<void>} - A Promise that resolves when the resource exists.
* @throws {ResourceNotAvailableError} - If the resource does not exist.
*/
assert(id: string, options?: ElasticEntityService.FindOneOptions): Promise<void>;
/**
* Adds a document to the specified index
*
* @param {PartialDTO<T>} input - The input data for creating the document.
* @param {ElasticEntityService.CreateOptions} [options] - The options for creating the document.
* @returns {Promise<PartialDTO<T>>} A promise that resolves to the created document.
* @throws {Error} if an unknown error occurs while creating the document.
*/
create(input: PartialDTO<T>, options?: ElasticEntityService.CreateOptions): Promise<estypes.CreateResponse>;
/**
* Returns the count of documents in the collection based on the provided options.
*
* @param {ElasticEntityService.CountOptions<T>} options - The options for the count operation.
* @return {Promise<number>} - A promise that resolves to the count of documents in the collection.
*/
count(options?: ElasticEntityService.CountOptions): Promise<number>;
/**
* Deletes a document from the collection.
*
* @param {string} id - The ID of the document to delete.
* @param {ElasticEntityService.DeleteOptions} [options] - Optional delete options.
* @return {Promise<number>} - A Promise that resolves to the number of documents deleted.
*/
delete(id: string, options?: ElasticEntityService.DeleteOptions): Promise<estypes.DeleteByQueryResponse>;
/**
* Deletes multiple documents from the collection that meet the specified filter criteria.
*
* @param {ElasticEntityService.DeleteManyOptions} options - The options for the delete operation.
* @return {Promise<number>} - A promise that resolves to the number of documents deleted.
*/
deleteMany(options?: ElasticEntityService.DeleteManyOptions): Promise<estypes.DeleteByQueryResponse>;
/**
* Checks if an object with the given id exists.
*
* @param {string} id - The id of the object to check.
* @param {ElasticEntityService.FindOneOptions} [options] - The options for the query (optional).
* @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the object exists or not.
*/
exists(id: string, options?: ElasticEntityService.FindOneOptions): Promise<boolean>;
/**
* Checks if an object with the given arguments exists.
*
* @param {ElasticEntityService.FindOneOptions} [options] - The options for the query (optional).
* @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the object exists or not.
*/
existsOne(options?: ElasticEntityService.FindOneOptions): Promise<boolean>;
/**
* Finds a document by its ID.
*
* @param {string} id - The ID of the document.
* @param {ElasticEntityService.FindOneOptions} [options] - The options for the find query.
* @return {Promise<PartialDTO<T | undefined>>} - A promise resolving to the found document, or undefined if not found.
*/
findById(id: string, options: RequiredSome<ElasticEntityService.FindOneOptions, 'projection'>): Promise<PartialDTO<T> | undefined>;
/**
* Finds a document by its ID.
*
* @param {string} id - The ID of the document.
* @param {ElasticEntityService.FindOneOptions} [options] - The options for the find query.
* @return {Promise<T | undefined>} - A promise resolving to the found document, or undefined if not found.
*/
findById(id: string, options?: ElasticEntityService.FindOneOptions): Promise<T | undefined>;
/**
* Finds a document in the collection that matches the specified options.
*
* @param {ElasticEntityService.FindOneOptions} [options] - The options for the query.
* @return {Promise<PartialDTO<T> | undefined>} A promise that resolves with the found document or undefined if no document is found.
*/
findOne(options: RequiredSome<ElasticEntityService.FindOneOptions, 'projection'>): Promise<PartialDTO<T> | undefined>;
/**
* Finds a document in the collection that matches the specified options.
*
* @param {ElasticEntityService.FindOneOptions} [options] - The options for the query.
* @return {Promise<T | undefined>} A promise that resolves with the found document or undefined if no document is found.
*/
findOne(options?: ElasticEntityService.FindOneOptions): Promise<T | undefined>;
/**
* Finds multiple documents in the ElasticDB collection.
*
* @param {ElasticEntityService.FindManyOptions} options - The options for the find operation.
* @return {Promise<PartialDTO<T> | undefined>} A Promise that resolves to an array of partial outputs of type T.
*/
findMany(options: RequiredSome<ElasticEntityService.FindManyOptions, 'projection'>): Promise<PartialDTO<T>[]>;
/**
* Finds multiple documents in the ElasticDB collection.
*
* @param {ElasticEntityService.FindManyOptions} options - The options for the find operation.
* @return {Promise<T | undefined>} A Promise that resolves to an array of partial outputs of type T.
*/
findMany(options?: ElasticEntityService.FindManyOptions): Promise<T[]>;
searchRaw(request: estypes.SearchRequest, options?: ElasticEntityService.SearchOptions): Promise<estypes.SearchResponse<PartialDTO<T>>>;
/**
* Finds multiple documents in the collection and returns both records (max limit)
* and total count that matched the given criteria
*
* @param {ElasticEntityService.FindManyOptions} [options] - The options for the find operation.
* @return {ElasticCollectionService.FindManyWithCountResult<PartialDTO<T>>} A Promise that resolves to an array of partial outputs of type T.
*/
findManyWithCount(options: RequiredSome<ElasticEntityService.FindManyOptions, 'projection'>): Promise<ElasticCollectionService.FindManyWithCountResult<PartialDTO<T>>>;
/**
* Finds multiple documents in the collection and returns both records (max limit)
* and total count that matched the given criteria
*
* @param {ElasticEntityService.FindManyOptions} [options] - The options for the find operation.
* @return {ElasticCollectionService.FindManyWithCountResult<T>} A Promise that resolves to an array of partial outputs of type T.
*/
findManyWithCount(options?: ElasticEntityService.FindManyOptions): Promise<ElasticCollectionService.FindManyWithCountResult<T>>;
/**
* Retrieves a document from the collection by its ID. Throws error if not found.
*
* @param {string} id - The ID of the document to retrieve.
* @param {ElasticEntityService.FindOneOptions<T>} [options] - Optional options for the findOne operation.
* @returns {Promise<PartialDTO<T>>} - A promise that resolves to the retrieved document,
* or rejects with a ResourceNotFoundError if the document does not exist.
* @throws {ResourceNotAvailableError} - If the document with the specified ID does not exist.
*/
get(id: string, options: RequiredSome<ElasticEntityService.FindOneOptions, 'projection'>): Promise<PartialDTO<T>>;
/**
* Retrieves a document from the collection by its ID. Throws error if not found.
*
* @param {string} id - The ID of the document to retrieve.
* @param {ElasticEntityService.FindOneOptions<T>} [options] - Optional options for the findOne operation.
* @returns {Promise<T>} - A promise that resolves to the retrieved document,
* or rejects with a ResourceNotFoundError if the document does not exist.
* @throws {ResourceNotAvailableError} - If the document with the specified ID does not exist.
*/
get(id: string, options?: ElasticEntityService.FindOneOptions): Promise<T>;
/**
* Updates a document in the collection with the specified ID.
*
* @param {string} id - The ID of the document to update.
* @param {PatchDTO<T>} input - The partial input data to update the document with.
* @param {ElasticEntityService.UpdateOneOptions} [options] - The options for updating the document.
* @returns {Promise<estypes.UpdateResponse>} - A promise that resolves to the number of documents modified.
*/
update(id: string, input: PatchDTO<T>, options?: ElasticEntityService.UpdateOneOptions): Promise<estypes.UpdateByQueryResponse>;
/**
* Updates multiple documents in the collection based on the specified input and options.
*
* @param {PatchDTO<T>} input - The partial input to update the documents with.
* @param {ElasticEntityService.UpdateManyOptions} options - The options for updating the documents.
* @return {Promise<number>} - A promise that resolves to the number of documents matched and modified.
*/
updateMany(input: PatchDTO<T>, options?: ElasticEntityService.UpdateManyOptions): Promise<estypes.UpdateByQueryResponse>;
/**
* Retrieves the common filter used for querying documents.
* This method is mostly used for security issues like securing multi-tenant applications.
*
* @protected
* @returns {FilterInput | Promise<FilterInput> | undefined} The common filter or a Promise
* that resolves to the common filter, or undefined if not available.
*/
protected _getDocumentFilter(command: ElasticService.CommandInfo): ElasticAdapter.FilterInput | Promise<ElasticAdapter.FilterInput> | undefined;
}