ElasticCollectionService
import { ElasticCollectionService } from '@opra/elastic';
Hierarchy: ServiceBase → ElasticService → ElasticEntityService → ElasticCollectionService
Constructor
new ElasticCollectionService<T>(dataType: Type<T> | string, options?: ElasticCollectionService.Options)
Properties
| Property | Type | Description |
|---|---|---|
defaultLimit | number | Maximum hits returned by findMany when no limit option is given (default: 10) |
documentFilter | DocumentFilter | 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.
| Option | Type | Default | Description |
|---|---|---|---|
defaultLimit | number | 10 | Default maximum number of hits returned by findMany |
documentFilter | DocumentFilter | 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.
| Property | Type | Description |
|---|---|---|
items | X[] | The matched documents |
count | number | Total number of matching documents |
relation | estypes.SearchTotalHitsRelation | Elasticsearch hit count relation ('eq' or 'gte') |
CreateOptions
| Option | Type | Description |
|---|---|---|
replaceIfExists | boolean | Replace the document if it already exists |
request | StrictOmit<estypes.CreateRequest, 'id' | 'index' | 'document'> | Raw Elasticsearch request options |
transport | TransportRequestOptions | Transport-level options |
CountOptions
| Option | Type | Description |
|---|---|---|
filter | ElasticAdapter.FilterInput | Filter criteria |
request | StrictOmit<estypes.CountRequest, 'index'> | Raw Elasticsearch request options |
transport | TransportRequestOptions | Transport-level options |
DeleteOptions
| Option | Type | Description |
|---|---|---|
filter | ElasticAdapter.FilterInput | Additional filter criteria |
request | StrictOmit<estypes.DeleteByQueryRequest, 'index'> | Raw Elasticsearch request options |
transport | TransportRequestOptions | Transport-level options |
DeleteManyOptions
Same shape as DeleteOptions.
FindOneOptions
Same as FindManyOptions without the limit property.
FindManyOptions
| Option | Type | Description |
|---|---|---|
filter | ElasticAdapter.FilterInput | Filter criteria |
projection | string | string[] | Fields to include |
sort | string[] | Sort directives, e.g. ['name', '-createdAt'] |
limit | number | Maximum number of hits. Defaults to defaultLimit. |
skip | number | Number of hits to skip |
noDecode | boolean | Skip OPRA output decoding and return raw documents |
request | StrictOmit<estypes.SearchRequest, 'index' | 'from' | 'size' | 'sort' | '_source'> | Raw Elasticsearch request options |
transport | TransportRequestOptions | Transport-level options |
SearchOptions
| Option | Type | Description |
|---|---|---|
noDecode | boolean | Skip OPRA output decoding |
transport | TransportRequestOptions | Transport-level options |
UpdateOneOptions
| Option | Type | Description |
|---|---|---|
filter | ElasticAdapter.FilterInput | Additional filter criteria |
request | StrictOmit<estypes.UpdateByQueryRequest, 'index'> | Raw Elasticsearch request options |
transport | TransportRequestOptions | Transport-level options |
UpdateManyOptions
Same shape as UpdateOneOptions.