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-mongo-crud

v0.1.0

Published

This npm package provides reusable CRUD functions for performing standard operations on MongoDB models using Mongoose. Each function handles common CRUD tasks, such as creating, reading, updating, and deleting documents, while providing structured respons

Downloads

253

Readme

mister-mongo-crud

This npm package provides reusable CRUD functions for performing standard operations on MongoDB models using Mongoose. Each function handles common CRUD tasks, such as creating, reading, updating, and deleting documents, while providing structured responses and error handling.

Installation

To install this package, use npm:

npm install mister-mongo-crud

Usage/Examples

Create

  1. misterCreate()

Creates a new document in the specified model.

Parameters:

  • model (Model): Mongoose model on which to perform the operation.
  • query (object): Data to create the new document.
  • important (array of strings): List of required fields. If any of these fields are missing in query, the function returns an error.
const result = await misterCreate({ model: YourModel, query: { name: 'John', age: 30 }, important: ['name', 'age'] });

Response:

  • Success: { item: <created item>, success: true, status: 200, message: "Item Created!" }
  • Error (missing fields): { item: null, success: false, message: "Missing important field: <field_name>", status: 400 }

Read

  1. misterRead()

Fetches multiple documents from the model based on a query.

Parameters:

  • model (Model): Mongoose model on which to perform the operation.
  • query (object, optional): Filter for retrieving documents.
  • toPopulate (array of strings, optional): List of fields to populate in the results.
const result = await misterRead({ model: YourModel, query: { age: 30 }, toPopulate: ['relatedField'] });

Response:

  • Success: { items: <array of documents>, success: true, status: 200, message: "Items Fetched!" }
  • Error: { success: false, message: <error message>, status: <error code> }

ReadOne

  1. misterReadOne()

Fetches a single document from the model based on a query.

Parameters:

  • model (Model): Mongoose model on which to perform the operation.
  • query (object): Filter for retrieving a single document.
  • toPopulate (array of strings, optional): List of fields to populate in the results.
const result = await misterReadOne({ model: YourModel, query: { _id: 'someId' }, toPopulate: ['relatedField'] });

Response:

  • Success: { items: <single document>, success: true, status: 200, message: "Item Fetched!" }

  • Error: { success: false, message: <error message>, status: <error code> }

Update

  1. misterUpdate()

Updates a document based on its ID.

Parameters:

  • model (Model): Mongoose model on which to perform the operation.

  • query (object): Data to update the document.

  • id (ObjectId or string): ID of the document to update.

const result = await misterUpdate({ model: YourModel, query: { age: 31 }, id: 'someId' });

Response:

  • Success: { updatedItem: <updated document>, success: true, status: 200, message: "Item Updated!" }

  • Error (not found): { success: false, status: 404, message: "Item with this id not found! while Updating." }

Delete

  1. misterDelete()

Deletes multiple documents based on an array of IDs.

Parameters:

  • model (Model): Mongoose model on which to perform the operation.

  • ids (array of ObjectId or string): Array of IDs of documents to delete

const result = await misterDelete({ model: YourModel, ids: ['id1', 'id2'] });

Response:

  • Success: { success: true, status: 200, message: "Items Deleted!", deletedCount: <number of items deleted> }

  • Error (not found): { success: false, status: 404, message: "No items found with the provided ids while Deleting Many." }

DeleteOne

  1. misterDeleteOne()

Deletes a single document based on its ID.

Parameters:

  • model (Model): Mongoose model on which to perform the operation.

  • id (ObjectId or string): ID of the document to delete.

const result = await misterDeleteOne({ model: YourModel, id: 'someId' });

Response:

  • Success: { success: true, status: 200, message: "Item Deleted!" }

  • Error (not found): { success: false, status: 404, message: "Item with this id not found! while Deleting One." }

License

MIT

Authors