ElasticEntityService
ElasticEntityService<T> is an abstract class that sits between ElasticService and ElasticCollectionService. It adds the index-level CRUD primitives, codec handling, and lifecycle hooks.
You do not extend it directly. Extend ElasticCollectionService instead.
import { ElasticEntityService } from '@opra/elastic';
Hierarchy: ServiceBase → ElasticService → ElasticEntityService
Lifecycle hooks
Override these in ElasticCollectionService subclasses. The base implementations are no-ops.
| Hook | Signature |
|---|---|
_beforeCreate(command) | (CreateCommand) => Promise<void> |
_afterCreate(command, result) | (CreateCommand, PartialDTO<T>) => Promise<void> |
_beforeUpdate(command) | (UpdateCommand<T>) => Promise<void> |
_afterUpdate(command, result) | (UpdateCommand<T>, PartialDTO<T> | undefined) => Promise<void> |
_beforeUpdateMany(command) | (UpdateCommand<T>) => Promise<void> |
_afterUpdateMany(command, affected) | (UpdateCommand<T>, number) => Promise<void> |
_beforeDelete(command) | (DeleteCommand) => Promise<void> |
_afterDelete(command, affected) | (DeleteCommand, number) => Promise<void> |
_beforeDeleteMany(command) | (DeleteCommand) => Promise<void> |
_afterDeleteMany(command, affected) | (DeleteCommand, number) => Promise<void> |
Interfaces
ElasticEntityService.Options
Extends ElasticService.Options.
| Option | Type | Description |
|---|---|---|
indexName | string | ((_this) => string) | Target Elasticsearch index name |
resourceName | string | ((_this) => string) | Resource name used in error messages |
scope | string | Comma-delimited scopes used to filter the API document |
idGenerator | (command, _this) => any | Custom ID generator for new documents |
Command interfaces
Command objects are passed to lifecycle hooks. Each interface extends CommandInfo and narrows the crud discriminant.
CreateCommand
| Property | Type |
|---|---|
crud | 'create' |
input | PartialDTO<T> |
options | CreateOptions |
CountCommand
| Property | Type |
|---|---|
crud | 'read' |
options | CountOptions |
DeleteCommand
| Property | Type |
|---|---|
crud | 'delete' |
documentId | string (optional) |
options | DeleteOptions | DeleteManyOptions |
DeleteManyCommand
| Property | Type |
|---|---|
crud | 'delete' |
options | DeleteManyOptions |
FindManyCommand
| Property | Type |
|---|---|
crud | 'read' |
options | FindManyOptions |
SearchCommand
| Property | Type |
|---|---|
crud | 'read' |
request | estypes.SearchRequest |
options | SearchOptions |
UpdateCommand<T>
| Property | Type |
|---|---|
crud | 'update' |
documentId | string (optional) |
input | PatchDTO<T> |
options | UpdateOneOptions | UpdateManyOptions |
Operation options
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 to return. 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.