npm i nice-query
example uri :
'/api/products?select=name,price,qte&page=2&sort=-price&quantity[gt]=0'
select fields : select=field1,field2
ex: select=name,price
page : page=pageNumber
ex: page=2
sort by : sort=(+ or -)fieldName
ex: sort=+price (asc)
ex2 : sort=-price (desc)
select where : fliedName[gt,lt,gte,lte]=value
quantity[gt]=0
import FilterRequest from 'nice-query';
const getProducts= (req,res,next)=>{
const filterProducts = new FilterRequest({ ...req.query },Product.find({}))
.filter()
.sort()
.select()
.paginate(10);
const products = await filterProducts.collection;
...
}
import FilterRequest from 'nice-query';
export default (req,res)=>{
const filterProducts = new FilterRequest({ ...req.query },Product.find({}))
.filter()
.sort()
.select()
.paginate(10);
const products = await filterProducts.collection;
...
}
...
const filterProducts = new FilterRequest<ProductsDocument>(
{ ...req.query },
Product.find({})
).filter()
.sort()
.select()
.paginate(10);
const products = await filterProducts.collection;
...