1.0.2 • Published 3 years ago
mongoose-filter-query v1.0.2
mongoose-filter-query
Middleware which implements a standardized format and maps an incoming http request's query params to a format which is supported by mongoose
Installation
# using npm
npm install mongoose-filter-query
# using yarn
yarn add mongoose-filter-queryUsage
const { default: mongooseFilterQuery } = require("mongoose-filter-query");
// or
import mongooseFilterQuery from "mongoose-filter-query";
.....
.....
// in your express app
app.use(mongooseFilterQuery);
// while invoking your database query
const data = await Model.find(req.query.filter).sort(req.query.sort)
console.log(data);
// rest of your codeBasic Usage Examples
'http://localhost:3000/api/users?filter[first_name]=eq(John)'- This will return all users with the first name John
'http://localhost:3000/api/users?filter[first_name]=ne(John)'- This will return all users with the first name not equal to John
'http://localhost:3000/api/users?filter[first_name]=reg(Jo)'- This will return all users with the first name matching the given regular expression
'http://localhost:3000/api/users?filter[age]=gt(20)'- This will return all users with the age greater than 20
'http://localhost:3000/api/users?filter[age]=gte(20)'- This will return all users with the age greater than or equal to 20
'http://localhost:3000/api/users?filter[age]=lt(20)'- This will return all users with the age less than 20
'http://localhost:3000/api/users?filter[age]=lte(20)'- This will return all users with the age less than or equal 20
'http://localhost:3000/api/users?filter[role]=in(staff,manager)'- This will return all users with a user role of either staff or manager
'http://localhost:3000/api/users?filter[role]=nin(staff,manager)'- This will return all users with a user role which is neither of staff and manager
'http://localhost:3000/api/users?filter[phone]=exists(true)'- This will return all users who have the phone number attribute in their database record
'http://localhost:3000/api/users?filter[phone]=exists(false)'- This will return all users who doesn't have the phone number attribute in their database record
Advanced Usage Examples
'http://localhost:3000/api/users?filter[first_name]=or(eq(John),eq(Eric),reg(Kat))'- This will return all users with a first name of John, Eric or matches the given regular expression
'http://localhost:3000/api/users?filter[age]=and(gt(20),lt(30))'- This will return all users with an age which is between 20 and 30
Multiple Filters
- Multiple filters can be chained together with the use of the & operator
'http://localhost:3000/api/users?filter[first_name]=eq(John)&filter[age]=gt(20)'- This will return all users with the first name John and an age greater than 20
Sorting Support
- Filters could be used along with sorting
- A value of 1 or asc for the sort parameter will sort the results in ascending order
- A value of -1 or desc for the sort parameter will sort the results in descending order
'http://localhost:3000/api/users?filter[first_name]=eq(John)&sort[age]=1'- This will return all users with the first name John and sorted by age in ascending order
'http://localhost:3000/api/users?filter[first_name]=eq(John)&sort[age]=desc'- This will return all users with the first name John and sorted by age in descending order