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

node-easy-crud

v1.0.4

Published

expose crud routes as api for express mongoose node app

Downloads

4

Readme

node-easy-crud

This package is ment to make creating CRUD application for your node express mongo environment easy. Light Weight with Zero Dependency

now build with TypeScript since version 1.0.4

It will Create API endpoints for Adding, Reading, Updating, Deleting form given mongoDB collection. you just simply have to provide a mongoose model to the constructor. It also provides form structure for frontend so that one can create a edit or add action forms easliy

a basic React component pacakge react-node-easy-crud is created to make quick curd UI for node-easy-crud. *I need help for creating a front end packages for Anguler, Vui, and React which will run greate with node-easy-crud*.

Installation

npm install node-easy-crud

Usage

you can simply create a crud api for mongoDB collection with following(Subject is mongoose model and router is express router):

new CURD(Subject, router);

Simple Example

const CURD = require("node-easy-crud");
const express = require("express");
const router = express.Router();

const Subject = require("../models/Subject"); //mongoose model

//to create CRUD routes for Subject model Simply use following code:
new CURD(Subject, router);

module.exports = router;

above code will create following endpoints

Read

GET <express-router-url>/<mongoose model name>

in our example mongoose model name is Subject so read endpoint is:

GET <express-router-url>/Subject


Add

this endpoint will be provided form structure for add action form

GET <express-router-url>/Subject/add-form


Insert

this endpoint will be provided for inserting a new document, you should provide the data to be inserted as JSON in request body.

POST <express-router-url>/Subject/insert


Edit form structure

this endpoint will be provided form structure and values of document to be edited, takes id of the document to be edited as paramenter

GET <express-router-url>/Subject/edit-form/:id


Update

this endpoint will be provided for updating a document, you should provide the data to be updated as JSON in request body. Data must contain id of document to be updated

POST <express-router-url>/Subject/update


Options

You can also pass options as object as 3rd argumnt to the constructor to customize

Example with options

this example shows how to use options

const CURD = require("node-easy-crud");
const express = require("express");
const router = express.Router();

const Subject = require("../models/Subject"); //mongoose model
const User = require("../models/User");

//to create CRUD routes for User model Simply use following code:
new CURD(Subject, router, {
  fields: ["name", "sem", "creator", "date"], // only select given fields for all CRUD oprations
  ref: { creator: { model: User, field: "name" } }, //replace value of creator field with value on given field i.e "name" from refrenced model
  route: "Subject", //will change route from default value model name to given value
});
module.exports = router;

Options Table

| name | default | data type | description | | -------------------- | ------------------- | --------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- | | idField | _id | string | set id field name of the model | | route | modelname | string | set base route for crud endpoints | | ref | null | object | set reference to another model and display field from another model in the place reference id. ex: ref :{ creator: { model: User, field: "name" } }, | | unsetAdd | false | boolean | set true to disable add on model | | unsetEdit | false | boolean | set true to disable edit on model | | unsetDelete | false | boolean | set true to disable delete on model | | fields | all fields in model | array | use to select specific fields while reading ex:fields: ["name", "sem", "creator", "topics"] | | addFields | fields | array | use to select specific fields while inserting | | editFields | fields | array | use to select specific fields while editing | | callbackBeforeRead | Undefined | function | use to add callback function to call before reading | | callbackBeforeDelete | Undefined | function | use to add callback function to call before deleting | | callbackBeforeUpdate | Undefined | function | use to add callback function to call before updating | | callbackBeforeInsert | Undefined | function | use to add callback function to call before inserting | | callbackAfterRead | Undefined | function | use to add callback function to call after reading | | callbackAfterDelete | Undefined | function | use to add callback function to call after deleting | | callbackAfterUpdate | Undefined | function | use to add callback function to call after updating | | callbackAfterInsert | Undefined | function | use to add callback function to call after inserting |

Callbacks

you can add callback functions after or before every CRUD opration, you can pass callback funtions in options while creating object. example below show how to add call back funtions before and after read opration:

const BeforeRead = () => {
  console.log("starting to read data");
};
const AfterRead = (data) => {
  console.log(data);
};

//New CRUD(Model,Router,Options)
new CURD(Subject, router, {
  fields: ["name", "sem", "creator", "date"],
  ref: { creator: { model: User, field: "name" } },
  route: "Subject", //Default value model name
  callbackBeforeRead: BeforeRead,
  callbackAfterRead: AfterRead,
});

callbackBeforeRead

you can pass a function in callbackBeforeRead. this function will run before reading the data. function you passed to callbackBeforeRead should not have any arguments required and does not need to return anything.

const BeforeRead = () => {
  console.log("starting to read data");
};

//New CRUD(Model,Router,Options)
new CURD(Subject, router, {
  callbackBeforeRead: BeforeRead,
});

callbackBeforeRead does not need to return anything but however if you want to stop reading in callbackBeforeRead you can return a error message and then node-easy-crud will not read and return data

const BeforeRead = () => {
  console.log("starting to read data");
  return { errorFromCallback: "you can not read this data" };
};

//New CRUD(Model,Router,Options)
new CURD(Subject, router, {
  callbackBeforeRead: BeforeRead,
});

if you return errorFromCallback in callbackBeforeRead node-easy-crud will not read data and return the error in responce as following:

{ error: errorFromCallback }

callbackBeforeDelete

you can pass a function in callbackBeforeDelete. this function will run before deleting a document from collection. function you passed to callbackBeforeDelete will recive request body containing id of document to be delated and does need to return reqest body.

const BeforeDelete = (body) => {
  console.log("deleting row with id:" + body.id);
  return body; //returning body is required
};

//New CRUD(Model,Router,Options)
new CURD(Subject, router, {
  callbackBeforeDelete: BeforeDelete,
});

callbackBeforeUpdate

you can pass a function in callbackBeforeUpdate. this function will run before updating a document from collection. function you passed to callbackBeforeUpdate will recive request body containing document to be updated and does need to return reqest body.

const BeforeUpdate = (body) => {
  console.log("updating row with id:" + body.id);
  body.name = body.name.toUpperCase(); //changing the name to uppercase just for example
  return body; //returning body is required
};

//New CRUD(Model,Router,Options)
new CURD(Subject, router, {
  callbackBeforeUpdate: BeforeUpdate,
});

callbackBeforeInsert

you can pass a function in callbackBeforeInsert. this function will run before inserting a document to collection. function you passed to callbackBeforeInsert will recive request body containing document to be added and does need to return reqest body.

const BeforeInsert = (body) => {
  console.log("inserting new row");
  body.name = body.name.toUpperCase(); //changing the name to uppercase just for example
  return body; //returning body is required
};

//New CRUD(Model,Router,Options)
new CURD(Subject, router, {
  callbackBeforeInsert: BeforeInsert,
});

errorFromCallback

To stop insert,update or delete opration in before call back, add errorFromCallback to body object and return it. then node-easy-crud will not perfore opration and only send error in responce

const BeforeDelete = (body) => {
  body.errorFromCallback = "you dont have permission to delete this object";
  return body;
};
//New CRUD(Model,Router,Options)
new CURD(Subject, router, {
  callbackBeforeDelete: BeforeDelete,
});

messageFromCallback

to add custome success message add messageFromCallback to body before returning it in any callbackBefore other than callbackBeforeRead.

const BeforeDelete = (body) => {
  body.messageFromCallback = "Just Trashed subject with id:" + body.id;
  return body;
};
//New CRUD(Model,Router,Options)
new CURD(Subject, router, {
  callbackBeforeDelete: BeforeDelete,
});

callbackAfterRead

you can pass a function in callbackAfterRead. this function will run after reading the data. function you passed to callbackAfterRead will recive data read form the given collection.

const AfterRead = (data) => {
  console.log(data);
};

//New CRUD(Model,Router,Options)
new CURD(Subject, router, {
  callbackAfterRead: AfterRead,
});

callbackAfterDelete

you can pass a function in callbackAfterDelete. this function will run after deleting a document. function you passed to callbackAfterDelete will recive object return by mongoose after deleting the document.

const AfterDelete = (deleted) => {
  console.log(deleted);
};

//New CRUD(Model,Router,Options)
new CURD(Subject, router, {
  callbackAfterDelete: AfterDelete,
});

callbackAfterUpdate

you can pass a function in callbackAfterUpdate. this function will run after updating a document. function you passed to callbackAfterUpdate will recive updated document as a argument.

const AfterUpdate = (updatedRow) => {
  console.log(updatedRow);
};

//New CRUD(Model,Router,Options)
new CURD(Subject, router, {
  callbackAfterUpdate: AfterUpdate,
});

callbackAfterInsert

you can pass a function in callbackAfterInsert. this function will run after inserting new document. function you passed to callbackAfterInsert will recive new document as a argument.

const AfterInsert = (newRow) => {
  console.log(newRow);
};

//New CRUD(Model,Router,Options)
new CURD(Subject, router, {
  callbackAfterInsert: AfterInsert,
});

In case of any query or suggetions to improve this package contact me

Email me: [email protected]

linkedIn: Shashank Padwal