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-mongoose-api

v1.1.9

Published

Simplify CRUD operations and MongoDB aggregations with this utility. Easily filter and paginate data by configuring fields in props

Downloads

951

Readme

Node Mongoose Api

node-mongoose-api is a transformative package for working with Express and MongoDB, designed to simplify CRUD operations and data aggregation. With this package, you can perform complex queries and aggregations with minimal effort. The key feature of this project is its optimized aggregation function, which allows you to filter data, count totals, and achieve your desired results efficiently.

Require Packages for this node-mongoose-api

"date-fns": "^3.6.0",
"date-fns-tz": "^3.1.3",
"express": "^4.19.2",
"formidable": "^3.5.1"
npm install date-fns date-fns-tz formidable

Key Features

  • Simplified Aggregations: Avoid writing lengthy aggregation pipelines. Easily filter data and perform counts using optimized aggregation functions.
  • Common Utility Functions: Access a range of utility functions commonly used in projects.

Tested

  • This package has been tested with various scenarios.
  • test-node-mongoose-api For practical examples clone this repo. This repository demonstrates the usage of node-mongoose-api for testing purposes.`

How to Use

To use the node-mongoose-api package in your project, follow these steps:

Import the Package

import { NodeMongooseApi, helpers, CustomParamsType, QueryType } from 'node-mongoose-api';
 const { createOp, updateOp, deleteOp, listAggregation, lookupUnwindStage,lookupStage  } = NodeMongooseApi(model);
const { handleAsync,handleAsyncSession,handleFormAsync,handleFormAsyncSession, ResponseJson, utility, message } = helpers;

Import Your Model

import { Member } from '../model/member'; // Adjust the path as necessary
const model = Member;  // Replace with your model
const modelName = 'Member'; // Replace with your model name

Initialize NodeMongooseApi

Initialize the NodeMongooseApi class with your model:

 const { createOp, updateOp, deleteOp, listAggregation, lookupUnwindStage,lookupStage  } = NodeMongooseApi(model);

Create Operation

To create a new document, use the createOp operation:

 const data = req.body;
 const response = await createOp.create({ data });

updateOp Operation

You have several options for updating documents:

  • findByIdAndUpdate
 const { id } = req.params;
 const data = req.body;
 const response = await updateOp.findByIdAndUpdate({ id, data });
  • updateMany
 const data = req.body;
 const { ids, ...updateData } = data;
 await updateOp.updateMany({ ids, data: updateData });
  • updateManyFast updateManyFastis a direct function for bulk updates. Ensure that the parameters match those sent from the client.
await updateOp.updateManyFast({ req, res });

deleteOp Operation

You can delete documents using the following methods:

  • deleteOne
 const { id } = req.params;
 await deleteOp.deleteOne({ id });
  • deleteMany
 const ids = req.body.ids;
 await deleteOp.deleteMany({ ids });
  • deleteManyFast
    deleteManyFast is a direct function for bulk deletions. Ensure that the parameters match those sent from the client.
 await deleteOp.deleteManyFast({ req, res });

listAggregation

  • This is the deep aggregation method*
  • simple
router.get('/list/:query?', list);
const result = await listAggregation({
    model,
    customParams= {
    projectionFields: {
      _id: 1,
      title: 1,
      category: 1,
      price:1,
      createdAt: 1,
      updatedAt: 1
    },
    },
    query: req.query
  });
   if (result) {
    const { data, total } = result;
    res.json...
  }
  • filter
 const customParams: CustomParamsType : {
    projectionFields: {
      _id: 1,
      title: 1,
      category: 1,
      price:1,
      createdAt: 1,
      updatedAt: 1
    },
    searchTerms: ['_id', 'title'] //use for searchTerm Filter
    numericSearchTerms:['price'] //use for filter number fields
  };
const result = await listAggregation({
    model,
    customParams,
    query: req.query
  });
  • params filter
  ***
   searchTerm="abc"

  /product/list?searchTerm=abc
  ***
  ***
  columnFilters=[{"id":"_id","value":["66cd4955ea80fe74a77a983b","66cc324a48a26ecb9a52bc19"]},{"id":"title","value":"abc"}]

  product/list?columnFilters=[{"id":"_id","value":["66cd4955ea80fe74a77a983b","66cc324a48a26ecb9a52bc19"]},{"id":"fullName","value":"testing3"}]

   columnFilters=[{"id":"category.name","value":["cat1","66cc324a48a26ecb9a52bc19"]},{"id":"title","value":"abc"}]
  ***
  • lookup
  const lookup = [
    ...lookupUnwindStage('categories', 'category', '_id', 'category'),
    ...lookupUnwindStage('members', 'publishby', '_id', 'publishby')
  ];
   const customParams: CustomParamsType = {
    lookup,
    projectionFields,
       searchTerms: ['_id', 'title','category.name']
        numericSearchTerms:['price']
  };
  • _ids
const result = await listAggregation({
    model,
    customParams,
    query: req.query
    ids: ['66cd4955ea80fe74a77a983b']
  });

Helper Functions

This package provides helper functions for handling asynchronous operations, processing form data, and performing utility tasks. Below is an overview of each helper function and its usage within this package.

const { handleAsync,handleAsyncSession,handleFormAsync,handleFormAsyncSession, ResponseJson, utility, message } = helpers;

  1. ResponseJson The ResponseJson function standardizes the format of JSON responses. It takes in parameters to set the HTTP status code, message, and optionally, the data, total count, and any custom properties.
 ResponseJson(res, 200, "customMessage", data,total,);
  1. Handling Asynchronous Operations
  • handleAsync Used to handle asynchronous requests with error management. The function automatically catches errors and logs them.
export const create = handleAsync(async (req,res,next) => {
  const data = req.body;
  utility.removeUndefined(data);
  const response = await createOp.create({ data });
  ResponseJson(res, 200, message.INSERT_SUCCESS, response);
}, modelName);
  • handleAsyncSession For handling MongoDB transactions with sessions. It ensures operations are performed within a single transaction context.
export const createSession = handleAsyncSession(async (req, res, next, session) => {
  let data = req.body;
  const userData = { fullName: 'session test', phone: '1234' };
  await userModel.createOp.create({ data: userData, options: { session } }); //user collection
  const response = await createOp.create({ data, options: { session } }); //product collection
  ResponseJson(res, 200, message.INSERT_SUCCESS, response);
}, modelName);
  1. Formidable Package Integration
  • handleFormAsync
export const create = handleFormAsync(
  async (req: Request, res: Response, next, err, fields, files) => {
   console.log(fields,files)
  },
  modelName
);
  • handleFormAsyncSession
export const create = handleFormAsyncSession(
  async (req: Request, res: Response, next,session, err, fields, files) => {
   console.log(fields,files)
  },
  modelName
);
  1. Utility Functions
  • removeUndefined
 const data = {
      name: "John",
      age: undefined,
      address: null,
      email: "",
      phone: "123-456-7890",
      unknown: "undefined",
    };
    const expected = {
      name: "John",
      phone: "123-456-7890",
    };
    Utility.removeUndefined(data);
    expect(data).toEqual(expected);
  • capitalizeFirstLetter
    const input = "example";
    const result = Utility.capitalizeFirstLetter(input);
     expect(result).toBe("Example");
  • capitalizeCamelSpace
    const input = "exampleCamelCaseString";
    const result = Utility.capitalizeCamelSpace(input);
    expect(result).toBe("Example Camel Case String");
  • isAllSameinArray
    const dataArray = [1, 1, 1];
    const result = Utility.isAllSameinArray(dataArray, 1);
     expect(result).toBe(true);
  • trimNameLower
    const input = "   Hello   world!   This is    a test.  ";
    const result = Utility.trimNameLower(input);
    expect(result).toBe("hello world! this is a test.");
  • pickObj
   const obj = {
      title: "Test",
      description: "Description",
      date: "2024-08-27",
    };
    const keys = ["title", "date"];
    const result = Utility.pickObj(obj, keys);
    expect(result).toEqual({ title: "Test", date: "2024-08-27" });
  • extractArrayItems
 const data = { a: ["texta"], b: ["textb"] };
    const result = Utility.extractArrayItems(data);
    expect(result).toEqual({ a: "texta", b: "textb" });