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

mongoose-query-build

v1.0.7

Published

query builder for filter and search use case across application models.

Downloads

3

Readme

Mongoose Query Builder

The Mongoose Query Builder is a powerful library designed to simplify the construction of MongoDB queries using Mongoose. It provides a convenient and intuitive way to generate complex queries without the need for manual query building.

With the Mongoose Query Builder, you can easily define query fields and their corresponding patterns, such as exact match, list values, date range, and search. By registering these query fields, you gain the ability to generate MongoDB queries effortlessly based on user input or predefined criteria.

Key Features

  • Simplified Query Construction: Constructing complex MongoDB queries becomes straightforward and intuitive with the Query Builder's easy-to-use API.
  • Pattern-Based Query Generation: Define patterns for different types of queries, including exact match, list values, date range, and search, allowing for flexible and dynamic query generation.
  • Customizable and Extensible: The Query Builder is highly customizable, enabling you to define and register your own query patterns and generators.
  • Seamless Integration with Mongoose: Utilize the generated queries seamlessly with Mongoose models, making it a perfect fit for Mongoose-based projects.
  • Examples and Usage Guide: The library comes with a comprehensive set of examples and a usage guide to help you get started quickly.

Documentation

Installation

First install Node.js and MongoDB. Then:

$ npm i mongoose-query-build

Example

NOTE:

  1. The default for value in query parameter splitting is comma to accommodate multiple clause usage but in case of search, you might want to split by "space" and "comma" etc, the "comma" usage is important to find multiple items at a time by providing the value separated by comma.

  2. Always log the register output to know what query parameter field u can define for your APIs per model you defined.

Simple Case 1

     import mongoose from 'mongoose';
     import { MongooseQueryBuilder } from 'mongoose-query-build';
     import { BuildFieldType, BuildPattern } from 'mongoose-query-build/utils';
     
     const { Schema } = mongoose;
     const blogSchema = new Schema({
       title: String,
       author: String,
       comments: [{ body: String, date: Date }],
     });
    const Blog = mongoose.model('Blog', blogSchema);


 
 // Schema definition for query register

   // This returns array of generated query parameters to use. 
   // You can assign to see the query names.
    MongooseQueryBuilder.register({
     model: 'blog',
     fields: [
      {
        name: 'title',
        type: BuildFieldType.STRING,
        patterns: [BuildPattern.SEARCH],
     },
     {
        name: 'author',
        type: BuildFieldType.STRING,
        patterns: [BuildPattern.SEARCH],
     },
     {
        name: 'comments.body',
        type: BuildFieldType.STRING,
        patterns: [BuildPattern.SEARCH],
     },   
     {
        name: 'comments.date',
        type: BuildFieldType.DATE,
        patterns: [BuildPattern.DATE_RANGE],
     },     
     ]
    })

  // Service usage 
  async findAll(req) {
    // sample URL looks like https://exampe.com/api/v1/blogs?blog_title=The Begining,Age of war&blog_author=Fola&blog_comments_body=news&blog_comments_date=2023-09-09,2023-09-10
    const result = MongooseQueryBuilder.generate(req.query);
    return Blog.find(result.dbQuery);
  }

Simple Case 2

Using Exact with boolean, objectId, number, String, date

 const queryFields = MongooseQueryBuilder.register({
        model: 'user',
        fields: [
          {
            name: 'business',
            type: BuildFieldType.OBJECT_ID,
            patterns: [BuildPattern.EXACT_LIST],
          },
          {
            name: 'status',
            type: BuildFieldType.BOOLEAN,
            patterns: [BuildPattern.EXACT_LIST],
          },
          {
            name: 'email',
            type: BuildFieldType.STRING,
            patterns: [BuildPattern.EXACT_LIST],
          },
          
          {
            name: 'amount',
            type: BuildFieldType.NUMBER,
            patterns: [BuildPattern.EXACT_LIST],
          },
        ],
      });
      

Sample case 3

Using mix of Exact and Search with boolean, objectId, number, String, date

 const queryFields = MongooseQueryBuilder.register({
        model: 'user',
        fields: [
          {
            name: 'business',
            type: BuildFieldType.OBJECT_ID,
            patterns: [BuildPattern.EXACT_LIST],
          },
          {
            name: 'status',
            type: BuildFieldType.BOOLEAN,
            patterns: [BuildPattern.EXACT_LIST],
          },
          {
            name: 'email',
            type: BuildFieldType.STRING,
            patterns: [BuildPattern.EXACT_LIST, BuildPattern.SEARCH],
          },
          
          {
            name: 'amount',
            type: BuildFieldType.NUMBER,
            patterns: [BuildPattern.EXACT_LIST, BuildPattern.SEARCH],
          },
        ],
      });

Other Notice

Please check the repository for sample test case to support your usage in code.

Thanks!!!