Skip to main content

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

ParameterTypeDescription
dataTypeComplexTypeThe OPRA ComplexType that describes the document schema
docPatchDTO<T>The patch object. Nested fields, array push/pull (_$push, _$pull), and null (unset) values are all supported.
optionsMongoPatchGenerator.OptionsSee Interfaces below

Returns

PropertyTypeDescription
updateUpdateFilter<T>The MongoDB update filter ($set, $unset, $push, $pull)
arrayFiltersRecord<string, any>[]Array filters needed for nested entity updates — pass as the arrayFilters option to MongoDB
initArrayFieldsstring[]Fields that must be initialized as empty arrays before the update is applied

Patch object conventions

ValueBehaviour
null for a fieldField is added to $unset
Non-null scalarField is added to $set
Nested object for a ComplexType fieldRecursively 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

OptionTypeDescription
currentPathstringDot-notation path prefix to prepend to all generated field paths (useful for nested service patches)
scopestringSchema scope to apply when resolving fields from the data type