Skip to content

Commit 25bdf4b

Browse files
committed
Merge with develop
2 parents 5ee3af0 + d58c16a commit 25bdf4b

File tree

5 files changed

+178
-154
lines changed

5 files changed

+178
-154
lines changed

.travis.yml

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
language: node_js
22
node_js:
3-
- "6.14"
4-
- "7.10"
5-
- "8.11"
6-
- "9.11"
7-
- "10.2"
8-
- "10.13"
9-
- "11.0"
3+
- "6"
4+
- "7"
5+
- "8"
6+
- "9"
7+
- "10"
8+
- "11"
9+
- "12"
1010
sudo: false
1111
cache:
1212
directories:
1313
- node_modules
1414
before_script:
15-
- npm install && npm install coveralls mocha-lcov-reporter --save-dev
15+
- npm install && npm install coveralls mocha-lcov-reporter --save-dev
1616
script:
17-
- npm run test:travis
17+
- npm test
18+
after_success:
19+
- nyc report --reporter=text-lcov | coveralls
1820
notifications:
1921
email:
2022
on_success: never
21-
on_failure: always
23+
on_failure: always

README.md

Lines changed: 139 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1,141 +1,149 @@
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]
21

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-
### 2. Using custom configurations:
71-
```js
72-
const express = require('express')
73-
const qs = require('query-strings-parser')
74-
const app = express()
75-
76-
app.use(qs({
77-
use_page: true,
78-
client_db: 'mongodb',
79-
date_field: {
80-
start_at: 'timestamp',
81-
end_at: 'timestamp'
82-
},
83-
default: {
84-
fields: {name: 1 , age: 1, number: 1, _id: 0},
85-
sort: { created_at: 'desc' },
86-
filters: {},
87-
pagination: {
88-
page: 1,
89-
limit: 100
90-
}
91-
}
92-
}))
93-
94-
/**
95-
* Request: http://localhost:3000?fields=name,age&age=30
96-
* Result (req.query):
97-
* {
98-
* fields: { name: 1, age: 1},
99-
* sort: { created_at: 'desc' }
100-
* filters: {age: 30},
101-
* pagination: {
102-
* limit: 100,
103-
* page: 1
104-
* },
105-
* original: '?fields=name,age&age=30'
106-
* }
107-
*/
108-
```
109-
110-
For more details, access the [wiki](https://github.com/nutes-uepb/query-strings-parser/wiki/2.-Usage-Examples) page.
111-
112-
## Supported Query Strings
113-
114-
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.
115-
116-
## New Features
117-
118-
- 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.
119-
## Future Features
120-
- ¹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.
121123

122124
[//]: # (These are reference links used in the body of this note.)
123-
[build-test]: <https://travis-ci.org/nutes-uepb/query-strings-parser>
124-
[test-coverage]: <https://coveralls.io/github/nutes-uepb/query-strings-parser?branch=master>
125-
[node.js]: <https://nodejs.org>
126-
[npm.js]: <https://www.npmjs.com/>
127-
[express]: <https://expressjs.com>
128-
[hapi]: <https://hapijs.com/>
129-
130-
[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
131130
[license-url]: https://github.com/nutes-uepb/query-strings-parser/blob/master/LICENSE
132-
[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
133132
[npm-url]: https://npmjs.org/package/query-strings-parser
134-
[downloads-image]: https://img.shields.io/npm/dt/query-strings-parser.svg
135-
[downloads-url]: https://npmjs.org/package/query-strings-parser
136-
[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
137135
[travis-url]: https://travis-ci.org/nutes-uepb/query-strings-parser
138136
[coverage-image]: https://coveralls.io/repos/github/nutes-uepb/query-strings-parser/badge.svg
139137
[coverage-url]: https://coveralls.io/github/nutes-uepb/query-strings-parser?branch=master
140138
[known-vulnerabilities-image]: https://snyk.io/test/github/nutes-uepb/query-strings-parser/badge.svg?targetFile=package.json
141139
[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

index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ exports = module.exports = function (params) {
77
return read.parser(params)
88
}
99

10+
exports = module.exports.parser = function (query, defaults) {
11+
return read.parseAll(query, defaults)
12+
}
13+
1014
exports = module.exports.parseFields = function (_query, _default) {
1115
return read.parseFields(_query, _default)
1216
}
@@ -25,4 +29,4 @@ exports = module.exports.parseFilter = function (_query, _default) {
2529

2630
exports = module.exports.parseDate = function (_query, _default) {
2731
return read.parseDate(_query, _default)
28-
}
32+
}

lib/read.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ exports = module.exports.parser = function (params) {
4141
}
4242
}
4343

44+
4445
function validate_options(params) {
4546
const options = init_default()
4647
if (typeof params === 'object' && Object.keys(params).length > 0) {
@@ -64,6 +65,16 @@ function validate_options(params) {
6465
return options
6566
}
6667

68+
exports = module.exports.parseAll = function (_query, _default) {
69+
return {
70+
fields: this.parseFields(_query, _default),
71+
sort: this.parseSort(_query),
72+
filters: this.parseFilter(_query, _default),
73+
pagination: this.parsePagination(_query, _default, true),
74+
original: _query
75+
}
76+
}
77+
6778
exports = module.exports.parseFields = function (query, fields_default) {
6879
const options = validate_options({'default': {'fields': fields_default}})
6980
return fields.fields(stringToJson(query), options)
@@ -95,4 +106,4 @@ exports = module.exports.parseDate = function (query, date_fields) {
95106
function stringToJson(query) {
96107
if (typeof query === 'string') return url.parse(query, true).query
97108
return query
98-
}
109+
}

0 commit comments

Comments
 (0)