MQuery is a TypeScript utility class designed to streamline MongoDB queries in Node.js applications using Mongoose. It provides features for filtering, sorting, projecting fields, and paginating results based on request parameters.
- Dynamic Filtering: Automatically filter data based on query parameters, supporting regex searches on specified fields.
- Sorting: Easily sort query results based on request parameters.
- Field Projection: Select specific fields to return in the query results.
- Pagination: Implement pagination with customizable page and limit values.
To install the necessary dependencies, use npm:
npm install mongooseimport MQuery from './path/to/MQuery';
import { Document } from 'mongoose';
interface YourDocument extends Document {
title: string;
description: string;
// Add other fields as necessary
}import { Request, Response } from 'express';
import MQuery from './MQuery'; // Adjust the import path
import { YourDocument } from './yourDocumentInterface'; // Adjust the path to your interface
import { YourModel } from './yourModel'; // Replace with your actual model
export const getItems = async (req: Request, res: Response) => {
try {
const features = new MQuery<YourDocument>(
YourModel.find(),
['title', 'description'], // Specify searchable fields
req.query
)
.filter()
.sort()
.project()
.paginate();
const items = await features.dbQuery;
res.status(200).json({
status: 'success',
results: items.length,
data: {
items,
},
});
} catch (error) {
res.status(500).json({
status: 'error',
message: error.message,
});
}
};- constructor(dbQuery: Query<T[], T>, searchFields: string[], reqQuery?: RequestQuery): Initializes the MQuery instance.
- dbQuery: Mongoose query object.
- searchFields: Array of fields to apply regex filtering.
- reqQuery: Optional request query parameters.
- filter(): MQuery: Applies filtering based on request query parameters.
- sort(): MQuery: Sorts the results based on sort query parameter or defaults to sorting by -createdAt.
- project(): MQuery: Projects specific fields to return based on the fields query parameter.
- paginate(): MQuery: Implements pagination logic based on page and limit query parameters.
If you’d like to contribute to the project, feel free to submit a pull request or open an issue with suggestions.
This project is licensed under the MIT License. Feel free to modify any sections further or add more details as needed! If you have any more requests, just let me know.