Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sifatullahsu/mongoose-null-error
A Mongoose plugin for enforcing validation against null values returned by certain query operations.
https://github.com/sifatullahsu/mongoose-null-error
mongoose mongoose-plugin
Last synced: 5 days ago
JSON representation
A Mongoose plugin for enforcing validation against null values returned by certain query operations.
- Host: GitHub
- URL: https://github.com/sifatullahsu/mongoose-null-error
- Owner: sifatullahsu
- Created: 2024-04-09T05:47:40.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2024-04-09T05:57:05.000Z (7 months ago)
- Last Synced: 2024-10-18T14:54:11.284Z (30 days ago)
- Topics: mongoose, mongoose-plugin
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/mongoose-null-error
- Size: 40 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# mongoose-null-error
`mongoose-null-error` is a plugin for Mongoose that provides functionality to enforce validation against null values returned by certain query operations. It'll throw an error if query return value is null.
## Installation
You can install `mongoose-null-error` via npm:
```bash
npm install mongoose-null-error
``````bash
yarn add mongoose-null-error
```## Usage
To use `mongoose-null-error`, apply the plugin to your Mongoose schema definition and pass the desired options when performing query operations.
```typescript
import mongoose from 'mongoose'
import mongooseNullError from 'mongoose-null-error'// Define your Mongoose schema
const MySchema = new mongoose.Schema({
// Define your schema fields
})// Apply the mongoose-null-error plugin to your schema
MySchema.plugin(mongooseNullError)// Define your Mongoose model
const MyModel = mongoose.model('MyModel', MySchema)
```## Performing Query Operations
When performing query operations such as `findOne`, `findById`, `findByIdAndUpdate`,`findOneAndUpdate`, `findByIdAndDelete`, `findOneAndDelete`, or `findOneAndReplace`, you can pass an option to enable null error validation.
```typescript
// Example usage with findById
try {
const user = await User.findById(id, '', { mongooseNullError: true })
// Handle successful query
} catch (error) {
// Handle error thrown due to null value
console.error(error.message)
}
```When the specified query operation returns a null value and the `mongooseNullError` option is set to `true`, an error will be thrown to indicate an invalid request.
## Contributing
We welcome contributions from the community. If you want to contribute to this project.
## License
This project is licensed under the MIT License.
## Contact
For questions, feedback, or support, please feel free to contact at [[email protected]](mailto:[email protected]).
#### Thank you for using Mongoose Null Error!