MongoNestedService
import { MongoNestedService } from '@opra/mongodb';
Hierarchy: ServiceBase → MongoService → MongoNestedService
Constructor
new MongoNestedService<T>(
dataType: Type | string,
fieldName: string,
options?: MongoNestedService.Options,
)
| Argument | Type | Description |
|---|---|---|
dataType | Type | string | The parent OPRA data type or its registered name |
fieldName | string | The array field name within the parent document |
options | MongoNestedService.Options | See Interfaces below |
Properties
| Property | Type | Description |
|---|---|---|
fieldName | string | The array field name in the parent document |
nestedKey | string | The primary key field of array elements |
defaultLimit | number | Default findMany page size |
nestedFilter | FilterInput | function | Common filter for nested operations |
Methods
All methods take documentId as the first argument. Methods that target a specific element also take nestedId as the second argument.
assert
assert(documentId: AnyId, id: AnyId, options?: ExistsOptions<T>): Promise<void>
Throws ResourceNotAvailableError if the nested element does not exist.
options — ExistsOptions<T>
create
create(documentId: AnyId, input: PartialDTO<T>, options: RequiredSome<CreateOptions, 'projection'>): Promise<PartialDTO<T>>
create(documentId: AnyId, input: PartialDTO<T>, options?: CreateOptions): Promise<T>
Pushes an element into the array field and returns it.
options — CreateOptions
createOnly
createOnly(documentId: AnyId, input: DTO<T>, options?: CreateOptions): Promise<PartialDTO<T>>
Pushes an element into the array field without fetching it back.
options — CreateOptions
count
count(documentId: AnyId, options?: CountOptions<T>): Promise<number>
Returns the number of array elements matching the filter.
options — CountOptions<T>
delete
delete(documentId: AnyId, nestedId: AnyId, options?: DeleteOptions<T>): Promise<number>
Removes the element with the given nestedId. Returns 1 if deleted, 0 otherwise.
options — DeleteOptions<T>
deleteMany
deleteMany(documentId: AnyId, options?: DeleteManyOptions<T>): Promise<number>
Removes all array elements matching the filter. Returns the number of deleted elements.
options — DeleteManyOptions<T>
exists
exists(documentId: AnyId, nestedId: AnyId, options?: ExistsOptions<T>): Promise<boolean>
Returns true if an element with the given nestedId exists.
options — ExistsOptions<T>
existsOne
existsOne(documentId: AnyId, options?: ExistsOptions<T>): Promise<boolean>
Returns true if at least one element matches the filter.
options — ExistsOptions<T>
findById
findById(documentId: AnyId, nestedId: AnyId, options: RequiredSome<FindOneOptions<T>, 'projection'>): Promise<PartialDTO<T> | undefined>
findById(documentId: AnyId, nestedId: AnyId, options?: FindOneOptions<T>): Promise<T | undefined>
Fetches an element by its nestedId. Returns undefined if not found.
options — FindOneOptions<T>
findOne
findOne(documentId: AnyId, options: RequiredSome<FindOneOptions<T>, 'projection'>): Promise<PartialDTO<T> | undefined>
findOne(documentId: AnyId, options?: FindOneOptions<T>): Promise<T | undefined>
Fetches the first element matching the filter. Returns undefined if not found.
options — FindOneOptions<T>
findMany
findMany(documentId: AnyId, options: RequiredSome<FindManyOptions<T>, 'projection'>): Promise<PartialDTO<T>[]>
findMany(documentId: AnyId, options?: FindManyOptions<T>): Promise<T[]>
Fetches a list of array elements. Applies defaultLimit when options.limit is not specified.
options — FindManyOptions<T>
findManyWithCount
findManyWithCount(documentId: AnyId, options: RequiredSome<FindManyOptions<T>, 'projection'>): Promise<{ count: number; items: PartialDTO<T>[] }>
findManyWithCount(documentId: AnyId, options?: FindManyOptions<T>): Promise<{ count: number; items: T[] }>
Same as findMany but also returns the total count of matching elements before pagination.
options — FindManyOptions<T>
get
get(documentId: AnyId, nestedId: AnyId, options: RequiredSome<FindOneOptions<T>, 'projection'>): Promise<PartialDTO<T>>
get(documentId: AnyId, nestedId: AnyId, options?: FindOneOptions<T>): Promise<T>
Fetches an element by its nestedId. Throws ResourceNotAvailableError if not found.
options — FindOneOptions<T>
update
update(documentId: AnyId, nestedId: AnyId, input: MongoPatchDTO<T>, options: RequiredSome<UpdateOneOptions<T>, 'projection'>): Promise<PartialDTO<T> | undefined>
update(documentId: AnyId, nestedId: AnyId, input: MongoPatchDTO<T>, options?: UpdateOneOptions<T>): Promise<T | undefined>
Applies a partial update to the element with the given nestedId and returns the updated element.
options — UpdateOneOptions<T>
updateOnly
updateOnly(documentId: AnyId, nestedId: AnyId, input: MongoPatchDTO<T>, options?: UpdateOneOptions<T>): Promise<number>
Same as update but does not fetch and return the updated element. Returns the number of matched elements.
options — UpdateOneOptions<T>
updateMany
updateMany(documentId: AnyId, input: MongoPatchDTO<T>, options?: UpdateManyOptions<T>): Promise<number>
Applies a partial update to all array elements matching the filter. Returns the number of affected elements.
options — UpdateManyOptions<T>
Lifecycle hooks
Override in subclasses. Base implementations are no-ops.
| Hook | Signature |
|---|---|
_beforeCreate(command) | (CreateCommand<T>) => Promise<void> |
_afterCreate(command, result) | (CreateCommand<T>, PartialDTO<T>) => Promise<void> |
_beforeUpdate(command) | (UpdateOneCommand<T>) => Promise<void> |
_afterUpdate(command, result) | (UpdateOneCommand<T>, PartialDTO<T> | undefined) => Promise<void> |
_beforeUpdateMany(command) | (UpdateManyCommand<T>) => Promise<void> |
_afterUpdateMany(command, affected) | (UpdateManyCommand<T>, number) => Promise<void> |
_beforeDelete(command) | (DeleteCommand<T>) => Promise<void> |
_afterDelete(command, affected) | (DeleteCommand<T>, number) => Promise<void> |
_beforeDeleteMany(command) | (DeleteCommand<T>) => Promise<void> |
_afterDeleteMany(command, affected) | (DeleteCommand<T>, number) => Promise<void> |
Interfaces
MongoNestedService.Options
Extends MongoService.Options.
| Option | Type | Default | Description |
|---|---|---|---|
defaultLimit | number | 10 | Default maximum number of elements returned by findMany |
nestedKey | string | '_id' | Primary key field name within the nested array elements |
nestedFilter | FilterInput | ((command, _this) => FilterInput) | — | Common filter applied to every nested read and write operation |
CreateOptions
Extends mongodb.InsertOneOptions.
| Option | Type | Description |
|---|---|---|
projection | string | string[] | Document | '*' | Fields to return in the fetched-back element. |
CountOptions
Extends mongodb.CountOptions.
| Option | Type | Description |
|---|---|---|
filter | FilterInput<T> | Filter criteria applied to the nested elements. |
documentFilter | FilterInput | Additional filter applied to the parent document. |
DeleteOptions
Extends mongodb.DeleteOptions.
| Option | Type | Description |
|---|---|---|
filter | FilterInput<T> | Additional filter for the element to delete. |
documentFilter | FilterInput | Additional filter applied to the parent document. |
DeleteManyOptions
Extends mongodb.DeleteOptions.
| Option | Type | Description |
|---|---|---|
filter | FilterInput<T> | Filter criteria — only matching elements are deleted. |
documentFilter | FilterInput | Additional filter applied to the parent document. |
ExistsOptions
Extends mongodb.CommandOperationOptions (minus session).
| Option | Type | Description |
|---|---|---|
filter | FilterInput<T> | Additional filter criteria for the existence check. |
FindOneOptions
Extends mongodb.AggregateOptions.
| Option | Type | Description |
|---|---|---|
filter | FilterInput<T> | Additional filter criteria. |
projection | string | string[] | Document | '*' | Fields to include. '*' returns all fields. |
sort | string[] | Sort directives. |
skip | number | Number of elements to skip. |
preStages | mongodb.Document[] | Aggregation stages inserted before the filter stage. |
postStages | mongodb.Document[] | Aggregation stages appended after other stages. |
documentFilter | FilterInput | Additional filter applied to the parent document. |
FindManyOptions
Extends mongodb.AggregateOptions.
| Option | Type | Description |
|---|---|---|
filter | FilterInput<T> | Filter criteria for the nested elements. |
projection | string | string[] | Document | '*' | Fields to include. '*' returns all fields. |
sort | string[] | Sort directives. |
limit | number | Maximum number of elements to return. Defaults to defaultLimit. |
skip | number | Number of elements to skip. |
preStages | mongodb.Document[] | Aggregation stages inserted before the filter stage. |
postStages | mongodb.Document[] | Aggregation stages appended after the limit stage. |
documentFilter | FilterInput | Additional filter applied to the parent document. |
nestedFilter | FilterInput | Per-call filter applied to nested elements, merged with the service-level nestedFilter. |
UpdateOneOptions
Extends mongodb.FindOneAndUpdateOptions (minus projection, returnDocument, includeResultMetadata).
| Option | Type | Description |
|---|---|---|
projection | string | string[] | Document | '*' | Fields to return in the updated element. |
filter | FilterInput<T> | Additional filter criteria for the update. |
documentFilter | FilterInput | Additional filter applied to the parent document. |
initArrayFields | string[] | Array field names to initialize as [] if they don't exist before the update. |
UpdateManyOptions
Extends mongodb.UpdateOptions (minus upsert).
| Option | Type | Description |
|---|---|---|
filter | FilterInput<T> | Filter criteria — only matching elements are updated. |
documentFilter | FilterInput | Additional filter applied to the parent document. |
initArrayFields | string[] | Array field names to initialize as [] if they don't exist before the update. |