Skip to main content

ElasticCollectionService

import { ElasticCollectionService } from '@opra/elastic';

Hierarchy: ServiceBase → ElasticService → ElasticEntityService → ElasticCollectionService


Constructor​

new ElasticCollectionService<T>(dataType: Type<T> | string, options?: ElasticCollectionService.Options)

Properties​

PropertyTypeDescription
defaultLimitnumberMaximum hits returned by findMany when no limit option is given (default: 10)
documentFilterDocumentFilter | DocumentFilter[]Common filter applied to every read and write operation

Methods​

assert​

assert(id: string, options?: FindOneOptions): Promise<void>

Throws ResourceNotAvailableError if no document with the given ID exists.

options — FindOneOptions


create​

create(input: PartialDTO<T>, options?: CreateOptions): Promise<estypes.CreateResponse>

Indexes a new document. Auto-generates an ID if input does not include one.

options — CreateOptions


count​

count(options?: CountOptions): Promise<number>

Returns the number of documents matching the filter.

options — CountOptions


delete​

delete(id: string, options?: DeleteOptions): Promise<estypes.DeleteByQueryResponse>

Deletes the document with the given ID.

options — DeleteOptions


deleteMany​

deleteMany(options?: DeleteManyOptions): Promise<estypes.DeleteByQueryResponse>

Deletes all documents matching the filter.

options — DeleteManyOptions


exists​

exists(id: string, options?: FindOneOptions): Promise<boolean>

Returns true if a document with the given ID exists.

options — FindOneOptions


existsOne​

existsOne(options?: FindOneOptions): Promise<boolean>

Returns true if at least one document matches the filter.

options — FindOneOptions


findById​

findById(id: string, options: RequiredSome<FindOneOptions, 'projection'>): Promise<PartialDTO<T> | undefined>
findById(id: string, options?: FindOneOptions): Promise<T | undefined>

Fetches a document by ID. Returns undefined if not found.

options — FindOneOptions


findOne​

findOne(options: RequiredSome<FindOneOptions, 'projection'>): Promise<PartialDTO<T> | undefined>
findOne(options?: FindOneOptions): Promise<T | undefined>

Fetches the first document matching the filter. Returns undefined if not found.

options — FindOneOptions


findMany​

findMany(options: RequiredSome<FindManyOptions, 'projection'>): Promise<PartialDTO<T>[]>
findMany(options?: FindManyOptions): Promise<T[]>

Fetches a list of documents. Applies defaultLimit when options.limit is not specified.

options — FindManyOptions


findManyWithCount​

findManyWithCount(options: RequiredSome<FindManyOptions, 'projection'>): Promise<FindManyWithCountResult<PartialDTO<T>>>
findManyWithCount(options?: FindManyOptions): Promise<FindManyWithCountResult<T>>

Same as findMany but also returns the total hit count and Elasticsearch's relation field.

options — FindManyOptions


get​

get(id: string, options: RequiredSome<FindOneOptions, 'projection'>): Promise<PartialDTO<T>>
get(id: string, options?: FindOneOptions): Promise<T>

Fetches a document by ID. Throws ResourceNotAvailableError if not found.

options — FindOneOptions


searchRaw​

searchRaw(request: estypes.SearchRequest, options?: SearchOptions): Promise<estypes.SearchResponse<PartialDTO<T>>>

Executes a raw Elasticsearch search request. Use when you need full control over the query DSL (aggregations, highlighting, etc.).

options — SearchOptions


update​

update(id: string, input: PatchDTO<T>, options?: UpdateOneOptions): Promise<estypes.UpdateByQueryResponse>

Applies a partial update to the document with the given ID.

options — UpdateOneOptions


updateMany​

updateMany(input: PatchDTO<T>, options?: UpdateManyOptions): Promise<estypes.UpdateByQueryResponse>

Applies a partial update to all documents matching the filter.

options — UpdateManyOptions


Interfaces​

ElasticCollectionService.Options​

Extends ElasticEntityService.Options.

OptionTypeDefaultDescription
defaultLimitnumber10Default maximum number of hits returned by findMany
documentFilterDocumentFilter | DocumentFilter[]—Common filter applied to every operation

ElasticCollectionService.DocumentFilter​

type DocumentFilter =
| ElasticAdapter.FilterInput
| ((args: ElasticEntityService.CommandInfo, _this: ElasticCollectionService) =>
ElasticAdapter.FilterInput | Promise<ElasticAdapter.FilterInput> | undefined);

A static filter or a function computed per request. Applied automatically to every operation alongside any method-level filter option.


ElasticCollectionService.FindManyWithCountResult<X>​

Returned by findManyWithCount.

PropertyTypeDescription
itemsX[]The matched documents
countnumberTotal number of matching documents
relationestypes.SearchTotalHitsRelationElasticsearch hit count relation ('eq' or 'gte')

CreateOptions​

OptionTypeDescription
replaceIfExistsbooleanReplace the document if it already exists
requestStrictOmit<estypes.CreateRequest, 'id' | 'index' | 'document'>Raw Elasticsearch request options
transportTransportRequestOptionsTransport-level options

CountOptions​

OptionTypeDescription
filterElasticAdapter.FilterInputFilter criteria
requestStrictOmit<estypes.CountRequest, 'index'>Raw Elasticsearch request options
transportTransportRequestOptionsTransport-level options

DeleteOptions​

OptionTypeDescription
filterElasticAdapter.FilterInputAdditional filter criteria
requestStrictOmit<estypes.DeleteByQueryRequest, 'index'>Raw Elasticsearch request options
transportTransportRequestOptionsTransport-level options

DeleteManyOptions​

Same shape as DeleteOptions.


FindOneOptions​

Same as FindManyOptions without the limit property.


FindManyOptions​

OptionTypeDescription
filterElasticAdapter.FilterInputFilter criteria
projectionstring | string[]Fields to include
sortstring[]Sort directives, e.g. ['name', '-createdAt']
limitnumberMaximum number of hits. Defaults to defaultLimit.
skipnumberNumber of hits to skip
noDecodebooleanSkip OPRA output decoding and return raw documents
requestStrictOmit<estypes.SearchRequest, 'index' | 'from' | 'size' | 'sort' | '_source'>Raw Elasticsearch request options
transportTransportRequestOptionsTransport-level options

SearchOptions​

OptionTypeDescription
noDecodebooleanSkip OPRA output decoding
transportTransportRequestOptionsTransport-level options

UpdateOneOptions​

OptionTypeDescription
filterElasticAdapter.FilterInputAdditional filter criteria
requestStrictOmit<estypes.UpdateByQueryRequest, 'index'>Raw Elasticsearch request options
transportTransportRequestOptionsTransport-level options

UpdateManyOptions​

Same shape as UpdateOneOptions.