MongoPatchGenerator
MongoPatchGenerator converts an OPRA patch DTO into a MongoDB update filter ($set, $unset, $push, $pull). It is used internally by MongoEntityService to translate MongoPatchDTO<T> objects, but can be used directly when you need to build a custom update pipeline.
import { MongoPatchGenerator } from '@opra/mongodb';
Constructor
new MongoPatchGenerator()
No constructor arguments.
Methods
generatePatch
generatePatch<T extends object>(
dataType: ComplexType,
doc: PatchDTO<T>,
options?: MongoPatchGenerator.Options,
): {
update: UpdateFilter<T>;
arrayFilters?: Record<string, any>[];
initArrayFields?: string[];
}
Generates a MongoDB update filter from an OPRA patch document.
Parameters
| Parameter | Type | Description |
|---|---|---|
dataType | ComplexType | The OPRA ComplexType that describes the document schema |
doc | PatchDTO<T> | The patch object. Nested fields, array push/pull (_$push, _$pull), and null (unset) values are all supported. |
options | MongoPatchGenerator.Options | See Interfaces below |
Returns
| Property | Type | Description |
|---|---|---|
update | UpdateFilter<T> | The MongoDB update filter ($set, $unset, $push, $pull) |
arrayFilters | Record<string, any>[] | Array filters needed for nested entity updates — pass as the arrayFilters option to MongoDB |
initArrayFields | string[] | Fields that must be initialized as empty arrays before the update is applied |
Patch object conventions
| Value | Behaviour |
|---|---|
null for a field | Field is added to $unset |
| Non-null scalar | Field is added to $set |
Nested object for a ComplexType field | Recursively processed — only changed sub-fields are set |
_$push: { fieldName: value } | Appends to an array field using $push |
_$pull: { fieldName: [values] } | Removes matching items from an array using $pull |
Interfaces
MongoPatchGenerator.Options
| Option | Type | Description |
|---|---|---|
currentPath | string | Dot-notation path prefix to prepend to all generated field paths (useful for nested service patches) |
scope | string | Schema scope to apply when resolving fields from the data type |