7.1.2 • Published 10 months ago

@sliit-foss/mongoose-filter-query v7.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

@sliit-foss/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 @sliit-foss/mongoose-filter-query

# using yarn
yarn add @sliit-foss/mongoose-filter-query

Usage

const { default: mongooseFilterQuery } = require("@sliit-foss/mongoose-filter-query");

// or

import mongooseFilterQuery from "@sliit-foss/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 code

Basic 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[or]=first_name=eq(John),last_name=eq(Eric)";
  • This will return all users with a first name of John or a last name of Eric
"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
"http://localhost:3000/api/users?filter[and]=age=gt(20),first_name=eq(John)";
  • This will return all users with an age greater than 20 and a first name of John

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

Selection Support

"http://localhost:3000/api/users?filter[first_name]=eq(John)&select=first_name,last_name";
  • This will return all users with the first name John and only the first_name and last_name attributes will be returned
"http://localhost:3000/api/users?filter[first_name]=eq(John)&select=-first_name,-last_name";
  • This will return all users with the first name John and all attributes except the first_name and last_name attributes will be returned

Population Support

"http://localhost:3000/api/users?filter[first_name]=eq(John)&include=posts";
  • This will return all users with the first name John and the posts attribute will be populated
"http://localhost:3000/api/users?filter[first_name]=eq(John)&include=posts,comments";
  • This will return all users with the first name John and the posts and comments attributes will be populated
7.1.2

10 months ago

7.0.3

11 months ago

7.1.1

10 months ago

7.1.0

10 months ago

6.0.0

2 years ago

7.0.0

2 years ago

7.0.2

2 years ago

7.0.1

2 years ago

5.1.1

2 years ago

5.1.0

2 years ago

5.0.3

2 years ago

5.0.2

2 years ago

5.0.1

2 years ago

5.0.0

2 years ago

5.0.0-blizzard.1

2 years ago

5.0.0-blizzard.0

2 years ago

5.0.0-blizzard.2

2 years ago

4.0.0

2 years ago

3.0.0

2 years ago

2.0.0-blizzard.0

2 years ago

2.0.0

2 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.0

3 years ago