- Notifications
You must be signed in to change notification settings - Fork3
4. Parsers
The middleware supports parser functions. These functions receive a query string and convert them to the format provided by the middleware, which by default is the format accepted by MongoDB.
To use these functions, simply call them through the middleware instance and pass them the query string to be converted and its default values. If you pass the default values a merge will be performed with the result of the query strings. Here are some examples of each analyzer:
Example: Use theparser function without configuration options.
constqs=require('query-strings-parser')constquery='?fields=name,age&skip=0&limit=10&sort=created_at'console.log(qs.parseFields(query))/*** Result:* {* fields: { name: 1, age: 1 },* sort: { created_at: 'asc' },* filters: {},* pagination: { limit: 10, skip: 0 },* original: '?fields=name,age&skip=0&limit=10&sort=created_at'* }*/
Example: Use theparser function with configuration options.
constqs=require('query-strings-parser')constquery='?fields=name,age&page=1&limit=10&sort=created_at'console.log(qs.parseFields(query,{},{use_page:true}))/*** Result:* {* fields: { name: 1, age: 1 },* sort: { created_at: 'asc' },* filters: {},* pagination: { limit: 10, page: 1 },* original: '?fields=name,age&page=1&limit=10&sort=created_at'* }*/
Example: Use theparseFields function without configuration options.
constqs=require('query-strings-parser')constquery='?fields=name,age,created_at'console.log(qs.parseFields(query,{}))/*** Result:* {* name: 1,* age: 1,* created_at: 1,* }*/
Example: Use theparseFields function with configuration options.
constqs=require('query-strings-parser')constquery='?fields=name,age,created_at'console.log(qs.parseFields(query,{_id:0}))/*** Result:* {* name: 1,* age: 1,* created_at: 1,* _id: 0* }*/
Example: Use theparseSort function without configuration options.
constqs=require('query-strings-parser')constquery='?sort=name,-age,created_at'console.log(qs.parseSort(query,{}))/*** Result:* {* name: 'asc',* age: 'desc',* created_at: 'asc'* }*/
Example: Use theparseSort function with configuration options.
constqs=require('query-strings-parser')constquery='?sort=name,-age'console.log(qs.parseSort(query,{created_at:'asc'}))/*** Result:* {* name: 'asc',* age: 'desc',* created_at: 'asc'* }*/
Note: This function receives three parameters, instead of two, like the others. The third parameter is to identify whether the analysis will perform a page or skip operation. To use parse with pagination, this parameter must be true, otherwise it should be false.
Example: Use theparsePagination function without configuration options and use page as true.
constqs=require('query-strings-parser')constquery='?limit=20&page=3'console.log(qs.parsePagination(query,{},true))/*** Result:* {* limit: 20,* page: 3* }*/
Example: Use theparsePagination function without configuration options and use page as false.
constqs=require('query-strings-parser')constquery='?limit=20&skip=3'console.log(qs.parsePagination(query,{},false))/*** Result:* {* limit: 20,* skip: 3* }*/
Example: Use theparsePagination function with configuration options and use page as true.
constqs=require('query-strings-parser')constquery='?page=3'console.log(qs.parsePagination(query,{limit:20},true))/*** Result:* {* limit: 20,* page: 3* }*/
Example: Use theparsePagination function with configuration options and use page as false.
constqs=require('query-strings-parser')constquery='?skip=3'console.log(qs.parsePagination(query,{limit:20},false))/*** Result:* {* limit: 20,* skip: 3* }*/
Example: Use theparseFilter function without configuration options.
constqs=require('query-strings-parser')constquery='?name=elvis&age=80'console.log(qs.parseFilter(query,{}))/*** Result:* {* name: 'elvis',* age: 80* }*/
Example: Use theparseFilter function with configuration options.
constqs=require('query-strings-parser')constquery='?name=elvis'console.log(qs.parseFilter(query,{age:80}))/*** Result:* {* name: 'elvis',* age: 80* }*/
Example: Use theparseDate function without configuration options.
constqs=require('query-strings-parser')constquery='?start_at=2019-02-05T00:00:00&end_at=2019-02-05T23:59:59'console.log(qs.parseDate(query,{}))/*** Result:* {* $and: [* { created_at: { lt: 2019-02-05T23:59:59 }},* { created_at: { gte: 2019-02-05T00:00:00 }}* ]}* }*/
Example: Use theparseDate function with configuration options.
constqs=require('query-strings-parser')constquery='?start_at=2019-02-05T00:00:00&end_at=2019-02-05T23:59:59'console.log(qs.parseDate(query,{start_at:'timestamp',end_at:'timestamp'}))/*** Result:* {* $and: [* { timestamp: { lt: 2019-02-05T23:59:59 }},* { timestamp: { gte: 2019-02-05T00:00:00 }}* ]}* }*/