Socket
Socket
Sign inDemoInstall

advanced-results

Package Overview
Dependencies
288
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.5.0 to 0.6.0

6

dist/middleware/advancedResults.d.ts
/// <reference types="qs" />
import { Model } from 'mongoose';
/**
* @description Full description and doc about this middleware: https://www.npmjs.com/package/advanced-results
* @param model - the model for which the advanced filtering will be done
* @param populate - field(String) or fields(Array) that you want to populate
* @param param - if except the query, you want the advancedResults to be done by certain paramatear from url. Expecting: ['user', 'userId'](the first is the FIELD NAME for which the query from the parametar will refrence to, and the second is the PARAMETAR NAME in the url ('/reviews/:trainingProgramId/dada). Example: Review.find({[param[0]]: req.params[param[1]]}) is same as Review.find({user: userId}))
*/
declare const advancedResults: (model: Model<any>, populate?: string | string[] | undefined, param?: [string, string] | undefined, consoleIt?: Boolean) => (req: import("express-serve-static-core").Request<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs>, res: import("express-serve-static-core").Response<any, number>, next: import("express-serve-static-core").NextFunction) => Promise<any>;
export = advancedResults;
//# sourceMappingURL=advancedResults.d.ts.map

21

dist/middleware/advancedResults.js

@@ -57,6 +57,7 @@ "use strict";

// Im handling cases for queryes like: ?name[regex]=Bla and NOT ?name[$regex]=Bla, so i can prevent nosql injection. Because im using express-mongo-sanitize and what it does is all cases where there's $ transformers them in something else so no injection can be done
/*
***model*** - the model for which the advanced filtering will be done
***populate*** - field(String) or fields(Array) that you want to populate
***param*** - if except the query, you want the advancedResults to be done by certain paramatear from url. Expecting: ['trainingProgram', 'trainingProgramId'](the first is the field for which the query with param will be done, and the second is the name of the param in the url ('/reviews/:trainingProgramId/dada). Example: Review.find({[param[0]]: req.params[param[1]]}) is same as Review.find({trainingProgram: trainingProgramId}))
/**
* @description Full description and doc about this middleware: https://www.npmjs.com/package/advanced-results
* @param model - the model for which the advanced filtering will be done
* @param populate - field(String) or fields(Array) that you want to populate
* @param param - if except the query, you want the advancedResults to be done by certain paramatear from url. Expecting: ['user', 'userId'](the first is the FIELD NAME for which the query from the parametar will refrence to, and the second is the PARAMETAR NAME in the url ('/reviews/:trainingProgramId/dada). Example: Review.find({[param[0]]: req.params[param[1]]}) is same as Review.find({user: userId}))
*/

@@ -118,8 +119,12 @@ var advancedResults = function (model, populate, param, consoleIt) {

if (filter['q']) {
// I must handle the all case, i populate the req.query.all with the filter.all value, so in the (if statement) below it fetches and queries by every possible field in the Model
req.query.all = req.query.all
? req.query.all + (" " + filter['q'])
: filter['q'];
filter.all = filter['q'];
delete filter['q'];
}
// I must handle the all case, i populate the req.query.all with the filter.all value, so in the (if statement) below it fetches and queries by every possible field in the Model
if (filter.all) {
req.query.all = req.query.all
? req.query.all + (" " + filter.all)
: filter.all;
delete filter.all;
}
parsedFilter = mongooseObjectParser_1.parseObject(filter);

@@ -126,0 +131,0 @@ for (qr in parsedFilter) {

{
"name": "advanced-results",
"version": "0.5.0",
"version": "0.6.0",
"description": "Advanced query middleware for expressjs and mongoose",

@@ -10,3 +10,3 @@ "main": "dist/index.js",

"dev": "nodemon src/index.dev.ts",
"publish": "npm run build & npm publish"
"myPublish": "npm run build & npm publish"
},

@@ -13,0 +13,0 @@ "files": [

@@ -12,2 +12,5 @@ **Middleware** used for advanced querying Mongoose Documents for a specific Model trough GET request using the queries and parametars prvoided by the url . **It only works with MongoDB (mongoose) and expressjs**. It also provides **pagination**

**Additional examples about this middleware**:
[HERE](https://github.com/Blagoj5/baze-packages/tree/main/advanced-results/MoreDocsAndExamples)
## Install

@@ -264,10 +267,10 @@

| Operations | Description |
| ---------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **select** | selects single or multiple fields from a document, example.com?select=field or multiple example.com?select=field1,field2 |
| **sort** | Default field: createdAt, example.com?sort=field |
| **limit** | **default: 6**, used for **limiting documents** affects pagination object inside res.advancedResults, example.com?limit=5 |
| **page** | **default: 1**, the current page if there are multiple pages, this uses limit in order to calculate and represents the current property inside pagination object of res.advancedResults, ex. example.com?page=2 |
| **all** or **q** | used for searching trough all fields of provided model for a certain value provided in the url, ex. example.com?all=value or multiple words example.com?all=value1+value2 |
| **filter** | more complex then the other 2, it requires additional parsing before sending request to backend to work. Short explanation: filter is an object that first needs to be JSON.stringified then using library like query-parser, parsed INTO url then sent to the backend. [Check this additional explanation for usage](./MoreDocsAndExamples) |
| Operations | Description |
| ---------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **select** | selects single or multiple fields from a document, example.com?select=field or multiple example.com?select=field1,field2 |
| **sort** | Default field: createdAt, example.com?sort=field. **Field can be ?sort=name (ASC Order) or ?sort=-name (DSC Order)** |
| **limit** | **default: 6**, used for **limiting documents** affects pagination object inside res.advancedResults, example.com?limit=5 |
| **page** | **default: 1**, the current page if there are multiple pages, this uses limit in order to calculate and represents the current property inside pagination object of res.advancedResults, ex. example.com?page=2 |
| **all** or **q** | used for searching trough all fields of provided model for a certain value provided in the url, ex. example.com?all=value or multiple words example.com?all=value1+value2 |
| **filter** | more complex then the other 2, it requires additional parsing before sending request to backend to work. Short explanation: filter is an object that first needs to be JSON.stringified then using library like query-parser, parsed INTO url then sent to the backend. [Check this additional explanation for usage](https://github.com/Blagoj5/baze-packages/tree/main/advanced-results/MoreDocsAndExamples) |

@@ -333,1 +336,27 @@ #### Result: res.advancedResults explained in Details

If this middleware throws error it will automatically call next(new Error) which will then go to your error middleware handler. Check expressjs error handling page where it teaches you how to define error middleware to handle all types of error and return them into the res object back to the client
### Some cheat codes for the ones that made it to the end
Instead of manually adding let's say parametars to url example: localhost?name=Tester1&select=name. You can do that with **stringified object**. **Here's how you do that**:
```
const query = {
all: "Test",
select: "name",
page: "2"
}
```
Now you need to use **additional library like query-string** to stringify object to be transformed into valid url parametars
```
const queryString = require('query-string');
const url = `http://localhost:5000?${queryString.stringify(query)}`;
await axios.get(url);
```
The query string transforms my url variable to -> http://localhost:5005?all=Test&select=name&page=2 accordingly
[**CHECK THE ADDITIONAL DOCS FOR MANY MORE EXAMPLES AND EXPLANATION ON HOW TO USE THE ?filter PARAMETAR**](https://github.com/Blagoj5/baze-packages/tree/main/advanced-results/MoreDocsAndExamples)

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc