Skip to content

4. Parsers

Douglas Rafael edited this page Sep 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 the parser function without configuration options.

const qs = require('query-strings-parser')

const query = '?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 the parser function with configuration options.

const qs = require('query-strings-parser')

const query = '?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 the parseFields function without configuration options.

const qs = require('query-strings-parser')

const query = '?fields=name,age,created_at'
console.log(qs.parseFields(query, {}))

/**
* Result: 
* { 
*     name: 1,
*     age: 1,
*     created_at: 1,
* }
*/

Example: Use the parseFields function with configuration options.

const qs = require('query-strings-parser')

const query = '?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 the parseSort function without configuration options.

const qs = require('query-strings-parser')

const query = '?sort=name,-age,created_at'
console.log(qs.parseSort(query, {}))

/**
* Result: 
* { 
*     name: 'asc',
*     age: 'desc',
*     created_at: 'asc'
* }
*/

Example: Use the parseSort function with configuration options.

const qs = require('query-strings-parser')

const query = '?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 the parsePagination function without configuration options and use page as true.

const qs = require('query-strings-parser')

const query = '?limit=20&page=3'
console.log(qs.parsePagination(query, {}, true))

/**
* Result: 
* { 
*     limit: 20,
*     page: 3 
* }
*/

Example: Use the parsePagination function without configuration options and use page as false.

const qs = require('query-strings-parser')

const query = '?limit=20&skip=3'
console.log(qs.parsePagination(query, {}, false))

/**
* Result: 
* { 
*     limit: 20,
*     skip: 3 
* }
*/

Example: Use the parsePagination function with configuration options and use page as true.

const qs = require('query-strings-parser')

const query = '?page=3'
console.log(qs.parsePagination(query, {limit: 20}, true))

/**
* Result: 
* { 
*     limit: 20,
*     page: 3 
* }
*/

Example: Use the parsePagination function with configuration options and use page as false.

const qs = require('query-strings-parser')

const query = '?skip=3'
console.log(qs.parsePagination(query, { limit: 20 }, false))

/**
* Result: 
* { 
*     limit: 20,
*     skip: 3 
* }
*/

parseFilter()

Example: Use the parseFilter function without configuration options.

const qs = require('query-strings-parser')

const query = '?name=elvis&age=80'
console.log(qs.parseFilter(query, {}))

/**
* Result: 
* { 
*     name: 'elvis',
*     age: 80
* }
*/

Example: Use the parseFilter function with configuration options.

const qs = require('query-strings-parser')

const query = '?name=elvis'
console.log(qs.parseFilter(query, { age: 80 }))

/**
* Result: 
* { 
*     name: 'elvis',
*     age: 80
* }
*/

parseDate()

Example: Use the parseDate function without configuration options.

const qs = require('query-strings-parser')

const query = '?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 the parseDate function with configuration options.

const qs = require('query-strings-parser')

const query = '?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 }}
*   ]}
* }
*/