MongoDB
MongoDB hooks
Each method to insert, update, fetch or delete the entity is provided with before and after hooks, pretty similar to service hooks, but in this case works in the storage layer.
This storage hooks are useful for instance database monitoring or intercept the DMO object before save it or after save it, or what you need.
The MongoDB Repository contains a reference to a HookBuilder
struct which implements the interfaces:
ISaveHooks
defines the before and after methods to be call at db save operationIFetchByIdHooks
defines the before and after methods to be call at db fetch by id operationIUpdateHooks
defines the before and after methods to be call at db update operationIDeleteByIdHooks
defines the before and after methods to be call at db delete by id operationIDeleteByHooks
defines the before and after methods to be call at db delete by field operationIQueryHooks
defines the before and after methods to be call at db query operation
Builder pattern
Hooks are following the same builder pattern as the Service's hooks
to ensure the hook atomicity
and non-shared
features.
Repository constructor
The hook builder instance should be set up at repository creation. In order to do it a constructor function is provided
with a hook builder parameter. By default, a NoOpHook
is provided.
Hook struct
The HookBuilder
requires an instance of the Hooks
struct which can be implemented with the needed hook functions.
Each hook function will return the specific built instance to run. For instance, imagine that we would like to track the entity saving time and we have an object that provide us with the logic to start and track the consumed time:
Save hooks
The save hook instances must implements the interface below:
Fetch by ID hooks
The FetchById hook instances must implements the interface below:
Update hooks
The Update hook instances must implements the interface below:
Delete by ID hooks
The DeleteById hook instances must implements the interface below:
Delete hooks
The Delete hook instances must implements the interface below:
Query hooks
The Query hook instances must implements the interface below: