Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mongoose-find-or-error

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongoose-find-or-error

Simple Mongoose plugin for rejecting findOne and findById promises which resolve null

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
30
increased by42.86%
Maintainers
1
Weekly downloads
 
Created
Source

mongoose-find-or-error

Build Status

Simple Mongoose plugin for rejecting findOne and findById promises which resolve null.

Installation

npm install mongoose-find-or-error

Why create this plugin?

I find myself many times wanting to end a promise chain if a document is not found in the database. Without this plugin or similar solution I would end up doing something similar to this:

User.findById(userId)
  .then(user => {
    if(!user) {
      return Promise.reject('User was not found');
    }

    return doSomethingWithUser(user)
  })
  .then(someData => {
    // ...
  });
  .catch(err => {
    // ...
  });

Why not reduce the boilerplate by doing something like this:

User.findByIdOrError(userId)
  .then(user => doSomethingWithUser(user))
  .then(someData => {
    // ...
  });
  .catch(err => {
    // ...
  });

How to use

Hook the plugin to a schema:

// example.js

const mongoose = require('mongoose');
const findOrErrorPlugin = require('mongoose-find-or-error');

const schema = new mongoose.Schema({
  name: String
});

schema.plugin(findOrErrorPlugin);

mongoose.model('Example', schema);

Execute a query:

const Example = require('./example');

// use with findById

Example.findByIdOrError('idNotInDatabase')
  .then(() => {
    console.log('Only executed when document is found');
  })
  .catch(err => {
    console.log(err);
  });

 // or use with findOne

 Example.findOneOrError({ name: 'plugin example' })
   .then(() => {
     console.log('Only executed when document is found');
   })
   .catch(err => {
     console.log(err);
   });

You can override the default errors by defining plugin options:

schema.plugin(findOrErrorPlugin, {
  getFindByIdError: (id, modelName) => new MyCustomError(`Couldn't find ${modelName} by id ${id}`),
  getFindOneError: (query, modelName) => new MyCustomError(`Couldn't find ${modelName} by query`)
});

Running tests

npm test

Keywords

FAQs

Package last updated on 22 Dec 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc