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 🙏

© 2025 – Pkg Stats / Ryan Hefner

crud-helper

v4.2.0

Published

This is a CRUD Helper module for expressjs apps used with mongoose

Readme

crud-helper

This is a CRUD Helper module for expressjs apps used with mongoose and Joi in order to simplify the common operations

Installation

$ npm install crud-helper --save

requirements

  • This module supports mongoDB database only for now.
  • This module supports soft remove only for now.
  • You need to add this snippet of code after schema definition for (search and soft remove):
// index all string attributes for search
mySchema.index({'$**': 'text'});

//*********  for soft remove ********* 
mySchema.pre('aggregate', function () {
    if(this._pipeline[0]['$match'].deleted === undefined)
        this._pipeline[0]['$match'].deleted = {_state: false};
});

mySchema.pre('countDocuments', function () {
    if(this._conditions.deleted === undefined)
        this.where({deleted: {_state: false}});
});

mySchema.pre('find', function () {
    if(this._conditions.deleted === undefined)
        this.where({deleted: {_state: false}});
});

mySchema.pre('findOne', function () {
    if(this._conditions.deleted === undefined)
        this.where({deleted: {_state: false}});
});

mySchema.pre('findOneAndUpdate', function () {
    if(this._conditions.deleted === undefined)
        this.where({deleted: {_state: false}});
    this.options.new = true;
    this.options.runValidators = true;

});
// ********************************

for more details check the panda api example.

Examples

const express = require('express');
const router = express.Router();
const crudHelper = require('crud-helper');
const pandaModel = require('../models/panda');


router.route('/pandas')
    // add panda using panda model
    .post(
            crudHelper.create(pandaModel,(req)=>{
                const {name,skill} = req.body;
                return {name:name,skill:skill};
        })
    )
    // get all pandas
    .get(
           crudHelper.get(pandaModel,'find',(req)=>{
                   return {};
           })
    )

router.route('/:id')
    // get panda by id
    .get(
            crudHelper.get(pandaModel,'findOne',(req)=>{
                    return {_id: req.params.id};
            })
    )
    // put panda by id
    .put(
            crudHelper.update(pandaModel,'findOneAndUpdate',(req)=>{
                    return {_id: req.params.id};
                },(req)=>{
                    const {name,skill} = req.body;
                    const myBody = {name:name,skill:skill};
                    return extra.flexible(myBody);
            })
    )
    //delete panda by id
    .delete(
            crudHelper.delete(pandaModel,'findOneAndDelete',(req)=>{
                return {_id: req.params.id};
            })
    )


module.exports = router;

Usage

  • create(resourceModel, data[,middleware])
  • get(resourceModel, type, filter[,projection,middleware])
  • update(resourceModel,type, filter, data[,middleware])
  • delete(resourceModel, type, filter[,middleware])

params description

  • resourceModel : mongoose schema object
  • type : type of query (eg. find, findOne, findOneAndUpdate)
  • data : callback function with req parameter which return the data for CREATE or UPDATE operations
(req)=>{
    // ...
    return data;
} 
  • filter : callback function with req parameter which return filter object just like mongoose, (eg. {name:'th3m7j0'} filter by name)
(req)=>{
    // ...
    return {name:'th3m7j0'};
} 
  • middleware : boolean (true,false) if middleware is true then the crud function will act as a middleware [optional]

Params GET

  1. supports display parameter in order to get the desired fields
  2. supports expand parameter in order to get all the data, the expansion of nested fields is done using .. (eg. user..photo this will expand the user and his photo)
    • filter in exapnd attributes using ';' delimiter (eg: expand=client;client.activity::1) supports only one neested attribute and equal operation using filter pure js
  3. supports sort parameter
  4. supports limit parameter
  5. supports start parameter
  6. supports filter parameter
    • supports several operations: ['equals','gt','lt','gte','lte','regex','ne','or']
    • the delimiter between the attribute and the operation is __
    • the delimiter between the attribute and the value is ::
    • usage: filter=attribute__operation::value by default operation=equals
  7. supports page (the number of the page) parameter used with limit
  8. supports search parameter, works with mongoDB text index search.
  9. supports count parameter

(eg. /SomeEndpoint?display=attr1,attr2&expand=attr3,attr4&sort=attr5&limit=5&start=1&filter=attr::val) (eg. /SomeEndpoint?count=1) (eg. /SomeEndpoint?count=1&filter=attr::val) (eg. /SomeEndpoint?search=val)

Params POST

  1. supports expand parameter in order to get all the data, the expansion of nested fields is done using .. (eg. user..photo this will expand the user and his photo)

Input Validation

  • crud-helper >= 4.0.0 supports input validation with the help of Joi. the validation of fields [data(req)/ filter(req)] will be done automatically using the types of mongoose schema.

Project example

check this example api : panda api