hvpl-mongoose-utility
v1.0.1
Published
A Mongoose plugin that restricts direct query method usage and enforces safe wrapper methods
Readme
hvpl-mongoose-utility
hvpl-mongoose-utility is a Mongoose plugin that standardizes database access across apps. It blocks direct use of common Mongoose query methods and provides approved wrapper methods instead, so teams can keep data access consistent, auditable, and safe.
Method Mapping
| Use this wrapper | Instead of this Mongoose method |
| --- | --- |
| findManyByHvpl() | find() |
| findOneByHvpl() | findOne() |
| findByIdByHvpl() | findById() |
| findLatestByHvpl() | findOne() with sort |
| findOneAndUpdateByHvpl() | findOneAndUpdate() |
| findByIdAndUpdateByHvpl() | findByIdAndUpdate() |
| updateOneByHvpl() | updateOne() |
| updateManyByHvpl() | updateMany() |
| findOneAndDeleteByHvpl() | findOneAndDelete() |
| findByIdAndDeleteByHvpl() | findByIdAndDelete() |
| deleteOneByHvpl() | deleteOne() |
| deleteManyByHvpl() | deleteMany() |
| countDocumentsByHvpl() | countDocuments() |
| existsByHvpl() | exists() |
| aggregateByHvpl() | aggregate() |
| createDocumentByHvpl() | create() |
Usage
Use wrapper methods
const users = await User.findManyByHvpl({ isActive: true });
const user = await User.findOneByHvpl({ email: "[email protected]" });
await User.updateOneByHvpl({ _id: userId }, { $set: { name: "Updated" } });Notes
- Direct Mongoose calls such as
find(),findOne(),updateOne(), andaggregate()are restricted on protected models. - An error is thrown to prevent the use of direct Mongoose calls.
License
ISC
