Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alorel/mongoose-find-or-throw-plugin
Don't check for nulls when running your Mongoose 5 queries - let rejected promises do it for you.
https://github.com/alorel/mongoose-find-or-throw-plugin
check find found if mongo mongodb mongoose not null nullcheck or plugin throw typed types
Last synced: 23 days ago
JSON representation
Don't check for nulls when running your Mongoose 5 queries - let rejected promises do it for you.
- Host: GitHub
- URL: https://github.com/alorel/mongoose-find-or-throw-plugin
- Owner: Alorel
- License: mit
- Created: 2018-02-13T21:59:39.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-10-07T21:50:42.000Z (about 6 years ago)
- Last Synced: 2024-04-14T00:17:48.363Z (7 months ago)
- Topics: check, find, found, if, mongo, mongodb, mongoose, not, null, nullcheck, or, plugin, throw, typed, types
- Language: JavaScript
- Homepage:
- Size: 12.7 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mongoose-throw-if-not-found-plugin
[![NPM link](https://nodei.co/npm/mongoose-throw-if-not-found-plugin.svg?compact=true)](https://www.npmjs.com/package/mongoose-throw-if-not-found-plugin)
[![Build Status](https://travis-ci.org/Alorel/mongoose-find-or-throw-plugin.svg?branch=master)](https://travis-ci.org/Alorel/mongoose-find-or-throw-plugin)
[![Coverage Status](https://coveralls.io/repos/github/Alorel/mongoose-find-or-throw-plugin/badge.svg?branch=master)](https://coveralls.io/github/Alorel/mongoose-find-or-throw-plugin?branch=master)
[![Greenkeeper badge](https://badges.greenkeeper.io/Alorel/mongoose-find-or-throw-plugin.svg)](https://greenkeeper.io/)
![Supports Node >= 6](https://img.shields.io/badge/Node-%3E=6-brightgreen.svg)Don't check for nulls when running your Mongoose 5 queries - let rejected promises do it for you.
Before:
```js
MyModel.findOne({foo: 'bar'})
.then(doc => {
if (!doc) {
throw new Error('Doc not found');
}
useDoc(doc);
});
```After:
```js
MyModel.findOne({foo: 'bar'})
.throwIfEmpty()
.then(doc => {
useDoc(doc);
})
.catch(e => {
console.log(e.status); // 404
console.log(e.message); // Not found
console.log(e.name); // MongooseDocumentNotFoundError
})
```# Usage
Simply import the library once anywhere in your code:
```js
require('mongoose-throw-if-not-found-plugin');
```It'll handle itself from there ;)
# TypeScript usage
While typings *are* provided, sometimes your IDE may fail to recognise them for code hints.
If this happens, simply add the import to the file you're using it from:```typescript
import 'mongoose-throw-if-not-found-plugin';
```