Skip to content

Commit f1a4654

Browse files
author
Lucas Cosmo Rocha
committed
Update README.md
1 parent a714599 commit f1a4654

File tree

3 files changed

+74
-16
lines changed

3 files changed

+74
-16
lines changed

README.md

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ options = {
5757
filters: {}
5858
},
5959
use_page: false,
60-
client_db: 'mongodb'
60+
client_db: 'mongodb',
61+
date_field: 'created_at'
6162
}
6263
```
6364
If the options are not provided, the default values will be used for the treatment of queries strings.
@@ -71,6 +72,7 @@ const app = express()
7172
app.use(qs({
7273
use_page: true,
7374
client_db: 'mongodb',
75+
date_field: 'created_at'
7476
default: {
7577
fields: {name: 1 , age: 1, number: 1, _id: 0},
7678
sort: { created_at: 'desc' },
@@ -96,11 +98,67 @@ app.use(qs({
9698
* }
9799
*/
98100
```
101+
102+
### 3. Most Usage In Queries
103+
#### 3.1 Partial Answers
104+
| Query | Description | Result |
105+
| ------ | ------ | ------ |
106+
|`?fields=name,age`| Search where the user wants only the name and age of the user. |`{ fields: { name: 1, age: 1 } }` |
107+
108+
#### 3.2 Pagination
109+
**3.2.1 Pagination With Skip**
110+
111+
| Query | Description | Result |
112+
| ------ | ------ | ------ |
113+
| `?limit=10&skip=0` | Search where the user wants only 10 results, without skip any. | `{ pagination: { limit: 10, skip: 0 } }` |
114+
115+
**3.2.2 Pagination With page**
116+
117+
| Query | Description | Result |
118+
| ------ | ------ | ------ |
119+
| `?limit=10&page=2` | Search where the user wants results only 10 results, but the second page (in this case, from the 11th to the 20th result). | `{ pagination: { limit: 10, page: 2 } }` |
120+
121+
#### 3.3 Ordination
122+
| Query | Description | Result |
123+
| ------ | ------ | ------ |
124+
| `?sort=name,-age` | Search where the user wants results to be sorted in ascending order by name and in descending order by age. | `{ sort: { name: 'asc', age: 'desc' } }` |
125+
126+
#### 3.4 Filtering
127+
| Query | Description | Result |
128+
| ------ | ------ | ------ |
129+
| `?name=elvis&age=80` | Search where the user wants results that name equals elvis and age equals 80. | `{ filters: { name: 'elvis', age: 80 } }` |
130+
| `?name=elvis&name=john` | Search where the usar wants results that name equals elvis and john. | `{ filters: { $and: [ { name: 'elvis' }, { name: john } ] } }` |
131+
| `?name=elvis,john` | Search where the user wants results that name equals elvis or john. | `{ filters: { $or: [ { name: 'elvis' }, { name: john } ] } }` |
132+
| `?age=gt:30` | Search where the user wants results that age is greater than 30. | `{ filters: { age: { $gt: 30 } } }` |
133+
| `?age=gte:30` | Search where the user wants results that age is greater or equal than 30. | `{ filters: { age: { $gte: 30 } } }` |
134+
| `age=lt:30` | Search where the user wants results that age is lower than 30. | `{ filters: { age: { $lt: 30 } } }` |
135+
| `age=lte:30` | Search where the user wants results that age is lower or equal than 30. | `{ filters: { age: { $lte: 30 } } }` |
136+
137+
#### 3.5 Data
138+
| Query | Description | Result |
139+
| ------ | ------ | ------ |
140+
| `?date_start=2018-11-10&date_end=2018-12-10` | Search where the user wants results between 2018-12-10 and 2018-12-12. | `{ filters: { $and: [ { created_at: { $lt: '2018-12-10T00:00:00.000Z' } }, { created_at: { $gte: '2018-11-10T00:00:00.000Z' } } ] } }` |
141+
| `?date_start=2018-12-10` | Search where the user wants results between 2018-12-10 and the current date. In this example, the current day is: 2018-12-12. | `{ filters: { $and: [ { created_at: { $lt: '2018-12-13T00:00:00.000Z' } }, { created_at: { $gte: '2018-12-10T00:00:00.000Z' } } ] } }` |
142+
| `?date_end=2018-12-11&period=10d` | Search where the user wants results from 10 days before 2018-12-12. | `{ filters: { $and: [ { created_at: { $lt: '2018-12-11T00:00:00.000Z' } }, { created_at: { $gte: '2018-11-30T00:00:00.000Z' } } ] } }` |
143+
| `?period=10d` | Search where the user wants results from 10 days before the current date. In this example, the current day is: 2018-12-12. | `{ filters: { $and: [ { created_at: { $lt: '2018-12-13T00:00:00.000Z' } }, { created_at: { $gte: '2018-12-02T00:00:00.000Z' } } ] } }` |
144+
| `?date_end=2018-12-11&period=8w` | Search where the user wants results from 8 weeks before 2018-12-12. | `{ filters: { $and: [ { created_at: { $lt: '2018-12-11T00:00:00.000Z' } }, { created_at: { $gte: '2018-10-15T00:00:00.000Z' } } ] } }` |
145+
| `?period=8w` | Search where the user wants results from 8 weeks before the current date. In this example, the current day is: 2018-12-12. | `{ filters: { $and: [ { created_at: { $lt: '2018-12-13T00:00:00.000Z' } }, { created_at: { $gte: '2018-10-17T00:00:00.000Z' } } ] } }` |
146+
| `?date_end=2018-12-11&period=6m` | Search where the user wants results from 6 months before 2018-12-11. | `{ filters: { $and: [ { created_at: { $lt: '2018-12-11T00:00:00.000Z' } }, { created_at: { $gte: '2018-06-10T03:00:00.000Z' } } ] } }` |
147+
| `?period=6m` | Search where the user wants results from 6 months before the current date. In this example, the current day is: 2018-12-12. | `{ filters: { $and: [ { created_at: { $lt: '2018-12-13T00:00:00.000Z' } }, { created_at: { $gte: '2018-06-12T03:00:00.000Z' } } ] } }` |
148+
| `?date_end=2018-12-11&period=4y` | Search where the user wants results from 4 years before 2018-12-11. | `{ filters: { $and: [ { created_at: { $lt: '2018-12-11T00:00:00.000Z' } }, { created_at: { $gte: '2014-12-10T00:00:00.000Z' } } ] } }` |
149+
| `?period=4y` | Search where the user wants results from 4 years before the current date. In this example, the current day is: 2018-12-12. | `{ filters: { $and: [ { created_at: { $lt: '2018-12-13T00:00:00.000Z' } }, { created_at: { $gte: '2014-12-12T00:00:00.000Z' } } ] } }` |
150+
151+
#### 3.6 Search
152+
// TODO
153+
99154
**NOTES** :
100155
* Default values are used only when they are not passed in the query string. For example, if you set the default value `?sort=-age` _(age in descending order)_ and your client makes a request with `?sort=name` _(name in ascending order)_, you will get in req.query the value `{ sort: { name: 'asc' } }`, since the values passed by the client will always have preference.
101156
* Remember to set whether you will use the page or not in the options. If you do not set this setting, it will work without page param, even if you pass the page setup on paging.
102157
* If you use custom configurations, the query configurations must be insert in json with name 'default'.
158+
* The accepted date format of Data params is yyyy-MM-dd or yyyy-MM-dd: hh: mm: ss. Any date outside this format will not work correctly.
159+
* The **date_field** parameter in the options is used in cases where you want to use filters with Date. If this value is not specified in the middleware configuration options, its default value will be 'created_at'.
103160
* The configurations that you don't set in middleware options it will be the default settings.
161+
104162
### Query Support
105163

106164
This middleware supports the queries as follow the pattern bellow:

lib/mapper/filters.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function parseDate(query, options) {
8181
if (query.period) {
8282
(query.date_end && isDate(query.date_end)) ?
8383
result.push(
84-
processQuery(options.date_field, 'lt:'.concat(query.date_end)),
84+
processQuery(options.date_field, 'lt:'.concat(getDateStart('0d', new Date(query.date_end)))),
8585
processQuery(options.date_field, 'gte:'.concat(
8686
getDateStart(query.period, new Date(query.date_end))))) :
8787
result.push(
@@ -110,7 +110,7 @@ function getDateStart(period, date_end) {
110110
return dateToString(new Date(
111111
date_end.getFullYear(),
112112
date_end.getMonth(),
113-
(date_end.getDate() - onlyNumbers(period))))
113+
(date_end.getDate() - onlyNumbers(period) + 1)))
114114
}
115115
else if (period.endsWith('w')) {
116116
return dateToString(new Date(
@@ -127,7 +127,7 @@ function getDateStart(period, date_end) {
127127
else {
128128
return dateToString(new Date(
129129
(date_end.getFullYear() - onlyNumbers(period)),
130-
date_end.getMonth() - onlyNumbers(period),
130+
date_end.getMonth(),
131131
date_end.getDate()))
132132
}
133133
}
@@ -146,4 +146,3 @@ function onlyNumbers(value) {
146146
exports = module.exports = {
147147
filters: filters
148148
}
149-

test/integration/index.spec.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ describe('queryFilter()', function () {
190190
context('when query contains date filters param', function () {
191191
it('should return req.query with set date_start params', function (done) {
192192
const now = new Date();
193-
const today = `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}`
193+
const today = dateToString(new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1))
194194
const expect_filters = {
195195
$and: [
196196
{ created_at: { $lt: new Date(today).toISOString() } },
@@ -235,14 +235,16 @@ describe('queryFilter()', function () {
235235

236236
it('should return req.query with set period and date_end param', function (done) {
237237
const now = new Date();
238-
const today = `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}`
238+
const today = dateToString(new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1))
239+
const beforeToday = dateToString(new Date(now.getFullYear(), now.getMonth(), now.getDate()))
240+
239241
const expect_filters = {
240242
$and: [
241243
{ created_at: { $lt: new Date(today).toISOString() } },
242-
{ created_at: { $gte: new Date(today).toISOString() } }
244+
{ created_at: { $gte: new Date(beforeToday).toISOString() } }
243245
]
244246
}
245-
const query = { period: '0d', date_end: today }
247+
const query = { period: '0d', date_end: beforeToday }
246248
const req = httpMocks.createRequest({ method: 'GET', url: '/', query: query })
247249
const res = httpMocks.createResponse()
248250

@@ -258,8 +260,8 @@ describe('queryFilter()', function () {
258260

259261
it('should return req.query with set period as day params', function (done) {
260262
const now = new Date();
261-
const today = `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}`
262-
const beforeToday = dateToString(new Date(`${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate() - 10}`))
263+
const today = dateToString(new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1))
264+
const beforeToday = dateToString(new Date(now.getFullYear(), now.getMonth(), now.getDate() - 9))
263265
const expect_filters = {
264266
$and: [
265267
{ created_at: { $lt: new Date(today).toISOString() } },
@@ -283,7 +285,7 @@ describe('queryFilter()', function () {
283285
it('should return req.query with set period as week params', function (done) {
284286

285287
const now = new Date();
286-
const today = `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}`
288+
const today = dateToString(new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1))
287289
const beforeToday = dateToString(new Date(now.getFullYear(), now.getMonth(), now.getDate() - 14))
288290
const expect_filters = {
289291
$and: [
@@ -308,7 +310,7 @@ describe('queryFilter()', function () {
308310
it('should return req.query with set period as month params', function (done) {
309311

310312
const now = new Date();
311-
const today = `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}`
313+
const today = dateToString(new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1))
312314
const beforeToday = dateToString(new Date(now.getFullYear(), (now.getMonth() - 1), now.getDate()))
313315
const expect_filters = {
314316
$and: [
@@ -333,8 +335,8 @@ describe('queryFilter()', function () {
333335
it('should return req.query with set period as year params', function (done) {
334336

335337
const now = new Date();
336-
const today = `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}`
337-
const beforeToday = dateToString(new Date(now.getFullYear() - 1, now.getMonth() - 1, now.getDate()))
338+
const today = dateToString(new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1))
339+
const beforeToday = dateToString(new Date(now.getFullYear() - 1, now.getMonth(), now.getDate()))
338340
const expect_filters = {
339341
$and: [
340342
{ created_at: { $lt: new Date(today).toISOString() } },
@@ -575,7 +577,6 @@ describe('queryFilter()', function () {
575577
done()
576578
})
577579
})
578-
579580
})
580581
})
581582

0 commit comments

Comments
 (0)