feathersjs-ecosystem / feathers-mongoose

Easily create a Mongoose Service for Feathersjs.
MIT License
189 stars 96 forks source link

Support document middleware #395

Open AdamGerthel opened 3 years ago

AdamGerthel commented 3 years ago

Feature request

Support document middleware

Background

AFAIK, document middleware, such as mongoose pre-save hooks, are not supported. I'm assuming that this is because feathers-mongoose uses queries such as findOneAndUpdate() rather than loading the document and then saving it (see https://mongoosejs.com/docs/middleware.html#notes).

As suggested in other issues, it's possible, and often recommended, to utilise Feathers service hooks instead. However, there are use cases when that option is not very fitting. An example is when you are re-using a single schema in several other schemas. Perhaps even nested on different levels in different models.

In such a use case, using service hooks would be very messy compared to a single, all-encompassing Mongoose pre validate/save/remove hook.

daffl commented 3 years ago

This should work already as long as you set the lean option to false.

AdamGerthel commented 3 years ago

@daffl I've read that too somewhere, but it doesn't! I've tried every possible way/setting/option.

daffl commented 3 years ago

Ideally we'd need an example of what isn't working as expected then.

AdamGerthel commented 3 years ago

Yep, I totally get that. Will see if I get time to throw together a small repro repo.