Hello,
I want to create a middleware to detect when a document is updated in my collection.
The goal is to reset the cache of the route when a document is updated.
So, I wanted to call a post-schema listener like this: when a schema is updated, it calls my function.
I have implemented something like this:
// cats.schema.ts
Cats.post(
[
'save',
'deleteMany',
'findOneAndDelete',
'findOneAndReplace',
'findOneAndUpdate',
'replaceOne',
'updateMany',
'updateOne',
'deleteOne',
],
function (doc) {
console.log(doc);
},
);
Right below SchemaFactory.createForClass.
But now, I need to place it in a NestJs @unborn owlable class. This way, I can have access to the cacheManager (or @nestjs/event-emitter; this is how I want to call my reset cache function).
However, when I put the same code in onModuleInit, it's not working. The console.log(doc) is not called.
// cats.repository.ts
onModuleInit() {
console.log('Here');
Cats.post(
[
'save',
'deleteMany',
'findOneAndDelete',
'findOneAndReplace',
'findOneAndUpdate',
'replaceOne',
'updateMany',
'updateOne',
'deleteOne',
],
function (doc) {
console.log(doc);
},
);
}
Do you have any idea of how I can achieve this? Or perhaps if I can directly call an event outside an @unborn owlable class?
Thanks