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

mister-crud

v1.0.7

Published

A lightweight Node.js package for simplifying CRUD (Create, Read, Update, Delete) operations with MongoDB. Easily perform common database operations in your Node.js applications using a straightforward and intuitive interface.

Downloads

13

Readme

Mister-Crud

A simple CRUD package for handling data operations. This package provides functions for creating, reading, updating, and deleting items in a data model.

Note: Use "express.js" Only while using this package.

v1.0.6

Now I have done some minor changes in this version now you can use the mister-crud functions to get the response and send it to the client according to your needs. The example for doing this is shown in the usage section down below...

Additionally, I have added the functionality for populating fields of the model, such as if your Mongoose model contains the field with the type of mongoose.Schema.Types.ObjectId, you can easily populate it by adding the array of strings to the second argument of getItems() function and to the third argument of getItem() function...

v1.0.5

Added File Uploading Option.

v1.0.4

Fixed some readme info's.

v1.0.3

Added The Authentication Crud options.

Installation

To install this package in your project, run:

npm install mister-crud

Usage

v1.0.6 (Usage):

const crud = require("mister-crud");
const express = require("express");
const Model = require("location/to/the/model");
const app = express();
const crud = require("mister-crud");

app.get("/getItems", async (req, res) => {
  const response = await crud.getItems(res, Model);
  if (response.success === false) {
    res.status(404).json(response);
  } else {
    res.json(response);
  }
});

For Populating The Fields:

app.get("/getItems", async (req, res) => {
  const response = await crud.getItems(Model, ["field1", "field2"]);
  if (response.success === false) {
    res.status(404).json(response);
  } else {
    res.json(response);
  }
});
app.get("/getItem/:id", async (req, res) => {
  const response = await crud.getItems(Model, ["field1", "field2"]);
  if (response.success === false) {
    res.status(404).json(response);
  } else {
    res.json(response);
  }
});

Updated Usage Of Functions IN NEW VERSION v1.0.6:

app.post("/addItem", async (req, res) => {
  const response = await crud.createItem(req, Model);
});
app.delete("/deleteItem/:id", async (req, res) => {
  const response = await crud.deleteItem(req, Model);
});
app.put("/updateItem/:id", async (req, res) => {
  const response = await crud.updateItem(req, Model);
});
app.get("/getItem/:id", async (req, res) => {
  const response = await crud.getItems(Model, ["field1", "field2"]);
});
app.get("/getItems/", async (req, res) => {
  const response = await crud.getItems(Model, ["field1", "field2"]);
});

Importing the Package

const crud = require("mister-crud");

Creating_Item

crud.createItem(req, res, model);

req: The request object containing the data for the new item. res: The response object to send back the result. model: The Mongoose model to interact with.

Uploading Files

 This is the function for uploading the image...
crud.upload.single("image");
 if u  want to upload the image then just write this inside the function e.g: app.post() in this case, this will work as middleware.
app.post("/createItem", crud.upload.single("image"), (req, res) => {
  {
    crud.createItem(req, res, model);
  }
});
Make sure to use the enctype="multipart/form-data" in your html form. and make sure that the name of the field in the form used for getting the image file, is same as using in the crud.upload.single('image') like in this case it's "image". Make sure that the field in your mongoose model for storing image's links are image not img or any other.

// note : your model must have image field in it to order to save the link of image. e.g:
    const myModel  =new mongoose.Schema({
        image:{
            url:{
                type:String,
                required:true
            }
            filePath:{
                type:String,
                required:true
            }
        }
        // other fields...
    });

Reading Items

crud.getItems(res, model);

res: The response object to send back the result. model: The Mongoose model to interact with.

Reading Single Item

crud.getItem(req, res, model);

req: The request object containing the item's ID. res: The response object to send back the result. model: The Mongoose model to interact with.

Updating Item

crud.updateItem(req, res, model);

req: The request object containing the item's ID and updated data. res: The response object to send back the result. model: The Mongoose model to interact with.

Deleting Item

crud.deleteItem(req, res, model);

req: The request object containing the item's ID. res: The response object to send back the result. model: The Mongoose model to interact with.

Signing Up

crud.signUp(req, res, model);

req: The request object containing the user information, Email And Password Are must. res: The response object to send back the result. model: The Mongoose model to interact with.

Login

crud.login(req, res, model, JWT_SECRET);

req: The request object containing the user information, Email And Password Are must. res: The response object to send back the result. model: The Mongoose model to interact with. JWT_SECRET: The secret key to encrypt the user id.

Get User

crud.getUser(req, res, model);

req: The request object containing the id of the user decrypted by the fetch();

res: The response object to send back the result.

Note: Dont forget to use fetch() before using this getUser() function

Fetch

crud.fetch(req, res, JWT_SECRET);

req: The request object containing the token returned by login function; you need to store the token in the headers with the name of "token";

res: The response object to send back the result. JWT_SECRET: The secret key to decrypt the user id, The Key should be same like used in the login function.

Note: You have to use this fetch function; where ever the login is required for Example before using getUser()

Contributions

If you find any issues or have suggestions for improvements, feel free to open an issue or submit a pull request.

License

This project is licensed under the ISC License - see the LICENSE.md file for details.

Feel free to use this template and adjust any details based on your specific package and preferences!