|
1 | | -[![License][license-image]][license-url] [![NPM Version][npm-image]][npm-url] [![NPM Downloads][downloads-image]][downloads-url] [![Travis][travis-image]][travis-url] [![Coverage][coverage-image]][coverage-url] [![Vulnerabilities][known-vulnerabilities-image]][known-vulnerabilities-url] |
2 | 1 |
|
3 | | -# Query Strings Parser |
4 | | -Middleware to transform query strings in a format that is recognized by the MongoDB, MySQL[¹](#future-features) and other databases. |
5 | | - |
6 | | -## Prerequisites |
7 | | -To ensure the smooth operation of the middleware, your web application must be built using the [express.js][express] or [hapi.js][hapi] frameworks. |
8 | | - |
9 | | -## Installing |
10 | | -Use the npm command to install this library into your project: |
11 | | -``` |
12 | | -npm i --save query-strings-parser |
13 | | -``` |
14 | | - |
15 | | -### Usage Examples |
16 | | -#### 1. Using default configurations |
17 | | -```js |
18 | | -const express = require('express') |
19 | | -const qs = require('query-strings-parser') |
20 | | -const app = express() |
21 | | - |
22 | | -app.use(qs()) // middleware query-strings-parser |
23 | | - |
24 | | -app.listen(3000, (req, res) => { |
25 | | - console.log('app listening on port 3000') |
26 | | -}) |
27 | | - |
28 | | -app.get('/', (req, res) => { |
29 | | - res.send('the query is:' + JSON.stringify(req.query)) |
30 | | -}) |
31 | | - |
32 | | -/** |
33 | | - * Request: http://localhost:3000?fields=name,age&skip=10&limit=10&sort=created_at |
34 | | - * Result (req.query): |
35 | | - * { |
36 | | - * fields: { name: 1, age: 1 }, |
37 | | - * sort: { created_at: 'asc' } |
38 | | - * filters: {}, |
39 | | - * pagination: { |
40 | | - * skip: 10, |
41 | | - * limit: 10 |
42 | | - * }, |
43 | | - * original: '?fields=name,age&skip=10&limit=10&sort=created_at' |
44 | | - * } |
45 | | - */ |
46 | | -``` |
47 | | -#### The middleware uses the following defaults: |
48 | | -```js |
49 | | -options = { |
50 | | - use_page: false, |
51 | | - client_db: 'mongodb', |
52 | | - date_field: { |
53 | | - start_at: 'created_at', |
54 | | - end_at: 'created_at' |
55 | | - }, |
56 | | - default: { |
57 | | - fields: {}, |
58 | | - sort: {}, |
59 | | - filters: {}, |
60 | | - pagination: { |
61 | | - limit: Number.MAX_SAFE_INTEGER, |
62 | | - skip: 0, |
63 | | - page: 1 |
64 | | - } |
65 | | - } |
66 | | -} |
67 | | -``` |
68 | | -If the options are not provided, the default values will be used for the treatment of queries strings. |
69 | | - |
70 | | - |
71 | | -### 2. Using custom configurations: |
72 | | -```js |
73 | | -const express = require('express') |
74 | | -const qs = require('query-strings-parser') |
75 | | -const app = express() |
76 | | - |
77 | | -app.use(qs({ |
78 | | - use_page: true, |
79 | | - client_db: 'mongodb', |
80 | | - date_field: { |
81 | | - start_at: 'timestamp', |
82 | | - end_at: 'timestamp' |
83 | | - }, |
84 | | - default: { |
85 | | - fields: {name: 1 , age: 1, number: 1, _id: 0}, |
86 | | - sort: { created_at: 'desc' }, |
87 | | - filters: {}, |
88 | | - pagination: { |
89 | | - page: 1, |
90 | | - limit: 100 |
91 | | - } |
92 | | - } |
93 | | -})) |
94 | | - |
95 | | -/** |
96 | | - * Request: http://localhost:3000?fields=name,age&age=30 |
97 | | - * Result (req.query): |
98 | | - * { |
99 | | - * fields: { name: 1, age: 1}, |
100 | | - * sort: { created_at: 'desc' } |
101 | | - * filters: {age: 30}, |
102 | | - * pagination: { |
103 | | - * limit: 100, |
104 | | - * page: 1 |
105 | | - * }, |
106 | | - * original: '?fields=name,age&age=30' |
107 | | - * } |
108 | | - */ |
109 | | -``` |
110 | | - |
111 | | -For more details, access the [wiki](https://github.com/nutes-uepb/query-strings-parser/wiki/2.-Usage-Examples) page. |
112 | | - |
113 | | -## Supported Query Strings |
114 | | - |
115 | | -For informations and details about the supported query strings, access the [wiki](https://github.com/nutes-uepb/query-strings-parser/wiki/3.-Supported-Query-Strings) page. |
116 | | - |
117 | | -## New Features |
118 | | - |
119 | | -- Support for parser functions. For informations and details about parser functions, access the [wiki](https://github.com/nutes-uepb/query-strings-parser/wiki/4.-Parsers) page. |
120 | | -## Future Features |
121 | | -- ¹Support for relational databases such as MySQL, PostgreSQL and SQLite. |
| 2 | +Query Strings Parser |
| 3 | +========================= |
| 4 | +[![License][license-image]][license-url] [![NPM Version][npm-image]][npm-url] [![NPM Downloads][downloads-image]][npm-url] [![Travis][travis-image]][travis-url] [![Coverage][coverage-image]][coverage-url] [![Dependencies][dependencies-image]][dependencies-url] [![DependenciesDev][dependencies-dev-image]][dependencies-dev-url] [![Vulnerabilities][known-vulnerabilities-image]][known-vulnerabilities-url] [![Releases][releases-image]][releases-url] [![Contributors][contributors-image]][contributors-url] [![Issues][issues-image]][issues-url] |
| 5 | + |
| 6 | +Middleware to transform query strings in a format that is recognized by the MongoDB, MySQL[¹](#future-features) and other databases. |
| 7 | + |
| 8 | +## Prerequisites |
| 9 | +To ensure the smooth operation of the middleware, your web application must be built using the [express.js][express] or [hapi.js][hapi] frameworks. |
| 10 | + |
| 11 | +## Installing |
| 12 | +Use the npm command to install this library into your project: |
| 13 | +``` |
| 14 | +npm i --save query-strings-parser |
| 15 | +``` |
| 16 | + |
| 17 | +### Usage Examples |
| 18 | +#### 1. Using default configurations |
| 19 | +```js |
| 20 | +const express = require('express') |
| 21 | +const qs = require('query-strings-parser') |
| 22 | +const app = express() |
| 23 | + |
| 24 | +app.use(qs()) // middleware query-strings-parser |
| 25 | + |
| 26 | +app.listen(3000, (req, res) => { |
| 27 | + console.log('app listening on port 3000') |
| 28 | +}) |
| 29 | + |
| 30 | +app.get('/', (req, res) => { |
| 31 | + res.send('the query is:' + JSON.stringify(req.query)) |
| 32 | +}) |
| 33 | + |
| 34 | +/** |
| 35 | + * Request: http://localhost:3000?fields=name,age&skip=10&limit=10&sort=created_at |
| 36 | + * Result (req.query): |
| 37 | + * { |
| 38 | + * fields: { name: 1, age: 1 }, |
| 39 | + * sort: { created_at: 'asc' } |
| 40 | + * filters: {}, |
| 41 | + * pagination: { |
| 42 | + * skip: 10, |
| 43 | + * limit: 10 |
| 44 | + * }, |
| 45 | + * original: '?fields=name,age&skip=10&limit=10&sort=created_at' |
| 46 | + * } |
| 47 | + */ |
| 48 | +``` |
| 49 | +#### The middleware uses the following defaults: |
| 50 | +```js |
| 51 | +options = { |
| 52 | + use_page: false, |
| 53 | + client_db: 'mongodb', |
| 54 | + date_field: { |
| 55 | + start_at: 'created_at', |
| 56 | + end_at: 'created_at' |
| 57 | + }, |
| 58 | + default: { |
| 59 | + fields: {}, |
| 60 | + sort: {}, |
| 61 | + filters: {}, |
| 62 | + pagination: { |
| 63 | + limit: Number.MAX_SAFE_INTEGER, |
| 64 | + skip: 0, |
| 65 | + page: 1 |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | +``` |
| 70 | +If the options are not provided, the default values will be used for the treatment of queries strings. |
| 71 | + |
| 72 | + |
| 73 | +### 2. Using custom configurations: |
| 74 | +```js |
| 75 | +const express = require('express') |
| 76 | +const qs = require('query-strings-parser') |
| 77 | +const app = express() |
| 78 | + |
| 79 | +app.use(qs({ |
| 80 | + use_page: true, |
| 81 | + client_db: 'mongodb', |
| 82 | + date_field: { |
| 83 | + start_at: 'timestamp', |
| 84 | + end_at: 'timestamp' |
| 85 | + }, |
| 86 | + default: { |
| 87 | + fields: {name: 1 , age: 1, number: 1, _id: 0}, |
| 88 | + sort: { created_at: 'desc' }, |
| 89 | + filters: {}, |
| 90 | + pagination: { |
| 91 | + page: 1, |
| 92 | + limit: 100 |
| 93 | + } |
| 94 | + } |
| 95 | +})) |
| 96 | + |
| 97 | +/** |
| 98 | + * Request: http://localhost:3000?fields=name,age&age=30 |
| 99 | + * Result (req.query): |
| 100 | + * { |
| 101 | + * fields: { name: 1, age: 1}, |
| 102 | + * sort: { created_at: 'desc' } |
| 103 | + * filters: {age: 30}, |
| 104 | + * pagination: { |
| 105 | + * limit: 100, |
| 106 | + * page: 1 |
| 107 | + * }, |
| 108 | + * original: '?fields=name,age&age=30' |
| 109 | + * } |
| 110 | + */ |
| 111 | +``` |
| 112 | + |
| 113 | +For more details, access the [wiki](https://github.com/nutes-uepb/query-strings-parser/wiki/2.-Usage-Examples) page. |
| 114 | + |
| 115 | +## Supported Query Strings |
| 116 | +For informations and details about the supported query strings, access the [wiki](https://github.com/nutes-uepb/query-strings-parser/wiki/3.-Supported-Query-Strings) page. |
| 117 | + |
| 118 | +## New Features |
| 119 | +- Support for parser functions. For informations and details about parser functions, access the [wiki](https://github.com/nutes-uepb/query-strings-parser/wiki/4.-Parsers) page. |
| 120 | + |
| 121 | +## Future Features |
| 122 | +- ¹Support for relational databases such as MySQL, PostgreSQL and SQLite. |
122 | 123 |
|
123 | 124 | [//]: # (These are reference links used in the body of this note.) |
124 | | -[build-test]: <https://travis-ci.org/nutes-uepb/query-strings-parser> |
125 | | -[test-coverage]: <https://coveralls.io/github/nutes-uepb/query-strings-parser?branch=master> |
126 | | -[node.js]: <https://nodejs.org> |
127 | | -[npm.js]: <https://www.npmjs.com/> |
128 | | -[express]: <https://expressjs.com> |
129 | | -[hapi]: <https://hapijs.com/> |
130 | | - |
131 | | -[license-image]: https://img.shields.io/github/license/mashape/apistatus.svg |
| 125 | +[node.js]: <https://nodejs.org> |
| 126 | +[npm.js]: <https://www.npmjs.com/> |
| 127 | +[express]: <https://expressjs.com> |
| 128 | +[hapi]: <https://hapijs.com/> |
| 129 | +[license-image]: https://img.shields.io/badge/license-Apache%202-blue.svg |
132 | 130 | [license-url]: https://github.com/nutes-uepb/query-strings-parser/blob/master/LICENSE |
133 | | -[npm-image]: https://img.shields.io/npm/v/query-strings-parser.svg |
| 131 | +[npm-image]: https://img.shields.io/npm/v/query-strings-parser.svg?color=red&logo=npm |
134 | 132 | [npm-url]: https://npmjs.org/package/query-strings-parser |
135 | | -[downloads-image]: https://img.shields.io/npm/dt/query-strings-parser.svg |
136 | | -[downloads-url]: https://npmjs.org/package/query-strings-parser |
137 | | -[travis-image]: https://travis-ci.org/nutes-uepb/query-strings-parser.svg?branch=master |
| 133 | +[downloads-image]: https://img.shields.io/npm/dt/query-strings-parser.svg?logo=npm |
| 134 | +[travis-image]: https://img.shields.io/travis/nutes-uepb/query-strings-parser.svg?logo=travis |
138 | 135 | [travis-url]: https://travis-ci.org/nutes-uepb/query-strings-parser |
139 | 136 | [coverage-image]: https://coveralls.io/repos/github/nutes-uepb/query-strings-parser/badge.svg |
140 | 137 | [coverage-url]: https://coveralls.io/github/nutes-uepb/query-strings-parser?branch=master |
141 | 138 | [known-vulnerabilities-image]: https://snyk.io/test/github/nutes-uepb/query-strings-parser/badge.svg?targetFile=package.json |
142 | 139 | [known-vulnerabilities-url]: https://snyk.io/test/github/nutes-uepb/query-strings-parser?targetFile=package.json |
| 140 | +[dependencies-image]: https://david-dm.org/nutes-uepb/query-strings-parser.svg |
| 141 | +[dependencies-url]: https://david-dm.org/nutes-uepb/query-strings-parser |
| 142 | +[dependencies-dev-image]: https://david-dm.org/nutes-uepb/query-strings-parser/dev-status.svg |
| 143 | +[dependencies-dev-url]: https://david-dm.org/nutes-uepb/query-strings-parser?type=dev |
| 144 | +[releases-image]: https://img.shields.io/github/release-date/nutes-uepb/query-strings-parser.svg |
| 145 | +[releases-url]: https://github.com/nutes-uepb/ip-allowed/releases |
| 146 | +[contributors-image]: https://img.shields.io/github/contributors/nutes-uepb/query-strings-parser.svg?color=green |
| 147 | +[contributors-url]: https://github.com/nutes-uepb/query-strings-parser/graphs/contributors |
| 148 | +[issues-image]: https://img.shields.io/github/issues/nutes-uepb/query-strings-parser.svg |
| 149 | +[issues-url]: https://github.com/nutes-uepb/query-strings-parser/issues |
0 commit comments