npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongoose-bread

v1.0.18

Published

A pagination and softdelete library for mongoose

Downloads

50

Readme

mongoose-bread

A pagination and softdelete library for Mongoose.

npm version unittests

Why This Plugin

mongoose-bread is a pagination and softdelete library for quick feature-rich setup of JSON apis. This plugin abstracts the complex task of setting up paramterized collection queries by applying the BREAD convention as Resource methods. So you can Browse Read Edit Add and Destroy a resource by calling the respective method. It creates consistent query results and makes data consumption a lot more predictable. It concerns itself only with the data access layer of your application, for data analysis and post-query mutations implement a service layer as you see fit.

Dependencies

mongoose-bread is build upon

Recommended Packages

mongoose-bread recommends the use of the following package(s)

Installation

npm install mongoose-bread

Quick Use

Import the plugin in your Model Models/Product.js

const mongooseBread = require('mongoose-bread')

Add the plugin to your Schema

ProductSchema.plugin(mongooseBread, { /* options */ })

In your Controller make use of breadHelper() and methods provided by the plugin Controller/ProductController.js

Import the Model

const Product = require('../Models/Product')

Add Controller method - i.e. browseProducts()

ProductController.browseProducts = async (req, res) => {
  const options = Product.breadHelper().createBrowseOptions(req)
  const result = await Product.browse(options)
  res.status(200).json(result)
}

Add a Route to use browseProducts() server.js

// your imports
const ProductController = require('../Controller/ProductController')

// your express setup
app.get('/api/v1/products', ProductController.browseProducts)

With your server running you can now paginate, search and filter the results.
Select only specific fields to return.
Make the output lean ...and much more.
All through the parameters in your URL.

A call to https://myapp.com/api/v1/products?page=2&limit=5&select=name+price&sort=quantity
responds with 5 products, sorted by quantity, on page 2 with only name and price selected

Usage

For a thorough example setup - see: Usage Documentation

Snippets

We have them - use them - see: Snippets Documentation

Plugin Options

mongoose-bread is configurable to your needs through global Options and when registering the plugin to your Schema
Here is a glimpse of what is possible:

ProductSchema.plugin(mongooseBread, { 
  defaultPageSize: 20, // fallback value for limit - default: 10
  maxPageSize: 50, // limits queries to return 50 documents max - default: 100
  searchableFields: ['name', 'description'], // enables search
  blacklistedFields: ['__v'], // exclude __v from all results
})

For all Options and recommended setups - see: Options Documentation

Methods

mongoose-bread adds the following methods to your Schema

  • browse( options )
  • read( options )
  • edit( options )
  • add( options )
  • destroy( options )

With softDelete enabled it adds two additional methods

  • softDelete( options )
  • rehabilitate( options )

It also inherits the methods from mongoose-paginate-v2 and mongoose-delete[^1]
BUT these methods are used and abstracted by mongoose-bread so you don't have the hassle to figure them out

For the respective output of these methods see: Returnvalue Documentation

Helper Methods

mongoose-bread provides the following helper methods through a Model.breadHelper() call

  • createBrowseOptions( request )
  • createReadOptions( request )
  • createEditOptions( request )
  • createAddOptions( request )
  • createDeleteOptions( request )

With softDelete enabled it adds three additional methods

  • createBrowseDeletedOptions( request )
  • createReadDeletedOptions( request )
  • createRehabilitateOptions( request )

For shorthands in your Controller/[Resource]Controller.js you can use the following snippet

const {
  createBrowseOptions,
  createReadOptions,
  createEditOptions,
  createAddOptions,
  createDeleteOptions,
  createBrowseDeletedOptions,
  createReadDeletedOptions,
  createRehabilitateOptions,
} = ResourceModel.breadHelper()

To understand how helper methods and methods work together see: Returnvalue Documentation

Typescript

Working on it
Looking for wizards

Development

  • Ensure all tests pass before you commit by running npm run test.
    In order to run the tests, you need to have the Mongo Deamon running locally.
  • There are pre-commit hooks that run to ensure the files you've changed are formatted correctly.
  • Optionally you can manually run npm run lint && npm run prettier to lint and format every relevant file
  • If using VS Code, install eslint and prettier for easy editor integration.

License

MIT

[^1]: only if softDelete is enabled pluginOptions.softDelete = true