Guides
Hooks
You control how your data is processed on each query
Hooks exist at two levels:
- schema hooks, attached to the schema definition,
- model hooks, attached to a specific model instance.
import { , } from "mongster";
const = .({
: .(),
: .(),
});
.("createOne", () => {
return {
: {
....,
: ...(),
},
};
});
.("find", () => {
.(..);
});
const = ("users", );
.("modify", () => {
return {
...,
: {
....,
: { : false },
},
};
});Supported group aliases
save:insertOne,insertMany,createOne,createManymodify:updateOne,updateMany,findOneAndUpdate,replaceOne,findOneAndReplace,upsertOneremove:deleteOne,deleteMany,findOneAndDelete
Execution order
When both schema and model hooks are present, the order is:
- model pre hook
- schema pre hook
- database operation
- schema post hook
- model post hook
Practical advice
- Use schema hooks for rules that belong to the data shape itself.
- Use model hooks for collection-specific behavior.
- Keep hook logic small and predictable, especially for write paths.