Zephyrusk
zephyrusk.bsky.social
Zephyrusk
@zephyrusk.bsky.social
Pfp: avogado6

he/him. aspiring non-buster and mediocre programmer. umaine CS/MAT '25.
This is super useful for implementing a generic cascading soft delete in my application, where I am managing soft deletion and reference management in separate plugins.

This would also allow me to extend an existing plugin without breaking the base version. Cool!
January 13, 2026 at 4:11 AM
This turned out to not be the case! On a whim, I tried passing "deleteOne" to the middleware function post() with a callback, and it worked!

Mongoose automagically took up the name of my instance method and enabled middleware on it. From what I can tell, this is not mentioned in the documentation.
January 13, 2026 at 4:11 AM
I wanted to trigger some middleware when softDelete() was called. The Mongoose documentation lists the strings that can be passed to pre/post -- like the built-ins save and find.

Cursory research seemed to suggest that this list was not extendable and I'd have to handle this without middleware.
January 13, 2026 at 4:11 AM
I'm writing middleware inside Mongoose plugins. Plugins can also add static and instance methods to Schemas.

For example, I define `schema.methods.softDelete = function(){}` as an instance method which sets the `deleted` property of a document (a Model instance) to true.
January 13, 2026 at 4:11 AM
Each query type also comes extendable middleware, so you can define code that runs before (pre) or after (post) every query of a specific type.

So, by doing `schema.post("find", function(){})`, you can define a function that runs every after each find() query on a particular Schema (e.g. a User).
January 13, 2026 at 4:11 AM