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_ts_query_builder

v1.0.2

Published

sql query build tootom,make native SQL and ORM will be mixed to write, complement each other.

Downloads

5

Readme

mongoose_ts_query_builder

sql query build tootom,make native SQL and ORM will be mixed to write, complement each other.

install

npm i mongoose_ts_query_builder

Documentation

Overview

The QueryBuilder class is designed to streamline and enhance querying operations. It provides methods to search, filter, sort, paginate, and select specific fields from a query. Each method is designed to be intuitive and efficient, allowing for flexible and powerful query construction.

Methods

search(searchableFields: string[])

  • Description: Adds a search condition to the query.
  • Functionality: Matches any of the specified fields using a case-insensitive regex.
  • Parameters:
    • searchableFields (string[]): An array of field names to be searched.

filter()

  • Description: Filters the query based on the provided query parameters.
  • Functionality: Excludes specific fields used for other operations such as search, sort, limit, page, and fields.
  • Parameters: None. The method operates based on the query parameters provided externally.

sort()

  • Description: Sorts the query based on the provided sort parameters.
  • Functionality: If no sort parameter is specified, it defaults to sorting by -createdAt. for descending sorting use "-" before the sorting field exp:-name,-ratings.
  • Parameters: None. The method uses the sort parameters provided externally.

paginate()

  • Description: Paginates the query based on the page and limit parameters.
  • Functionality: Defaults to page 1 and limit 10 if not specified.
  • Parameters: None. The method uses the page and limit parameters provided externally.

fields()

  • Description: Selects specific fields from the query based on the provided fields parameter.
  • Functionality: Excludes __v by default.
  • Parameters: None. The method uses the fields parameter provided externally.

Usage

To use the QueryBuilder class, instantiate it and utilize its methods to build your query. Each method can be chained to create a comprehensive query operation.

const query = req.query;
const queryBuilder = new QueryBuilder(Student.find(), query);
queryBuilder
  .search(["name", "description"])
  .filter()
  .sort()
  .paginate()
  .fields();

exmaple

import QueryBuilder from "mongoose_ts_query_builder";
import { Student } from "./student.model";

const studentSearchableFields = ["name", "email", "studentId"];

const getAllStudentService = async (query: Record<string, unknown>) => {
  const student = Student.find().populate("admissionSemester");

  const studentQuery = new QueryBuilder(student, query)
    .search(studentSearchableFields)
    .filter()
    .sort()
    .paginate()
    .fields();

  const result = await studentQuery.modelQuery;
  return result;
};

How to use

.filter()

The .filter() method is used to filter the query results based on the provided query parameters. It excludes specific fields used for other operations such as searchTerm, sort, limit, page, and fields.

URL req

"/get/students?name=robert&address=colony"

code

new QueryBuilder(student, query).filter();
const result = await studentQuery.modelQuery;
return result;

.sort()

Sorts the query based on the provided sort parameters. Defaults to -createdAt if not specified.

URL req

"/get/students?sort=name,-createdAt"

code

new QueryBuilder(student, query).sort();
const result = await studentQuery.modelQuery;
return result;

.paginate()

Paginates the query based on the page and limit parameters. Defaults to page 1 and limit 10 if not specified.

URL req

"/get/students?page=2&limit=10"

code

const student = Student.find();

const studentQuery = new QueryBuilder(student, query).paginate();
const result = await studentQuery.modelQuery;
return result;

.fields()

Selects specific fields from the query based on the provided fields parameter. Excludes __v by default.

URL req

"/get/students?fields=name,email"

code

const student = Student.find();

const studentQuery = new QueryBuilder(student, query).fields();
const result = await studentQuery.modelQuery;
return result;