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

crud-route-builder

v1.2.5

Published

Creates routes for basic CRUD operations for the provided Mongoose models

Downloads

28

Readme

crud-route-builder

This is used to build simple routes for create/read/update/delete operations for your Mongoose models.

Installation

npm install crud-route-builder --save

Usage

router.use(basePath, buildRoutes(mongooseModel, {
    extensions: [Routes],
    before: [Routes],
    after: [Routes],
    resourceModifier: (Resource, req, res) => { ...; return Resource; }
}))

The buildRoutes function expects two parameters:

mongooseModel - A Mongoose model object

args - This is used to specify additional routes, overwrite existing ones, or add middleware before or after the default functions. The args object can have the following properties, all of which accept only arrays of the Route object: extensions, before, after.

resourceModifier is used to modify the Resource object during runtime. It could be used for example in combination with the mongo-tenant module, to specify the tenancy.

    router.use('/example-1', buildRoutes(Example1, {
        resourceModifier: (Resource, req, res) => {
            let modifiedResource = Resource.byTenant(req.tenancyId);
            return modifiedResource;
        }
    }));

Example

const express = require('express');
const router = express.Router();
const {buildRoutes, Route} = require('crud-route-builder');
const Example1 = require('../models/Example1');
const Example2 = require('../models/Example2');

const app = express();

router.use('/example-1', buildRoutes(Example1));
router.use('/example-2', buildRoutes(Example2,{ extensions: [
        Route('post', '/custom-route', (req, res, next) => {
            ...
        })
    ],
    before: [{
        Route('get', '/all', (req, res, next) => {
            // This will be executed before the default GET example-2/all functionality
            ...
        })
    }],
    after: [{
        Route('get', '/all', (req, res, next) => {
            // This will be executed after the default GET example-2/all functionality 
            ...
            res.send(req.crbResult);    // When an After middleware is specified the result is saved in req.cebResult
        })
    }],
}));

app.use('/', router);

This example will create the following routes:

GET /example-1/all - Returns all documents. Additional GET params can be sent to filter or sort the request:

_start  = NUMBER
_end    = NUMBER
_order  = DESC | ASC    
_sort   = FIELD NAME    
field   = value

Example: GET /example-1/all/?start=0&_end=10_order=ASC&_sort=name&color=red This request will return the first 10 documents, where the field "color" is "red", sorted by the field "name" in ascending order.

GET /example-1/single/:id - Returns a single document by id.

POST /example-1/ - Creates a document. The data is expected to be sent in the body.

DELETE /example-1/:id - Deletes a document by id.

PUT /example-1/:id - Updates a document. The data is expected to be sent in the body. Only updates the provided fields.

The same routes will be created for /example-2 plus the additional POST /example-2/custom-route

You can find a simple example app in the example folder.

Additional notes

All CRUD functions are exposed and can be used where necessary:

    const {getSingle, getAll, create, update, remove} = require('crud-route-builder');

This module requires you to use body-parser. It won't work without it.