Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

4. Parsers

Douglas Rafael edited this pageSep 30, 2019 ·8 revisions

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:


parser()

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'* }*/

parseFields()

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* }*/

parseSort()

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'* }*/

parsePagination()

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* }*/

parseFilter()

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* }*/

parseDate()

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 }}*   ]}* }*/

Clone this wiki locally


[8]ページ先頭

©2009-2025 Movatter.jp